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