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 | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 18 | import { AccountDetails, Message, 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 | bfed173 | 2022-11-01 20:49:35 -0400 | [diff] [blame] | 37 | import { JamiSwig, StringMap, stringMapToRecord, stringVectToArray, vectMapToRecordArray } from './jami-swig.js'; |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 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 |
Misha Krieger-Raynauld | bfed173 | 2022-11-01 20:49:35 -0400 | [diff] [blame] | 41 | // 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] | 42 | |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 43 | @Service() |
| 44 | export class Jamid { |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 45 | private readonly jamiSwig: JamiSwig; |
| 46 | private readonly usernamesToAccountIds: Map<string, string>; |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 47 | private readonly events; |
| 48 | |
| 49 | constructor() { |
| 50 | // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment |
simon | 7d4386c | 2022-10-26 17:47:59 -0400 | [diff] [blame] | 51 | 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] | 52 | |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 53 | this.usernamesToAccountIds = new Map<string, string>(); |
| 54 | |
| 55 | // Setup signal handlers |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 56 | const handlers: Record<string, unknown> = {}; |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 57 | |
| 58 | // Add default handler for all signals |
| 59 | const createDefaultHandler = (signal: string) => { |
| 60 | return (...args: unknown[]) => log.warn('Unhandled', signal, args); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 61 | }; |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 62 | for (const signal in JamiSignal) { |
| 63 | handlers[signal] = createDefaultHandler(signal); |
| 64 | } |
| 65 | |
| 66 | // Overwrite handlers for handled signals using RxJS Subjects, converting multiple arguments to objects |
| 67 | const onAccountsChanged = new Subject<void>(); |
| 68 | handlers.AccountsChanged = () => onAccountsChanged.next(); |
| 69 | |
| 70 | const onAccountDetailsChanged = new Subject<AccountDetailsChanged>(); |
| 71 | handlers.AccountDetailsChanged = (accountId: string, details: AccountDetails) => |
| 72 | onAccountDetailsChanged.next({ accountId, details }); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 73 | |
| 74 | const onVolatileDetailsChanged = new Subject<VolatileDetailsChanged>(); |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 75 | handlers.VolatileDetailsChanged = (accountId: string, details: VolatileDetails) => |
| 76 | onVolatileDetailsChanged.next({ accountId, details }); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 77 | |
| 78 | const onRegistrationStateChanged = new Subject<RegistrationStateChanged>(); |
| 79 | handlers.RegistrationStateChanged = (accountId: string, state: string, code: number, details: string) => |
| 80 | onRegistrationStateChanged.next({ accountId, state, code, details }); |
| 81 | |
| 82 | const onNameRegistrationEnded = new Subject<NameRegistrationEnded>(); |
| 83 | handlers.NameRegistrationEnded = (accountId: string, state: number, username: string) => |
| 84 | onNameRegistrationEnded.next({ accountId, state, username }); |
| 85 | |
| 86 | const onRegisteredNameFound = new Subject<RegisteredNameFound>(); |
| 87 | handlers.RegisteredNameFound = (accountId: string, state: number, address: string, username: string) => |
| 88 | onRegisteredNameFound.next({ accountId, state, address, username }); |
| 89 | |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 90 | const onKnownDevicesChanged = new Subject<KnownDevicesChanged>(); |
| 91 | handlers.KnownDevicesChanged = (accountId: string, devices: Record<string, string>) => |
| 92 | onKnownDevicesChanged.next({ accountId, devices }); |
| 93 | |
Charlie | b62c678 | 2022-10-30 15:14:56 -0400 | [diff] [blame] | 94 | const onIncomingAccountMessage = new Subject<IncomingAccountMessage>(); |
| 95 | handlers.IncomingAccountMessage = (accountId: string, from: string, message: Record<string, string>) => |
| 96 | onIncomingAccountMessage.next({ accountId, from, message }); |
| 97 | |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 98 | const onConversationReady = new Subject<ConversationReady>(); |
| 99 | handlers.ConversationReady = (accountId: string, conversationId: string) => |
| 100 | onConversationReady.next({ accountId, conversationId }); |
| 101 | |
| 102 | const onConversationRemoved = new Subject<ConversationRemoved>(); |
| 103 | handlers.ConversationRemoved = (accountId: string, conversationId: string) => |
| 104 | onConversationRemoved.next({ accountId, conversationId }); |
| 105 | |
| 106 | const onConversationLoaded = new Subject<ConversationLoaded>(); |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 107 | handlers.ConversationLoaded = (id: number, accountId: string, conversationId: string, messages: Message[]) => |
| 108 | onConversationLoaded.next({ id, accountId, conversationId, messages }); |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 109 | |
| 110 | const onMessageReceived = new Subject<MessageReceived>(); |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 111 | handlers.MessageReceived = (accountId: string, conversationId: string, message: Message) => |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 112 | onMessageReceived.next({ accountId, conversationId, message }); |
| 113 | |
| 114 | // 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] | 115 | this.events = { |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 116 | onAccountsChanged: onAccountsChanged.asObservable(), |
| 117 | onAccountDetailsChanged: onAccountDetailsChanged.asObservable(), |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 118 | onVolatileDetailsChanged: onVolatileDetailsChanged.asObservable(), |
| 119 | onRegistrationStateChanged: onRegistrationStateChanged.asObservable(), |
| 120 | onNameRegistrationEnded: onNameRegistrationEnded.asObservable(), |
| 121 | onRegisteredNameFound: onRegisteredNameFound.asObservable(), |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 122 | onKnownDevicesChanged: onKnownDevicesChanged.asObservable(), |
Charlie | b62c678 | 2022-10-30 15:14:56 -0400 | [diff] [blame] | 123 | onIncomingAccountMessage: onIncomingAccountMessage.asObservable(), |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 124 | onConversationReady: onConversationReady.asObservable(), |
| 125 | onConversationRemoved: onConversationRemoved.asObservable(), |
| 126 | onConversationLoaded: onConversationLoaded.asObservable(), |
| 127 | onMessageReceived: onMessageReceived.asObservable(), |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 128 | }; |
| 129 | |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 130 | this.setupSignalHandlers(); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 131 | |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 132 | // RxJS Subjects are used as signal handlers for the following reasons: |
| 133 | // 1. You cannot change event handlers after calling jamiSwig.init() |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 134 | // 2. You cannot specify multiple handlers for the same event |
| 135 | // 3. You cannot specify a default handler |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 136 | this.jamiSwig.init(handlers); |
Charlie | 6ddaefe | 2022-11-01 18:36:29 -0400 | [diff] [blame] | 137 | |
| 138 | // TODO: Bind websocket callbacks for webrtc action on Incoming account message |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 141 | stop(): void { |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 142 | this.jamiSwig.fini(); |
Misha Krieger-Raynauld | 62a0da9 | 2022-10-22 13:46:59 -0400 | [diff] [blame] | 143 | } |
| 144 | |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 145 | getVolatileAccountDetails(accountId: string): VolatileDetails { |
| 146 | return stringMapToRecord(this.jamiSwig.getVolatileAccountDetails(accountId)) as unknown as VolatileDetails; |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 149 | getAccountDetails(accountId: string): AccountDetails { |
| 150 | return stringMapToRecord(this.jamiSwig.getAccountDetails(accountId)) as unknown as AccountDetails; |
| 151 | } |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 152 | |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 153 | setAccountDetails(accountId: string, accountDetails: AccountDetails): void { |
simon | 43da57b | 2022-10-26 18:22:22 -0400 | [diff] [blame] | 154 | const accountDetailsStringMap: StringMap = new this.jamiSwig.StringMap(); |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 155 | for (const [key, value] of Object.entries(accountDetails)) { |
| 156 | accountDetailsStringMap.set(key, value); |
| 157 | } |
| 158 | this.jamiSwig.setAccountDetails(accountId, accountDetailsStringMap); |
| 159 | } |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 160 | |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 161 | async addAccount(details: Map<string, string | number | boolean>): Promise<string> { |
simon | 43da57b | 2022-10-26 18:22:22 -0400 | [diff] [blame] | 162 | const detailsStringMap: StringMap = new this.jamiSwig.StringMap(); |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 163 | |
| 164 | detailsStringMap.set('Account.type', 'RING'); |
| 165 | for (const [key, value] of details.entries()) { |
| 166 | detailsStringMap.set('Account.' + key, value.toString()); |
| 167 | } |
| 168 | |
| 169 | const accountId = this.jamiSwig.addAccount(detailsStringMap); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 170 | return firstValueFrom( |
| 171 | this.events.onRegistrationStateChanged.pipe( |
Misha Krieger-Raynauld | 6f9c7ae | 2022-10-28 11:41:45 -0400 | [diff] [blame] | 172 | filter((value) => value.accountId === accountId), |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 173 | // TODO: is it the only state? |
Misha Krieger-Raynauld | 6f9c7ae | 2022-10-28 11:41:45 -0400 | [diff] [blame] | 174 | // TODO: Replace with string enum in common/ |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 175 | filter((value) => value.state === 'REGISTERED'), |
| 176 | map((value) => value.accountId) |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 177 | ) |
| 178 | ); |
| 179 | } |
| 180 | |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 181 | removeAccount(accountId: string): void { |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 182 | this.jamiSwig.removeAccount(accountId); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 185 | getAccountIds(): string[] { |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 186 | return stringVectToArray(this.jamiSwig.getAccountList()); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 189 | sendAccountTextMessage(accountId: string, contactId: string, type: string, message: string): void { |
Charlie | b62c678 | 2022-10-30 15:14:56 -0400 | [diff] [blame] | 190 | const messageStringMap: StringMap = new this.jamiSwig.StringMap(); |
Charlie | 6ddaefe | 2022-11-01 18:36:29 -0400 | [diff] [blame] | 191 | messageStringMap.set(type, JSON.stringify(message)); |
Charlie | b62c678 | 2022-10-30 15:14:56 -0400 | [diff] [blame] | 192 | this.jamiSwig.sendAccountTextMessage(accountId, contactId, messageStringMap); |
| 193 | } |
| 194 | |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 195 | // TODO: Add interface for returned type |
Misha Krieger-Raynauld | 6f9c7ae | 2022-10-28 11:41:45 -0400 | [diff] [blame] | 196 | async lookupUsername(username: string, accountId?: string) { |
| 197 | const hasRingNs = this.jamiSwig.lookupName(accountId || '', '', username); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 198 | if (!hasRingNs) { |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 199 | throw new Error('Jami does not have NS'); |
| 200 | } |
Misha Krieger-Raynauld | 6f9c7ae | 2022-10-28 11:41:45 -0400 | [diff] [blame] | 201 | return firstValueFrom( |
| 202 | this.events.onRegisteredNameFound.pipe( |
| 203 | filter((value) => value.username === username), |
| 204 | map(({ accountId: _, ...response }) => response) // Remove accountId from response |
| 205 | ) |
| 206 | ); |
| 207 | } |
| 208 | |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 209 | // TODO: Add interface for returned type |
Misha Krieger-Raynauld | 6f9c7ae | 2022-10-28 11:41:45 -0400 | [diff] [blame] | 210 | async lookupAddress(address: string, accountId?: string) { |
| 211 | const hasRingNs = this.jamiSwig.lookupAddress(accountId || '', '', address); |
| 212 | if (!hasRingNs) { |
| 213 | throw new Error('Jami does not have NS'); |
| 214 | } |
| 215 | return firstValueFrom( |
| 216 | this.events.onRegisteredNameFound.pipe( |
| 217 | filter((value) => value.address === address), |
| 218 | map(({ accountId: _, ...response }) => response) // Remove accountId from response |
| 219 | ) |
| 220 | ); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 223 | // TODO: Create enum for state and return that rather than a number |
| 224 | async registerUsername(accountId: string, username: string, password: string): Promise<number> { |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 225 | const hasRingNs = this.jamiSwig.registerName(accountId, password, username); |
| 226 | if (!hasRingNs) { |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 227 | throw new Error('Jami does not have NS'); |
| 228 | } |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 229 | return firstValueFrom( |
| 230 | this.events.onNameRegistrationEnded.pipe( |
| 231 | filter((value) => value.accountId === accountId), |
| 232 | map((value) => value.state) |
| 233 | ) |
| 234 | ); |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | getDevices(accountId: string): Record<string, string> { |
| 238 | return stringMapToRecord(this.jamiSwig.getKnownRingDevices(accountId)); |
| 239 | } |
| 240 | |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 241 | addContact(accountId: string, contactId: string): void { |
Misha Krieger-Raynauld | bfed173 | 2022-11-01 20:49:35 -0400 | [diff] [blame] | 242 | this.jamiSwig.addContact(accountId, contactId); |
| 243 | } |
| 244 | |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 245 | removeContact(accountId: string, contactId: string): void { |
Misha Krieger-Raynauld | bfed173 | 2022-11-01 20:49:35 -0400 | [diff] [blame] | 246 | this.jamiSwig.removeContact(accountId, contactId, false); |
| 247 | } |
| 248 | |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 249 | blockContact(accountId: string, contactId: string): void { |
Misha Krieger-Raynauld | bfed173 | 2022-11-01 20:49:35 -0400 | [diff] [blame] | 250 | this.jamiSwig.removeContact(accountId, contactId, true); |
| 251 | } |
| 252 | |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 253 | // TODO: Replace Record with interface |
Misha Krieger-Raynauld | bfed173 | 2022-11-01 20:49:35 -0400 | [diff] [blame] | 254 | getContacts(accountId: string): Record<string, string>[] { |
| 255 | return vectMapToRecordArray(this.jamiSwig.getContacts(accountId)); |
| 256 | } |
| 257 | |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 258 | // TODO: Replace Record with interface |
Misha Krieger-Raynauld | bfed173 | 2022-11-01 20:49:35 -0400 | [diff] [blame] | 259 | getContactDetails(accountId: string, contactId: string): Record<string, string> { |
| 260 | return stringMapToRecord(this.jamiSwig.getContactDetails(accountId, contactId)); |
| 261 | } |
| 262 | |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 263 | getDefaultModerators(accountId: string): string[] { |
| 264 | return stringVectToArray(this.jamiSwig.getDefaultModerators(accountId)); |
| 265 | } |
| 266 | |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 267 | getConversationIds(accountId: string): string[] { |
| 268 | return stringVectToArray(this.jamiSwig.getConversations(accountId)); |
| 269 | } |
| 270 | |
| 271 | // TODO: Replace Record with interface |
| 272 | getConversationInfos(accountId: string, conversationId: string): Record<string, string> { |
| 273 | return stringMapToRecord(this.jamiSwig.conversationInfos(accountId, conversationId)); |
| 274 | } |
| 275 | |
| 276 | // TODO: Replace Record with interface |
| 277 | getConversationMembers(accountId: string, conversationId: string): Record<string, string>[] { |
| 278 | return vectMapToRecordArray(this.jamiSwig.getConversationMembers(accountId, conversationId)); |
| 279 | } |
| 280 | |
| 281 | async getConversationMessages(accountId: string, conversationId: string, fromMessage?: string): Promise<Message[]> { |
| 282 | const requestId = this.jamiSwig.loadConversationMessages(accountId, conversationId, fromMessage || '', 32); |
| 283 | return firstValueFrom( |
| 284 | this.events.onConversationLoaded.pipe( |
| 285 | filter((value) => value.id === requestId), |
| 286 | map((value) => value.messages) |
| 287 | ) |
| 288 | ); |
| 289 | } |
| 290 | |
| 291 | sendConversationMessage(accountId: string, conversationId: string, message: string, replyTo?: string): void { |
| 292 | this.jamiSwig.sendMessage(accountId, conversationId, message, replyTo || ''); |
| 293 | } |
| 294 | |
Misha Krieger-Raynauld | 7f95933 | 2022-11-04 15:12:53 -0400 | [diff] [blame] | 295 | getCallIds(accountId: string): string[] { |
| 296 | return stringVectToArray(this.jamiSwig.getCallList(accountId)); |
| 297 | } |
| 298 | |
| 299 | // TODO: Replace Record with interface |
| 300 | getCallDetails(accountId: string, callId: string): Record<string, string> { |
| 301 | return stringMapToRecord(this.jamiSwig.getCallDetails(accountId, callId)); |
| 302 | } |
| 303 | |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 304 | getAccountIdFromUsername(username: string): string | undefined { |
| 305 | return this.usernamesToAccountIds.get(username); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 306 | } |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 307 | |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 308 | private setupSignalHandlers(): void { |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 309 | this.events.onAccountsChanged.subscribe(() => { |
| 310 | log.debug('Received AccountsChanged'); |
| 311 | }); |
| 312 | |
| 313 | this.events.onAccountDetailsChanged.subscribe((signal) => { |
| 314 | log.debug('Received AccountsDetailsChanged', JSON.stringify(signal)); |
| 315 | }); |
| 316 | |
| 317 | this.events.onVolatileDetailsChanged.subscribe(({ accountId, details }) => { |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 318 | const username = details['Account.registeredName']; |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 319 | log.debug( |
| 320 | `Received VolatileDetailsChanged: {"accountId":"${accountId}",` + |
| 321 | `"details":{"Account.registeredName":"${username}", ...}}` |
| 322 | ); |
| 323 | // Keep map of usernames to account IDs |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 324 | if (username) { |
| 325 | this.usernamesToAccountIds.set(username, accountId); |
| 326 | } |
| 327 | }); |
| 328 | |
| 329 | this.events.onRegistrationStateChanged.subscribe((signal) => { |
| 330 | log.debug('Received RegistrationStateChanged:', JSON.stringify(signal)); |
| 331 | }); |
| 332 | |
| 333 | this.events.onNameRegistrationEnded.subscribe((signal) => { |
| 334 | log.debug('Received NameRegistrationEnded:', JSON.stringify(signal)); |
| 335 | }); |
| 336 | |
| 337 | this.events.onRegisteredNameFound.subscribe((signal) => { |
| 338 | log.debug('Received RegisteredNameFound:', JSON.stringify(signal)); |
| 339 | }); |
| 340 | |
| 341 | this.events.onKnownDevicesChanged.subscribe(({ accountId }) => { |
| 342 | log.debug(`Received KnownDevicesChanged: {"accountId":"${accountId}", ...}`); |
| 343 | }); |
| 344 | |
| 345 | this.events.onIncomingAccountMessage.subscribe((signal) => { |
| 346 | log.debug('Received IncomingAccountMessage:', JSON.stringify(signal)); |
| 347 | }); |
| 348 | |
| 349 | this.events.onConversationReady.subscribe((signal) => { |
| 350 | log.debug('Received ConversationReady:', JSON.stringify(signal)); |
| 351 | }); |
| 352 | |
| 353 | this.events.onConversationRemoved.subscribe((signal) => { |
| 354 | log.debug('Received ConversationRemoved:', JSON.stringify(signal)); |
| 355 | }); |
| 356 | |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 357 | this.events.onConversationLoaded.subscribe(({ id, accountId, conversationId }) => { |
| 358 | log.debug( |
| 359 | `Received ConversationLoaded: {"id":"${id}","accountId":"${accountId}",` + |
| 360 | `"conversationId":"${conversationId}","messages":[...]}` |
| 361 | ); |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 362 | }); |
| 363 | |
| 364 | this.events.onMessageReceived.subscribe((signal) => { |
| 365 | log.debug('Received MessageReceived:', JSON.stringify(signal)); |
Charlie | 6ddaefe | 2022-11-01 18:36:29 -0400 | [diff] [blame] | 366 | // TODO: Send message to client using WS service |
Misha Krieger-Raynauld | 68a9b56 | 2022-10-28 19:47:46 -0400 | [diff] [blame] | 367 | }); |
| 368 | } |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 369 | } |