diff --git a/src/lib/accounts/accountKeyDB.ts b/src/lib/accounts/accountKeyDB.ts index 6b47730..d42db35 100644 --- a/src/lib/accounts/accountKeyDB.ts +++ b/src/lib/accounts/accountKeyDB.ts @@ -9,12 +9,14 @@ type SavedObject = { encodedIdKeyPair?: EncodedBlahKeyPair; actKeyId: string; actKeyPrivate: CryptoKey; + chatServers: string[]; }; export type AccountCredentials = { idKeyId: string; encodedIdKeyPair?: EncodedBlahKeyPair; actKeyPair: BlahKeyPair; + chatServers: string[]; }; interface AccountKeyDBSchema extends DBSchema { @@ -32,7 +34,8 @@ async function savedObjectToAccountCredentials( return { idKeyId: savedObject.idKeyId, encodedIdKeyPair: savedObject.encodedIdKeyPair, - actKeyPair: new BlahKeyPair(publicKey, savedObject.actKeyPrivate) + actKeyPair: new BlahKeyPair(publicKey, savedObject.actKeyPrivate), + chatServers: savedObject.chatServers }; } @@ -59,13 +62,15 @@ class AccountKeyDB { async addAccount( idKeyId: string, actKeyPair: BlahKeyPair, - encodedIdKeyPair?: EncodedBlahKeyPair + encodedIdKeyPair?: EncodedBlahKeyPair, + chatServers: string[] = [] ): Promise { const newObject: SavedObject = { idKeyId, encodedIdKeyPair, actKeyId: actKeyPair.id, - actKeyPrivate: actKeyPair.privateKey + actKeyPrivate: actKeyPair.privateKey, + chatServers }; const tx = this.db.transaction(IDB_OBJECT_STORE_NAME, 'readwrite'); @@ -73,7 +78,7 @@ class AccountKeyDB { await tx.store.put({ ...currentObject, ...newObject }); await tx.done; - return { idKeyId, encodedIdKeyPair, actKeyPair }; + return { idKeyId, encodedIdKeyPair, actKeyPair, chatServers }; } async updateEncodedIdKeyPair( diff --git a/src/lib/accounts/manager.svelte.ts b/src/lib/accounts/manager.svelte.ts index 1349068..8aed4c4 100644 --- a/src/lib/accounts/manager.svelte.ts +++ b/src/lib/accounts/manager.svelte.ts @@ -25,6 +25,7 @@ class AccountManager { currentAccount: Account | null = $derived( this.accounts.find((account) => account.id_key === this.currentAccountId) ?? null ); + #connectionPool: ChatServerConnectionPool; constructor() { if (browser) { diff --git a/src/lib/blah/connection/connection.ts b/src/lib/blah/connection/connection.ts index 6add45e..52efe78 100644 --- a/src/lib/blah/connection/connection.ts +++ b/src/lib/blah/connection/connection.ts @@ -138,7 +138,7 @@ export class BlahChatServerConnection { } }; - const response = await this.apiCall('POST', '/user/register', request, { + await this.apiCall('POST', '/user/register', request, { powDifficulty: data.register_challenge.pow.difficulty }); } else { @@ -186,6 +186,7 @@ export class BlahChatServerConnection { } connect() { + this.tryRegisterIfNoyYet(); if (!this.webSocket && this.identity) this.webSocket = this.createWebSocket(); } diff --git a/src/lib/chatServers.ts b/src/lib/chatServers.ts index a830ed4..efc79d4 100644 --- a/src/lib/chatServers.ts +++ b/src/lib/chatServers.ts @@ -1,29 +1,29 @@ -import { persisted } from 'svelte-persisted-store'; import { get } from 'svelte/store'; -import { BlahChatServerConnection } from './blah/connection/chatServer'; +import { BlahChatServerConnection } from './blah/connection'; import { BlahKeyPair, type EncodedBlahKeyPair } from '@blah-im/core/crypto'; import { ChatListManager } from './chatList'; import { browser } from '$app/environment'; import { GlobalSearchManager } from './globalSearch'; +import type { BlahIdentity } from '@blah-im/core/identity'; -export const chatServers = persisted('weblah-chat-servers', ['https://blah.oxa.li/api']); +export const defaultChatServers = ['https://blah.oxa.li/api']; + +export class ChatServerConnectionPool { + #connections: Map = new Map(); + #identity: BlahIdentity | null = null; + #chatServers: string[] = defaultChatServers; -class ChatServerConnectionPool { - private connections: Map = new Map(); - private keypair: BlahKeyPair | null = null; chatList: ChatListManager = new ChatListManager(); searchManager: GlobalSearchManager = new GlobalSearchManager(this.connections); - constructor() { - if (browser) { - chatServers.subscribe(this.onChatServersChange.bind(this)); - // currentKeyPair.subscribe(this.onKeyPairChange.bind(this)); - } + constructor(identity: BlahIdentity, chatServers: string[] = defaultChatServers) { + this.#identity = identity; + this.#chatServers = chatServers; } private createAndConnect(endpoint: string) { - const connection = new BlahChatServerConnection(endpoint, this.keypair); - this.connections.set(endpoint, connection); + const connection = new BlahChatServerConnection(endpoint, this.#identity); + this.#connections.set(endpoint, connection); connection.connect(); this.setupChatList(connection); } @@ -84,5 +84,3 @@ class ChatServerConnectionPool { return this.connections.get(endpoint) ?? null; } } - -export const chatServerConnectionPool = new ChatServerConnectionPool();