simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 1 | import Contact from './Contact.js'; |
Adrien Béraud | 0cb76c9 | 2021-04-07 19:59:08 -0400 | [diff] [blame] | 2 | |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 3 | class Account { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 4 | constructor(id, details, volatileDetails) { |
| 5 | this.id = id; |
| 6 | this.details = details; |
| 7 | this.volatileDetails = volatileDetails; |
| 8 | this.contactCache = {}; |
| 9 | this.contacts = {}; |
| 10 | this.conversations = {}; |
| 11 | this.lookups = []; |
| 12 | this.devices = {}; |
| 13 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 14 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 15 | static from(object) { |
| 16 | const account = new Account(object.id, object.details, object.volatileDetails); |
| 17 | if (object.defaultModerators) account.defaultModerators = object.defaultModerators.map((m) => Contact.from(m)); |
| 18 | return account; |
| 19 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 20 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 21 | update(data) { |
| 22 | this.details = data.details; |
| 23 | this.volatileDetails = data.volatileDetails; |
| 24 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 25 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 26 | async getObject() { |
| 27 | const hasModerators = this.defaultModerators && this.defaultModerators.length; |
| 28 | return { |
| 29 | id: this.id, |
| 30 | details: this.details, |
| 31 | defaultModerators: hasModerators |
| 32 | ? await Promise.all(this.defaultModerators.map(async (c) => await c.getObject())) |
| 33 | : undefined, |
| 34 | volatileDetails: this.volatileDetails, |
| 35 | }; |
| 36 | } |
Adrien Béraud | 2b3c2cd | 2022-09-18 14:24:33 -0400 | [diff] [blame] | 37 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 38 | getId() { |
| 39 | return this.id; |
| 40 | } |
Adrien Béraud | 2b3c2cd | 2022-09-18 14:24:33 -0400 | [diff] [blame] | 41 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 42 | getType() { |
| 43 | return this.details['Account.type']; |
| 44 | } |
Adrien Béraud | 2b3c2cd | 2022-09-18 14:24:33 -0400 | [diff] [blame] | 45 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 46 | getUri() { |
| 47 | return this.details['Account.username']; |
| 48 | } |
Adrien Béraud | 2b3c2cd | 2022-09-18 14:24:33 -0400 | [diff] [blame] | 49 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 50 | getRegisteredName() { |
| 51 | return this.volatileDetails['Account.registeredName']; |
| 52 | } |
Adrien Béraud | 2b3c2cd | 2022-09-18 14:24:33 -0400 | [diff] [blame] | 53 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 54 | isRendezVous() { |
| 55 | return this.details['Account.rendezVous'] === Account.BOOL_TRUE; |
| 56 | } |
Adrien Béraud | 2b3c2cd | 2022-09-18 14:24:33 -0400 | [diff] [blame] | 57 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 58 | isPublicIn() { |
| 59 | return this.details['DHT.PublicInCalls'] === Account.BOOL_TRUE; |
| 60 | } |
Adrien Béraud | 2b3c2cd | 2022-09-18 14:24:33 -0400 | [diff] [blame] | 61 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 62 | setDetail(detail, value) { |
| 63 | this.details[detail] = value; |
| 64 | } |
Adrien Béraud | 2b3c2cd | 2022-09-18 14:24:33 -0400 | [diff] [blame] | 65 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 66 | updateDetails(details) { |
| 67 | return Object.assign(this.details, details); |
| 68 | } |
Adrien Béraud | 2b3c2cd | 2022-09-18 14:24:33 -0400 | [diff] [blame] | 69 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 70 | getDetails() { |
| 71 | return this.details; |
| 72 | } |
Adrien Béraud | 2b3c2cd | 2022-09-18 14:24:33 -0400 | [diff] [blame] | 73 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 74 | getSummary() { |
| 75 | return this.getObject(); |
| 76 | } |
Adrien Béraud | 2b3c2cd | 2022-09-18 14:24:33 -0400 | [diff] [blame] | 77 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 78 | getDisplayName() { |
| 79 | return this.details['Account.displayName'] || this.getDisplayUri(); |
| 80 | } |
Adrien Béraud | 2b3c2cd | 2022-09-18 14:24:33 -0400 | [diff] [blame] | 81 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 82 | getDisplayUri() { |
| 83 | return this.getRegisteredName() || this.getUri(); |
| 84 | } |
Adrien Béraud | 2b3c2cd | 2022-09-18 14:24:33 -0400 | [diff] [blame] | 85 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 86 | getDisplayNameNoFallback() { |
| 87 | return this.details['Account.displayName'] || this.getRegisteredName(); |
| 88 | } |
Adrien Béraud | 2b3c2cd | 2022-09-18 14:24:33 -0400 | [diff] [blame] | 89 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 90 | getConversationIds() { |
| 91 | return Object.keys(this.conversations); |
| 92 | } |
| 93 | getConversations() { |
| 94 | return this.conversations; |
| 95 | } |
Adrien Béraud | 2b3c2cd | 2022-09-18 14:24:33 -0400 | [diff] [blame] | 96 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 97 | getConversation(conversationId) { |
| 98 | return this.conversations[conversationId]; |
| 99 | } |
Adrien Béraud | 2b3c2cd | 2022-09-18 14:24:33 -0400 | [diff] [blame] | 100 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 101 | addConversation(conversation) { |
| 102 | this.conversations[conversation.getId()] = conversation; |
| 103 | } |
Adrien Béraud | 2b3c2cd | 2022-09-18 14:24:33 -0400 | [diff] [blame] | 104 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 105 | removeConversation(conversationId) { |
| 106 | delete this.conversations[conversationId]; |
| 107 | } |
Adrien Béraud | 2b3c2cd | 2022-09-18 14:24:33 -0400 | [diff] [blame] | 108 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 109 | getContactFromCache(uri) { |
| 110 | let contact = this.contactCache[uri]; |
| 111 | if (!contact) { |
| 112 | contact = new Contact(uri); |
| 113 | this.contactCache[uri] = contact; |
Adrien Béraud | 2b3c2cd | 2022-09-18 14:24:33 -0400 | [diff] [blame] | 114 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 115 | return contact; |
| 116 | } |
Adrien Béraud | 2b3c2cd | 2022-09-18 14:24:33 -0400 | [diff] [blame] | 117 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 118 | getContacts() { |
| 119 | return this.contacts; |
| 120 | } |
Adrien Béraud | 2b3c2cd | 2022-09-18 14:24:33 -0400 | [diff] [blame] | 121 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 122 | getDefaultModerators() { |
| 123 | return this.defaultModerators; |
| 124 | } |
Adrien Béraud | 2b3c2cd | 2022-09-18 14:24:33 -0400 | [diff] [blame] | 125 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 126 | setDevices(devices) { |
| 127 | this.devices = { ...devices }; |
| 128 | } |
| 129 | getDevices() { |
| 130 | return this.devices; |
| 131 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 132 | } |
| 133 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 134 | Account.TYPE_JAMI = 'RING'; |
| 135 | Account.TYPE_SIP = 'SIP'; |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 136 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 137 | Account.BOOL_TRUE = 'true'; |
| 138 | Account.BOOL_FALSE = 'false'; |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 139 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 140 | export default Account; |