blob: 60ac1d8863a6e500546ab5885d990a194c4eed39 [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';
21import { Conversation } from './conversation';
22
23export type AccountType = 'RING' | 'SIP';
24
25export class Account implements IAccount {
26 readonly id: string;
27 details: AccountDetails;
28 volatileDetails: VolatileDetails;
29 defaultModerators: Contact[] = [];
30 devices: Devices = {};
31 contacts: Contact[] = [];
32
33 private _conversations: Record<string, Conversation> = {};
34
35 constructor(id: string, details: AccountDetails, volatileDetails: VolatileDetails) {
36 this.id = id;
37 this.details = details;
38 this.volatileDetails = volatileDetails;
39 }
40
41 static fromInterface(accountInterface: IAccount) {
42 const account = new Account(accountInterface.id, accountInterface.details, accountInterface.volatileDetails);
43 account.defaultModerators = accountInterface.defaultModerators.map(Contact.fromInterface);
44 return account;
45 }
46
47 getType(): AccountType {
48 return this.details['Account.type'] as AccountType;
49 }
50
51 getUri() {
52 return this.details['Account.username'];
53 }
54
55 getRegisteredName() {
56 return this.volatileDetails['Account.registeredName'];
57 }
58
59 isRendezVous() {
60 return this.details['Account.rendezVous'] === 'true';
61 }
62
63 isPublicIn() {
64 return this.details['DHT.PublicInCalls'] === 'true';
65 }
66
67 updateDetails(details: Partial<AccountDetails>) {
68 this.details = { ...this.details, ...details };
69 }
70
71 getDisplayUri() {
72 return this.getRegisteredName() ?? this.getUri();
73 }
74
75 getDisplayName() {
76 return this.details['Account.displayName'] ?? this.getDisplayUri();
77 }
78
79 getDisplayNameNoFallback() {
80 return this.details['Account.displayName'] ?? this.getRegisteredName();
81 }
82
83 get conversations() {
84 return this._conversations;
85 }
86
87 addConversation(conversation: Conversation) {
88 if (conversation.id === undefined) {
89 throw new Error('Conversation ID cannot be undefined');
90 }
91 this._conversations[conversation.id] = conversation;
92 }
93
94 removeConversation(conversationId: string) {
95 delete this.conversations[conversationId];
96 }
97}