Adrien Béraud | e74741b | 2021-04-19 13:22:54 -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 { |
Adrien Béraud | 2b3c2cd | 2022-09-18 14:24:33 -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 = {} |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 13 | } |
| 14 | |
Adrien Béraud | 2b3c2cd | 2022-09-18 14:24:33 -0400 | [diff] [blame] | 15 | static from(object) { |
| 16 | const account = new Account( |
| 17 | object.id, |
| 18 | object.details, |
| 19 | object.volatileDetails |
| 20 | ) |
| 21 | if (object.defaultModerators) |
| 22 | account.defaultModerators = object.defaultModerators.map((m) => |
| 23 | Contact.from(m) |
| 24 | ) |
| 25 | return account |
| 26 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 27 | |
Adrien Béraud | 2b3c2cd | 2022-09-18 14:24:33 -0400 | [diff] [blame] | 28 | update(data) { |
| 29 | this.details = data.details |
| 30 | this.volatileDetails = data.volatileDetails |
| 31 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 32 | |
Adrien Béraud | 2b3c2cd | 2022-09-18 14:24:33 -0400 | [diff] [blame] | 33 | async getObject() { |
| 34 | const hasModerators = this.defaultModerators && this.defaultModerators.length |
| 35 | return { |
| 36 | id: this.id, |
| 37 | details: this.details, |
| 38 | defaultModerators: hasModerators |
| 39 | ? await Promise.all( |
| 40 | this.defaultModerators.map(async (c) => await c.getObject()) |
| 41 | ) |
| 42 | : undefined, |
| 43 | volatileDetails: this.volatileDetails, |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | getId() { |
| 48 | return this.id |
| 49 | } |
| 50 | |
| 51 | getType() { |
| 52 | return this.details['Account.type'] |
| 53 | } |
| 54 | |
| 55 | getUri() { |
| 56 | return this.details['Account.username'] |
| 57 | } |
| 58 | |
| 59 | getRegisteredName() { |
| 60 | return this.volatileDetails['Account.registeredName'] |
| 61 | } |
| 62 | |
| 63 | isRendezVous() { |
| 64 | return this.details['Account.rendezVous'] === Account.BOOL_TRUE |
| 65 | } |
| 66 | |
| 67 | isPublicIn() { |
| 68 | return this.details['DHT.PublicInCalls'] === Account.BOOL_TRUE |
| 69 | } |
| 70 | |
| 71 | setDetail(detail, value) { |
| 72 | this.details[detail] = value |
| 73 | } |
| 74 | |
| 75 | updateDetails(details) { |
| 76 | return Object.assign(this.details, details) |
| 77 | } |
| 78 | |
| 79 | getDetails() { |
| 80 | return this.details |
| 81 | } |
| 82 | |
| 83 | getSummary() { |
| 84 | return this.getObject() |
| 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 | getConversations() { |
| 103 | return this.conversations |
| 104 | } |
| 105 | |
| 106 | getConversation(conversationId) { |
| 107 | return this.conversations[conversationId] |
| 108 | } |
| 109 | |
| 110 | addConversation(conversation) { |
| 111 | this.conversations[conversation.getId()] = conversation |
| 112 | } |
| 113 | |
| 114 | removeConversation(conversationId) { |
| 115 | delete this.conversations[conversationId] |
| 116 | } |
| 117 | |
| 118 | getContactFromCache(uri) { |
| 119 | let contact = this.contactCache[uri] |
| 120 | if (!contact) { |
| 121 | contact = new Contact(uri) |
| 122 | this.contactCache[uri] = contact |
| 123 | } |
| 124 | return contact |
| 125 | } |
| 126 | |
| 127 | getContacts() { |
| 128 | return this.contacts |
| 129 | } |
| 130 | |
| 131 | getDefaultModerators() { |
| 132 | return this.defaultModerators |
| 133 | } |
| 134 | |
| 135 | setDevices(devices) { |
| 136 | this.devices = { ...devices } |
| 137 | } |
| 138 | getDevices() { |
| 139 | return this.devices |
| 140 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 141 | } |
| 142 | |
Adrien Béraud | 2b3c2cd | 2022-09-18 14:24:33 -0400 | [diff] [blame] | 143 | Account.TYPE_JAMI = 'RING' |
| 144 | Account.TYPE_SIP = 'SIP' |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 145 | |
Adrien Béraud | 2b3c2cd | 2022-09-18 14:24:33 -0400 | [diff] [blame] | 146 | Account.BOOL_TRUE = 'true' |
| 147 | Account.BOOL_FALSE = 'false' |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 148 | |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 149 | export default Account |