blob: ae2299e7c662f493c8165cb231b97511b3443cc8 [file] [log] [blame]
Misha Krieger-Raynauldcfa44302022-11-30 18:36:36 -05001/*
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 */
18import { AccountDetails, Devices, IAccount, VolatileDetails } from 'jami-web-common';
19
20import { Contact } from './contact';
Misha Krieger-Raynauldcfa44302022-11-30 18:36:36 -050021
22export type AccountType = 'RING' | 'SIP';
23
24export class Account implements IAccount {
25 readonly id: string;
26 details: AccountDetails;
27 volatileDetails: VolatileDetails;
28 defaultModerators: Contact[] = [];
29 devices: Devices = {};
30 contacts: Contact[] = [];
31
Misha Krieger-Raynauldcfa44302022-11-30 18:36:36 -050032 constructor(id: string, details: AccountDetails, volatileDetails: VolatileDetails) {
33 this.id = id;
34 this.details = details;
35 this.volatileDetails = volatileDetails;
36 }
37
38 static fromInterface(accountInterface: IAccount) {
39 const account = new Account(accountInterface.id, accountInterface.details, accountInterface.volatileDetails);
40 account.defaultModerators = accountInterface.defaultModerators.map(Contact.fromInterface);
41 return account;
42 }
43
44 getType(): AccountType {
45 return this.details['Account.type'] as AccountType;
46 }
47
48 getUri() {
49 return this.details['Account.username'];
50 }
51
52 getRegisteredName() {
53 return this.volatileDetails['Account.registeredName'];
54 }
55
56 isRendezVous() {
57 return this.details['Account.rendezVous'] === 'true';
58 }
59
60 isPublicIn() {
61 return this.details['DHT.PublicInCalls'] === 'true';
62 }
63
64 updateDetails(details: Partial<AccountDetails>) {
65 this.details = { ...this.details, ...details };
66 }
67
68 getDisplayUri() {
69 return this.getRegisteredName() ?? this.getUri();
70 }
71
72 getDisplayName() {
73 return this.details['Account.displayName'] ?? this.getDisplayUri();
74 }
75
76 getDisplayNameNoFallback() {
77 return this.details['Account.displayName'] ?? this.getRegisteredName();
78 }
Misha Krieger-Raynauldcfa44302022-11-30 18:36:36 -050079}