blob: 1f70c33a32db8c2af84198ed9fb6b838c8b4ef43 [file] [log] [blame]
Adrien Béraud0cb76c92021-04-07 19:59:08 -04001const Contact = require('./Contact')
2
Adrien Béraud6ecaa402021-04-06 17:37:25 -04003class Account {
4 constructor(id, details, volatileDetails) {
5 this.id = id
6 this.details = details
7 this.volatileDetails = volatileDetails
Adrien Béraud0cb76c92021-04-07 19:59:08 -04008 this.contactCache = {}
9 this.contacts = {}
10 this.conversations = {}
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040011 this.lookups = []
Adrien Béraud6ecaa402021-04-06 17:37:25 -040012 }
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éraud0cb76c92021-04-07 19:59:08 -040023 getObject() {
24 return {
25 id: this.id,
26 details: this.details,
27 volatileDetails: this.volatileDetails
28 }
29 }
30
Adrien Béraud6ecaa402021-04-06 17:37:25 -040031 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éraud6ecaa402021-04-06 17:37:25 -040040
Adrien Béraud0cb76c92021-04-07 19:59:08 -040041 isPublicIn() { return this.details["DHT.PublicInCalls"] === Account.BOOL_TRUE }
Adrien Béraud6ecaa402021-04-06 17:37:25 -040042
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éraud0cb76c92021-04-07 19:59:08 -040054
55 getConversationIds() {
56 return Object.keys(this.conversations)
57 }
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040058 getConversations() {
59 return this.conversations
60 }
Adrien Béraud0cb76c92021-04-07 19:59:08 -040061
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éraud6ecaa402021-04-06 17:37:25 -040086}
87
88Account.TYPE_JAMI = "RING"
89Account.TYPE_SIP = "SIP"
90
91Account.BOOL_TRUE = "true"
92Account.BOOL_FALSE = "false"
93
Adrien Béraud0cb76c92021-04-07 19:59:08 -040094module.exports = Account