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 | */ |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 18 | import { AccountDetails, VolatileDetails } 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 | |
Misha Krieger-Raynauld | addd6fe | 2022-10-22 12:46:04 -0400 | [diff] [blame] | 23 | import { JamiSignal } from './jami-signal.js'; |
| 24 | import { |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame^] | 25 | AccountDetailsChanged, |
| 26 | ConversationLoaded, |
| 27 | ConversationReady, |
| 28 | ConversationRemoved, |
Charlie | b62c678 | 2022-10-30 15:14:56 -0400 | [diff] [blame] | 29 | IncomingAccountMessage, |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame^] | 30 | KnownDevicesChanged, |
| 31 | MessageReceived, |
Misha Krieger-Raynauld | addd6fe | 2022-10-22 12:46:04 -0400 | [diff] [blame] | 32 | NameRegistrationEnded, |
| 33 | RegisteredNameFound, |
| 34 | RegistrationStateChanged, |
| 35 | VolatileDetailsChanged, |
| 36 | } from './jami-signal-interfaces.js'; |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 37 | import { JamiSwig, StringMap, stringMapToRecord, stringVectToArray } from './jami-swig.js'; |
| 38 | import { require } from './utils.js'; |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 39 | |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame^] | 40 | // TODO: Mechanism to map account IDs to a list of WebSockets |
| 41 | |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 42 | @Service() |
| 43 | export class Jamid { |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 44 | private readonly jamiSwig: JamiSwig; |
| 45 | private readonly usernamesToAccountIds: Map<string, string>; |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 46 | private readonly events; |
| 47 | |
| 48 | constructor() { |
| 49 | // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment |
simon | 7d4386c | 2022-10-26 17:47:59 -0400 | [diff] [blame] | 50 | 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] | 51 | |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame^] | 52 | this.usernamesToAccountIds = new Map<string, string>(); |
| 53 | |
| 54 | // Setup signal handlers |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 55 | const handlers: Record<string, unknown> = {}; |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame^] | 56 | |
| 57 | // Add default handler for all signals |
| 58 | const createDefaultHandler = (signal: string) => { |
| 59 | return (...args: unknown[]) => log.warn('Unhandled', signal, args); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 60 | }; |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame^] | 61 | for (const signal in JamiSignal) { |
| 62 | handlers[signal] = createDefaultHandler(signal); |
| 63 | } |
| 64 | |
| 65 | // Overwrite handlers for handled signals using RxJS Subjects, converting multiple arguments to objects |
| 66 | const onAccountsChanged = new Subject<void>(); |
| 67 | handlers.AccountsChanged = () => onAccountsChanged.next(); |
| 68 | |
| 69 | const onAccountDetailsChanged = new Subject<AccountDetailsChanged>(); |
| 70 | handlers.AccountDetailsChanged = (accountId: string, details: AccountDetails) => |
| 71 | onAccountDetailsChanged.next({ accountId, details }); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 72 | |
| 73 | const onVolatileDetailsChanged = new Subject<VolatileDetailsChanged>(); |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame^] | 74 | handlers.VolatileDetailsChanged = (accountId: string, details: VolatileDetails) => |
| 75 | onVolatileDetailsChanged.next({ accountId, details }); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 76 | |
| 77 | const onRegistrationStateChanged = new Subject<RegistrationStateChanged>(); |
| 78 | handlers.RegistrationStateChanged = (accountId: string, state: string, code: number, details: string) => |
| 79 | onRegistrationStateChanged.next({ accountId, state, code, details }); |
| 80 | |
| 81 | const onNameRegistrationEnded = new Subject<NameRegistrationEnded>(); |
| 82 | handlers.NameRegistrationEnded = (accountId: string, state: number, username: string) => |
| 83 | onNameRegistrationEnded.next({ accountId, state, username }); |
| 84 | |
| 85 | const onRegisteredNameFound = new Subject<RegisteredNameFound>(); |
| 86 | handlers.RegisteredNameFound = (accountId: string, state: number, address: string, username: string) => |
| 87 | onRegisteredNameFound.next({ accountId, state, address, username }); |
| 88 | |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame^] | 89 | const onKnownDevicesChanged = new Subject<KnownDevicesChanged>(); |
| 90 | handlers.KnownDevicesChanged = (accountId: string, devices: Record<string, string>) => |
| 91 | onKnownDevicesChanged.next({ accountId, devices }); |
| 92 | |
Charlie | b62c678 | 2022-10-30 15:14:56 -0400 | [diff] [blame] | 93 | const onIncomingAccountMessage = new Subject<IncomingAccountMessage>(); |
| 94 | handlers.IncomingAccountMessage = (accountId: string, from: string, message: Record<string, string>) => |
| 95 | onIncomingAccountMessage.next({ accountId, from, message }); |
| 96 | |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame^] | 97 | const onConversationReady = new Subject<ConversationReady>(); |
| 98 | handlers.ConversationReady = (accountId: string, conversationId: string) => |
| 99 | onConversationReady.next({ accountId, conversationId }); |
| 100 | |
| 101 | const onConversationRemoved = new Subject<ConversationRemoved>(); |
| 102 | handlers.ConversationRemoved = (accountId: string, conversationId: string) => |
| 103 | onConversationRemoved.next({ accountId, conversationId }); |
| 104 | |
| 105 | const onConversationLoaded = new Subject<ConversationLoaded>(); |
| 106 | handlers.ConversationLoaded = ( |
| 107 | id: number, |
| 108 | accountId: string, |
| 109 | conversationId: string, |
| 110 | messages: Record<string, string>[] |
| 111 | ) => onConversationLoaded.next({ id, accountId, conversationId, messages }); |
| 112 | |
| 113 | const onMessageReceived = new Subject<MessageReceived>(); |
| 114 | handlers.MessageReceived = (accountId: string, conversationId: string, message: Record<string, string>) => |
| 115 | onMessageReceived.next({ accountId, conversationId, message }); |
| 116 | |
| 117 | // 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] | 118 | this.events = { |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame^] | 119 | onAccountsChanged: onAccountsChanged.asObservable(), |
| 120 | onAccountDetailsChanged: onAccountDetailsChanged.asObservable(), |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 121 | onVolatileDetailsChanged: onVolatileDetailsChanged.asObservable(), |
| 122 | onRegistrationStateChanged: onRegistrationStateChanged.asObservable(), |
| 123 | onNameRegistrationEnded: onNameRegistrationEnded.asObservable(), |
| 124 | onRegisteredNameFound: onRegisteredNameFound.asObservable(), |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame^] | 125 | onKnownDevicesChanged: onKnownDevicesChanged.asObservable(), |
Charlie | b62c678 | 2022-10-30 15:14:56 -0400 | [diff] [blame] | 126 | onIncomingAccountMessage: onIncomingAccountMessage.asObservable(), |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame^] | 127 | onConversationReady: onConversationReady.asObservable(), |
| 128 | onConversationRemoved: onConversationRemoved.asObservable(), |
| 129 | onConversationLoaded: onConversationLoaded.asObservable(), |
| 130 | onMessageReceived: onMessageReceived.asObservable(), |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 131 | }; |
| 132 | |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame^] | 133 | this.setupSignalHandlers(); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 134 | |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame^] | 135 | // RxJS Subjects are used as signal handlers for the following reasons: |
| 136 | // 1. You cannot change event handlers after calling jamiSwig.init() |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 137 | // 2. You cannot specify multiple handlers for the same event |
| 138 | // 3. You cannot specify a default handler |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 139 | this.jamiSwig.init(handlers); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Misha Krieger-Raynauld | 62a0da9 | 2022-10-22 13:46:59 -0400 | [diff] [blame] | 142 | stop() { |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 143 | this.jamiSwig.fini(); |
Misha Krieger-Raynauld | 62a0da9 | 2022-10-22 13:46:59 -0400 | [diff] [blame] | 144 | } |
| 145 | |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 146 | getVolatileAccountDetails(accountId: string): VolatileDetails { |
| 147 | return stringMapToRecord(this.jamiSwig.getVolatileAccountDetails(accountId)) as unknown as VolatileDetails; |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 150 | getAccountDetails(accountId: string): AccountDetails { |
| 151 | return stringMapToRecord(this.jamiSwig.getAccountDetails(accountId)) as unknown as AccountDetails; |
| 152 | } |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 153 | |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 154 | setAccountDetails(accountId: string, accountDetails: AccountDetails) { |
simon | 43da57b | 2022-10-26 18:22:22 -0400 | [diff] [blame] | 155 | const accountDetailsStringMap: StringMap = new this.jamiSwig.StringMap(); |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 156 | for (const [key, value] of Object.entries(accountDetails)) { |
| 157 | accountDetailsStringMap.set(key, value); |
| 158 | } |
| 159 | this.jamiSwig.setAccountDetails(accountId, accountDetailsStringMap); |
| 160 | } |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 161 | |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 162 | async addAccount(details: Map<string, string | number | boolean>) { |
simon | 43da57b | 2022-10-26 18:22:22 -0400 | [diff] [blame] | 163 | const detailsStringMap: StringMap = new this.jamiSwig.StringMap(); |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 164 | |
| 165 | detailsStringMap.set('Account.type', 'RING'); |
| 166 | for (const [key, value] of details.entries()) { |
| 167 | detailsStringMap.set('Account.' + key, value.toString()); |
| 168 | } |
| 169 | |
| 170 | const accountId = this.jamiSwig.addAccount(detailsStringMap); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 171 | return firstValueFrom( |
| 172 | this.events.onRegistrationStateChanged.pipe( |
Misha Krieger-Raynauld | 6f9c7ae | 2022-10-28 11:41:45 -0400 | [diff] [blame] | 173 | filter((value) => value.accountId === accountId), |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 174 | // TODO: is it the only state? |
Misha Krieger-Raynauld | 6f9c7ae | 2022-10-28 11:41:45 -0400 | [diff] [blame] | 175 | // TODO: Replace with string enum in common/ |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 176 | filter(({ state }) => state === 'REGISTERED') |
| 177 | ) |
| 178 | ); |
| 179 | } |
| 180 | |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 181 | removeAccount(accountId: string) { |
| 182 | this.jamiSwig.removeAccount(accountId); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 185 | getAccountList(): string[] { |
| 186 | return stringVectToArray(this.jamiSwig.getAccountList()); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Charlie | b62c678 | 2022-10-30 15:14:56 -0400 | [diff] [blame] | 189 | sendAccountTextMessage(accountId: string, contactId: string, type: string, message: string) { |
| 190 | const messageStringMap: StringMap = new this.jamiSwig.StringMap(); |
| 191 | messageStringMap.set(type, message); |
| 192 | this.jamiSwig.sendAccountTextMessage(accountId, contactId, messageStringMap); |
| 193 | } |
| 194 | |
Misha Krieger-Raynauld | 6f9c7ae | 2022-10-28 11:41:45 -0400 | [diff] [blame] | 195 | async lookupUsername(username: string, accountId?: string) { |
| 196 | const hasRingNs = this.jamiSwig.lookupName(accountId || '', '', username); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 197 | if (!hasRingNs) { |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 198 | throw new Error('Jami does not have NS'); |
| 199 | } |
Misha Krieger-Raynauld | 6f9c7ae | 2022-10-28 11:41:45 -0400 | [diff] [blame] | 200 | return firstValueFrom( |
| 201 | this.events.onRegisteredNameFound.pipe( |
| 202 | filter((value) => value.username === username), |
| 203 | map(({ accountId: _, ...response }) => response) // Remove accountId from response |
| 204 | ) |
| 205 | ); |
| 206 | } |
| 207 | |
| 208 | async lookupAddress(address: string, accountId?: string) { |
| 209 | const hasRingNs = this.jamiSwig.lookupAddress(accountId || '', '', address); |
| 210 | if (!hasRingNs) { |
| 211 | throw new Error('Jami does not have NS'); |
| 212 | } |
| 213 | return firstValueFrom( |
| 214 | this.events.onRegisteredNameFound.pipe( |
| 215 | filter((value) => value.address === address), |
| 216 | map(({ accountId: _, ...response }) => response) // Remove accountId from response |
| 217 | ) |
| 218 | ); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 221 | async registerUsername(accountId: string, username: string, password: string) { |
| 222 | const hasRingNs = this.jamiSwig.registerName(accountId, password, username); |
| 223 | if (!hasRingNs) { |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 224 | throw new Error('Jami does not have NS'); |
| 225 | } |
Misha Krieger-Raynauld | 6f9c7ae | 2022-10-28 11:41:45 -0400 | [diff] [blame] | 226 | return firstValueFrom(this.events.onNameRegistrationEnded.pipe(filter((value) => value.accountId === accountId))); |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | getDevices(accountId: string): Record<string, string> { |
| 230 | return stringMapToRecord(this.jamiSwig.getKnownRingDevices(accountId)); |
| 231 | } |
| 232 | |
| 233 | getDefaultModerators(accountId: string): string[] { |
| 234 | return stringVectToArray(this.jamiSwig.getDefaultModerators(accountId)); |
| 235 | } |
| 236 | |
| 237 | // TODO: Ideally, we would fetch the username directly from Jami instead of |
| 238 | // keeping an internal map. |
| 239 | getAccountIdFromUsername(username: string): string | undefined { |
| 240 | return this.usernamesToAccountIds.get(username); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 241 | } |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame^] | 242 | |
| 243 | private setupSignalHandlers() { |
| 244 | this.events.onAccountsChanged.subscribe(() => { |
| 245 | log.debug('Received AccountsChanged'); |
| 246 | }); |
| 247 | |
| 248 | this.events.onAccountDetailsChanged.subscribe((signal) => { |
| 249 | log.debug('Received AccountsDetailsChanged', JSON.stringify(signal)); |
| 250 | }); |
| 251 | |
| 252 | this.events.onVolatileDetailsChanged.subscribe(({ accountId, details }) => { |
| 253 | log.debug(`Received VolatileDetailsChanged: {"accountId":"${accountId}", ...}`); |
| 254 | // Keep map of usernames to account IDs |
| 255 | const username = details['Account.registeredName']; |
| 256 | if (username) { |
| 257 | this.usernamesToAccountIds.set(username, accountId); |
| 258 | } |
| 259 | }); |
| 260 | |
| 261 | this.events.onRegistrationStateChanged.subscribe((signal) => { |
| 262 | log.debug('Received RegistrationStateChanged:', JSON.stringify(signal)); |
| 263 | }); |
| 264 | |
| 265 | this.events.onNameRegistrationEnded.subscribe((signal) => { |
| 266 | log.debug('Received NameRegistrationEnded:', JSON.stringify(signal)); |
| 267 | }); |
| 268 | |
| 269 | this.events.onRegisteredNameFound.subscribe((signal) => { |
| 270 | log.debug('Received RegisteredNameFound:', JSON.stringify(signal)); |
| 271 | }); |
| 272 | |
| 273 | this.events.onKnownDevicesChanged.subscribe(({ accountId }) => { |
| 274 | log.debug(`Received KnownDevicesChanged: {"accountId":"${accountId}", ...}`); |
| 275 | }); |
| 276 | |
| 277 | this.events.onIncomingAccountMessage.subscribe((signal) => { |
| 278 | log.debug('Received IncomingAccountMessage:', JSON.stringify(signal)); |
| 279 | }); |
| 280 | |
| 281 | this.events.onConversationReady.subscribe((signal) => { |
| 282 | log.debug('Received ConversationReady:', JSON.stringify(signal)); |
| 283 | }); |
| 284 | |
| 285 | this.events.onConversationRemoved.subscribe((signal) => { |
| 286 | log.debug('Received ConversationRemoved:', JSON.stringify(signal)); |
| 287 | }); |
| 288 | |
| 289 | this.events.onConversationLoaded.subscribe((signal) => { |
| 290 | log.debug('Received ConversationLoaded:', JSON.stringify(signal)); |
| 291 | }); |
| 292 | |
| 293 | this.events.onMessageReceived.subscribe((signal) => { |
| 294 | log.debug('Received MessageReceived:', JSON.stringify(signal)); |
| 295 | // TODO: Send message to client using WebSocket |
| 296 | }); |
| 297 | } |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 298 | } |