Adrien Béraud | 0cb76c9 | 2021-04-07 19:59:08 -0400 | [diff] [blame] | 1 | const Contact = require('./Contact') |
| 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) { |
| 15 | return new Account(object.id, object.details, object.volatileDetails) |
| 16 | } |
| 17 | |
| 18 | update(data) { |
| 19 | this.details = data.details |
| 20 | this.volatileDetails = data.volatileDetails |
| 21 | } |
| 22 | |
Adrien Béraud | 0cb76c9 | 2021-04-07 19:59:08 -0400 | [diff] [blame] | 23 | getObject() { |
| 24 | return { |
| 25 | id: this.id, |
| 26 | details: this.details, |
| 27 | volatileDetails: this.volatileDetails |
| 28 | } |
| 29 | } |
| 30 | |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 31 | getId() { return this.id } |
| 32 | |
| 33 | getType() { return this.details["Account.type"] } |
| 34 | |
| 35 | getUri() { return this.details["Account.username"] } |
| 36 | |
| 37 | getRegisteredName() { return this.volatileDetails["Account.registeredName"] } |
| 38 | |
| 39 | isRendezVous() { return this.details["Account.rendezVous"] === Account.BOOL_TRUE } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 40 | |
Adrien Béraud | 0cb76c9 | 2021-04-07 19:59:08 -0400 | [diff] [blame] | 41 | isPublicIn() { return this.details["DHT.PublicInCalls"] === Account.BOOL_TRUE } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 42 | |
| 43 | getSummary() { |
| 44 | return this.getObject() |
| 45 | } |
| 46 | |
| 47 | getDisplayName() { |
| 48 | return this.details["Account.displayName"] || this.getDisplayUri() |
| 49 | } |
| 50 | |
| 51 | getDisplayUri() { |
| 52 | return this.getRegisteredName() || this.getUri() |
| 53 | } |
Adrien Béraud | 0cb76c9 | 2021-04-07 19:59:08 -0400 | [diff] [blame] | 54 | |
| 55 | getConversationIds() { |
| 56 | return Object.keys(this.conversations) |
| 57 | } |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame^] | 58 | getConversations() { |
| 59 | return this.conversations |
| 60 | } |
Adrien Béraud | 0cb76c9 | 2021-04-07 19:59:08 -0400 | [diff] [blame] | 61 | |
| 62 | getConversation(conversationId) { |
| 63 | return this.conversations[conversationId] |
| 64 | } |
| 65 | |
| 66 | addConversation(conversation) { |
| 67 | this.conversations[conversation.getId()] = conversation |
| 68 | } |
| 69 | |
| 70 | removeConversation(conversationId) { |
| 71 | delete this.conversations[conversationId] |
| 72 | } |
| 73 | |
| 74 | getContactFromCache(uri) { |
| 75 | let contact = this.contactCache[uri] |
| 76 | if (!contact) { |
| 77 | contact = new Contact(uri) |
| 78 | this.contactCache[uri] = contact |
| 79 | } |
| 80 | return contact |
| 81 | } |
| 82 | |
| 83 | getContacts() { |
| 84 | return this.contacts |
| 85 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | Account.TYPE_JAMI = "RING" |
| 89 | Account.TYPE_SIP = "SIP" |
| 90 | |
| 91 | Account.BOOL_TRUE = "true" |
| 92 | Account.BOOL_FALSE = "false" |
| 93 | |
Adrien Béraud | 0cb76c9 | 2021-04-07 19:59:08 -0400 | [diff] [blame] | 94 | module.exports = Account |