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'; |
| 20 | import { filter, firstValueFrom, Subject } from 'rxjs'; |
| 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 { |
Charlie | b62c678 | 2022-10-30 15:14:56 -0400 | [diff] [blame] | 25 | IncomingAccountMessage, |
Misha Krieger-Raynauld | addd6fe | 2022-10-22 12:46:04 -0400 | [diff] [blame] | 26 | NameRegistrationEnded, |
| 27 | RegisteredNameFound, |
| 28 | RegistrationStateChanged, |
| 29 | VolatileDetailsChanged, |
| 30 | } from './jami-signal-interfaces.js'; |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 31 | import { JamiSwig, StringMap, stringMapToRecord, stringVectToArray } from './jami-swig.js'; |
| 32 | import { require } from './utils.js'; |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 33 | |
| 34 | @Service() |
| 35 | export class Jamid { |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 36 | private readonly jamiSwig: JamiSwig; |
| 37 | private readonly usernamesToAccountIds: Map<string, string>; |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 38 | private readonly events; |
| 39 | |
| 40 | constructor() { |
| 41 | // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment |
simon | 7d4386c | 2022-10-26 17:47:59 -0400 | [diff] [blame] | 42 | 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] | 43 | |
| 44 | const handlers: Record<string, unknown> = {}; |
| 45 | const handler = (sig: string) => { |
| 46 | return (...args: unknown[]) => log.warn('Unhandled', sig, args); |
| 47 | }; |
| 48 | Object.keys(JamiSignal).forEach((sig) => (handlers[sig] = handler(sig))); |
| 49 | |
| 50 | const onVolatileDetailsChanged = new Subject<VolatileDetailsChanged>(); |
| 51 | handlers.VolatileDetailsChanged = (accountId: string, details: Record<string, string>) => |
| 52 | onVolatileDetailsChanged.next({ accountId, details: new Map(Object.entries(details)) }); |
| 53 | |
| 54 | const onRegistrationStateChanged = new Subject<RegistrationStateChanged>(); |
| 55 | handlers.RegistrationStateChanged = (accountId: string, state: string, code: number, details: string) => |
| 56 | onRegistrationStateChanged.next({ accountId, state, code, details }); |
| 57 | |
| 58 | const onNameRegistrationEnded = new Subject<NameRegistrationEnded>(); |
| 59 | handlers.NameRegistrationEnded = (accountId: string, state: number, username: string) => |
| 60 | onNameRegistrationEnded.next({ accountId, state, username }); |
| 61 | |
| 62 | const onRegisteredNameFound = new Subject<RegisteredNameFound>(); |
| 63 | handlers.RegisteredNameFound = (accountId: string, state: number, address: string, username: string) => |
| 64 | onRegisteredNameFound.next({ accountId, state, address, username }); |
| 65 | |
Charlie | b62c678 | 2022-10-30 15:14:56 -0400 | [diff] [blame] | 66 | const onIncomingAccountMessage = new Subject<IncomingAccountMessage>(); |
| 67 | handlers.IncomingAccountMessage = (accountId: string, from: string, message: Record<string, string>) => |
| 68 | onIncomingAccountMessage.next({ accountId, from, message }); |
| 69 | |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 70 | this.events = { |
| 71 | onVolatileDetailsChanged: onVolatileDetailsChanged.asObservable(), |
| 72 | onRegistrationStateChanged: onRegistrationStateChanged.asObservable(), |
| 73 | onNameRegistrationEnded: onNameRegistrationEnded.asObservable(), |
| 74 | onRegisteredNameFound: onRegisteredNameFound.asObservable(), |
Charlie | b62c678 | 2022-10-30 15:14:56 -0400 | [diff] [blame] | 75 | onIncomingAccountMessage: onIncomingAccountMessage.asObservable(), |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | this.events.onVolatileDetailsChanged.subscribe(({ accountId, details }) => { |
| 79 | log.debug('[1] Received onVolatileDetailsChanged with', { accountId, details }); |
| 80 | // Keep map of usernames to account IDs as Jamid cannot do this by itself (AFAIK) |
| 81 | const username = details.get('Account.registeredName'); |
| 82 | if (username) { |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 83 | this.usernamesToAccountIds.set(username, accountId); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 84 | } |
| 85 | }); |
| 86 | this.events.onRegistrationStateChanged.subscribe((ctx) => |
| 87 | log.debug('[1] Received onRegistrationStateChanged with', ctx) |
| 88 | ); |
| 89 | this.events.onNameRegistrationEnded.subscribe((ctx) => log.debug('[1] Received onNameRegistrationEnded with', ctx)); |
| 90 | this.events.onRegisteredNameFound.subscribe((ctx) => log.debug('[1] Received onRegisteredNameFound with', ctx)); |
Charlie | b62c678 | 2022-10-30 15:14:56 -0400 | [diff] [blame] | 91 | this.events.onIncomingAccountMessage.subscribe((ctx) => |
| 92 | log.debug('[1] Received onIncomingAccountMessage with', ctx) |
| 93 | ); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 94 | |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 95 | this.usernamesToAccountIds = new Map<string, string>(); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 96 | |
| 97 | // 1. You cannot change event handlers after init |
| 98 | // 2. You cannot specify multiple handlers for the same event |
| 99 | // 3. You cannot specify a default handler |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 100 | // So we rely on Subject() instead of Observable() |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 101 | // Also, handlers receive multiple argument instead of tuple or object! |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 102 | this.jamiSwig.init(handlers); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Misha Krieger-Raynauld | 62a0da9 | 2022-10-22 13:46:59 -0400 | [diff] [blame] | 105 | stop() { |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 106 | this.jamiSwig.fini(); |
Misha Krieger-Raynauld | 62a0da9 | 2022-10-22 13:46:59 -0400 | [diff] [blame] | 107 | } |
| 108 | |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 109 | getVolatileAccountDetails(accountId: string): VolatileDetails { |
| 110 | return stringMapToRecord(this.jamiSwig.getVolatileAccountDetails(accountId)) as unknown as VolatileDetails; |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 113 | getAccountDetails(accountId: string): AccountDetails { |
| 114 | return stringMapToRecord(this.jamiSwig.getAccountDetails(accountId)) as unknown as AccountDetails; |
| 115 | } |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 116 | |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 117 | setAccountDetails(accountId: string, accountDetails: AccountDetails) { |
simon | 43da57b | 2022-10-26 18:22:22 -0400 | [diff] [blame] | 118 | const accountDetailsStringMap: StringMap = new this.jamiSwig.StringMap(); |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 119 | for (const [key, value] of Object.entries(accountDetails)) { |
| 120 | accountDetailsStringMap.set(key, value); |
| 121 | } |
| 122 | this.jamiSwig.setAccountDetails(accountId, accountDetailsStringMap); |
| 123 | } |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 124 | |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 125 | async addAccount(details: Map<string, string | number | boolean>) { |
simon | 43da57b | 2022-10-26 18:22:22 -0400 | [diff] [blame] | 126 | const detailsStringMap: StringMap = new this.jamiSwig.StringMap(); |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 127 | |
| 128 | detailsStringMap.set('Account.type', 'RING'); |
| 129 | for (const [key, value] of details.entries()) { |
| 130 | detailsStringMap.set('Account.' + key, value.toString()); |
| 131 | } |
| 132 | |
| 133 | const accountId = this.jamiSwig.addAccount(detailsStringMap); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 134 | return firstValueFrom( |
| 135 | this.events.onRegistrationStateChanged.pipe( |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 136 | filter(({ accountId: addedAccountId }) => addedAccountId === accountId), |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 137 | // TODO: is it the only state? |
| 138 | filter(({ state }) => state === 'REGISTERED') |
| 139 | ) |
| 140 | ); |
| 141 | } |
| 142 | |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 143 | removeAccount(accountId: string) { |
| 144 | this.jamiSwig.removeAccount(accountId); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 147 | getAccountList(): string[] { |
| 148 | return stringVectToArray(this.jamiSwig.getAccountList()); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Charlie | b62c678 | 2022-10-30 15:14:56 -0400 | [diff] [blame] | 151 | sendAccountTextMessage(accountId: string, contactId: string, type: string, message: string) { |
| 152 | const messageStringMap: StringMap = new this.jamiSwig.StringMap(); |
| 153 | messageStringMap.set(type, message); |
| 154 | this.jamiSwig.sendAccountTextMessage(accountId, contactId, messageStringMap); |
| 155 | } |
| 156 | |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 157 | async lookupUsername(username: string) { |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 158 | const hasRingNs = this.jamiSwig.lookupName('', '', username); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 159 | if (!hasRingNs) { |
| 160 | log.error('Jami does not have NS'); |
| 161 | throw new Error('Jami does not have NS'); |
| 162 | } |
| 163 | return firstValueFrom(this.events.onRegisteredNameFound.pipe(filter((r) => r.username === username))); |
| 164 | } |
| 165 | |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 166 | async registerUsername(accountId: string, username: string, password: string) { |
| 167 | const hasRingNs = this.jamiSwig.registerName(accountId, password, username); |
| 168 | if (!hasRingNs) { |
| 169 | log.error('Jami does not have NS'); |
| 170 | throw new Error('Jami does not have NS'); |
| 171 | } |
| 172 | return firstValueFrom( |
| 173 | this.events.onNameRegistrationEnded.pipe( |
| 174 | filter(({ accountId: registeredAccountId }) => registeredAccountId === accountId) |
| 175 | ) |
| 176 | ); |
| 177 | } |
| 178 | |
| 179 | getDevices(accountId: string): Record<string, string> { |
| 180 | return stringMapToRecord(this.jamiSwig.getKnownRingDevices(accountId)); |
| 181 | } |
| 182 | |
| 183 | getDefaultModerators(accountId: string): string[] { |
| 184 | return stringVectToArray(this.jamiSwig.getDefaultModerators(accountId)); |
| 185 | } |
| 186 | |
| 187 | // TODO: Ideally, we would fetch the username directly from Jami instead of |
| 188 | // keeping an internal map. |
| 189 | getAccountIdFromUsername(username: string): string | undefined { |
| 190 | return this.usernamesToAccountIds.get(username); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 191 | } |
| 192 | } |