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