Misha Krieger-Raynauld | 6bbdacf | 2022-11-29 21:45:40 -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, VolatileDetails } from 'jami-web-common'; |
| 19 | |
| 20 | import { Contact } from './Contact.js'; |
| 21 | import { Conversation } from './Conversation.js'; |
| 22 | |
| 23 | export class Account { |
| 24 | private readonly id: string; |
| 25 | private _details: AccountDetails; |
| 26 | private _volatileDetails: VolatileDetails; |
| 27 | private _contacts: Contact[]; |
| 28 | private readonly conversations: Record<string, Conversation>; |
| 29 | private defaultModerators: Contact[]; |
| 30 | private devices: Record<string, string>; |
| 31 | |
| 32 | static readonly TYPE_JAMI: string = 'RING'; |
| 33 | static readonly TYPE_SIP: string = 'SIP'; |
| 34 | |
| 35 | constructor(id: string, details: AccountDetails, volatileDetails: VolatileDetails) { |
| 36 | this.id = id; |
| 37 | this._details = details || {}; |
| 38 | this._volatileDetails = volatileDetails || {}; |
| 39 | this._contacts = []; |
| 40 | this.conversations = {}; |
| 41 | this.defaultModerators = []; |
| 42 | this.devices = {}; |
| 43 | } |
| 44 | |
| 45 | static from(object: any) { |
| 46 | const account = new Account(object.id, object.details, object.volatileDetails); |
| 47 | if (object.defaultModerators) account.defaultModerators = object.defaultModerators.map((m: any) => Contact.from(m)); |
| 48 | return account; |
| 49 | } |
| 50 | |
| 51 | getId() { |
| 52 | return this.id; |
| 53 | } |
| 54 | |
| 55 | getType() { |
| 56 | return this._details['Account.type']; |
| 57 | } |
| 58 | |
| 59 | getUri() { |
| 60 | return this._details['Account.username']; |
| 61 | } |
| 62 | |
| 63 | getRegisteredName() { |
| 64 | return this._volatileDetails['Account.registeredName']; |
| 65 | } |
| 66 | |
| 67 | isRendezVous() { |
| 68 | return this._details['Account.rendezVous'] === 'true'; |
| 69 | } |
| 70 | |
| 71 | isPublicIn() { |
| 72 | return this._details['DHT.PublicInCalls'] === 'true'; |
| 73 | } |
| 74 | |
| 75 | setDetail(detail: keyof AccountDetails, value: string) { |
| 76 | this._details[detail] = value; |
| 77 | } |
| 78 | |
| 79 | updateDetails(details: Partial<AccountDetails>) { |
| 80 | return Object.assign(this._details, details); |
| 81 | } |
| 82 | |
| 83 | getDetails() { |
| 84 | return this._details; |
| 85 | } |
| 86 | |
| 87 | getDisplayName() { |
| 88 | return this._details['Account.displayName'] || this.getDisplayUri(); |
| 89 | } |
| 90 | |
| 91 | getDisplayUri() { |
| 92 | return this.getRegisteredName() || this.getUri(); |
| 93 | } |
| 94 | |
| 95 | getDisplayNameNoFallback() { |
| 96 | return this._details['Account.displayName'] || this.getRegisteredName(); |
| 97 | } |
| 98 | |
| 99 | getConversationIds() { |
| 100 | return Object.keys(this.conversations); |
| 101 | } |
| 102 | |
| 103 | getConversations() { |
| 104 | return this.conversations; |
| 105 | } |
| 106 | |
| 107 | getConversation(conversationId: string) { |
| 108 | return this.conversations[conversationId]; |
| 109 | } |
| 110 | |
| 111 | addConversation(conversation: Conversation) { |
| 112 | const conversationId = conversation.getId(); |
| 113 | if (conversationId != null) { |
| 114 | this.conversations[conversationId] = conversation; |
| 115 | } else { |
| 116 | throw new Error('Conversation ID cannot be undefined'); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | removeConversation(conversationId: string) { |
| 121 | delete this.conversations[conversationId]; |
| 122 | } |
| 123 | |
| 124 | getContacts() { |
| 125 | return this._contacts; |
| 126 | } |
| 127 | |
| 128 | set contacts(contacts: Contact[]) { |
| 129 | this._contacts = contacts; |
| 130 | } |
| 131 | |
| 132 | getDefaultModerators() { |
| 133 | return this.defaultModerators; |
| 134 | } |
| 135 | |
| 136 | set details(value: AccountDetails) { |
| 137 | this._details = value; |
| 138 | } |
| 139 | |
| 140 | set volatileDetails(value: VolatileDetails) { |
| 141 | this._volatileDetails = value; |
| 142 | } |
| 143 | |
| 144 | setDevices(devices: Record<string, string>) { |
| 145 | this.devices = { ...devices }; |
| 146 | } |
| 147 | |
| 148 | getDevices() { |
| 149 | return this.devices; |
| 150 | } |
| 151 | } |