From 9d76ab40c628d1db61a2f59165e8560396204b7a Mon Sep 17 00:00:00 2001 From: balzack Date: Mon, 1 Jul 2024 22:46:44 -0700 Subject: [PATCH] adding aggregating interfaces --- app/sdk/__tests__/session.tests.ts | 4 +- app/sdk/src/api.ts | 69 ++++++++++++++++++---- app/sdk/src/attribute.ts | 10 ++-- app/sdk/src/contact.ts | 31 +++++++--- app/sdk/src/content.ts | 39 +++++++++---- app/sdk/src/focus.ts | 93 ++++++++++++++++++++++++++++++ app/sdk/src/session.ts | 18 +++++- app/sdk/src/stream.ts | 25 ++++++++ app/sdk/src/types.ts | 11 ++-- 9 files changed, 257 insertions(+), 43 deletions(-) create mode 100644 app/sdk/src/focus.ts create mode 100644 app/sdk/src/stream.ts diff --git a/app/sdk/__tests__/session.tests.ts b/app/sdk/__tests__/session.tests.ts index c6570bff..3e7a67a2 100644 --- a/app/sdk/__tests__/session.tests.ts +++ b/app/sdk/__tests__/session.tests.ts @@ -1,8 +1,10 @@ import { DatabagSDK } from '../src/index'; +import { type SessionParams } from '../src/types'; test('allocates session correctly', async () => { const sdk = new DatabagSDK(); - const session = await sdk.login('handle', 'password', 'url'); + const params: SessionParams = { initialTopicCount: 0, channelTypes: [], topicTypes: [], tagTypes: [] }; + const session = await sdk.login('handle', 'password', 'url', params); const account = session.getAccount(); account.setNotifications(true); //expect(r).toBe(5); diff --git a/app/sdk/src/api.ts b/app/sdk/src/api.ts index dec9b0a8..5b5fa3cd 100644 --- a/app/sdk/src/api.ts +++ b/app/sdk/src/api.ts @@ -91,16 +91,21 @@ export interface Contact { removeChannel(cardId: string, channelId: string): Promise; addTopic(cardId: string, channelId: string, type: string, message: string, assets: Asset[]): Promise; removeTopic(cardId: string, channelId: string, topicId: string): Promise; - setTopicSubject(cardId: string, channelId: string, topicId: string, type: string, value: string): Promise; - addTag(cardId: string, channelId: string, topicId: string, type: string, value: string): Promise; - removeTag(cardId: string, tagId: string): Promise; + setTopicSubject(cardId: string, channelId: string, topicId: string, subject: string): Promise; + addTag(cardId: string, channelId: string, topicId: string, type: string, subject: string): Promise; + removeTag(cardId: string, topicId: string, tagId: string): Promise; + setTagSubject(cardId: string, topicId: string, tagId: string, subject: string): Promise; + + getTopics(cardId: string, channelId: string): Promise; + getMoreTopics(cardId: string, channelId: string): Promise; + getTags(cardId: string, channelId: string, topicId: string): Promise; + getMoreTags(cardId: string, channelId: string, topicId: string): Promise; - viewMoreTopics(cardId: string, channelId: string): Promise; setUnreadChannel(cardId: string, channelId: string): Promise; clearUnreadChannel(cardId: string, channelId: string): Promise; getRegistry(server: string): Promise; - getRegistryImageUrl(server: string, string: guid): string; + getRegistryImageUrl(server: string, guid: string): string; getCardImageUrl(cardId: string): string; getTopicAssetUrl(cardId: string, channelId: string, topicId: string, assetId: string): string; @@ -125,9 +130,9 @@ export interface Alias { } export interface Attribute { - addArticle(type: string, subject: string, cardIds: string[], groupIds: string[]): Promise; + addArticle(sealed: boolean, type: string, subject: string, cardIds: string[], groupIds: string[]): Promise; removeArticle(articleId: string): Promise; - setArticleSubject(articleId: string, type: string, subject: string): Promise; + setArticleSubject(articleId: string, subject: string): Promise; setArticleCard(articleId: string, cardId: string): Promise; clearArticleCard(articleId: string, cardId: string): Promise; setArticleGroup(articleId: string, groupId: string): Promise; @@ -138,18 +143,19 @@ export interface Attribute { } export interface Content { - addChannel(type: string, subject: string, cardIds: string[], groupIds: string[]): Promise; + addChannel(sealed: boolean, type: string, subject: string, cardIds: string[], groupIds: string[]): Promise; removeChannel(channelId: string): Promise; - setChannelSubject(channelId: string, type: string, subject: string): Promise; + setChannelSubject(channelId: string, subject: string): Promise; setChannelCard(channelId: string, cardId: string): Promise; clearChannelCard(channelId: string, cardId: string): Promise; setChannelGroup(channelId: string, cardId: string): Promise; clearChannelGroup(channelId: string, cardId: string): Promise; addTopic(channelId: string, type: string, message: string, assets: Asset[]): Promise; removeTopic(channelId: string, topicId: string): Promise; - setTopicSubject(channelId: string, topicId: string, type: string, subject: string): Promise; + setTopicSubject(channelId: string, topicId: string, subject: string): Promise; addTag(channelId: string, topicId: string, type: string, value: string): Promise; removeTag(channelId: string, topicId: string, tagId: string): Promise; + setTagSubject(channelId: string, topicId: string, tagId: string, subject: string): Promise; getTopicAssetUrl(channelId: string, topicId: string, assetId: string): string; flagTopic(channelId: string, topicId: string): Promise; @@ -161,7 +167,11 @@ export interface Content { getBlockedTopics(): Promise<{ channelId: string, topicId: string }[]>; getBlockedTags(): Promise<{ channelId: string, topicId: string, tagId: string }[]>; - viewMoreTopics(channelId: string): Promise; + getTopics(channelId: string): Promise; + getMoreTopics(channelId: string): Promise; + getTags(channelId: string, topicId: string): Promise; + getMoreTags(channelId: string, topicId: string): Promise; + setUnreadChannel(channelId: string): Promise; clearUnreadChannel(channelId: string): Promise; @@ -172,6 +182,43 @@ export interface Content { removeChannelListener(ev: (channels: Channel[]) => void): void; } +export interface Stream { + addChannelListener(ev: (channels: Channel[]) => void): void; + removeChannelListener(ev: (channels: Channel[]) => void): void; +} + +export interface Focus { + blur(): void; + + addTopic(type: string, message: string, assets: Asset[]): Promise; + removeTopic(topicId: string): Promise; + setTopicSubject(topicId: string, subject: string): Promise; + addTag(topicId: string, type: string, subject: string): Promise; + removeTag(cardId: string, tagId: string): Promise; + setTagSubject(topicId: string, tagId: string, subject: string): Promise; + + viewMoreTopics(): Promise; + viewMoreTags(topicId: string): Promise; + + setUnreadChannel(cardId: string, channelId: string): Promise; + clearUnreadChannel(cardId: string, channelId: string): Promise; + + getTopicAssetUrl(topicId: string, assetId: string): string; + + addRepeaterAccess(name: string): Promise; + removeRepeaterAccess(repeaterId: string): Promise; + + flagTopic(topicId: string): Promise; + flagTag(topicId: string, tagId: string): Promise; + setBlockTopic(topicId: string): Promise; + setBlockTag(topicId: string, tagId: string): Promise; + clearBlockTopic(topicId: string): Promise; + clearBlockTag(topicId: string, tagId: string): Promise; + + addTopicListener(ev: (topics: Topic[]) => void): void; + removeTopicListener(ev: (topics: Topic[]) => void): void; +} + export interface Node { getAccounts(): Promise; createAccountAccess(): Promise; diff --git a/app/sdk/src/attribute.ts b/app/sdk/src/attribute.ts index fb116e69..f2fc5002 100644 --- a/app/sdk/src/attribute.ts +++ b/app/sdk/src/attribute.ts @@ -1,5 +1,5 @@ import { EventEmitter } from 'events'; -import { type Attribute } from './api'; +import type { Attribute, Account } from './api'; import type { Article } from './types'; export class AttributeModule implements Attribute { @@ -7,12 +7,14 @@ export class AttributeModule implements Attribute { private token: string; private url: string; private sync: (flag: boolean) => void; + private account: Account; private emitter: EventEmitter; - constructor(token: string, url: string, sync: (flag: boolean) => void) { + constructor(token: string, url: string, sync: (flag: boolean) => void, account: Account) { this.token = token; this.url = url; this.sync = sync; + this.account = account; this.emitter = new EventEmitter(); } @@ -30,14 +32,14 @@ export class AttributeModule implements Attribute { public async resync(): Promise { } - public async addArticle(type: string, subject: string, cardIds: string[], groupIds: string[]): Promise { + public async addArticle(sealed: boolean, type: string, subject: string, cardIds: string[], groupIds: string[]): Promise { return ''; } public async removeArticle(articleId: string): Promise { } - public async setArticleSubject(articleId: string, type: string, subject: string): Promise { + public async setArticleSubject(articleId: string, subject: string): Promise { } public async setArticleCard(articleId: string, cardId: string): Promise { diff --git a/app/sdk/src/contact.ts b/app/sdk/src/contact.ts index 181b8c2d..61a2c4f3 100644 --- a/app/sdk/src/contact.ts +++ b/app/sdk/src/contact.ts @@ -1,6 +1,6 @@ import { EventEmitter } from 'events'; -import { type Contact } from './api'; -import { type Card, type Topic, type Asset, type Profile, type Repeater} from './types'; +import type { Contact } from './api'; +import type { Card, Topic, Asset, Tag, Profile, Repeater} from './types'; export class ContactModule implements Contact { @@ -133,18 +133,33 @@ export class ContactModule implements Contact { public async removeTopic(cardId: string, channelId: string, topicId: string): Promise { } - public async setTopicSubject(cardId: string, channelId: string, topicId: string, type: string, subject: string): Promise { + public async setTopicSubject(cardId: string, channelId: string, topicId: string, subject: string): Promise { } public async addTag(cardId: string, channelId: string, topicId: string, type: string, value: string): Promise { return ''; } - public async removeTag(cardId: string, tagId: string): Promise { + public async removeTag(cardId: string, topicId: string, tagId: string): Promise { } - public async viewMoreTopics(cardId: string, channelId: string): Promise { - return 0; + public async setTagSubject(cardId: string, topicId: string, tagId: string, subject: string): Promise { + } + + public async getTopics(cardId: string, channelId: string): Promise { + return []; + } + + public async getMoreTopics(cardId: string, channelId: string): Promise { + return []; + } + + public async getTags(cardId: string, channelId: string, topicId: string): Promise { + return []; + } + + public async getMoreTags(cardId: string, channelId: string, topicId: string): Promise { + return []; } public async setUnreadChannel(cardId: string, channelId: string): Promise { @@ -153,11 +168,11 @@ export class ContactModule implements Contact { public async clearUnreadChannel(cardId: string, channelId: string): Promise { } - public getRegistry(server: string): Promise { + public async getRegistry(server: string): Promise { return []; } - public getRegistryImageUrl(server: string, string: guid): string { + public getRegistryImageUrl(server: string, guid: string): string { return ''; } diff --git a/app/sdk/src/content.ts b/app/sdk/src/content.ts index 72c99fa0..d6f663ec 100644 --- a/app/sdk/src/content.ts +++ b/app/sdk/src/content.ts @@ -1,18 +1,20 @@ import { EventEmitter } from 'events'; -import { type Content } from './api'; -import type { Channel, Topic, Asset, Repeater } from './types'; +import type { Content, Account } from './api'; +import type { Channel, Topic, Asset, Tag, Repeater } from './types'; export class ContentModule implements Content { private token: string; private url: string; private sync: (flag: boolean) => void; + private account: Account; private emitter: EventEmitter; - constructor(token: string, url: string, sync: (flag: boolean) => void) { + constructor(token: string, url: string, sync: (flag: boolean) => void, account: Account) { this.token = token; this.url = url; this.sync = sync; + this.account = account; this.emitter = new EventEmitter(); } @@ -30,14 +32,14 @@ export class ContentModule implements Content { public async resync(): Promise { } - public async addChannel(type: string, subject: string, cardIds: string[], groupIds: string[]): Promise { + public async addChannel(sealed: boolean, type: string, subject: string, cardIds: string[], groupIds: string[]): Promise { return ''; } public async removeChannel(channelId: string): Promise { } - public async setChannelSubject(channelId: string, type: string, subject: string): Promise { + public async setChannelSubject(channelId: string, subject: string): Promise { } public async setChannelCard(channelId: string, cardId: string): Promise { @@ -52,7 +54,7 @@ export class ContentModule implements Content { public async clearChannelGroup(channelId: string, groupId: string): Promise { } - public async addTopic(channelId: string, type: string, message: string, assets: Asset[]): Promise { + public async addTopic(channelId: string, type: string, subject: string, assets: Asset[]): Promise { return ''; } @@ -62,7 +64,7 @@ export class ContentModule implements Content { public async flagTopic(channelId: string, topicId: string): Promise { } - public async setTopicSubject(channelId: string, topicId: string, type: string, subject: string): Promise { + public async setTopicSubject(channelId: string, topicId: string, subject: string): Promise { } public async addTag(channelId: string, topicId: string, type: string, value: string): Promise { @@ -72,6 +74,9 @@ export class ContentModule implements Content { public async removeTag(channelId: string, topicId: string, tagId: string): Promise { } + public async setTagSubject(channelId: string, topicId: string, tagId: string, subject: string): Promise { + } + public async flagTag(channelId: string, topicId: string, tagId: string): Promise { } @@ -99,14 +104,26 @@ export class ContentModule implements Content { return ''; } - public async viewMoreTopics(cardId: string, channelId: string): Promise { - return 0; + public async getTopics(channelId: string): Promise { + return []; } - public async setUnreadChannel(cardId: string, channelId: string): Promise { + public async getMoreTopics(channelId: string): Promise { + return []; } - public async clearUnreadChannel(cardId: string, channelId: string): Promise { + public async getTags(channelId: string, topicId: string): Promise { + return []; + } + + public async getMoreTags(channelId: string, topicId: string): Promise { + return []; + } + + public async setUnreadChannel(channelId: string): Promise { + } + + public async clearUnreadChannel(channelId: string): Promise { } public async addRepeaterAccess(channelId: string, name: string): Promise { diff --git a/app/sdk/src/focus.ts b/app/sdk/src/focus.ts new file mode 100644 index 00000000..280703bf --- /dev/null +++ b/app/sdk/src/focus.ts @@ -0,0 +1,93 @@ +import { EventEmitter } from 'events'; +import type { Identity, Contact, Content, Focus } from './api'; +import type { Topic, Asset, Repeater } from './types'; + +export class FocusModule implements Focus { + + private identity: Identity; + private contact: Contact; + private content: Content; + private cardId?: string; + private channelId: string; + private emitter: EventEmitter; + + constructor(identity: Identity, contact: Contact, content: Content, channelId: string, cardId?: string) { + this.identity = identity; + this.contact = contact; + this.content = content; + this.cardId = cardId; + this.channelId = channelId; + this.emitter = new EventEmitter(); + } + + public blur(): void { + } + + public async addTopic(type: string, message: string, assets: Asset[]): Promise { + return ''; + } + + public async removeTopic(topicId: string): Promise { + } + + public async setTopicSubject(topicId: string, subject: string): Promise { + } + + public async addTag(topicId: string, type: string, subject: string): Promise { + return ''; + } + + public async removeTag(cardId: string, tagId: string): Promise { + } + + public async setTagSubject(topicId: string, tagId: string, subject: string): Promise { + } + + public async viewMoreTopics(): Promise { + } + + public async viewMoreTags(topicId: string): Promise { + } + + public async setUnreadChannel(cardId: string, channelId: string): Promise { + } + + public async clearUnreadChannel(cardId: string, channelId: string): Promise { + } + + public getTopicAssetUrl(topicId: string, assetId: string): string { + return ''; + } + + public async addRepeaterAccess(name: string): Promise { + return { id: '', guid: '', name: '', server: '', token: '' }; + } + + public async removeRepeaterAccess(repeaterId: string): Promise { + } + + public async flagTopic(topicId: string): Promise { + } + + public async flagTag(topicId: string, tagId: string): Promise { + } + + public async setBlockTopic(topicId: string): Promise { + } + + public async setBlockTag(topicId: string, tagId: string): Promise { + } + + public async clearBlockTopic(topicId: string): Promise { + } + + public async clearBlockTag(topicId: string, tagId: string): Promise { + } + + public addTopicListener(ev: (topics: Topic[]) => void): void { + } + + public removeTopicListener(ev: (topics: Topic[]) => void): void { + } + +} diff --git a/app/sdk/src/session.ts b/app/sdk/src/session.ts index b119b3c8..3209907b 100644 --- a/app/sdk/src/session.ts +++ b/app/sdk/src/session.ts @@ -6,8 +6,10 @@ import { ContactModule } from './contact'; import { AliasModule } from './alias'; import { AttributeModule } from './attribute'; import { ContentModule } from './content'; +import { StreamModule } from './stream'; +import { FocusModule } from './focus'; -import { type Session, type SqlStore, type WebStore, type Account, type Identity, type Contact, type Alias, type Attribute, type Content } from './api'; +import type { Session, SqlStore, WebStore, Account, Identity, Contact, Alias, Attribute, Content, Stream, Focus } from './api'; export class SessionModule implements Session { @@ -23,6 +25,7 @@ export class SessionModule implements Session { public alias: AliasModule; public attribute: AttributeModule; public content: ContentModule; + public stream: StreamModule; constructor(store: SqlStore | WebStore | null, token: string, url: string) { this.store = store; @@ -34,8 +37,9 @@ export class SessionModule implements Session { this.identity = new IdentityModule(token, url, this.setSync); this.contact = new ContactModule(token, url, this.setSync); this.alias = new AliasModule(token, url, this.setSync); - this.attribute = new AttributeModule(token, url, this.setSync); - this.content = new ContentModule(token, url, this.setSync); + this.attribute = new AttributeModule(token, url, this.setSync, this.account); + this.content = new ContentModule(token, url, this.setSync, this.account); + this.stream = new StreamModule(this.contact, this.content); } public addStatusListener(ev: (status: string) => void): void { @@ -77,4 +81,12 @@ export class SessionModule implements Session { public getContent(): Content { return this.content; } + + public getStream(): Stream { + return this.stream; + } + + public getFocus(channelId: string, cardId?: string): Focus { + return new FocusModule(this.identity, this.contact, this.content, channelId, cardId); + } } diff --git a/app/sdk/src/stream.ts b/app/sdk/src/stream.ts new file mode 100644 index 00000000..6cea3ec9 --- /dev/null +++ b/app/sdk/src/stream.ts @@ -0,0 +1,25 @@ +import { EventEmitter } from 'events'; +import type { Contact, Content, Stream } from './api'; +import type { Channel } from './types'; + +export class StreamModule implements Stream { + + private contact: Contact; + private content: Content; + private emitter: EventEmitter; + + constructor(contact: Contact, content: Content) { + this.contact = contact; + this.content = content; + this.emitter = new EventEmitter(); + } + + public addChannelListener(ev: (channels: Channel[]) => void): void { + this.emitter.on('channel', ev); + } + + public removeChannelListener(ev: (channels: Channel[]) => void): void { + this.emitter.off('channel', ev); + } + +} diff --git a/app/sdk/src/types.ts b/app/sdk/src/types.ts index ddc1a65a..53f44da2 100644 --- a/app/sdk/src/types.ts +++ b/app/sdk/src/types.ts @@ -16,7 +16,7 @@ export type Card = { export type Channel = { id: string, - guid: string, + cardId?: string, lastTopic: { guid: string, dataType: string, @@ -27,6 +27,7 @@ export type Channel = { transform: string } unread: boolean, + sealed: boolean, dataType: string, data: string, created: number, @@ -55,7 +56,7 @@ export type Topic = { updated: number, status: string, transform: string, - tags: [ Tag ] + tags: Tag[] } export type Tag = { @@ -78,7 +79,7 @@ export type Group = { data: string, created: number, updated: number, - cards: [ string ] + cards: string[] } export type Article = { @@ -88,8 +89,8 @@ export type Article = { created: number, updated: number, contacts?: { - cards: [ string ], - groups: [ string ], + cards: string[], + groups: string[], } }