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 | */ |
| 18 | import log from 'loglevel'; |
| 19 | import { filter, firstValueFrom, Subject } from 'rxjs'; |
| 20 | import { Service } from 'typedi'; |
| 21 | |
Misha Krieger-Raynauld | addd6fe | 2022-10-22 12:46:04 -0400 | [diff] [blame^] | 22 | import { itMap, require } from '../utils.js'; |
| 23 | import { JamiSignal } from './jami-signal.js'; |
| 24 | import { |
| 25 | NameRegistrationEnded, |
| 26 | RegisteredNameFound, |
| 27 | RegistrationStateChanged, |
| 28 | VolatileDetailsChanged, |
| 29 | } from './jami-signal-interfaces.js'; |
| 30 | import { JamiSwig, StringMap, stringMapToMap, stringVectToArr } from './jami-swig.js'; |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 31 | |
| 32 | @Service() |
| 33 | export class Jamid { |
| 34 | private readonly jamid: JamiSwig; |
| 35 | private readonly mapUsernameToAccountId: Map<string, string>; |
| 36 | private readonly events; |
| 37 | |
| 38 | constructor() { |
| 39 | // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment |
| 40 | this.jamid = require('../jamid.node') as JamiSwig; |
| 41 | |
| 42 | const handlers: Record<string, unknown> = {}; |
| 43 | const handler = (sig: string) => { |
| 44 | return (...args: unknown[]) => log.warn('Unhandled', sig, args); |
| 45 | }; |
| 46 | Object.keys(JamiSignal).forEach((sig) => (handlers[sig] = handler(sig))); |
| 47 | |
| 48 | const onVolatileDetailsChanged = new Subject<VolatileDetailsChanged>(); |
| 49 | handlers.VolatileDetailsChanged = (accountId: string, details: Record<string, string>) => |
| 50 | onVolatileDetailsChanged.next({ accountId, details: new Map(Object.entries(details)) }); |
| 51 | |
| 52 | const onRegistrationStateChanged = new Subject<RegistrationStateChanged>(); |
| 53 | handlers.RegistrationStateChanged = (accountId: string, state: string, code: number, details: string) => |
| 54 | onRegistrationStateChanged.next({ accountId, state, code, details }); |
| 55 | |
| 56 | const onNameRegistrationEnded = new Subject<NameRegistrationEnded>(); |
| 57 | handlers.NameRegistrationEnded = (accountId: string, state: number, username: string) => |
| 58 | onNameRegistrationEnded.next({ accountId, state, username }); |
| 59 | |
| 60 | const onRegisteredNameFound = new Subject<RegisteredNameFound>(); |
| 61 | handlers.RegisteredNameFound = (accountId: string, state: number, address: string, username: string) => |
| 62 | onRegisteredNameFound.next({ accountId, state, address, username }); |
| 63 | |
| 64 | this.events = { |
| 65 | onVolatileDetailsChanged: onVolatileDetailsChanged.asObservable(), |
| 66 | onRegistrationStateChanged: onRegistrationStateChanged.asObservable(), |
| 67 | onNameRegistrationEnded: onNameRegistrationEnded.asObservable(), |
| 68 | onRegisteredNameFound: onRegisteredNameFound.asObservable(), |
| 69 | }; |
| 70 | |
| 71 | this.events.onVolatileDetailsChanged.subscribe(({ accountId, details }) => { |
| 72 | log.debug('[1] Received onVolatileDetailsChanged with', { accountId, details }); |
| 73 | // Keep map of usernames to account IDs as Jamid cannot do this by itself (AFAIK) |
| 74 | const username = details.get('Account.registeredName'); |
| 75 | if (username) { |
| 76 | this.mapUsernameToAccountId.set(username, accountId); |
| 77 | } |
| 78 | }); |
| 79 | this.events.onRegistrationStateChanged.subscribe((ctx) => |
| 80 | log.debug('[1] Received onRegistrationStateChanged with', ctx) |
| 81 | ); |
| 82 | this.events.onNameRegistrationEnded.subscribe((ctx) => log.debug('[1] Received onNameRegistrationEnded with', ctx)); |
| 83 | this.events.onRegisteredNameFound.subscribe((ctx) => log.debug('[1] Received onRegisteredNameFound with', ctx)); |
| 84 | |
| 85 | this.mapUsernameToAccountId = new Map<string, string>(); |
| 86 | |
| 87 | // 1. You cannot change event handlers after init |
| 88 | // 2. You cannot specify multiple handlers for the same event |
| 89 | // 3. You cannot specify a default handler |
| 90 | // So we rely on the Subject() instead of Observable() |
| 91 | // Also, handlers receive multiple argument instead of tuple or object! |
| 92 | this.jamid.init(handlers); |
| 93 | } |
| 94 | |
| 95 | getAccountList() { |
| 96 | return stringVectToArr(this.jamid.getAccountList()); |
| 97 | } |
| 98 | |
| 99 | async createAccount(details: Map<string, string | number | boolean>) { |
| 100 | // TODO: add proper typing directly into JamiSwig |
| 101 | const stringMapDetails: StringMap = new (this.jamid as any).StringMap(); |
| 102 | |
| 103 | stringMapDetails.set('Account.type', 'RING'); |
| 104 | itMap(details.entries(), ([k, v]) => stringMapDetails.set('Account.' + k, v.toString())); |
| 105 | |
| 106 | const id = this.jamid.addAccount(stringMapDetails); |
| 107 | return firstValueFrom( |
| 108 | this.events.onRegistrationStateChanged.pipe( |
| 109 | filter(({ accountId }) => accountId === id), |
| 110 | // TODO: is it the only state? |
| 111 | filter(({ state }) => state === 'REGISTERED') |
| 112 | ) |
| 113 | ); |
| 114 | } |
| 115 | |
| 116 | destroyAccount(id: string) { |
| 117 | this.jamid.removeAccount(id); |
| 118 | } |
| 119 | |
| 120 | async registerUsername(id: string, username: string, password: string) { |
| 121 | const hasRingNs = this.jamid.registerName(id, password, username); |
| 122 | if (!hasRingNs) { |
| 123 | log.error('Jami does not have NS'); |
| 124 | throw new Error('Jami does not have NS'); |
| 125 | } |
| 126 | return firstValueFrom(this.events.onNameRegistrationEnded.pipe(filter(({ accountId }) => accountId === id))); |
| 127 | } |
| 128 | |
| 129 | // TODO: Ideally, we would fetch the username directly from Jami instead of |
| 130 | // keeping an internal map. |
| 131 | usernameToAccountId(username: string) { |
| 132 | return this.mapUsernameToAccountId.get(username); |
| 133 | } |
| 134 | |
| 135 | async lookupUsername(username: string) { |
| 136 | const hasRingNs = this.jamid.lookupName('', '', username); |
| 137 | if (!hasRingNs) { |
| 138 | log.error('Jami does not have NS'); |
| 139 | throw new Error('Jami does not have NS'); |
| 140 | } |
| 141 | return firstValueFrom(this.events.onRegisteredNameFound.pipe(filter((r) => r.username === username))); |
| 142 | } |
| 143 | |
| 144 | getAccountDetails(id: string) { |
| 145 | return stringMapToMap(this.jamid.getAccountDetails(id)); |
| 146 | } |
| 147 | } |