Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 Savoir-faire Linux Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Affero General Public License as |
| 6 | * published by the Free Software Foundation; either version 3 of the |
| 7 | * License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU Affero General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Affero General Public |
| 15 | * License along with this program. If not, see |
| 16 | * <https://www.gnu.org/licenses/>. |
| 17 | */ |
Charlie | 2bc0d67 | 2022-11-04 11:53:44 -0400 | [diff] [blame] | 18 | import { AccountDetails, Message, VolatileDetails, WebSocketMessageType } from 'jami-web-common'; |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 19 | import log from 'loglevel'; |
Misha Krieger-Raynauld | 6f9c7ae | 2022-10-28 11:41:45 -0400 | [diff] [blame] | 20 | import { filter, firstValueFrom, map, Subject } from 'rxjs'; |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 21 | import { Service } from 'typedi'; |
| 22 | |
Charlie | 2bc0d67 | 2022-11-04 11:53:44 -0400 | [diff] [blame] | 23 | import { Ws } from '../ws.js'; |
Misha Krieger-Raynauld | addd6fe | 2022-10-22 12:46:04 -0400 | [diff] [blame] | 24 | import { JamiSignal } from './jami-signal.js'; |
| 25 | import { |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 26 | AccountDetailsChanged, |
Misha Krieger-Raynauld | cb11bba | 2022-11-11 18:08:33 -0500 | [diff] [blame^] | 27 | AccountMessageStatusChanged, |
| 28 | ContactAdded, |
| 29 | ContactRemoved, |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 30 | ConversationLoaded, |
| 31 | ConversationReady, |
| 32 | ConversationRemoved, |
Charlie | b62c678 | 2022-10-30 15:14:56 -0400 | [diff] [blame] | 33 | IncomingAccountMessage, |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 34 | KnownDevicesChanged, |
| 35 | MessageReceived, |
Misha Krieger-Raynauld | addd6fe | 2022-10-22 12:46:04 -0400 | [diff] [blame] | 36 | NameRegistrationEnded, |
| 37 | RegisteredNameFound, |
| 38 | RegistrationStateChanged, |
| 39 | VolatileDetailsChanged, |
| 40 | } from './jami-signal-interfaces.js'; |
Misha Krieger-Raynauld | bfed173 | 2022-11-01 20:49:35 -0400 | [diff] [blame] | 41 | import { JamiSwig, StringMap, stringMapToRecord, stringVectToArray, vectMapToRecordArray } from './jami-swig.js'; |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 42 | import { require } from './utils.js'; |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 43 | |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 44 | // TODO: Mechanism to map account IDs to a list of WebSockets |
Misha Krieger-Raynauld | bfed173 | 2022-11-01 20:49:35 -0400 | [diff] [blame] | 45 | // TODO: Convert Records to interfaces and replace them in common/ (e.g. Contact) |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 46 | |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 47 | @Service() |
| 48 | export class Jamid { |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 49 | private readonly jamiSwig: JamiSwig; |
| 50 | private readonly usernamesToAccountIds: Map<string, string>; |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 51 | private readonly events; |
| 52 | |
Charlie | 2bc0d67 | 2022-11-04 11:53:44 -0400 | [diff] [blame] | 53 | constructor(private ws: Ws) { |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 54 | // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment |
simon | 7d4386c | 2022-10-26 17:47:59 -0400 | [diff] [blame] | 55 | this.jamiSwig = require('../../jamid.node') as JamiSwig; // TODO: we should put the path in the .env |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 56 | |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 57 | this.usernamesToAccountIds = new Map<string, string>(); |
| 58 | |
| 59 | // Setup signal handlers |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 60 | const handlers: Record<string, unknown> = {}; |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 61 | |
| 62 | // Add default handler for all signals |
| 63 | const createDefaultHandler = (signal: string) => { |
| 64 | return (...args: unknown[]) => log.warn('Unhandled', signal, args); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 65 | }; |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 66 | for (const signal in JamiSignal) { |
| 67 | handlers[signal] = createDefaultHandler(signal); |
| 68 | } |
| 69 | |
| 70 | // Overwrite handlers for handled signals using RxJS Subjects, converting multiple arguments to objects |
| 71 | const onAccountsChanged = new Subject<void>(); |
| 72 | handlers.AccountsChanged = () => onAccountsChanged.next(); |
| 73 | |
| 74 | const onAccountDetailsChanged = new Subject<AccountDetailsChanged>(); |
| 75 | handlers.AccountDetailsChanged = (accountId: string, details: AccountDetails) => |
| 76 | onAccountDetailsChanged.next({ accountId, details }); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 77 | |
| 78 | const onVolatileDetailsChanged = new Subject<VolatileDetailsChanged>(); |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 79 | handlers.VolatileDetailsChanged = (accountId: string, details: VolatileDetails) => |
| 80 | onVolatileDetailsChanged.next({ accountId, details }); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 81 | |
| 82 | const onRegistrationStateChanged = new Subject<RegistrationStateChanged>(); |
| 83 | handlers.RegistrationStateChanged = (accountId: string, state: string, code: number, details: string) => |
| 84 | onRegistrationStateChanged.next({ accountId, state, code, details }); |
| 85 | |
| 86 | const onNameRegistrationEnded = new Subject<NameRegistrationEnded>(); |
| 87 | handlers.NameRegistrationEnded = (accountId: string, state: number, username: string) => |
| 88 | onNameRegistrationEnded.next({ accountId, state, username }); |
| 89 | |
| 90 | const onRegisteredNameFound = new Subject<RegisteredNameFound>(); |
| 91 | handlers.RegisteredNameFound = (accountId: string, state: number, address: string, username: string) => |
| 92 | onRegisteredNameFound.next({ accountId, state, address, username }); |
| 93 | |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 94 | const onKnownDevicesChanged = new Subject<KnownDevicesChanged>(); |
| 95 | handlers.KnownDevicesChanged = (accountId: string, devices: Record<string, string>) => |
| 96 | onKnownDevicesChanged.next({ accountId, devices }); |
| 97 | |
Charlie | b62c678 | 2022-10-30 15:14:56 -0400 | [diff] [blame] | 98 | const onIncomingAccountMessage = new Subject<IncomingAccountMessage>(); |
Misha Krieger-Raynauld | cb11bba | 2022-11-11 18:08:33 -0500 | [diff] [blame^] | 99 | handlers.IncomingAccountMessage = (accountId: string, from: string, payload: Record<string, string>) => |
| 100 | onIncomingAccountMessage.next({ accountId, from, payload }); |
| 101 | |
| 102 | const onAccountMessageStatusChanged = new Subject<AccountMessageStatusChanged>(); |
| 103 | handlers.AccountMessageStatusChanged = (accountId: string, messageId: string, peer: string, state: number) => |
| 104 | onAccountMessageStatusChanged.next({ accountId, messageId, peer, state }); |
| 105 | |
| 106 | const onContactAdded = new Subject<ContactAdded>(); |
| 107 | handlers.ContactAdded = (accountId: string, contactId: string, confirmed: boolean) => |
| 108 | onContactAdded.next({ accountId, contactId, confirmed }); |
| 109 | |
| 110 | const onContactRemoved = new Subject<ContactRemoved>(); |
| 111 | handlers.ContactRemoved = (accountId: string, contactId: string, banned: boolean) => |
| 112 | onContactRemoved.next({ accountId, contactId, banned }); |
Charlie | b62c678 | 2022-10-30 15:14:56 -0400 | [diff] [blame] | 113 | |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 114 | const onConversationReady = new Subject<ConversationReady>(); |
| 115 | handlers.ConversationReady = (accountId: string, conversationId: string) => |
| 116 | onConversationReady.next({ accountId, conversationId }); |
| 117 | |
| 118 | const onConversationRemoved = new Subject<ConversationRemoved>(); |
| 119 | handlers.ConversationRemoved = (accountId: string, conversationId: string) => |
| 120 | onConversationRemoved.next({ accountId, conversationId }); |
| 121 | |
| 122 | const onConversationLoaded = new Subject<ConversationLoaded>(); |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 123 | handlers.ConversationLoaded = (id: number, accountId: string, conversationId: string, messages: Message[]) => |
| 124 | onConversationLoaded.next({ id, accountId, conversationId, messages }); |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 125 | |
| 126 | const onMessageReceived = new Subject<MessageReceived>(); |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 127 | handlers.MessageReceived = (accountId: string, conversationId: string, message: Message) => |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 128 | onMessageReceived.next({ accountId, conversationId, message }); |
| 129 | |
| 130 | // Expose all signals in an events object to allow other handlers to subscribe after jamiSwig.init() |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 131 | this.events = { |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 132 | onAccountsChanged: onAccountsChanged.asObservable(), |
| 133 | onAccountDetailsChanged: onAccountDetailsChanged.asObservable(), |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 134 | onVolatileDetailsChanged: onVolatileDetailsChanged.asObservable(), |
| 135 | onRegistrationStateChanged: onRegistrationStateChanged.asObservable(), |
| 136 | onNameRegistrationEnded: onNameRegistrationEnded.asObservable(), |
| 137 | onRegisteredNameFound: onRegisteredNameFound.asObservable(), |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 138 | onKnownDevicesChanged: onKnownDevicesChanged.asObservable(), |
Charlie | b62c678 | 2022-10-30 15:14:56 -0400 | [diff] [blame] | 139 | onIncomingAccountMessage: onIncomingAccountMessage.asObservable(), |
Misha Krieger-Raynauld | cb11bba | 2022-11-11 18:08:33 -0500 | [diff] [blame^] | 140 | onAccountMessageStatusChanged: onAccountMessageStatusChanged.asObservable(), |
| 141 | onContactAdded: onContactAdded.asObservable(), |
| 142 | onContactRemoved: onContactRemoved.asObservable(), |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 143 | onConversationReady: onConversationReady.asObservable(), |
| 144 | onConversationRemoved: onConversationRemoved.asObservable(), |
| 145 | onConversationLoaded: onConversationLoaded.asObservable(), |
| 146 | onMessageReceived: onMessageReceived.asObservable(), |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 147 | }; |
| 148 | |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 149 | this.setupSignalHandlers(); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 150 | |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 151 | // RxJS Subjects are used as signal handlers for the following reasons: |
| 152 | // 1. You cannot change event handlers after calling jamiSwig.init() |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 153 | // 2. You cannot specify multiple handlers for the same event |
| 154 | // 3. You cannot specify a default handler |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 155 | this.jamiSwig.init(handlers); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 158 | stop(): void { |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 159 | this.jamiSwig.fini(); |
Misha Krieger-Raynauld | 62a0da9 | 2022-10-22 13:46:59 -0400 | [diff] [blame] | 160 | } |
| 161 | |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 162 | getVolatileAccountDetails(accountId: string): VolatileDetails { |
| 163 | return stringMapToRecord(this.jamiSwig.getVolatileAccountDetails(accountId)) as unknown as VolatileDetails; |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 166 | getAccountDetails(accountId: string): AccountDetails { |
| 167 | return stringMapToRecord(this.jamiSwig.getAccountDetails(accountId)) as unknown as AccountDetails; |
| 168 | } |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 169 | |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 170 | setAccountDetails(accountId: string, accountDetails: AccountDetails): void { |
simon | 43da57b | 2022-10-26 18:22:22 -0400 | [diff] [blame] | 171 | const accountDetailsStringMap: StringMap = new this.jamiSwig.StringMap(); |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 172 | for (const [key, value] of Object.entries(accountDetails)) { |
| 173 | accountDetailsStringMap.set(key, value); |
| 174 | } |
| 175 | this.jamiSwig.setAccountDetails(accountId, accountDetailsStringMap); |
| 176 | } |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 177 | |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 178 | async addAccount(details: Map<string, string | number | boolean>): Promise<string> { |
simon | 43da57b | 2022-10-26 18:22:22 -0400 | [diff] [blame] | 179 | const detailsStringMap: StringMap = new this.jamiSwig.StringMap(); |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 180 | |
| 181 | detailsStringMap.set('Account.type', 'RING'); |
| 182 | for (const [key, value] of details.entries()) { |
| 183 | detailsStringMap.set('Account.' + key, value.toString()); |
| 184 | } |
| 185 | |
| 186 | const accountId = this.jamiSwig.addAccount(detailsStringMap); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 187 | return firstValueFrom( |
| 188 | this.events.onRegistrationStateChanged.pipe( |
Misha Krieger-Raynauld | 6f9c7ae | 2022-10-28 11:41:45 -0400 | [diff] [blame] | 189 | filter((value) => value.accountId === accountId), |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 190 | // TODO: is it the only state? |
Misha Krieger-Raynauld | 6f9c7ae | 2022-10-28 11:41:45 -0400 | [diff] [blame] | 191 | // TODO: Replace with string enum in common/ |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 192 | filter((value) => value.state === 'REGISTERED'), |
| 193 | map((value) => value.accountId) |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 194 | ) |
| 195 | ); |
| 196 | } |
| 197 | |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 198 | removeAccount(accountId: string): void { |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 199 | this.jamiSwig.removeAccount(accountId); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 202 | getAccountIds(): string[] { |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 203 | return stringVectToArray(this.jamiSwig.getAccountList()); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Charlie | 2bc0d67 | 2022-11-04 11:53:44 -0400 | [diff] [blame] | 206 | sendAccountTextMessage(from: string, to: string, message: string): void { |
Charlie | b62c678 | 2022-10-30 15:14:56 -0400 | [diff] [blame] | 207 | const messageStringMap: StringMap = new this.jamiSwig.StringMap(); |
Charlie | 2bc0d67 | 2022-11-04 11:53:44 -0400 | [diff] [blame] | 208 | messageStringMap.set('application/json', message); |
| 209 | this.jamiSwig.sendAccountTextMessage(from, to, messageStringMap); |
Charlie | b62c678 | 2022-10-30 15:14:56 -0400 | [diff] [blame] | 210 | } |
| 211 | |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 212 | // TODO: Add interface for returned type |
Misha Krieger-Raynauld | 6f9c7ae | 2022-10-28 11:41:45 -0400 | [diff] [blame] | 213 | async lookupUsername(username: string, accountId?: string) { |
| 214 | const hasRingNs = this.jamiSwig.lookupName(accountId || '', '', username); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 215 | if (!hasRingNs) { |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 216 | throw new Error('Jami does not have NS'); |
| 217 | } |
Misha Krieger-Raynauld | 6f9c7ae | 2022-10-28 11:41:45 -0400 | [diff] [blame] | 218 | return firstValueFrom( |
| 219 | this.events.onRegisteredNameFound.pipe( |
| 220 | filter((value) => value.username === username), |
| 221 | map(({ accountId: _, ...response }) => response) // Remove accountId from response |
| 222 | ) |
| 223 | ); |
| 224 | } |
| 225 | |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 226 | // TODO: Add interface for returned type |
Misha Krieger-Raynauld | 6f9c7ae | 2022-10-28 11:41:45 -0400 | [diff] [blame] | 227 | async lookupAddress(address: string, accountId?: string) { |
| 228 | const hasRingNs = this.jamiSwig.lookupAddress(accountId || '', '', address); |
| 229 | if (!hasRingNs) { |
| 230 | throw new Error('Jami does not have NS'); |
| 231 | } |
| 232 | return firstValueFrom( |
| 233 | this.events.onRegisteredNameFound.pipe( |
| 234 | filter((value) => value.address === address), |
| 235 | map(({ accountId: _, ...response }) => response) // Remove accountId from response |
| 236 | ) |
| 237 | ); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 240 | // TODO: Create enum for state and return that rather than a number |
| 241 | async registerUsername(accountId: string, username: string, password: string): Promise<number> { |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 242 | const hasRingNs = this.jamiSwig.registerName(accountId, password, username); |
| 243 | if (!hasRingNs) { |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 244 | throw new Error('Jami does not have NS'); |
| 245 | } |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 246 | return firstValueFrom( |
| 247 | this.events.onNameRegistrationEnded.pipe( |
| 248 | filter((value) => value.accountId === accountId), |
| 249 | map((value) => value.state) |
| 250 | ) |
| 251 | ); |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | getDevices(accountId: string): Record<string, string> { |
| 255 | return stringMapToRecord(this.jamiSwig.getKnownRingDevices(accountId)); |
| 256 | } |
| 257 | |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 258 | addContact(accountId: string, contactId: string): void { |
Misha Krieger-Raynauld | bfed173 | 2022-11-01 20:49:35 -0400 | [diff] [blame] | 259 | this.jamiSwig.addContact(accountId, contactId); |
| 260 | } |
| 261 | |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 262 | removeContact(accountId: string, contactId: string): void { |
Misha Krieger-Raynauld | bfed173 | 2022-11-01 20:49:35 -0400 | [diff] [blame] | 263 | this.jamiSwig.removeContact(accountId, contactId, false); |
| 264 | } |
| 265 | |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 266 | blockContact(accountId: string, contactId: string): void { |
Misha Krieger-Raynauld | bfed173 | 2022-11-01 20:49:35 -0400 | [diff] [blame] | 267 | this.jamiSwig.removeContact(accountId, contactId, true); |
| 268 | } |
| 269 | |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 270 | // TODO: Replace Record with interface |
Misha Krieger-Raynauld | bfed173 | 2022-11-01 20:49:35 -0400 | [diff] [blame] | 271 | getContacts(accountId: string): Record<string, string>[] { |
| 272 | return vectMapToRecordArray(this.jamiSwig.getContacts(accountId)); |
| 273 | } |
| 274 | |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 275 | // TODO: Replace Record with interface |
Misha Krieger-Raynauld | bfed173 | 2022-11-01 20:49:35 -0400 | [diff] [blame] | 276 | getContactDetails(accountId: string, contactId: string): Record<string, string> { |
| 277 | return stringMapToRecord(this.jamiSwig.getContactDetails(accountId, contactId)); |
| 278 | } |
| 279 | |
Misha Krieger-Raynauld | 153a148 | 2022-11-05 12:00:41 -0400 | [diff] [blame] | 280 | getDefaultModeratorUris(accountId: string): string[] { |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 281 | return stringVectToArray(this.jamiSwig.getDefaultModerators(accountId)); |
| 282 | } |
| 283 | |
Misha Krieger-Raynauld | 153a148 | 2022-11-05 12:00:41 -0400 | [diff] [blame] | 284 | addDefaultModerator(accountId: string, contactId: string): void { |
| 285 | this.jamiSwig.setDefaultModerator(accountId, contactId, true); |
| 286 | } |
| 287 | |
| 288 | removeDefaultModerator(accountId: string, contactId: string): void { |
| 289 | this.jamiSwig.setDefaultModerator(accountId, contactId, false); |
| 290 | } |
| 291 | |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 292 | getConversationIds(accountId: string): string[] { |
| 293 | return stringVectToArray(this.jamiSwig.getConversations(accountId)); |
| 294 | } |
| 295 | |
| 296 | // TODO: Replace Record with interface |
| 297 | getConversationInfos(accountId: string, conversationId: string): Record<string, string> { |
| 298 | return stringMapToRecord(this.jamiSwig.conversationInfos(accountId, conversationId)); |
| 299 | } |
| 300 | |
| 301 | // TODO: Replace Record with interface |
| 302 | getConversationMembers(accountId: string, conversationId: string): Record<string, string>[] { |
| 303 | return vectMapToRecordArray(this.jamiSwig.getConversationMembers(accountId, conversationId)); |
| 304 | } |
| 305 | |
| 306 | async getConversationMessages(accountId: string, conversationId: string, fromMessage?: string): Promise<Message[]> { |
| 307 | const requestId = this.jamiSwig.loadConversationMessages(accountId, conversationId, fromMessage || '', 32); |
| 308 | return firstValueFrom( |
| 309 | this.events.onConversationLoaded.pipe( |
| 310 | filter((value) => value.id === requestId), |
| 311 | map((value) => value.messages) |
| 312 | ) |
| 313 | ); |
| 314 | } |
| 315 | |
| 316 | sendConversationMessage(accountId: string, conversationId: string, message: string, replyTo?: string): void { |
| 317 | this.jamiSwig.sendMessage(accountId, conversationId, message, replyTo || ''); |
| 318 | } |
| 319 | |
Misha Krieger-Raynauld | 7f95933 | 2022-11-04 15:12:53 -0400 | [diff] [blame] | 320 | getCallIds(accountId: string): string[] { |
| 321 | return stringVectToArray(this.jamiSwig.getCallList(accountId)); |
| 322 | } |
| 323 | |
| 324 | // TODO: Replace Record with interface |
| 325 | getCallDetails(accountId: string, callId: string): Record<string, string> { |
| 326 | return stringMapToRecord(this.jamiSwig.getCallDetails(accountId, callId)); |
| 327 | } |
| 328 | |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 329 | getAccountIdFromUsername(username: string): string | undefined { |
| 330 | return this.usernamesToAccountIds.get(username); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 331 | } |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 332 | |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 333 | private setupSignalHandlers(): void { |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 334 | this.events.onAccountsChanged.subscribe(() => { |
| 335 | log.debug('Received AccountsChanged'); |
| 336 | }); |
| 337 | |
| 338 | this.events.onAccountDetailsChanged.subscribe((signal) => { |
| 339 | log.debug('Received AccountsDetailsChanged', JSON.stringify(signal)); |
| 340 | }); |
| 341 | |
| 342 | this.events.onVolatileDetailsChanged.subscribe(({ accountId, details }) => { |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 343 | const username = details['Account.registeredName']; |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 344 | log.debug( |
| 345 | `Received VolatileDetailsChanged: {"accountId":"${accountId}",` + |
| 346 | `"details":{"Account.registeredName":"${username}", ...}}` |
| 347 | ); |
| 348 | // Keep map of usernames to account IDs |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 349 | if (username) { |
| 350 | this.usernamesToAccountIds.set(username, accountId); |
| 351 | } |
| 352 | }); |
| 353 | |
| 354 | this.events.onRegistrationStateChanged.subscribe((signal) => { |
| 355 | log.debug('Received RegistrationStateChanged:', JSON.stringify(signal)); |
| 356 | }); |
| 357 | |
| 358 | this.events.onNameRegistrationEnded.subscribe((signal) => { |
| 359 | log.debug('Received NameRegistrationEnded:', JSON.stringify(signal)); |
| 360 | }); |
| 361 | |
| 362 | this.events.onRegisteredNameFound.subscribe((signal) => { |
| 363 | log.debug('Received RegisteredNameFound:', JSON.stringify(signal)); |
| 364 | }); |
| 365 | |
| 366 | this.events.onKnownDevicesChanged.subscribe(({ accountId }) => { |
| 367 | log.debug(`Received KnownDevicesChanged: {"accountId":"${accountId}", ...}`); |
| 368 | }); |
| 369 | |
| 370 | this.events.onIncomingAccountMessage.subscribe((signal) => { |
| 371 | log.debug('Received IncomingAccountMessage:', JSON.stringify(signal)); |
Misha Krieger-Raynauld | cb11bba | 2022-11-11 18:08:33 -0500 | [diff] [blame^] | 372 | const message = JSON.parse(signal.payload['application/json']); |
Charlie | 2bc0d67 | 2022-11-04 11:53:44 -0400 | [diff] [blame] | 373 | |
| 374 | if (message === undefined) { |
| 375 | log.debug('Undefined account message'); |
| 376 | return; |
| 377 | } |
| 378 | |
| 379 | if (!Object.values(WebSocketMessageType).includes(message.type)) { |
| 380 | log.warn(`Unhandled account message type: ${message.type}`); |
| 381 | return; |
| 382 | } |
| 383 | |
| 384 | message.data.from = signal.from; |
| 385 | message.data.to = signal.accountId; |
| 386 | log.info(`Sending ${JSON.stringify(message)} to ${signal.accountId}`); |
| 387 | this.ws.send(signal.accountId, message); |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 388 | }); |
| 389 | |
Misha Krieger-Raynauld | cb11bba | 2022-11-11 18:08:33 -0500 | [diff] [blame^] | 390 | this.events.onAccountMessageStatusChanged.subscribe((signal) => { |
| 391 | log.debug('Received AccountMessageStatusChanged:', JSON.stringify(signal)); |
| 392 | }); |
| 393 | |
| 394 | this.events.onContactAdded.subscribe((signal) => { |
| 395 | log.debug('Received ContactAdded:', JSON.stringify(signal)); |
| 396 | }); |
| 397 | |
| 398 | this.events.onContactRemoved.subscribe((signal) => { |
| 399 | log.debug('Received ContactRemoved:', JSON.stringify(signal)); |
| 400 | }); |
| 401 | |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 402 | this.events.onConversationReady.subscribe((signal) => { |
| 403 | log.debug('Received ConversationReady:', JSON.stringify(signal)); |
| 404 | }); |
| 405 | |
| 406 | this.events.onConversationRemoved.subscribe((signal) => { |
| 407 | log.debug('Received ConversationRemoved:', JSON.stringify(signal)); |
| 408 | }); |
| 409 | |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 410 | this.events.onConversationLoaded.subscribe(({ id, accountId, conversationId }) => { |
| 411 | log.debug( |
| 412 | `Received ConversationLoaded: {"id":"${id}","accountId":"${accountId}",` + |
| 413 | `"conversationId":"${conversationId}","messages":[...]}` |
| 414 | ); |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 415 | }); |
| 416 | |
| 417 | this.events.onMessageReceived.subscribe((signal) => { |
| 418 | log.debug('Received MessageReceived:', JSON.stringify(signal)); |
Charlie | 6ddaefe | 2022-11-01 18:36:29 -0400 | [diff] [blame] | 419 | // TODO: Send message to client using WS service |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 420 | }); |
| 421 | } |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 422 | } |