blob: 46f96ed2626c740bb89b9e5e783fdab66655d398 [file] [log] [blame]
Adrien Béraude74741b2021-04-19 13:22:54 -04001import Contact from './Contact.js'
Adrien Béraud0cb76c92021-04-07 19:59:08 -04002
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
Adrien Béraude74741b2021-04-19 13:22:54 -040055 getDisplayNameNoFallback() {
56 return this.details["Account.displayName"] || this.getRegisteredName()
57 }
58
Adrien Béraud0cb76c92021-04-07 19:59:08 -040059 getConversationIds() {
60 return Object.keys(this.conversations)
61 }
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040062 getConversations() {
63 return this.conversations
64 }
Adrien Béraud0cb76c92021-04-07 19:59:08 -040065
66 getConversation(conversationId) {
67 return this.conversations[conversationId]
68 }
69
70 addConversation(conversation) {
71 this.conversations[conversation.getId()] = conversation
72 }
73
74 removeConversation(conversationId) {
75 delete this.conversations[conversationId]
76 }
77
78 getContactFromCache(uri) {
79 let contact = this.contactCache[uri]
80 if (!contact) {
81 contact = new Contact(uri)
82 this.contactCache[uri] = contact
83 }
84 return contact
85 }
86
87 getContacts() {
88 return this.contacts
89 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -040090}
91
92Account.TYPE_JAMI = "RING"
93Account.TYPE_SIP = "SIP"
94
95Account.BOOL_TRUE = "true"
96Account.BOOL_FALSE = "false"
97
Adrien Béraude74741b2021-04-19 13:22:54 -040098export default Account