Misha Krieger-Raynauld | cfa4430 | 2022-11-30 18:36:36 -0500 | [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 { AccountDetails, Devices, IAccount, VolatileDetails } from 'jami-web-common'; |
| 19 | |
| 20 | import { Contact } from './contact'; |
Misha Krieger-Raynauld | cfa4430 | 2022-11-30 18:36:36 -0500 | [diff] [blame] | 21 | |
| 22 | export type AccountType = 'RING' | 'SIP'; |
| 23 | |
| 24 | export 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-Raynauld | cfa4430 | 2022-11-30 18:36:36 -0500 | [diff] [blame] | 32 | 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() { |
idillon | 9e542ca | 2022-12-15 17:54:07 -0500 | [diff] [blame] | 73 | return this.details['Account.displayName'] || this.getDisplayUri(); |
Misha Krieger-Raynauld | cfa4430 | 2022-11-30 18:36:36 -0500 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | getDisplayNameNoFallback() { |
idillon | 9e542ca | 2022-12-15 17:54:07 -0500 | [diff] [blame] | 77 | return this.details['Account.displayName'] || this.getRegisteredName(); |
Misha Krieger-Raynauld | cfa4430 | 2022-11-30 18:36:36 -0500 | [diff] [blame] | 78 | } |
Misha Krieger-Raynauld | cfa4430 | 2022-11-30 18:36:36 -0500 | [diff] [blame] | 79 | } |