From 7b02b5a33bc9f37dca0ba3d61905649711df1150 Mon Sep 17 00:00:00 2001 From: balzack Date: Mon, 8 Jul 2024 08:56:46 -0700 Subject: [PATCH] added logging interface --- app/sdk/__tests__/session.tests.ts | 2 +- app/sdk/src/account.ts | 6 ++-- app/sdk/src/alias.ts | 6 ++-- app/sdk/src/api.ts | 6 ++++ app/sdk/src/attribute.ts | 6 ++-- app/sdk/src/bot.ts | 7 +++-- app/sdk/src/connection.ts | 5 +++- app/sdk/src/contact.ts | 6 ++-- app/sdk/src/content.ts | 6 ++-- app/sdk/src/focus.ts | 6 ++-- app/sdk/src/identity.ts | 8 ++++-- app/sdk/src/index.ts | 23 +++++++++------- app/sdk/src/node.ts | 6 ++-- app/sdk/src/ring.ts | 6 ++-- app/sdk/src/session.ts | 44 +++++++++++++++++++++--------- app/sdk/src/stream.ts | 6 ++-- 16 files changed, 101 insertions(+), 48 deletions(-) diff --git a/app/sdk/__tests__/session.tests.ts b/app/sdk/__tests__/session.tests.ts index a0221809..77178482 100644 --- a/app/sdk/__tests__/session.tests.ts +++ b/app/sdk/__tests__/session.tests.ts @@ -86,7 +86,7 @@ jest.mock('../src/ring', () => { test('allocates session correctly', async () => { let status: string = ''; - const sdk = new DatabagSDK(null); + const sdk = new DatabagSDK(null, null); const params: SessionParams = { topicBatch: 0, tagBatch: 0, channelTypes: [], pushType: '', deviceToken: '', notifications: [], deviceId: '', version: '', appName: '' }; const session = await sdk.login('handle', 'password', 'url', null, params); session.addStatusListener((ev: string) => { status = ev; }); diff --git a/app/sdk/src/account.ts b/app/sdk/src/account.ts index 7ad330d9..bd69a888 100644 --- a/app/sdk/src/account.ts +++ b/app/sdk/src/account.ts @@ -1,5 +1,5 @@ import { EventEmitter } from 'eventemitter3'; -import { type Account } from './api'; +import type { Account, Logging } from './api'; import type { AccountStatus } from './types'; export class AccountModule implements Account { @@ -7,8 +7,10 @@ export class AccountModule implements Account { private emitter: EventEmitter; private token: string; private url: string; + private log: Logging; - constructor(token: string, url: string) { + constructor(log: Logging, token: string, url: string) { + this.log = log; this.emitter = new EventEmitter(); this.token = token; this.url = url; diff --git a/app/sdk/src/alias.ts b/app/sdk/src/alias.ts index 0eec70fe..106fc79f 100644 --- a/app/sdk/src/alias.ts +++ b/app/sdk/src/alias.ts @@ -1,17 +1,19 @@ import { EventEmitter } from 'eventemitter3'; -import type { Alias, Account } from './api'; +import type { Alias, Account, Logging } from './api'; import type { Group } from './types'; export class AliasModule implements Alias { + private log: Logging; private token: string; private url: string; private account: Account; private emitter: EventEmitter; - constructor(token: string, url: string, account: Account) { + constructor(log: Logging, token: string, url: string, account: Account) { this.token = token; this.url = url; + this.log = log; this.account = account; this.emitter = new EventEmitter(); } diff --git a/app/sdk/src/api.ts b/app/sdk/src/api.ts index d7d2b438..446c5d11 100644 --- a/app/sdk/src/api.ts +++ b/app/sdk/src/api.ts @@ -21,6 +21,12 @@ export interface WebStore { export interface Crypto { } +export interface Logging { + error(m: any): void; + warn(m: any): void; + info(m: any): void; +} + export interface Session { close(): void; diff --git a/app/sdk/src/attribute.ts b/app/sdk/src/attribute.ts index e1dec422..b0f8139b 100644 --- a/app/sdk/src/attribute.ts +++ b/app/sdk/src/attribute.ts @@ -1,17 +1,19 @@ import { EventEmitter } from 'eventemitter3'; -import type { Attribute, Account } from './api'; +import type { Attribute, Account, Logging } from './api'; import type { Article } from './types'; export class AttributeModule implements Attribute { + private log: Logging; private token: string; private url: string; private account: Account; private emitter: EventEmitter; - constructor(token: string, url: string, account: Account) { + constructor(log: Logging, token: string, url: string, account: Account) { this.token = token; this.url = url; + this.log = log; this.account = account; this.emitter = new EventEmitter(); } diff --git a/app/sdk/src/bot.ts b/app/sdk/src/bot.ts index ac25eaf0..a5f20e6a 100644 --- a/app/sdk/src/bot.ts +++ b/app/sdk/src/bot.ts @@ -1,9 +1,12 @@ -import { type Bot } from './api'; +import type { Bot, Logging } from './api'; import type { Asset } from './types'; export class BotModule implements Bot { - constructor() { + private log: Logging; + + constructor(log: Logging) { + this.log = log; } public async addTopic(server: string, token: string, type: string, message: string, assets: Asset[]): Promise { diff --git a/app/sdk/src/connection.ts b/app/sdk/src/connection.ts index 5802a280..dd506311 100644 --- a/app/sdk/src/connection.ts +++ b/app/sdk/src/connection.ts @@ -1,15 +1,18 @@ import { EventEmitter } from 'eventemitter3'; +import { Logging } from './api'; import { Revision } from './entities'; import { Call } from './types'; export class Connection { + private log: Logging; private closed: boolean; private emitter: EventEmitter; private websocket: WebSocket; - constructor(token: string, url: string) { + constructor(log: Logging, token: string, url: string) { this.closed = false; + this.log = log; this.emitter = new EventEmitter(); this.websocket = this.setWebSocket(token, url); } diff --git a/app/sdk/src/contact.ts b/app/sdk/src/contact.ts index 77f17f72..9e26faf6 100644 --- a/app/sdk/src/contact.ts +++ b/app/sdk/src/contact.ts @@ -1,16 +1,18 @@ import { EventEmitter } from 'eventemitter3'; -import type { Contact } from './api'; +import type { Contact, Logging } from './api'; import type { Card, Topic, Asset, Tag, Profile, Repeater} from './types'; export class ContactModule implements Contact { + private log: Logging; private token: string; private url: string; private emitter: EventEmitter; - constructor(token: string, url: string) { + constructor(log: Logging, token: string, url: string) { this.token = token; this.url = url; + this.log = log; this.emitter = new EventEmitter(); } diff --git a/app/sdk/src/content.ts b/app/sdk/src/content.ts index 2d5ca5fc..3b5332ae 100644 --- a/app/sdk/src/content.ts +++ b/app/sdk/src/content.ts @@ -1,17 +1,19 @@ import { EventEmitter } from 'eventemitter3'; -import type { Content, Account } from './api'; +import type { Content, Account, Logging } from './api'; import type { Channel, Topic, Asset, Tag, Repeater } from './types'; export class ContentModule implements Content { + private log: Logging; private token: string; private url: string; private account: Account; private emitter: EventEmitter; - constructor(token: string, url: string, account: Account) { + constructor(log: Logging, token: string, url: string, account: Account) { this.token = token; this.url = url; + this.log = log; this.account = account; this.emitter = new EventEmitter(); } diff --git a/app/sdk/src/focus.ts b/app/sdk/src/focus.ts index 57edab99..98f791b0 100644 --- a/app/sdk/src/focus.ts +++ b/app/sdk/src/focus.ts @@ -1,5 +1,5 @@ import { EventEmitter } from 'eventemitter3'; -import type { Identity, Contact, Content, Focus } from './api'; +import type { Identity, Contact, Content, Focus, Logging } from './api'; import type { Topic, Asset, Repeater } from './types'; export class FocusModule implements Focus { @@ -9,14 +9,16 @@ export class FocusModule implements Focus { private content: Content; private cardId: string | null; private channelId: string; + private log: Logging; private emitter: EventEmitter; - constructor(identity: Identity, contact: Contact, content: Content, cardId: string | null, channelId: string) { + constructor(log: Logging, identity: Identity, contact: Contact, content: Content, cardId: string | null, channelId: string) { this.identity = identity; this.contact = contact; this.content = content; this.cardId = cardId; this.channelId = channelId; + this.log = log; this.emitter = new EventEmitter(); } diff --git a/app/sdk/src/identity.ts b/app/sdk/src/identity.ts index 8bcf4091..3f87b4e6 100644 --- a/app/sdk/src/identity.ts +++ b/app/sdk/src/identity.ts @@ -1,16 +1,18 @@ import { EventEmitter } from 'eventemitter3'; -import { type Identity } from './api'; -import { type Profile } from './types'; +import type { Identity, Logging } from './api'; +import type { Profile } from './types'; export class IdentityModule implements Identity { private token: string; private url: string; + private log: Logging; private emitter: EventEmitter; - constructor(token: string, url: string) { + constructor(log: Logging, token: string, url: string) { this.token = token; this.url = url; + this.log = log; this.emitter = new EventEmitter(); } diff --git a/app/sdk/src/index.ts b/app/sdk/src/index.ts index 3da02142..e2d6322a 100644 --- a/app/sdk/src/index.ts +++ b/app/sdk/src/index.ts @@ -1,8 +1,9 @@ import { SessionModule } from './session'; import { NodeModule } from './node'; import { BotModule } from './bot'; +import { ConsoleLogging } from './logging'; -import type { Session, Node, Bot, SqlStore, WebStore } from './api'; +import type { Session, Node, Bot, SqlStore, WebStore, Crypto, Logging } from './api'; import type { SessionParams } from './types'; export * from './api'; @@ -10,36 +11,38 @@ export * from './types'; export class DatabagSDK { + private logging: Logging; private crypto: Crypto | null; private store: SqlStore | WebStore | null = null; - constructor(crypto: Crypto | null) { - console.log("databag sdk"); + constructor(crypto: Crypto | null, logging: Logging | null) { this.crypto = crypto; + this.logging = logging ? logging : new ConsoleLogging(); + this.logging.info("databag sdk"); } public async initOfflineStore(sql: SqlStore): Promise { this.store = sql; // initialize - return new SessionModule(this.store, this.crypto, '', ''); + return new SessionModule(this.store, this.crypto, this.logging, '', ''); } public async initOnlineStore(web: WebStore): Promise { this.store = web; // initialize - return new SessionModule(this.store, this.crypto, '', ''); + return new SessionModule(this.store, this.crypto, this.logging, '', ''); } public async login(handle: string, password: string, url: string, mfaCode: string | null, params: SessionParams): Promise { - return new SessionModule(this.store, this.crypto, '', ''); + return new SessionModule(this.store, this.crypto, this.logging, '', ''); } public async access(url: string, token: string, params: SessionParams): Promise { - return new SessionModule(this.store, this.crypto, '', ''); + return new SessionModule(this.store, this.crypto, this.logging, '', ''); } public async create(handle: string, password: string, url: string, token: string | null, params: SessionParams): Promise { - return new SessionModule(this.store, this.crypto, '', ''); + return new SessionModule(this.store, this.crypto, this.logging, '', ''); } public async logout(session: Session): Promise { @@ -47,10 +50,10 @@ export class DatabagSDK { } public async configure(token: string, url: string, mfaCode: string | null): Promise { - return new NodeModule('', ''); + return new NodeModule(this.logging, '', ''); } public async automate() { - return new BotModule(); + return new BotModule(this.logging); } } diff --git a/app/sdk/src/node.ts b/app/sdk/src/node.ts index 1ea5ce4c..bb5cbd3a 100644 --- a/app/sdk/src/node.ts +++ b/app/sdk/src/node.ts @@ -1,14 +1,16 @@ -import { type Node } from './api'; +import type { Node, Logging } from './api'; import type { NodeAccount, NodeConfig } from './types'; export class NodeModule implements Node { + private log: Logging; private token: string; private url: string; - constructor(token: string, url: string) { + constructor(log: Logging, token: string, url: string) { this.token = token; this.url = url; + this.log = log; } public async createAccountAccess(): Promise { diff --git a/app/sdk/src/ring.ts b/app/sdk/src/ring.ts index 11054c7e..4d62831d 100644 --- a/app/sdk/src/ring.ts +++ b/app/sdk/src/ring.ts @@ -1,12 +1,14 @@ import { EventEmitter } from 'eventemitter3'; -import type { Ring } from './api'; +import type { Ring, Logging } from './api'; import type { Call } from './types'; export class RingModule implements Ring { + private log: Logging; private emitter: EventEmitter; - constructor() { + constructor(log: Logging) { + this.log = log; this.emitter = new EventEmitter(); } diff --git a/app/sdk/src/session.ts b/app/sdk/src/session.ts index 1e1f816e..304f991f 100644 --- a/app/sdk/src/session.ts +++ b/app/sdk/src/session.ts @@ -12,7 +12,7 @@ import { RingModule } from './ring'; import { Connection } from './connection'; -import type { Session, SqlStore, WebStore, Crypto, Account, Identity, Contact, Ring, Alias, Attribute, Content, Stream, Focus } from './api'; +import type { Session, SqlStore, WebStore, Account, Identity, Contact, Ring, Alias, Attribute, Content, Stream, Focus, Crypto, Logging } from './api'; import { Revision } from './entities'; import { Call } from './types'; @@ -21,6 +21,7 @@ export class SessionModule implements Session { private emitter: EventEmitter; private store: SqlStore | WebStore | null; private crypto: Crypto | null; + private log: Logging; private token: string; private url: string; private syncRevision: Revision | null; @@ -35,25 +36,26 @@ export class SessionModule implements Session { private ring: RingModule; private connection: Connection; - constructor(store: SqlStore | WebStore | null, crypto: Crypto | null, token: string, url: string) { + constructor(store: SqlStore | WebStore | null, crypto: Crypto | null, log: Logging, token: string, url: string) { this.store = store; this.crypto = crypto; + this.log = log; this.token = token; this.url = url; this.syncRevision = null; this.status = 'connecting' this.emitter = new EventEmitter(); - this.account = new AccountModule(token, url); - this.identity = new IdentityModule(token, url); - this.contact = new ContactModule(token, url); - this.alias = new AliasModule(token, url, this.account); - this.attribute = new AttributeModule(token, url, this.account); - this.content = new ContentModule(token, url, this.account); - this.stream = new StreamModule(this.contact, this.content); - this.ring = new RingModule(); - this.connection = new Connection(token, url); + this.account = new AccountModule(log, token, url); + this.identity = new IdentityModule(log, token, url); + this.contact = new ContactModule(log, token, url); + this.alias = new AliasModule(log, token, url, this.account); + this.attribute = new AttributeModule(log, token, url, this.account); + this.content = new ContentModule(log, token, url, this.account); + this.stream = new StreamModule(log, this.contact, this.content); + this.ring = new RingModule(log); + this.connection = new Connection(log, token, url); const onStatus = (ev: string) => { this.status = ev; @@ -74,6 +76,7 @@ export class SessionModule implements Session { } } catch(err) { + this.log.warn(err); this.syncRevision = ev; this.emitter.emit('status', this.getStatus()); } @@ -103,7 +106,22 @@ export class SessionModule implements Session { return this.status; } - public resync() { + public async resync() { + if (this.syncRevision) { + try { + await this.identity.setRevision(this.syncRevision.profile); + await this.account.setRevision(this.syncRevision.account); + await this.contact.setRevision(this.syncRevision.card); + await this.attribute.setRevision(this.syncRevision.article); + await this.alias.setRevision(this.syncRevision.group); + await this.content.setRevision(this.syncRevision.channel); + this.syncRevision = null + this.emitter.emit('status', this.getStatus()); + } + catch(err) { + this.log.warn(err); + } + } } public close() { @@ -150,7 +168,7 @@ export class SessionModule implements Session { } public addFocus(cardId: string | null, channelId: string): Focus { - return new FocusModule(this.identity, this.contact, this.content, cardId, channelId); + return new FocusModule(this.log, this.identity, this.contact, this.content, cardId, channelId); } public removeFocus(focus: Focus): void { diff --git a/app/sdk/src/stream.ts b/app/sdk/src/stream.ts index cbe1188b..33e91ed3 100644 --- a/app/sdk/src/stream.ts +++ b/app/sdk/src/stream.ts @@ -1,16 +1,18 @@ import { EventEmitter } from 'eventemitter3'; -import type { Contact, Content, Stream } from './api'; +import type { Contact, Content, Stream, Logging } from './api'; import type { Channel } from './types'; export class StreamModule implements Stream { + private log: Logging; private contact: Contact; private content: Content; private emitter: EventEmitter; - constructor(contact: Contact, content: Content) { + constructor(log: Logging, contact: Contact, content: Content) { this.contact = contact; this.content = content; + this.log = log; this.emitter = new EventEmitter(); }