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