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 | |
| 48 | getSummary() { |
| 49 | return this.getObject() |
| 50 | } |
| 51 | |
| 52 | getDisplayName() { |
| 53 | return this.details["Account.displayName"] || this.getDisplayUri() |
| 54 | } |
| 55 | |
| 56 | getDisplayUri() { |
| 57 | return this.getRegisteredName() || this.getUri() |
| 58 | } |
Adrien Béraud | 0cb76c9 | 2021-04-07 19:59:08 -0400 | [diff] [blame] | 59 | |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 60 | getDisplayNameNoFallback() { |
| 61 | return this.details["Account.displayName"] || this.getRegisteredName() |
| 62 | } |
| 63 | |
Adrien Béraud | 0cb76c9 | 2021-04-07 19:59:08 -0400 | [diff] [blame] | 64 | getConversationIds() { |
| 65 | return Object.keys(this.conversations) |
| 66 | } |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 67 | getConversations() { |
| 68 | return this.conversations |
| 69 | } |
Adrien Béraud | 0cb76c9 | 2021-04-07 19:59:08 -0400 | [diff] [blame] | 70 | |
| 71 | getConversation(conversationId) { |
| 72 | return this.conversations[conversationId] |
| 73 | } |
| 74 | |
| 75 | addConversation(conversation) { |
| 76 | this.conversations[conversation.getId()] = conversation |
| 77 | } |
| 78 | |
| 79 | removeConversation(conversationId) { |
| 80 | delete this.conversations[conversationId] |
| 81 | } |
| 82 | |
| 83 | getContactFromCache(uri) { |
| 84 | let contact = this.contactCache[uri] |
| 85 | if (!contact) { |
| 86 | contact = new Contact(uri) |
| 87 | this.contactCache[uri] = contact |
| 88 | } |
| 89 | return contact |
| 90 | } |
| 91 | |
| 92 | getContacts() { |
| 93 | return this.contacts |
| 94 | } |
Adrien Béraud | 21c53cf | 2021-04-22 00:04:32 -0400 | [diff] [blame] | 95 | |
| 96 | getDefaultModerators() { |
| 97 | return this.defaultModerators |
| 98 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | Account.TYPE_JAMI = "RING" |
| 102 | Account.TYPE_SIP = "SIP" |
| 103 | |
| 104 | Account.BOOL_TRUE = "true" |
| 105 | Account.BOOL_FALSE = "false" |
| 106 | |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 107 | export default Account |