blob: 6b43f1c98c4235c72aa36de357c74f2126d809c9 [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éraud6ecaa402021-04-06 17:37:25 -040011 }
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éraud0cb76c92021-04-07 19:59:08 -040022 getObject() {
23 return {
24 id: this.id,
25 details: this.details,
26 volatileDetails: this.volatileDetails
27 }
28 }
29
Adrien Béraud6ecaa402021-04-06 17:37:25 -040030 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éraud6ecaa402021-04-06 17:37:25 -040039
Adrien Béraud0cb76c92021-04-07 19:59:08 -040040 isPublicIn() { return this.details["DHT.PublicInCalls"] === Account.BOOL_TRUE }
Adrien Béraud6ecaa402021-04-06 17:37:25 -040041
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éraud0cb76c92021-04-07 19:59:08 -040053
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éraud6ecaa402021-04-06 17:37:25 -040082}
83
84Account.TYPE_JAMI = "RING"
85Account.TYPE_SIP = "SIP"
86
87Account.BOOL_TRUE = "true"
88Account.BOOL_FALSE = "false"
89
Adrien Béraud0cb76c92021-04-07 19:59:08 -040090module.exports = Account