change formatting style

This commit is contained in:
Roland Osborne 2024-10-04 14:49:00 -07:00
parent 5ea52edd5c
commit aa2ba3b072
56 changed files with 398 additions and 1991 deletions

View File

@ -10,7 +10,7 @@
],
"scripts": {
"build": "tsup",
"format": "prettier --write src",
"format": "prettier --print-width 200 --write src",
"test": "jest"
},
"repository": {

View File

@ -12,15 +12,7 @@ export class AliasModule implements Alias {
private settings: Settings;
private emitter: EventEmitter;
constructor(
log: Logging,
settings: Settings,
store: Store,
guid: string,
token: string,
node: string,
secure: boolean,
) {
constructor(log: Logging, settings: Settings, store: Store, guid: string, token: string, node: string, secure: boolean) {
this.guid = guid;
this.token = token;
this.node = node;
@ -44,30 +36,19 @@ export class AliasModule implements Alias {
console.log("set alias revision:", rev);
}
public async addGroup(
sealed: boolean,
dataType: string,
subject: string,
cardIds: string[],
): Promise<string> {
public async addGroup(sealed: boolean, dataType: string, subject: string, cardIds: string[]): Promise<string> {
return "";
}
public async removeGroup(groupId: string): Promise<void> {}
public async setGroupSubject(
groupId: string,
subject: string,
): Promise<void> {}
public async setGroupSubject(groupId: string, subject: string): Promise<void> {}
public async setGroupCard(groupId: string, cardId: string): Promise<void> {}
public async clearGroupCard(groupId: string, cardId: string): Promise<void> {}
public async compare(
groupIds: string[],
cardIds: string[],
): Promise<Map<string, string[]>> {
public async compare(groupIds: string[], cardIds: string[]): Promise<Map<string, string[]>> {
return new Map<string, string[]>();
}
}

View File

@ -1,4 +1,4 @@
import type { Channel, Topic, Asset, Tag, Article, Group, Card, Profile, Call, Config, NodeConfig, NodeAccount, Participant } from './types';
import type { Channel, Topic, Asset, Tag, Article, Group, Card, Profile, Call, Config, NodeConfig, NodeAccount, Participant } from "./types";
export interface Session {
getSettings(): Settings;
@ -36,7 +36,7 @@ export interface Settings {
disableNotifications(): Promise<void>;
enableRegistry(): Promise<void>;
disableRegistry(): Promise<void>;
enableMFA(): Promise<{ secretImage: string, secretText: string }>;
enableMFA(): Promise<{ secretImage: string; secretText: string }>;
disableMFA(): Promise<void>;
confirmMFA(code: string): Promise<void>;
setSeal(password: string): Promise<void>;
@ -84,8 +84,8 @@ export interface Contact {
clearBlockArticle(cardId: string, articleId: string): Promise<void>;
clearBlockChannel(cardId: string, channelId: string): Promise<void>;
getBlockedCards(): Promise<{ cardId: string }[]>;
getBlockedChannels(): Promise<{ cardId: string, channelId: string }[]>;
getBlockedArticles(): Promise<{ cardId: string, articleId: string }[]>;
getBlockedChannels(): Promise<{ cardId: string; channelId: string }[]>;
getBlockedArticles(): Promise<{ cardId: string; articleId: string }[]>;
enableChannelNotifications(cardId: string, channelId: string): Promise<void>;
disableChannelNotifications(chardId: string, channelId: string): Promise<void>;
@ -109,7 +109,7 @@ export interface Alias {
setGroupCard(groupId: string, cardId: string): Promise<void>;
clearGroupCard(groupId: string, cardId: string): Promise<void>;
compare(groupIds: string[], cardIds: string[]): Promise<Map<string, string[]>>;
addGroupListener(ev: (groups: Group[]) => void): void;
removeGroupListener(ev: (groups: Group[]) => void): void;
}
@ -151,8 +151,8 @@ export interface Content {
setBlockTag(channelId: string, topicId: string, tagId: string): Promise<void>;
clearBlockTopic(channelId: string, topicId: string): Promise<void>;
clearBlockTag(channelId: string, topicId: string, tagId: string): Promise<void>;
getBlockedTopics(): Promise<{ channelId: string, topicId: string }[]>;
getBlockedTags(): Promise<{ channelId: string, topicId: string, tagId: string }[]>;
getBlockedTopics(): Promise<{ channelId: string; topicId: string }[]>;
getBlockedTags(): Promise<{ channelId: string; topicId: string; tagId: string }[]>;
enableNotifications(channelId: string, memberId: string): Promise<void>;
disableNotifications(channelId: string, memberId: string): Promise<void>;
@ -237,5 +237,3 @@ export interface Contributor {
addTag(topicId: string, type: string, value: string): Promise<string>;
removeTag(topicId: string, tagId: string): Promise<void>;
}

View File

@ -12,15 +12,7 @@ export class AttributeModule implements Attribute {
private settings: Settings;
private emitter: EventEmitter;
constructor(
log: Logging,
settings: Settings,
store: Store,
guid: string,
token: string,
node: string,
secure: boolean,
) {
constructor(log: Logging, settings: Settings, store: Store, guid: string, token: string, node: string, secure: boolean) {
this.guid = guid;
this.token = token;
this.node = node;
@ -44,42 +36,21 @@ export class AttributeModule implements Attribute {
console.log("set attribute revision:", rev);
}
public async addArticle(
sealed: boolean,
type: string,
subject: string,
cardIds: string[],
groupIds: string[],
): Promise<string> {
public async addArticle(sealed: boolean, type: string, subject: string, cardIds: string[], groupIds: string[]): Promise<string> {
return "";
}
public async removeArticle(articleId: string): Promise<void> {}
public async setArticleSubject(
articleId: string,
subject: string,
): Promise<void> {}
public async setArticleSubject(articleId: string, subject: string): Promise<void> {}
public async setArticleCard(
articleId: string,
cardId: string,
): Promise<void> {}
public async setArticleCard(articleId: string, cardId: string): Promise<void> {}
public async clearArticleCard(
articleId: string,
cardId: string,
): Promise<void> {}
public async clearArticleCard(articleId: string, cardId: string): Promise<void> {}
public async setArticleGroup(
articleId: string,
groupId: string,
): Promise<void> {}
public async setArticleGroup(articleId: string, groupId: string): Promise<void> {}
public async clearArticleGroup(
articleId: string,
groupId: string,
): Promise<void> {}
public async clearArticleGroup(articleId: string, groupId: string): Promise<void> {}
public addArticleListener(ev: (articles: Article[]) => void): void {}

View File

@ -47,11 +47,7 @@ export class Connection {
this.emitter.off("status", ev);
}
private setWebSocket(
token: string,
node: string,
secure: boolean,
): WebSocket {
private setWebSocket(token: string, node: string, secure: boolean): WebSocket {
if (this.closed) {
this.emitter.emit("status", "closed");
return this.websocket;
@ -70,15 +66,7 @@ export class Connection {
if (activity.revision) {
this.emitter.emit("revision", activity.revision as Revision);
} else if (activity.ring) {
const {
cardId,
callId,
calleeToken,
ice,
iceUrl,
iceUsername,
icePassword,
} = activity.ring;
const { cardId, callId, calleeToken, ice, iceUrl, iceUsername, icePassword } = activity.ring;
const call: Call = {
cardId,
callId,

View File

@ -2,17 +2,7 @@ import { EventEmitter } from "eventemitter3";
import type { Contact, Logging } from "./api";
import type { Card, Topic, Asset, Tag, Profile, Participant } from "./types";
import type { CardEntity } from "./entities";
import type {
ArticleRevision,
ArticleDetail,
ChannelRevision,
ChannelSummary,
ChannelDetail,
CardRevision,
CardNotification,
CardProfile,
CardDetail,
} from "./items";
import type { ArticleRevision, ArticleDetail, ChannelRevision, ChannelSummary, ChannelDetail, CardRevision, CardNotification, CardProfile, CardDetail } from "./items";
import { defaultCardItem, defaultChannelItem } from "./items";
import { Store } from "./store";
import { getCards } from "./net/getCards";
@ -23,11 +13,11 @@ import { getContactProfile } from "./net/getContactProfile";
import { getContactChannels } from "./net/getContactChannels";
import { getContactChannelDetail } from "./net/getContactChannelDetail";
import { getContactChannelSummary } from "./net/getContactChannelSummary";
import { addCard } from './net/addCard';
import { getContactListing } from './net/getContactListing';
import { setCardConfirmed } from './net/setCardConfirmed';
import { setCardConnecting } from './net/setCardConnecting';
import { setCardConnected } from './net/setCardConnected';
import { addCard } from "./net/addCard";
import { getContactListing } from "./net/getContactListing";
import { setCardConfirmed } from "./net/setCardConfirmed";
import { setCardConnecting } from "./net/setCardConnecting";
import { setCardConnected } from "./net/setCardConnected";
const CLOSE_POLL_MS = 100;
const RETRY_POLL_MS = 2000;
@ -55,28 +45,12 @@ export class ContactModule implements Contact {
private cardEntries: Map<string, { item: CardItem; card: Card }>;
// view of articles
private articleEntries: Map<
string,
Map<string, { item: ArticleItem; article: Article }>
>;
private articleEntries: Map<string, Map<string, { item: ArticleItem; article: Article }>>;
// view of channels
private channelEntries: Map<
string,
Map<string, { item: ChannelItem; channel: Channel }>
>;
private channelEntries: Map<string, Map<string, { item: ChannelItem; channel: Channel }>>;
constructor(
log: Logging,
store: Store,
crypto: Crypto | null,
guid: string,
token: string,
node: string,
secure: boolean,
articleTypes,
channelTypes,
) {
constructor(log: Logging, store: Store, crypto: Crypto | null, guid: string, token: string, node: string, secure: boolean, articleTypes, channelTypes) {
this.guid = guid;
this.token = token;
this.node = node;
@ -90,14 +64,8 @@ export class ContactModule implements Contact {
this.unsealAll = false;
this.cardEntries = new Map<string, { item: CardItem; card: Card }>();
this.articleEntries = new Map<
string,
Map<string, { item: ArticleItem; article: Article }>
>();
this.channelEntries = new Map<
string,
Map<string, { item: ChannelItem; channel: Channel }>
>();
this.articleEntries = new Map<string, Map<string, { item: ArticleItem; article: Article }>>();
this.channelEntries = new Map<string, Map<string, { item: ChannelItem; channel: Channel }>>();
this.revision = 0;
this.sycning = true;
@ -121,13 +89,7 @@ export class ContactModule implements Contact {
const articles = await this.store.getContactCardArticles(guid);
articles.forEach(({ cardId, articleId, item }) => {
if (!this.articleEntries.has(cardId)) {
this.articleEntries.set(
cardId,
new Map<
string,
Map<string, { item: ArticleItem; article: Article }>
>(),
);
this.articleEntries.set(cardId, new Map<string, Map<string, { item: ArticleItem; article: Article }>>());
}
const article = setArticle(cardId, articleId, item);
this.articleEntries.set(cardId).set(articleId, { item, article });
@ -137,13 +99,7 @@ export class ContactModule implements Contact {
const channels = await this.store.getContactCardChannels(guid);
channels.forEach(({ cardId, channelId, item }) => {
if (!this.channelEntries.has(cardId)) {
this.channelEntries.set(
cardId,
new Map<
string,
Map<string, { item: ChannelItem; channel: Channel }>
>(),
);
this.channelEntries.set(cardId, new Map<string, Map<string, { item: ChannelItem; channel: Channel }>>());
}
const channel = setChannel(cardId, channelId, item);
this.channelEntries.set(cardId).set(channelId, { item, channel });
@ -169,9 +125,7 @@ export class ContactModule implements Contact {
const entry = await this.getCardEntry(id);
if (data.detailRevision !== entry.item.detail.revison) {
const detail = data.cardDetail
? data.cardDetail
: await getCardDetail(node, secure, token, id);
const detail = data.cardDetail ? data.cardDetail : await getCardDetail(node, secure, token, id);
const { status, statusUpdated, token: cardToken } = detail;
entry.item.detail = {
revision: data.detailRevision,
@ -180,17 +134,11 @@ export class ContactModule implements Contact {
token: cardToken,
};
entry.card = this.setCard(id, entry.item);
await this.store.setContactCardDetail(
guid,
id,
entry.item.detail,
);
await this.store.setContactCardDetail(guid, id, entry.item.detail);
}
if (data.profileRevision !== entry.item.profile.revision) {
const profile = data.cardProfile
? data.cardProfile
: await getCardProfile(node, secure, token, id);
const profile = data.cardProfile ? data.cardProfile : await getCardProfile(node, secure, token, id);
entry.item.profile = {
revision: data.profileRevision,
handle: profile.handle,
@ -203,31 +151,14 @@ export class ContactModule implements Contact {
seal: profile.seal,
};
entry.card = this.setCard(id, entry.item);
await this.store.setContactCardProfile(
guid,
id,
entry.item.profile,
);
await this.store.setContactCardProfile(guid, id, entry.item.profile);
}
if (
data.notifiedProfile > entry.item.profile.revision &&
data.notifiedProfile !== entry.item.profileRevision
) {
if (data.notifiedProfile > entry.item.profile.revision && data.notifiedProfile !== entry.item.profileRevision) {
try {
await this.syncProfile(
id,
entry.item.profile.node,
entry.item.profile.guid,
entry.item.detail.token,
entry.item.profileRevision,
);
await this.syncProfile(id, entry.item.profile.node, entry.item.profile.guid, entry.item.detail.token, entry.item.profileRevision);
entry.item.profileRevision = data.notifiedProfile;
await this.store.setContactCardProfileRevision(
guid,
id,
data.notifiedProfile,
);
await this.store.setContactCardProfileRevision(guid, id, data.notifiedProfile);
} catch (err) {
this.log.warn(err);
entry.item.offsync = true;
@ -237,19 +168,9 @@ export class ContactModule implements Contact {
if (data.notifiedArticle !== entry.item.articleRevision) {
try {
await this.syncArticles(
id,
entry.item.profile.node,
entry.item.profile.guid,
entry.item.detail.token,
entry.item.articleRevision,
);
await this.syncArticles(id, entry.item.profile.node, entry.item.profile.guid, entry.item.detail.token, entry.item.articleRevision);
entry.item.articleRevision = data.notifiedArticle;
await this.store.setContactCardArticlesRevision(
guid,
id,
data.notifiedArticle,
);
await this.store.setContactCardArticlesRevision(guid, id, data.notifiedArticle);
} catch (err) {
this.log.warn(err);
entry.item.offsync = true;
@ -261,17 +182,9 @@ export class ContactModule implements Contact {
if (data.notifiedChannel !== entry.item.channelRevision) {
try {
const { profile, detail } = entry.item;
await this.syncChannels(
id,
{ guid: profile.guid, node: profile.node, token: detail.token },
entry.item.channelRevision,
);
await this.syncChannels(id, { guid: profile.guid, node: profile.node, token: detail.token }, entry.item.channelRevision);
entry.item.channelRevision = data.notifiedChannel;
await this.store.setContactCardChannelsRevision(
guid,
id,
data.notifiedChannel,
);
await this.store.setContactCardChannelsRevision(guid, id, data.notifiedChannel);
} catch (err) {
this.log.warn(err);
entry.item.offsync = true;
@ -315,20 +228,10 @@ export class ContactModule implements Contact {
const cardId = card.key;
const channelId = channel.key;
if (await this.unsealChannelDetail(cardId, channelId, item)) {
await this.store.setContactCardChannelUnsealedDetail(
guid,
cardId,
channelId,
item.unsealedDetail,
);
await this.store.setContactCardChannelUnsealedDetail(guid, cardId, channelId, item.unsealedDetail);
}
if (await this.unsealChannelSummary(cardId, channelId, item)) {
await this.store.setContactCardChannelUnsealedSummary(
guid,
cardId,
channelId,
item.unsealedSummary,
);
await this.store.setContactCardChannelUnsealedSummary(guid, cardId, channelId, item.unsealedSummary);
}
const channel = setChannel(cardId, channelId, item);
this.channelEntries.set(cardId).set(channelId, { item, channel });
@ -344,87 +247,36 @@ export class ContactModule implements Contact {
}
}
private async syncProfile(
cardId: string,
cardNode: string,
cardGuid: string,
cardToken: string,
revision: number,
): Promise<void> {
private async syncProfile(cardId: string, cardNode: string, cardGuid: string, cardToken: string, revision: number): Promise<void> {
const { node, secure, token } = this;
const server = cardNode ? cardNode : node;
const insecure =
/^(?!0)(?!.*\.$)((1?\d?\d|25[0-5]|2[0-4]\d)(\.|:\d+$|$)){4}$/.test(
server,
);
const message = await getContactProfile(
server,
!insecure,
cardGuid,
cardToken,
);
const insecure = /^(?!0)(?!.*\.$)((1?\d?\d|25[0-5]|2[0-4]\d)(\.|:\d+$|$)){4}$/.test(server);
const message = await getContactProfile(server, !insecure, cardGuid, cardToken);
await setCardProfile(node, secure, token, cardId, message);
}
private async syncArticles(
cardId: string,
cardNode: string,
cardGuid: string,
cardToken: string,
revision: number,
): Promise<void> {
private async syncArticles(cardId: string, cardNode: string, cardGuid: string, cardToken: string, revision: number): Promise<void> {
const { node, secure, token } = this;
const server = cardNode ? cardNode : node;
const insecure =
/^(?!0)(?!.*\.$)((1?\d?\d|25[0-5]|2[0-4]\d)(\.|:\d+$|$)){4}$/.test(
server,
);
const insecure = /^(?!0)(?!.*\.$)((1?\d?\d|25[0-5]|2[0-4]\d)(\.|:\d+$|$)){4}$/.test(server);
}
private async syncChannels(
cardId: string,
card: { guid, node, token },
revision: number,
): Promise<void> {
private async syncChannels(cardId: string, card: { guid; node; token }, revision: number): Promise<void> {
const { guid, node, secure, token, channelTypes } = this;
const server = card.node ? card.node : node;
const insecure =
/^(?!0)(?!.*\.$)((1?\d?\d|25[0-5]|2[0-4]\d)(\.|:\d+$|$)){4}$/.test(
server,
);
const delta = await getContactChannels(
server,
!insecure,
card.guid,
card.token,
revision,
channelTypes,
);
const insecure = /^(?!0)(?!.*\.$)((1?\d?\d|25[0-5]|2[0-4]\d)(\.|:\d+$|$)){4}$/.test(server);
const delta = await getContactChannels(server, !insecure, card.guid, card.token, revision, channelTypes);
for (const entity of delta) {
const { id, revision, data } = entity;
if (data) {
const { detailRevision, topicRevision, channelSummary, channelDetail } =
data;
const { detailRevision, topicRevision, channelSummary, channelDetail } = data;
const entries = this.getChannelEntries(cardId);
const entry = await this.getChannelEntry(entries, cardId, id);
if (detailRevision !== entry.item.detail.revision) {
const detail = channelDetail
? channelDetail
: await getContactChannelDetail(server, !insecure, id);
const {
dataType,
data,
created,
updated,
enableImage,
enableAudio,
enableVideo,
enableBinary,
contacts,
members,
} = detail;
const detail = channelDetail ? channelDetail : await getContactChannelDetail(server, !insecure, id);
const { dataType, data, created, updated, enableImage, enableAudio, enableVideo, enableBinary, contacts, members } = detail;
entry.item.detail = {
revision: detailRevision,
sealed: dataType === "sealed",
@ -442,21 +294,12 @@ export class ContactModule implements Contact {
entry.item.unsealedDetail = null;
await this.unsealChannelDetail(cardId, id, entry.item);
entry.channel = this.setChannel(cardId, id, entry.item);
await this.store.setContactCardChannelDetail(
guid,
cardId,
id,
entry.item.detail,
entry.item.unsealedDetail,
);
await this.store.setContactCardChannelDetail(guid, cardId, id, entry.item.detail, entry.item.unsealedDetail);
}
if (topicRevision !== entry.item.summary.revision) {
const summary = channelSummary
? channelSummary
: await getContactChannelSummary(server, !insecure, id);
const { guid, dataType, data, created, updated, status, transform } =
summary.lastTopic;
const summary = channelSummary ? channelSummary : await getContactChannelSummary(server, !insecure, id);
const { guid, dataType, data, created, updated, status, transform } = summary.lastTopic;
entry.item.summary = {
revision: topicRevision,
guid,
@ -470,14 +313,8 @@ export class ContactModule implements Contact {
entry.item.unsealedSummary = null;
await this.unsealChannelSummary(cardId, id, entry.item);
entry.channel = this.setChannel(cardId, id, entry.item);
await this.store.setContactCardChannelSummary(
guid,
cardId,
id,
entry.item.summary,
entry.item.unsealedSummary,
);
this.emitTopicRevision(cardId, id, card, topicRevision)
await this.store.setContactCardChannelSummary(guid, cardId, id, entry.item.summary, entry.item.unsealedSummary);
this.emitTopicRevision(cardId, id, card, topicRevision);
}
} else {
const channels = this.getChannelEntries(cardId);
@ -487,7 +324,6 @@ export class ContactModule implements Contact {
}
}
public addCardListener(ev: (cards: Card[]) => void): void {
this.emitter.on("card", ev);
const cards = Array.from(this.cardEntries, ([cardId, entry]) => entry.card);
@ -503,35 +339,23 @@ export class ContactModule implements Contact {
this.emitter.emit("card", cards);
}
public addArticleListener(
id: string | null,
ev: (arg: { cardId: string; articles: Article[] }) => void,
): void {
public addArticleListener(id: string | null, ev: (arg: { cardId: string; articles: Article[] }) => void): void {
if (id) {
const cardId = id as string;
this.emitter.on(`article::${cardId}`, ev);
const entries = this.articleEntries.get(cardId);
const articles = entries
? Array.from(entries, ([articleId, entry]) => entry.article)
: [];
const articles = entries ? Array.from(entries, ([articleId, entry]) => entry.article) : [];
ev({ cardId, articles });
} else {
this.emitter.on("article", ev);
this.articleEntries.forEach((entries, cardId) => {
const articles = Array.from(
entries,
([articleId, entry]) => entry.article,
);
const articles = Array.from(entries, ([articleId, entry]) => entry.article);
ev({ cardId, articles });
});
}
}
public removeArticleListener(
id: string | null,
ev: (arg: { cardId: string; articles: Article[] }) => void,
): void {
public removeArticleListener(id: string | null, ev: (arg: { cardId: string; articles: Article[] }) => void): void {
if (id) {
const cardId = id as string;
this.emitter.off(`article::${cardId}`, ev);
@ -542,42 +366,28 @@ export class ContactModule implements Contact {
private emitArticles(cardId: string) {
const entries = this.articleEntries.get(cardId);
const articles = entries
? Array.from(entries, ([articleId, entry]) => entry.article)
: [];
const articles = entries ? Array.from(entries, ([articleId, entry]) => entry.article) : [];
this.emitter.emit("article", { cardId, articles });
this.emitter.emit(`article::${cardId}`, { cardId, articles });
}
public addChannelListener(
id: string | null,
ev: (arg: { cardId: string; channels: Channel[] }) => void,
): void {
public addChannelListener(id: string | null, ev: (arg: { cardId: string; channels: Channel[] }) => void): void {
if (id) {
const cardId = id as string;
this.emitter.on(`channel::${cardId}`, ev);
const entries = this.channelEntries.get(cardId);
const channels = entries
? Array.from(entries, ([channelId, entry]) => entry.channel)
: [];
const channels = entries ? Array.from(entries, ([channelId, entry]) => entry.channel) : [];
ev({ cardId, channels });
} else {
this.emitter.on("channel", ev);
this.channelEntries.forEach((entries, cardId) => {
const channels = Array.from(
entries,
([channelId, entry]) => entry.channel,
);
const channels = Array.from(entries, ([channelId, entry]) => entry.channel);
ev({ cardId, channels });
});
}
}
public removeChannelListener(
id: string | null,
ev: (arg: { cardId: string; channels: Channel[] }) => void,
): void {
public removeChannelListener(id: string | null, ev: (arg: { cardId: string; channels: Channel[] }) => void): void {
if (id) {
const cardId = id as string;
this.emitter.off(`channel::${cardId}`, ev);
@ -588,18 +398,12 @@ export class ContactModule implements Contact {
private emitChannels(cardId: string) {
const entries = this.channelEntries.get(cardId);
const channels = entries
? Array.from(entries, ([channelId, entry]) => entry.channel)
: [];
const channels = entries ? Array.from(entries, ([channelId, entry]) => entry.channel) : [];
this.emitter.emit("channel", { cardId, channels });
this.emitter.emit(`channel::${cardId}`, { cardId, channels });
}
public addTopicRevisionListener(
cardId: string,
channelId: string,
ev: (arg: { cardId: string; channelId: string, guid: string, node: string, token: string, revision: number }) => void,
): void {
public addTopicRevisionListener(cardId: string, channelId: string, ev: (arg: { cardId: string; channelId: string; guid: string; node: string; token: string; revision: number }) => void): void {
this.emitter.on(`revision::${cardId}::${channelId}`, ev);
const entries = this.channelEntries.get(cardId);
if (entries) {
@ -607,28 +411,22 @@ export class ContactModule implements Contact {
if (entry) {
const { profile, detail } = entry.item;
const { guid, node } = profile;
const { token } = detail;
const { token } = detail;
ev({ cardId, channelId, guid, node, token, revision });
}
}
}
public removeTopicRevisionListener(
cardId: string,
channelId: string,
ev: (arg: { cardId: string, channelId: string, guid: string, node: string, token: string, revision: number }) => void,
): void {
public removeTopicRevisionListener(cardId: string, channelId: string, ev: (arg: { cardId: string; channelId: string; guid: string; node: string; token: string; revision: number }) => void): void {
this.emitter.off(`revision::${cardId}::${channelId}`, ev);
}
private emitTopicRevision(cardId: string, channelId: string, card: { guid: string, node: string, token: string }, revision: number) {
private emitTopicRevision(cardId: string, channelId: string, card: { guid: string; node: string; token: string }, revision: number) {
const { guid, node, token } = card;
const channel = { cardId, channelId, guid, node, token, revision };
this.emitter.emit(`revision::${cardId}::${channelId}`, channel);
}
public async setSeal(seal: { privateKey: string; publicKey: string } | null) {
this.seal = seal;
this.unsealAll = true;
@ -643,10 +441,7 @@ export class ContactModule implements Contact {
}
public async addCard(server: string, guid: string): Promise<string> {
const insecure =
/^(?!0)(?!.*\.$)((1?\d?\d|25[0-5]|2[0-4]\d)(\.|:\d+$|$)){4}$/.test(
server,
);
const insecure = /^(?!0)(?!.*\.$)((1?\d?\d|25[0-5]|2[0-4]\d)(\.|:\d+$|$)){4}$/.test(server);
const message = await getContactListing(server, !insecure, guid);
const { node, secure, token } = this;
return await addCard(node, secure, token, message);
@ -657,13 +452,6 @@ export class ContactModule implements Contact {
await removeCard(node, secure, token, cardId);
}
public async confirmCard(cardId: string): Promise<void> {
const { node, secure, token } = this;
await setCardConfirmed(node, secure, token, cardId);
@ -678,19 +466,15 @@ export class ContactModule implements Contact {
const entry = this.cardEntries.get(cardId);
if (entry) {
const server = entry.item.profile.node ? entry.item.profile.node : node;
const insecure =
/^(?!0)(?!.*\.$)((1?\d?\d|25[0-5]|2[0-4]\d)(\.|:\d+$|$)){4}$/.test(
server,
);
const insecure = /^(?!0)(?!.*\.$)((1?\d?\d|25[0-5]|2[0-4]\d)(\.|:\d+$|$)){4}$/.test(server);
const contact = await setCardOpenMessage(server, !insecure, message);
if (contact.status === 'connected') {
if (contact.status === "connected") {
const { token, articleRevision, channelRevision, profileRevision } = contact;
await setCardConnected(node, secure, token, cardId, token, articleRevision, channelRevision, profileRevision);
}
}
}
catch (err) {
this.log.error('failed to deliver open message');
} catch (err) {
this.log.error("failed to deliver open message");
}
}
@ -702,15 +486,11 @@ export class ContactModule implements Contact {
const entry = this.cardEntries.get(cardId);
if (entry) {
const server = entry.item.profile.node ? entry.item.profile.node : node;
const insecure =
/^(?!0)(?!.*\.$)((1?\d?\d|25[0-5]|2[0-4]\d)(\.|:\d+$|$)){4}$/.test(
server,
);
const insecure = /^(?!0)(?!.*\.$)((1?\d?\d|25[0-5]|2[0-4]\d)(\.|:\d+$|$)){4}$/.test(server);
await setCardCloseMessage(server, !insecure, message);
}
}
catch (err) {
this.log.warn('failed to deliver close message');
} catch (err) {
this.log.warn("failed to deliver close message");
}
}
@ -728,15 +508,9 @@ export class ContactModule implements Contact {
public async setBlockCard(cardId: string): Promise<void> {}
public async setBlockArticle(
cardId: string,
articleId: string,
): Promise<void> {}
public async setBlockArticle(cardId: string, articleId: string): Promise<void> {}
public async setBlockChannel(
cardId: string,
channelId: string,
): Promise<void> {
public async setBlockChannel(cardId: string, channelId: string): Promise<void> {
const entries = this.channelEntries.get(cardId);
if (entries) {
const entry = entries.get(channelId);
@ -751,61 +525,33 @@ export class ContactModule implements Contact {
public async clearBlockCard(cardId: string): Promise<void> {}
public async clearBlockArticle(
cardId: string,
articleId: string,
): Promise<void> {}
public async clearBlockArticle(cardId: string, articleId: string): Promise<void> {}
public async clearBlockChannel(
cardId: string,
channelId: string,
): Promise<void> {}
public async clearBlockChannel(cardId: string, channelId: string): Promise<void> {}
public async getBlockedCards(): Promise<{ cardId: string }[]> {
return [];
}
public async getBlockedChannels(): Promise<
{ cardId: string; channelId: string }[]
> {
public async getBlockedChannels(): Promise<{ cardId: string; channelId: string }[]> {
return [];
}
public async getBlockedArticles(): Promise<
{ cardId: string; articleId: string }[]
> {
public async getBlockedArticles(): Promise<{ cardId: string; articleId: string }[]> {
return [];
}
public async removeArticle(
cardId: string,
articleId: string,
): Promise<void> {}
public async removeArticle(cardId: string, articleId: string): Promise<void> {}
public async removeChannel(
cardId: string,
channelId: string,
): Promise<void> {}
public async removeChannel(cardId: string, channelId: string): Promise<void> {}
public async enableChannelNotifications(
cardId: string,
channelId: string,
): Promise<void> {}
public async enableChannelNotifications(cardId: string, channelId: string): Promise<void> {}
public async disableChannelNotifications(
cardId: string,
channelid: string,
): Promise<void> {}
public async disableChannelNotifications(cardId: string, channelid: string): Promise<void> {}
public async setUnreadChannel(
cardId: string,
channelId: string,
): Promise<void> {}
public async setUnreadChannel(cardId: string, channelId: string): Promise<void> {}
public async clearUnreadChannel(
cardId: string,
channelId: string,
): Promise<void> {}
public async clearUnreadChannel(cardId: string, channelId: string): Promise<void> {}
public async getRegistry(server: string): Promise<Profile[]> {
return [];
@ -819,17 +565,9 @@ export class ContactModule implements Contact {
return "";
}
private setCard(cardId: string, item: CardItem): Card {
const { offsync, blocked, profile, detail } = item;
const { guid, handle, name, description, location, imageSet, node } =
profile;
const { guid, handle, name, description, location, imageSet, node } = profile;
const { status, statusUpdated } = detail;
return {
cardId,
@ -847,11 +585,7 @@ export class ContactModule implements Contact {
};
}
private setArticle(
cardId: string,
articleId: string,
item: ArticleItem,
): Article {
private setArticle(cardId: string, articleId: string, item: ArticleItem): Article {
const { unread, blocked, detail, unsealedData } = item;
const { dataType, sealed, data, created, updated } = detail;
const articleData = sealed ? unsealedData : data;
@ -866,46 +600,24 @@ export class ContactModule implements Contact {
};
}
private async getChannelKey(
seals: [{ publicKey: string; sealedKey: string }],
): Promise<string | null> {
const seal = seals.find(
({ publicKey }) => publicKey === this.seal.publicKey,
);
private async getChannelKey(seals: [{ publicKey: string; sealedKey: string }]): Promise<string | null> {
const seal = seals.find(({ publicKey }) => publicKey === this.seal.publicKey);
if (seal) {
const key = await this.crypto.rsaDecrypt(
seal.sealedKey,
this.seal.privateKey,
);
const key = await this.crypto.rsaDecrypt(seal.sealedKey, this.seal.privateKey);
return key.data;
}
return null;
}
private async unsealChannelDetail(
cardId: string,
channelId: string,
item: ChannelItem,
): Promise<boolean> {
if (
item.unsealedDetail == null &&
item.detail.dataType === "sealed" &&
this.seal &&
this.crypto
) {
private async unsealChannelDetail(cardId: string, channelId: string, item: ChannelItem): Promise<boolean> {
if (item.unsealedDetail == null && item.detail.dataType === "sealed" && this.seal && this.crypto) {
try {
const { subjectEncrypted, subjectIv, seals } = JSON.parse(
item.detail.data,
);
const { subjectEncrypted, subjectIv, seals } = JSON.parse(item.detail.data);
if (!item.channelKey) {
item.channelKey = await this.getChannelKey(seals);
}
if (item.channelKey) {
const { data } = await this.crypto.aesDecrypt(
subjectEncrypted,
subjectIv,
item.channelKey,
);
const { data } = await this.crypto.aesDecrypt(subjectEncrypted, subjectIv, item.channelKey);
const { subject } = JSON.parse(data);
item.unsealedDetail = subject;
return true;
@ -917,17 +629,8 @@ export class ContactModule implements Contact {
return false;
}
private async unsealChannelSummary(
cardId: string,
channelId: string,
item: ChannelItem,
): Promise<boolean> {
if (
item.unsealedSummary == null &&
item.summary.dataType === "sealedtopic" &&
this.seal &&
this.crypto
) {
private async unsealChannelSummary(cardId: string, channelId: string, item: ChannelItem): Promise<boolean> {
if (item.unsealedSummary == null && item.summary.dataType === "sealedtopic" && this.seal && this.crypto) {
try {
if (!item.channelKey) {
const { seals } = JSON.parse(item.detail.data);
@ -935,11 +638,7 @@ export class ContactModule implements Contact {
}
if (item.channelKey) {
const { messageEncrypted, messageIv } = JSON.parse(item.summary.data);
const { data } = await this.crypto.aesDecrypt(
messageEncrypted,
messageIv,
item.channelKey,
);
const { data } = await this.crypto.aesDecrypt(messageEncrypted, messageIv, item.channelKey);
const { message } = JSON.parse(data);
item.unsealedSummary = message;
return true;
@ -951,17 +650,12 @@ export class ContactModule implements Contact {
return false;
}
private setChannel(
cardId: string,
channelId: string,
item: ChannelItem,
): Channel {
private setChannel(cardId: string, channelId: string, item: ChannelItem): Channel {
const { summary, detail } = item;
const detailData = detail.sealed ? item.unsealedDetail : detail.data;
const summaryData = summary.sealed ? item.unsealedSummary : summary.data;
const { pushEnabled } =
detail.members.find(({ member }) => member === this.guid) | {};
const { pushEnabled } = detail.members.find(({ member }) => member === this.guid) | {};
const contacts = detail.members
.filter(({ member }) => member != this.guid)
.map(({ member, pushEnabled }) => {
@ -997,8 +691,6 @@ export class ContactModule implements Contact {
};
}
private async getCardEntry(cardId: string) {
const { guid } = this;
const entry = this.cardEntries.get(cardId);
@ -1023,11 +715,7 @@ export class ContactModule implements Contact {
return channels;
}
private async getChannelEntry(
channels: Map<string, { item: ChannelItem; channel: Channel }>,
cardId: string,
channelId: string,
) {
private async getChannelEntry(channels: Map<string, { item: ChannelItem; channel: Channel }>, cardId: string, channelId: string) {
const { guid } = this;
const entry = channels.get(channelId);
if (entry) {
@ -1040,6 +728,4 @@ export class ContactModule implements Contact {
await this.store.addContactCardChannel(guid, cardId, channelId, item);
return channelEntry;
}
}

View File

@ -12,15 +12,7 @@ export class ContentModule implements Content {
private settings: Settings;
private emitter: EventEmitter;
constructor(
log: Logging,
settings: Settings,
store: Store,
guid: string,
token: string,
node: string,
secure: boolean,
) {
constructor(log: Logging, settings: Settings, store: Store, guid: string, token: string, node: string, secure: boolean) {
this.guid = guid;
this.token = token;
this.node = node;
@ -44,49 +36,23 @@ export class ContentModule implements Content {
console.log("set content revision:", rev);
}
public async addChannel(
sealed: boolean,
type: string,
subject: string,
cardIds: string[],
groupIds: string[],
): Promise<string> {
public async addChannel(sealed: boolean, type: string, subject: string, cardIds: string[], groupIds: string[]): Promise<string> {
return "";
}
public async removeChannel(channelId: string): Promise<void> {}
public async setChannelSubject(
channelId: string,
subject: string,
): Promise<void> {}
public async setChannelSubject(channelId: string, subject: string): Promise<void> {}
public async setChannelCard(
channelId: string,
cardId: string,
): Promise<void> {}
public async setChannelCard(channelId: string, cardId: string): Promise<void> {}
public async clearChannelCard(
channelId: string,
cardId: string,
): Promise<void> {}
public async clearChannelCard(channelId: string, cardId: string): Promise<void> {}
public async setChannelGroup(
channelId: string,
groupId: string,
): Promise<void> {}
public async setChannelGroup(channelId: string, groupId: string): Promise<void> {}
public async clearChannelGroup(
channelId: string,
groupId: string,
): Promise<void> {}
public async clearChannelGroup(channelId: string, groupId: string): Promise<void> {}
public async addTopic(
channelId: string,
type: string,
subject: string,
assets: Asset[],
): Promise<string> {
public async addTopic(channelId: string, type: string, subject: string, assets: Asset[]): Promise<string> {
return "";
}
@ -94,162 +60,67 @@ export class ContentModule implements Content {
public async flagTopic(channelId: string, topicId: string): Promise<void> {}
public async setTopicSubject(
channelId: string,
topicId: string,
subject: string,
): Promise<void> {}
public async setTopicSubject(channelId: string, topicId: string, subject: string): Promise<void> {}
public async setTopicSort(
channelId: string,
topicId: string,
sort: number,
): Promise<void> {}
public async setTopicSort(channelId: string, topicId: string, sort: number): Promise<void> {}
public async addTag(
channelId: string,
topicId: string,
type: string,
value: string,
): Promise<string> {
public async addTag(channelId: string, topicId: string, type: string, value: string): Promise<string> {
return "";
}
public async removeTag(
channelId: string,
topicId: string,
tagId: string,
): Promise<void> {}
public async removeTag(channelId: string, topicId: string, tagId: string): Promise<void> {}
public async setTagSubject(
channelId: string,
topicId: string,
tagId: string,
subject: string,
): Promise<void> {}
public async setTagSubject(channelId: string, topicId: string, tagId: string, subject: string): Promise<void> {}
public async setTagSort(
channelId: string,
topicId: string,
tagId: string,
sort: number,
): Promise<void> {}
public async setTagSort(channelId: string, topicId: string, tagId: string, sort: number): Promise<void> {}
public async flagTag(
channelId: string,
topicId: string,
tagId: string,
): Promise<void> {}
public async flagTag(channelId: string, topicId: string, tagId: string): Promise<void> {}
public async setBlockTopic(
channelId: string,
topicId: string,
): Promise<void> {}
public async setBlockTopic(channelId: string, topicId: string): Promise<void> {}
public async setBlockTag(
channelId: string,
topicId: string,
tagId: string,
): Promise<void> {}
public async setBlockTag(channelId: string, topicId: string, tagId: string): Promise<void> {}
public async clearBlockTopic(
channelId: string,
topicId: string,
): Promise<void> {}
public async clearBlockTopic(channelId: string, topicId: string): Promise<void> {}
public async clearBlockTag(
channelId: string,
topicId: string,
tagId: string,
): Promise<void> {}
public async clearBlockTag(channelId: string, topicId: string, tagId: string): Promise<void> {}
public async getBlockedTopics(): Promise<
{ channelId: string; topicId: string }[]
> {
public async getBlockedTopics(): Promise<{ channelId: string; topicId: string }[]> {
return [];
}
public async getBlockedTags(): Promise<
{ channelId: string; topicId: string; tagId: string }[]
> {
public async getBlockedTags(): Promise<{ channelId: string; topicId: string; tagId: string }[]> {
return [];
}
public async enableNotifications(
channelId: string,
memberId: string,
): Promise<void> {}
public async enableNotifications(channelId: string, memberId: string): Promise<void> {}
public async disableNotifications(
channelId: string,
memberId: string,
): Promise<void> {}
public async disableNotifications(channelId: string, memberId: string): Promise<void> {}
public async enableSortTopic(
channelId: string,
memberId: string,
): Promise<void> {}
public async enableSortTopic(channelId: string, memberId: string): Promise<void> {}
public async disableSortTopic(
channelId: string,
memberId: string,
): Promise<void> {}
public async disableSortTopic(channelId: string, memberId: string): Promise<void> {}
public async enableSortTag(
channelId: string,
memberId: string,
): Promise<void> {}
public async enableSortTag(channelId: string, memberId: string): Promise<void> {}
public async disableSortTag(
channelId: string,
memberId: string,
): Promise<void> {}
public async disableSortTag(channelId: string, memberId: string): Promise<void> {}
public async enableAddTopic(
channelId: string,
memberId: string,
): Promise<void> {}
public async enableAddTopic(channelId: string, memberId: string): Promise<void> {}
public async disableAddTopic(
channelId: string,
memberId: string,
): Promise<void> {}
public async disableAddTopic(channelId: string, memberId: string): Promise<void> {}
public async enableAddTag(
channelId: string,
memberId: string,
): Promise<void> {}
public async enableAddTag(channelId: string, memberId: string): Promise<void> {}
public async disableAddTag(
channelId: string,
memberId: string,
): Promise<void> {}
public async disableAddTag(channelId: string, memberId: string): Promise<void> {}
public async enableAddAsset(
channelId: string,
memberId: string,
): Promise<void> {}
public async enableAddAsset(channelId: string, memberId: string): Promise<void> {}
public async disableAddAsset(
channelId: string,
memberId: string,
): Promise<void> {}
public async disableAddAsset(channelId: string, memberId: string): Promise<void> {}
public async enableAddParticipant(
channelId: string,
memberId: string,
): Promise<void> {}
public async enableAddParticipant(channelId: string, memberId: string): Promise<void> {}
public async disableAddParticipant(
channelId: string,
memberId: string,
): Promise<void> {}
public async disableAddParticipant(channelId: string, memberId: string): Promise<void> {}
public getTopicAssetUrl(
channelId: string,
topicId: string,
assetId: string,
): string {
public getTopicAssetUrl(channelId: string, topicId: string, assetId: string): string {
return "";
}
@ -273,15 +144,9 @@ export class ContentModule implements Content {
public async clearUnreadChannel(channelId: string): Promise<void> {}
public async addParticipantAccess(
channelId: string,
name: string,
): Promise<Participant> {
public async addParticipantAccess(channelId: string, name: string): Promise<Participant> {
return { id: "", guid: "", name: "", server: "", token: "" };
}
public async removeParticipantAccess(
channelId: string,
repeaterId: string,
): Promise<void> {}
public async removeParticipantAccess(channelId: string, repeaterId: string): Promise<void> {}
}

View File

@ -10,13 +10,7 @@ export class ContributorModule implements Contributor {
private secure: boolean;
private token: string;
constructor(
log: Logging,
crypto: Crypto | null,
node: string,
secure: boolean,
token: string,
) {
constructor(log: Logging, crypto: Crypto | null, node: string, secure: boolean, token: string) {
this.log = log;
this.crypto = crypto;
this.node = node;
@ -24,21 +18,13 @@ export class ContributorModule implements Contributor {
this.token = token;
}
public async addTopic(
type: string,
message: string,
assets: Asset[],
): Promise<string> {
public async addTopic(type: string, message: string, assets: Asset[]): Promise<string> {
return "";
}
public async removeTopic(topicId: string): Promise<void> {}
public async addTag(
topicId: string,
type: string,
value: string,
): Promise<string> {
public async addTag(topicId: string, type: string, value: string): Promise<string> {
return "";
}

View File

@ -12,31 +12,17 @@ export interface Crypto {
aesIv(): Promise<{ ivHex: string }>;
// encrypt data with aes key and iv
aesEncrypt(
data: string,
ivHex: string,
aesKeyHex: string,
): Promise<{ encryptedDataB64: string }>;
aesEncrypt(data: string, ivHex: string, aesKeyHex: string): Promise<{ encryptedDataB64: string }>;
// decrypt data with aes key and iv
aesDecrypt(
encryptedDataB64: string,
ivHex: string,
aesKeyHex: string,
): Promise<{ data: string }>;
aesDecrypt(encryptedDataB64: string, ivHex: string, aesKeyHex: string): Promise<{ data: string }>;
// generate rsa key
rsaKey(): Promise<{ publicKeyB64: string; privateKeyB64: string }>;
// encrypt data with public rsa key
rsaEncrypt(
data: string,
publicKeyB64: string,
): Promise<{ encryptedDataB64: string }>;
rsaEncrypt(data: string, publicKeyB64: string): Promise<{ encryptedDataB64: string }>;
// decrypt data with private rsa key
rsaDecrypt(
encryptedDataB64: string,
privateKeyB64: string,
): Promise<{ data: string }>;
rsaDecrypt(encryptedDataB64: string, privateKeyB64: string): Promise<{ data: string }>;
}

View File

@ -1,12 +1,12 @@
import type { Profile } from "./types";
export type ContactStatus = {
token: string,
status: string,
viewRevision: number,
channelRevision: number,
profileRevision: number,
articleRevision: number,
token: string;
status: string;
viewRevision: number;
channelRevision: number;
profileRevision: number;
articleRevision: number;
};
export type DataMessage = {

View File

@ -13,15 +13,7 @@ export class FocusModule implements Focus {
private log: Logging;
private emitter: EventEmitter;
constructor(
log: Logging,
identity: Identity,
contact: Contact,
content: Content,
store: Store,
cardId: string | null,
channelId: string,
) {
constructor(log: Logging, identity: Identity, contact: Contact, content: Content, store: Store, cardId: string | null, channelId: string) {
this.identity = identity;
this.contact = contact;
this.content = content;
@ -33,58 +25,33 @@ export class FocusModule implements Focus {
public blur(): void {}
public async addTopic(
type: string,
message: string,
assets: Asset[],
): Promise<string> {
public async addTopic(type: string, message: string, assets: Asset[]): Promise<string> {
return "";
}
public async removeTopic(topicId: string): Promise<void> {}
public async setTopicSubject(
topicId: string,
subject: string,
): Promise<void> {}
public async setTopicSubject(topicId: string, subject: string): Promise<void> {}
public async setTopicSort(topicId: string, sort: number): Promise<void> {}
public async addTag(
topicId: string,
type: string,
subject: string,
): Promise<string> {
public async addTag(topicId: string, type: string, subject: string): Promise<string> {
return "";
}
public async removeTag(cardId: string, tagId: string): Promise<void> {}
public async setTagSubject(
topicId: string,
tagId: string,
subject: string,
): Promise<void> {}
public async setTagSubject(topicId: string, tagId: string, subject: string): Promise<void> {}
public async setTagSort(
topicId: string,
tagId: string,
sort: number,
): Promise<void> {}
public async setTagSort(topicId: string, tagId: string, sort: number): Promise<void> {}
public async viewMoreTopics(): Promise<void> {}
public async viewMoreTags(topicId: string): Promise<void> {}
public async setUnreadChannel(
cardId: string,
channelId: string,
): Promise<void> {}
public async setUnreadChannel(cardId: string, channelId: string): Promise<void> {}
public async clearUnreadChannel(
cardId: string,
channelId: string,
): Promise<void> {}
public async clearUnreadChannel(cardId: string, channelId: string): Promise<void> {}
public getTopicAssetUrl(topicId: string, assetId: string): string {
return "";

View File

@ -27,14 +27,7 @@ export class IdentityModule implements Identity {
private closing: boolean;
private emitter: EventEmitter;
constructor(
log: Logging,
store: Store,
guid: string,
token: string,
node: string,
secure: boolean,
) {
constructor(log: Logging, store: Store, guid: string, token: string, node: string, secure: boolean) {
this.guid = guid;
this.token = token;
this.node = node;
@ -99,18 +92,7 @@ export class IdentityModule implements Identity {
}
public setProfile() {
const {
guid,
handle,
name,
description,
location,
image,
revision,
seal,
version,
node,
} = this.profile;
const { guid, handle, name, description, location, image, revision, seal, version, node } = this.profile;
return {
guid,
handle,
@ -145,11 +127,7 @@ export class IdentityModule implements Identity {
await this.sync();
}
public async setProfileData(
name: string,
location: string,
description: string,
): Promise<void> {
public async setProfileData(name: string, location: string, description: string): Promise<void> {
const { node, secure, token } = this;
await setProfileData(node, secure, token, name, location, description);
}

View File

@ -40,79 +40,28 @@ export class DatabagSDK {
const { articleTypes, channelTypes } = this.params;
this.store = new OfflineStore(this.log, sql);
const login = await this.store.init();
return login
? new SessionModule(
this.store,
this.crypto,
this.log,
login.guid,
login.token,
login.node,
login.secure,
login.timestamp,
articleTypes,
channelTypes,
)
: null;
return login ? new SessionModule(this.store, this.crypto, this.log, login.guid, login.token, login.node, login.secure, login.timestamp, articleTypes, channelTypes) : null;
}
public async initOnlineStore(web: WebStore): Promise<Session | null> {
const { articleTypes, channelTypes } = this.params;
this.store = new OnlineStore(this.log, web);
const login = await this.store.init();
return login
? new SessionModule(
this.store,
this.crypto,
this.log,
login.guid,
login.token,
login.node,
login.secure,
login.timestamp,
articleTypes,
channelTypes,
)
: null;
return login ? new SessionModule(this.store, this.crypto, this.log, login.guid, login.token, login.node, login.secure, login.timestamp, articleTypes, channelTypes) : null;
}
public async available(node: string, secure: boolean): Promise<number> {
return await getAvailable(node, secure);
}
public async username(
name: string,
token: string,
node: string,
secure: boolean,
): Promise<boolean> {
public async username(name: string, token: string, node: string, secure: boolean): Promise<boolean> {
return await getUsername(name, token, null, node, secure);
}
public async login(
handle: string,
password: string,
node: string,
secure: boolean,
mfaCode: string | null,
params: SessionParams,
): Promise<Session> {
public async login(handle: string, password: string, node: string, secure: boolean, mfaCode: string | null, params: SessionParams): Promise<Session> {
const { articleTypes, channelTypes } = this.params;
const { appName, version, deviceId, deviceToken, pushType, notifications } =
params;
const { guid, appToken, created, pushSupported } = await setLogin(
node,
secure,
handle,
password,
mfaCode,
appName,
version,
deviceId,
deviceToken,
pushType,
notifications,
);
const { appName, version, deviceId, deviceToken, pushType, notifications } = params;
const { guid, appToken, created, pushSupported } = await setLogin(node, secure, handle, password, mfaCode, appName, version, deviceId, deviceToken, pushType, notifications);
const login: Login = {
guid,
node,
@ -122,40 +71,13 @@ export class DatabagSDK {
pushSupported,
};
await this.store.setLogin(login);
return new SessionModule(
this.store,
this.crypto,
this.log,
guid,
appToken,
node,
secure,
created,
articleTypes,
channelTypes,
);
return new SessionModule(this.store, this.crypto, this.log, guid, appToken, node, secure, created, articleTypes, channelTypes);
}
public async access(
node: string,
secure: boolean,
token: string,
params: SessionParams,
): Promise<Session> {
public async access(node: string, secure: boolean, token: string, params: SessionParams): Promise<Session> {
const { articleTypes, channelTypes } = this.params;
const { appName, version, deviceId, deviceToken, pushType, notifications } =
params;
const { guid, appToken, created, pushSupported } = await setAccess(
node,
secure,
token,
appName,
version,
deviceId,
deviceToken,
pushType,
notifications,
);
const { appName, version, deviceId, deviceToken, pushType, notifications } = params;
const { guid, appToken, created, pushSupported } = await setAccess(node, secure, token, appName, version, deviceId, deviceToken, pushType, notifications);
const login: Login = {
guid,
node,
@ -165,45 +87,14 @@ export class DatabagSDK {
pushSupported,
};
await this.store.setLogin(login);
return new SessionModule(
this.store,
this.crypto,
this.log,
guid,
appToken,
node,
secure,
created,
articleTypes,
channelTypes,
);
return new SessionModule(this.store, this.crypto, this.log, guid, appToken, node, secure, created, articleTypes, channelTypes);
}
public async create(
handle: string,
password: string,
node: string,
secure: boolean,
token: string | null,
params: SessionParams,
): Promise<Session> {
public async create(handle: string, password: string, node: string, secure: boolean, token: string | null, params: SessionParams): Promise<Session> {
const { articleTypes, channelTypes } = this.params;
await addAccount(node, secure, handle, password, token);
const { appName, version, deviceId, deviceToken, pushType, notifications } =
params;
const { guid, appToken, created, pushSupported } = await setLogin(
node,
secure,
handle,
password,
null,
appName,
version,
deviceId,
deviceToken,
pushType,
notifications,
);
const { appName, version, deviceId, deviceToken, pushType, notifications } = params;
const { guid, appToken, created, pushSupported } = await setLogin(node, secure, handle, password, null, appName, version, deviceId, deviceToken, pushType, notifications);
const login: Login = {
guid,
node,
@ -213,18 +104,7 @@ export class DatabagSDK {
pushSupported,
};
await this.store.setLogin(login);
return new SessionModule(
this.store,
this.crypto,
this.log,
guid,
appToken,
node,
secure,
created,
articleTypes,
channelTypes,
);
return new SessionModule(this.store, this.crypto, this.log, guid, appToken, node, secure, created, articleTypes, channelTypes);
}
public async remove(session: Session): Promise<void> {
@ -255,21 +135,12 @@ export class DatabagSDK {
});
}
public async configure(
node: string,
secure: boolean,
token: string,
mfaCode: string | null,
): Promise<Node> {
public async configure(node: string, secure: boolean, token: string, mfaCode: string | null): Promise<Node> {
const access = await setAdmin(node, secure, token, mfaCode);
return new NodeModule(this.log, node, secure, token);
}
public async automate(
node: string,
secure: boolean,
token: string,
): Promise<Contributor> {
public async automate(node: string, secure: boolean, token: string): Promise<Contributor> {
return new ContributorModule(this.log, this.crypto, node, secure, token);
}
}

View File

@ -1,22 +1,12 @@
import { checkResponse, fetchWithTimeout } from "./fetchUtil";
import { encode } from "./base64";
export async function addAccount(
node: string,
secure: boolean,
username: string,
password: string,
token: string | null,
): Promise<void> {
export async function addAccount(node: string, secure: boolean, username: string, password: string, token: string | null): Promise<void> {
const access = token ? `?token=${token}` : "";
const endpoint = `http${secure ? "s" : ""}://${node}/account/profile${access}`;
const auth = encode(`${username}:${password}`);
const headers = new Headers();
headers.append("Credentials", `Basic ${auth}`);
const { status } = await fetchWithTimeout(
endpoint,
{ method: "POST", headers },
60000,
);
const { status } = await fetchWithTimeout(endpoint, { method: "POST", headers }, 60000);
checkResponse(status);
}

View File

@ -1,10 +1,6 @@
import { checkResponse, fetchWithTimeout } from "./fetchUtil";
export async function addAccountMFAuth(
node: string,
secure: boolean,
token: string,
): Promise<{ secretText: string; secretImage: string }> {
export async function addAccountMFAuth(node: string, secure: boolean, token: string): Promise<{ secretText: string; secretImage: string }> {
const endpoint = `http${secure ? "s" : ""}://${node}/account/mfauth?agent=${token}`;
const auth = await fetchWithTimeout(endpoint, { method: "POST" });
checkResponse(auth.status);

View File

@ -1,11 +1,6 @@
import { checkResponse, fetchWithTimeout } from "./fetchUtil";
export async function addCard(
node: string,
secure: boolean,
token: string,
message: string,
) {
export async function addCard(node: string, secure: boolean, token: string, message: string) {
const endpoint = `http${secure ? "s" : ""}://${node}/contact/cards?agent=${token}`;
const { status } = await fetchWithTimeout(endpoint, {
method: "POST",

View File

@ -3,8 +3,7 @@ export function encode(input: string) {
let chr1, chr2, chr3, enc1, enc2, enc3, enc4;
let i = 0;
const _keyStr =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
const _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
input = _utf8_encode(input);
@ -24,12 +23,7 @@ export function encode(input: string) {
enc4 = 64;
}
output =
output +
_keyStr.charAt(enc1) +
_keyStr.charAt(enc2) +
_keyStr.charAt(enc3) +
_keyStr.charAt(enc4);
output = output + _keyStr.charAt(enc1) + _keyStr.charAt(enc2) + _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
}
return output;

View File

@ -1,10 +1,6 @@
import { checkResponse, fetchWithTimeout } from "./fetchUtil";
export async function clearAccountSeal(
node: string,
secure: boolean,
token: string,
) {
export async function clearAccountSeal(node: string, secure: boolean, token: string) {
const endpoint = `http${secure ? "s" : ""}://${node}/account/seal?agent=${token}`;
const seal = await fetchWithTimeout(endpoint, { method: "DELETE" });
checkResponse(seal.status);

View File

@ -1,11 +1,6 @@
import { checkResponse, fetchWithTimeout } from "./fetchUtil";
export async function clearLogin(
node: string,
secure: boolean,
token: string,
all: boolean,
): Promise<void> {
export async function clearLogin(node: string, secure: boolean, token: string, all: boolean): Promise<void> {
const param = all ? "&all=true" : "";
const endpoint = `http${secure ? "s" : ""}://${node}/account/apps?agent=${token}${param}`;
const { status } = await fetchWithTimeout(endpoint, { method: "DELETE" });

View File

@ -8,17 +8,11 @@ export function checkResponse(code: number) {
}
}
export async function fetchWithTimeout(
url: string,
options: RequestInit,
timeout: number = TIMEOUT,
): Promise<Response> {
export async function fetchWithTimeout(url: string, options: RequestInit, timeout: number = TIMEOUT): Promise<Response> {
return Promise.race<Response>([
fetch(url, options).catch((err) => {
throw new Error(url + " failed");
}),
new Promise((_, reject) =>
setTimeout(() => reject(new Error(url + " timeout")), TIMEOUT),
),
new Promise((_, reject) => setTimeout(() => reject(new Error(url + " timeout")), TIMEOUT)),
]);
}

View File

@ -1,11 +1,7 @@
import { checkResponse, fetchWithTimeout } from "./fetchUtil";
import { ConfigEntity } from "../entities";
export async function getAccountStatus(
node: string,
secure: boolean,
token: string,
): Promise<ConfigEntity> {
export async function getAccountStatus(node: string, secure: boolean, token: string): Promise<ConfigEntity> {
const endpoint = `http${secure ? "s" : ""}://${node}/account/status?agent=${token}`;
const status = await fetchWithTimeout(endpoint, { method: "GET" });
checkResponse(status.status);

View File

@ -1,9 +1,6 @@
import { checkResponse, fetchWithTimeout } from "./fetchUtil";
export async function getAvailable(
node: string,
secure: boolean,
): Promise<number> {
export async function getAvailable(node: string, secure: boolean): Promise<number> {
const endpoint = `http${secure ? "s" : ""}://${node}/account/available`;
const available = await fetchWithTimeout(endpoint, { method: "GET" });
checkResponse(available.status);

View File

@ -1,8 +1,7 @@
import { checkResponse, fetchWithTimeout } from './fetchUtil';
import { checkResponse, fetchWithTimeout } from "./fetchUtil";
export async function getCardCloseMessage(node: string, secure: boolean): Promise<void> {
const endpoint = `http${secure ? "s" : ""}://${node}/contact/closeMessage`;
const close = await fetchWithTimeout(endpoint, { method: "GET" });
checkResponse(close.status);
}

View File

@ -1,12 +1,7 @@
import { checkResponse, fetchWithTimeout } from "./fetchUtil";
import { CardDetailEntity } from "../entities";
export async function getCardDetail(
node: string,
secure: boolean,
token: string,
cardId: string,
): Promise<CardDetailEntity> {
export async function getCardDetail(node: string, secure: boolean, token: string, cardId: string): Promise<CardDetailEntity> {
const endpoint = `http${secure ? "s" : ""}://${node}/contact/cards/${cardId}/detail?agent=${token}`;
const detail = await fetchWithTimeout(endpoint, { method: "GET" });
checkResponse(detail.status);

View File

@ -1,9 +1,8 @@
import { checkResponse, fetchWithTimeout } from './fetchUtil';
import { CardStatus } from '../entities';
import { checkResponse, fetchWithTimeout } from "./fetchUtil";
import { CardStatus } from "../entities";
export async function getCardOpenMessage(node: string, secure: boolean): Promise<CardStatus> {
const endpoint = `http${secure ? "s" : ""}://${node}/contact/openMessage`;
const open = await fetchWithTimeout(endpoint, { method: "GET" });
checkResponse(open.status);
}

View File

@ -1,12 +1,7 @@
import { checkResponse, fetchWithTimeout } from "./fetchUtil";
import { CardProfileEntity } from "../entities";
export async function getCardProfile(
node: string,
secure: boolean,
token: string,
cardId: string,
): Promise<CardProfileEntity> {
export async function getCardProfile(node: string, secure: boolean, token: string, cardId: string): Promise<CardProfileEntity> {
const endpoint = `http${secure ? "s" : ""}://${node}/contact/cards/${cardId}/profile?agent=${token}`;
const profile = await fetchWithTimeout(endpoint, { method: "GET" });
checkResponse(profile.status);

View File

@ -1,12 +1,7 @@
import { checkResponse, fetchWithTimeout } from "./fetchUtil";
import { CardEntity } from "../entities";
export async function getCards(
node: string,
secure: boolean,
token: string,
revision: number,
): Promise<CardEntity[]> {
export async function getCards(node: string, secure: boolean, token: string, revision: number): Promise<CardEntity[]> {
const param = revision ? `&revision=${revision}` : "";
const endpoint = `http${secure ? "s" : ""}://${node}/contact/cards?agent=${token}${param}`;
const cards = await fetchWithTimeout(endpoint, { method: "GET" });

View File

@ -1,12 +1,7 @@
import { checkResponse, fetchWithTimeout } from "./fetchUtil";
import { ChannelDetailEntity } from "../entities";
export async function getContactChannelDetail(
server: string,
secure: boolean,
token: string,
channelId: string,
): Promise<ChannelDetailEntity> {
export async function getContactChannelDetail(server: string, secure: boolean, token: string, channelId: string): Promise<ChannelDetailEntity> {
const endpoint = `http${secure ? "s" : ""}://${node}/content/channels/${channelId}/detail?contact=${guid}.${token}`;
const detail = await fetchWithTimeout(endpoint, { method: "GET" });
checkResponse(detail.status);

View File

@ -1,12 +1,7 @@
import { checkResponse, fetchWithTimeout } from "./fetchUtil";
import { ChannelSummaryEntity } from "../entities";
export async function getContactChannelSummary(
server: string,
secure: boolean,
token: string,
channelId: string,
): Promise<ChannelSummaryEntity> {
export async function getContactChannelSummary(server: string, secure: boolean, token: string, channelId: string): Promise<ChannelSummaryEntity> {
const endpoint = `http${secure ? "s" : ""}://${node}/content/channels/${channelId}/summary?contact=${guid}.${token}`;
const summary = await fetchWithTimeout(endpoint, { method: "GET" });
checkResponse(summary.status);

View File

@ -1,18 +1,9 @@
import { checkResponse, fetchWithTimeout } from "./fetchUtil";
import { ChannelEntity } from "../entities";
export async function getContactChannels(
node: string,
secure: boolean,
guid: string,
token: string,
revision: number,
types: string[],
): Promise<ChannelEntity> {
export async function getContactChannels(node: string, secure: boolean, guid: string, token: string, revision: number, types: string[]): Promise<ChannelEntity> {
const type = `types=${encodeURIComponent(JSON.stringify(types))}`;
const param = revision
? `viewRevision=1&channelRevision=${revision}`
: `viewRevision=1`;
const param = revision ? `viewRevision=1&channelRevision=${revision}` : `viewRevision=1`;
const endpoint = `http${secure ? "s" : ""}://${node}/content/channels?contact=${guid}.${token}&${param}&${type}`;
const channels = await fetchWithTimeout(endpoint, { method: "GET" });
checkResponse(channels.status);

View File

@ -1,11 +1,7 @@
import { checkResponse, fetchWithTimeout } from "./fetchUtil";
import { DataMessage } from "../entities";
export async function getContactListing(
node: string,
secure: boolean,
guid: string
): Promise<DataMessage> {
export async function getContactListing(node: string, secure: boolean, guid: string): Promise<DataMessage> {
const endpoint = `http${secure ? "s" : ""}://${node}/account/listing/${guid}/message`;
const listing = await fetchWithTimeout(endpoint, { method: "GET" });
checkResponse(listing.status);

View File

@ -1,12 +1,7 @@
import { checkResponse, fetchWithTimeout } from "./fetchUtil";
import { DataMessage } from "../entities";
export async function getContactProfile(
node: string,
secure: boolean,
guid: string,
token: string,
): Promise<DataMessage> {
export async function getContactProfile(node: string, secure: boolean, guid: string, token: string): Promise<DataMessage> {
const endpoint = `http${secure ? "s" : ""}://${node}/profile/message?contact=${guid}.${token}`;
const profile = await fetchWithTimeout(endpoint, { method: "GET" });
checkResponse(profile.status);

View File

@ -1,11 +1,7 @@
import { checkResponse, fetchWithTimeout } from "./fetchUtil";
import { ProfileEntity } from "../entities";
export async function getProfile(
node: string,
secure: boolean,
token: string,
): Promise<ProfileEntity> {
export async function getProfile(node: string, secure: boolean, token: string): Promise<ProfileEntity> {
const endpoint = `http${secure ? "s" : ""}://${node}/profile?agent=${token}`;
const profile = await fetchWithTimeout(endpoint, { method: "GET" });
checkResponse(profile.status);

View File

@ -1,8 +1,3 @@
export function getProfileImageUrl(
node: string,
secure: boolean,
token: string,
revision: number,
) {
export function getProfileImageUrl(node: string, secure: boolean, token: string, revision: number) {
return `http${secure ? "s" : ""}://${node}/profile/image?agent=${token}&revision=${revision}`;
}

View File

@ -1,12 +1,6 @@
import { checkResponse, fetchWithTimeout } from "./fetchUtil";
export async function getUsername(
name: string,
token: string | null,
agent: string | null,
node: string,
secure: boolean,
): Promise<boolean> {
export async function getUsername(name: string, token: string | null, agent: string | null, node: string, secure: boolean): Promise<boolean> {
const param = token ? `&token=${token}` : agent ? `&agent=${agent}` : "";
const username = encodeURIComponent(name);
const endpoint = `http${secure ? "s" : ""}://${node}/account/username?name=${username}${param}`;

View File

@ -1,10 +1,6 @@
import { checkResponse, fetchWithTimeout } from "./fetchUtil";
export async function removeAccount(
node: string,
secure: boolean,
token: string,
): Promise<void> {
export async function removeAccount(node: string, secure: boolean, token: string): Promise<void> {
const endpoint = `http${secure ? "s" : ""}://${node}/profile?agent=${token}`;
const { status } = await fetchWithTimeout(endpoint, { method: "DELETE" });
checkResponse(status);

View File

@ -1,10 +1,6 @@
import { checkResponse, fetchWithTimeout } from "./fetchUtil";
export async function removeAccountMFAuth(
node: string,
secure: boolean,
token: string,
) {
export async function removeAccountMFAuth(node: string, secure: boolean, token: string) {
const endpoint = `http${secure ? "s" : ""}://${node}/account/mfauth?agent=${token}`;
const { status } = await fetchWithTimeout(endpoint, { method: "DELETE" });
checkResponse(status);

View File

@ -1,11 +1,6 @@
import { checkResponse, fetchWithTimeout } from "./fetchUtil";
export async function removeAccount(
node: string,
secure: boolean,
token: string,
cardId: string,
): Promise<void> {
export async function removeAccount(node: string, secure: boolean, token: string, cardId: string): Promise<void> {
const endpoint = `http${secure ? "s" : ""}://${node}/contact/cards/${cardId}?agent=${token}`;
const { status } = await fetchWithTimeout(endpoint, { method: "DELETE" });
checkResponse(status);

View File

@ -1,13 +1,7 @@
import { checkResponse, fetchWithTimeout } from "./fetchUtil";
import { encode } from "./base64";
export async function setAccountLogin(
node: string,
secure: boolean,
token: string,
username: string,
password: string,
) {
export async function setAccountLogin(node: string, secure: boolean, token: string, username: string, password: string) {
const endpoint = `http${secure ? "s" : ""}://${node}/account/login?agent=${token}`;
const auth = encode(`${username}:${password}`);
const headers = new Headers();

View File

@ -1,11 +1,6 @@
import { checkResponse, fetchWithTimeout } from "./fetchUtil";
export async function setAccountMFAuth(
node: string,
secure: boolean,
token: string,
code: string,
) {
export async function setAccountMFAuth(node: string, secure: boolean, token: string, code: string) {
const endpoint = `http${secure ? "s" : ""}://${node}/account/mfauth?agent=${token}&code=${code}`;
const { status } = await fetchWithTimeout(endpoint, { method: "PUT" });
checkResponse(status);

View File

@ -1,11 +1,6 @@
import { checkResponse, fetchWithTimeout } from "./fetchUtil";
export async function setAccountNotifications(
node: string,
secure: boolean,
token: string,
flag: boolean,
) {
export async function setAccountNotifications(node: string, secure: boolean, token: string, flag: boolean) {
const endpoint = `http${secure ? "s" : ""}://${node}/account/notification?agent=${token}`;
const { status } = await fetchWithTimeout(endpoint, {
method: "PUT",

View File

@ -1,12 +1,7 @@
import { checkResponse, fetchWithTimeout } from "./fetchUtil";
import { SealEntity } from "../entities";
export async function setAccountSeal(
node: string,
secure: boolean,
token: string,
seal: SealEntity,
) {
export async function setAccountSeal(node: string, secure: boolean, token: string, seal: SealEntity) {
const endpoint = `http${secure ? "s" : ""}://${node}/account/seal?agent=${token}`;
const { status } = await fetchWithTimeout(endpoint, {
method: "PUT",

View File

@ -1,11 +1,6 @@
import { checkResponse, fetchWithTimeout } from "./fetchUtil";
export async function setAccountSearchable(
node: string,
secure: boolean,
token: string,
flag: boolean,
) {
export async function setAccountSearchable(node: string, secure: boolean, token: string, flag: boolean) {
const endpoint = `http${secure ? "s" : ""}://${node}/account/searchable?agent=${token}`;
const { status } = await fetchWithTimeout(endpoint, {
method: "PUT",

View File

@ -1,11 +1,6 @@
import { checkResponse, fetchWithTimeout } from "./fetchUtil";
export async function setAdmin(
node: string,
secure: boolean,
token: string,
mfaCode: string | null,
): Promise<string> {
export async function setAdmin(node: string, secure: boolean, token: string, mfaCode: string | null): Promise<string> {
const mfa = mfaCode ? `&code=${mfaCode}` : "";
const endpoint = `http${secure ? "s" : ""}://${node}/admin/access?token=${encodeURIComponent(token)}${mfa}`;
const admin = await fetchWithTimeout(endpoint, { method: "PUT" });

View File

@ -1,8 +1,7 @@
import { checkResponse, fetchWithTimeout } from './fetchUtil';
import { checkResponse, fetchWithTimeout } from "./fetchUtil";
export async function setCardCloseMessage(node: string, secure: boolean, message: string): Promise<void> {
const endpoint = `http${secure ? "s" : ""}://${node}/contact/closeMessage`;
const close = await fetchWithTimeout(endpoint, { method: "PUT", JSON.stringify(message) });
const close = await fetchWithTimeout(endpoint, { method: "PUT", body: JSON.stringify(message) });
checkResponse(close.status);
}

View File

@ -1,15 +1,10 @@
import { checkResponse, fetchWithTimeout } from "./fetchUtil";
export async function setCardConfirmed(
node: string,
secure: boolean,
token: string,
cardId: string,
): Promise<void> {
export async function setCardConfirmed(node: string, secure: boolean, token: string, cardId: string): Promise<void> {
const endpoint = `http${secure ? "s" : ""}://${node}/contact/cards/${cardId}/status?agent=${token}`;
const { status } = await fetchWithTimeout(endpoint, {
method: "PUT",
body: JSON.stringify('confirmed'),
body: JSON.stringify("confirmed"),
});
checkResponse(status);
}

View File

@ -1,19 +1,10 @@
import { checkResponse, fetchWithTimeout } from "./fetchUtil";
export async function setCardConnected(
node: string,
secure: boolean,
token: string,
cardId: string,
access: string,
article: number,
channel: number,
profile: number,
): Promise<void> {
const endpoint = `http${secure ? "s" : ""}://${node}/contact/cards/${cardId}/status?agent=${token}&token=${access}&viewRevision=1&articleRevision=${article}&channelRevision=${channel}&profileRevision=${profile}`
export async function setCardConnected(node: string, secure: boolean, token: string, cardId: string, access: string, article: number, channel: number, profile: number): Promise<void> {
const endpoint = `http${secure ? "s" : ""}://${node}/contact/cards/${cardId}/status?agent=${token}&token=${access}&viewRevision=1&articleRevision=${article}&channelRevision=${channel}&profileRevision=${profile}`;
const { status } = await fetchWithTimeout(endpoint, {
method: "PUT",
body: JSON.stringify('connecting'),
body: JSON.stringify("connecting"),
});
checkResponse(status);
}

View File

@ -1,15 +1,10 @@
import { checkResponse, fetchWithTimeout } from "./fetchUtil";
export async function setCardConnecting(
node: string,
secure: boolean,
token: string,
cardId: string,
): Promise<void> {
export async function setCardConnecting(node: string, secure: boolean, token: string, cardId: string): Promise<void> {
const endpoint = `http${secure ? "s" : ""}://${node}/contact/cards/${cardId}/status?agent=${token}`;
const { status } = await fetchWithTimeout(endpoint, {
method: "PUT",
body: JSON.stringify('connecting'),
body: JSON.stringify("connecting"),
});
checkResponse(status);
}

View File

@ -1,8 +1,7 @@
import { checkResponse, fetchWithTimeout } from './fetchUtil';
import { checkResponse, fetchWithTimeout } from "./fetchUtil";
export async function setCardOpenMessage(node: string, secure: boolean, message: string): Promise<void> {
const endpoint = `http${secure ? "s" : ""}://${node}/contact/openMessage`;
const open = await fetchWithTimeout(endpoint, { method: "PUT", JSON.stringify(message) });
const open = await fetchWithTimeout(endpoint, { method: "PUT", body: JSON.stringify(message) });
checkResponse(open.status);
}

View File

@ -1,13 +1,7 @@
import { checkResponse, fetchWithTimeout } from "./fetchUtil";
import { DataMessage } from "./entities";
export async function setCardProfile(
node: string,
secure: boolean,
token: string,
cardId: string,
data: DataMessage,
): Promise<void> {
export async function setCardProfile(node: string, secure: boolean, token: string, cardId: string, data: DataMessage): Promise<void> {
const endpoint = `http${secure ? "s" : ""}://${node}/contact/cards/${cardId}/profile?agent=${token}`;
const { status } = await fetchWithTimeout(endpoint, {
method: "PUT",

View File

@ -1,18 +1,8 @@
import { checkResponse, fetchWithTimeout } from "./fetchUtil";
export async function setProfileData(
node: string,
secure: boolean,
token: string,
name: string,
location: string,
description: string,
): Promise<void> {
export async function setProfileData(node: string, secure: boolean, token: string, name: string, location: string, description: string): Promise<void> {
const data = { name: name, location: location, description: description };
const endpoint = `http${secure ? "s" : ""}://${node}/profile/data?agent=${token}`;
const { status } = await fetchWithTimeout(endpoint, {
method: "PUT",
body: JSON.stringify(data),
});
const { status } = await fetchWithTimeout(endpoint, { method: "PUT", body: JSON.stringify(data) });
checkResponse(status);
}

View File

@ -1,11 +1,6 @@
import { checkResponse, fetchWithTimeout } from "./fetchUtil";
export async function setProfileImage(
node: string,
secure: boolean,
token: string,
image: string,
) {
export async function setProfileImage(node: string, secure: boolean, token: string, image: string) {
const endpoint = `http${secure ? "s" : ""}://${node}/profile/image?agent=${token}`;
const { status } = await fetchWithTimeout(endpoint, {
method: "PUT",

View File

@ -12,18 +12,7 @@ import { RingModule } from "./ring";
import { Connection } from "./connection";
import type {
Session,
Settings,
Identity,
Contact,
Ring,
Alias,
Attribute,
Content,
Stream,
Focus,
} from "./api";
import type { Session, Settings, Identity, Contact, Ring, Alias, Attribute, Content, Stream, Focus } from "./api";
import { Revision } from "./entities";
import { Call } from "./types";
import { Store } from "./store";
@ -53,18 +42,7 @@ export class SessionModule implements Session {
private channelTypes: string[];
private articleTypes: string[];
constructor(
store: Store,
crypto: Crypto | null,
log: Logging,
guid: string,
token: string,
node: string,
secure: boolean,
loginTimestamp: number,
channelTypes: string[],
articleTypes: string[],
) {
constructor(store: Store, crypto: Crypto | null, log: Logging, guid: string, token: string, node: string, secure: boolean, loginTimestamp: number, channelTypes: string[], articleTypes: string[]) {
log.info("new databag session");
this.store = store;
@ -80,68 +58,13 @@ export class SessionModule implements Session {
this.status = "connecting";
this.emitter = new EventEmitter();
this.identity = new IdentityModule(
log,
this.store,
guid,
token,
node,
secure,
);
this.settings = new SettingsModule(
log,
this.store,
this.crypto,
guid,
token,
node,
secure,
);
this.contact = new ContactModule(
log,
this.store,
this.crypto,
guid,
token,
node,
secure,
channelTypes,
articleTypes,
);
this.alias = new AliasModule(
log,
this.settings,
this.store,
guid,
token,
node,
secure,
);
this.attribute = new AttributeModule(
log,
this.settings,
this.store,
guid,
token,
node,
secure,
);
this.content = new ContentModule(
log,
this.settings,
this.store,
guid,
token,
node,
secure,
);
this.stream = new StreamModule(
log,
this.contact,
this.content,
this.store,
guid,
);
this.identity = new IdentityModule(log, this.store, guid, token, node, secure);
this.settings = new SettingsModule(log, this.store, this.crypto, guid, token, node, secure);
this.contact = new ContactModule(log, this.store, this.crypto, guid, token, node, secure, channelTypes, articleTypes);
this.alias = new AliasModule(log, this.settings, this.store, guid, token, node, secure);
this.attribute = new AttributeModule(log, this.settings, this.store, guid, token, node, secure);
this.content = new ContentModule(log, this.settings, this.store, guid, token, node, secure);
this.stream = new StreamModule(log, this.contact, this.content, this.store, guid);
this.ring = new RingModule(log);
this.connection = new Connection(log, token, node, secure);
@ -234,15 +157,7 @@ export class SessionModule implements Session {
}
public addFocus(cardId: string | null, channelId: string): Focus {
return new FocusModule(
this.log,
this.identity,
this.contact,
this.content,
this.store,
cardId,
channelId,
);
return new FocusModule(this.log, this.identity, this.contact, this.content, this.store, cardId, channelId);
}
public removeFocus(focus: Focus): void {

View File

@ -35,15 +35,7 @@ export class SettingsModule implements Settings {
private config: ConfigEntity;
private seal: { privateKey: string; publicKey: string } | null;
constructor(
log: Logging,
store: Store,
crypto: Crypto | null,
guid: string,
token: string,
node: string,
secure: boolean,
) {
constructor(log: Logging, store: Store, crypto: Crypto | null, guid: string, token: string, node: string, secure: boolean) {
this.log = log;
this.store = store;
this.crypto = crypto;
@ -110,27 +102,10 @@ export class SettingsModule implements Settings {
}
public getConfig() {
const {
storageUsed,
storageAvailable,
forwardingAddress,
searchable,
allowUnsealed,
pushEnabled,
sealable,
seal,
enableIce,
mfaEnabled,
webPushKey,
} = this.config;
const { passwordSalt, privateKeyIv, privateKeyEncrypted, publicKey } =
seal || {};
const sealSet = Boolean(
passwordSalt && privateKeyIv && privateKeyEncrypted && publicKey,
);
const sealUnlocked = Boolean(
sealSet && this.seal?.privateKey && this.seal?.publicKey == publicKey,
);
const { storageUsed, storageAvailable, forwardingAddress, searchable, allowUnsealed, pushEnabled, sealable, seal, enableIce, mfaEnabled, webPushKey } = this.config;
const { passwordSalt, privateKeyIv, privateKeyEncrypted, publicKey } = seal || {};
const sealSet = Boolean(passwordSalt && privateKeyIv && privateKeyEncrypted && publicKey);
const sealUnlocked = Boolean(sealSet && this.seal?.privateKey && this.seal?.publicKey == publicKey);
return {
storageUsed,
storageAvailable,
@ -156,16 +131,12 @@ export class SettingsModule implements Settings {
this.emitter.off("config", ev);
}
public addSealListener(
ev: (seal: { privateKey: string; publicKey: string } | null) => void,
): void {
public addSealListener(ev: (seal: { privateKey: string; publicKey: string } | null) => void): void {
this.emitter.on("seal", ev);
ev(this.seal);
}
public removeSealListener(
ev: (seal: { privateKey: string; publicKey: string } | null) => void,
): void {
public removeSealListener(ev: (seal: { privateKey: string; publicKey: string } | null) => void): void {
this.emitter.off("seal", ev);
}
@ -206,11 +177,7 @@ export class SettingsModule implements Settings {
secretText: string;
}> {
const { node, secure, token } = this;
const { secretImage, secretText } = await addAccountMFAuth(
node,
secure,
token,
);
const { secretImage, secretText } = await addAccountMFAuth(node, secure, token);
return { secretImage, secretText };
}
@ -233,11 +200,7 @@ export class SettingsModule implements Settings {
const { aesKeyHex } = await crypto.pbkdfKey(saltHex, password);
const { publicKeyB64, privateKeyB64 } = await crypto.rsaKey();
const { ivHex } = await crypto.aesIv();
const { encryptedDataB64 } = await crypto.aesEncrypt(
privateKeyB64,
ivHex,
aesKeyHex,
);
const { encryptedDataB64 } = await crypto.aesEncrypt(privateKeyB64, ivHex, aesKeyHex);
const seal = {
passwordSalt: saltHex,
privateKeyIv: ivHex,
@ -262,11 +225,7 @@ export class SettingsModule implements Settings {
const { saltHex } = await crypto.pbkdfSalt();
const { aesKeyHex } = await crypto.pbkdfKey(saltHex, password);
const { ivHex } = await crypto.aesIv();
const { encryptedDataB64 } = await crypto.aesEncrypt(
this.seal.privateKey,
ivHex,
aesKeyHex,
);
const { encryptedDataB64 } = await crypto.aesEncrypt(this.seal.privateKey, ivHex, aesKeyHex);
const seal = {
passwordSalt: saltHex,
privateKeyIv: ivHex,
@ -287,8 +246,7 @@ export class SettingsModule implements Settings {
public async unlockSeal(password: string): Promise<void> {
const { guid, config, crypto } = this;
const { passwordSalt, privateKeyIv, privateKeyEncrypted, publicKey } =
config.seal;
const { passwordSalt, privateKeyIv, privateKeyEncrypted, publicKey } = config.seal;
if (!passwordSalt || !privateKeyIv || !privateKeyEncrypted || !publicKey) {
throw new Error("account seal not set");
}
@ -296,11 +254,7 @@ export class SettingsModule implements Settings {
throw new Error("crypto not set");
}
const { aesKeyHex } = await crypto.pbkdfKey(passwordSalt, password);
const { data } = await crypto.aesDecrypt(
privateKeyEncrypted,
privateKeyIv,
aesKeyHex,
);
const { data } = await crypto.aesDecrypt(privateKeyEncrypted, privateKeyIv, aesKeyHex);
const seal = { publicKey: publicKey, privateKey: data };
this.store.setSeal(guid, seal);
this.seal = seal;

File diff suppressed because it is too large Load Diff

View File

@ -11,13 +11,7 @@ export class StreamModule implements Stream {
private content: Content;
private emitter: EventEmitter;
constructor(
log: Logging,
contact: Contact,
content: Content,
store: Store,
guid: string,
) {
constructor(log: Logging, contact: Contact, content: Content, store: Store, guid: string) {
this.contact = contact;
this.content = content;
this.log = log;