blob: c2883d2f814b5c63aa8eb9b8b07ec8ce84117e08 [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) {
Adrien Béraud21c53cf2021-04-22 00:04:32 -040015 const account = new Account(object.id, object.details, object.volatileDetails)
16 if (object.defaultModerators)
17 account.defaultModerators = object.defaultModerators.map(m => Contact.from(m))
18 return account
Adrien Béraud6ecaa402021-04-06 17:37:25 -040019 }
20
21 update(data) {
22 this.details = data.details
23 this.volatileDetails = data.volatileDetails
24 }
25
Adrien Béraud4e287b92021-04-24 16:15:56 -040026 async getObject() {
27 const hasModerators = this.defaultModerators && this.defaultModerators.length
Adrien Béraud0cb76c92021-04-07 19:59:08 -040028 return {
29 id: this.id,
30 details: this.details,
Adrien Béraud4e287b92021-04-24 16:15:56 -040031 defaultModerators: hasModerators ? await Promise.all(this.defaultModerators.map(async c => await c.getObject())) : undefined,
Adrien Béraud0cb76c92021-04-07 19:59:08 -040032 volatileDetails: this.volatileDetails
33 }
34 }
35
Adrien Béraud6ecaa402021-04-06 17:37:25 -040036 getId() { return this.id }
37
38 getType() { return this.details["Account.type"] }
39
40 getUri() { return this.details["Account.username"] }
41
42 getRegisteredName() { return this.volatileDetails["Account.registeredName"] }
43
44 isRendezVous() { return this.details["Account.rendezVous"] === Account.BOOL_TRUE }
Adrien Béraud6ecaa402021-04-06 17:37:25 -040045
Adrien Béraud0cb76c92021-04-07 19:59:08 -040046 isPublicIn() { return this.details["DHT.PublicInCalls"] === Account.BOOL_TRUE }
Adrien Béraud6ecaa402021-04-06 17:37:25 -040047
48 getSummary() {
49 return this.getObject()
50 }
51
52 getDisplayName() {
53 return this.details["Account.displayName"] || this.getDisplayUri()
54 }
55
56 getDisplayUri() {
57 return this.getRegisteredName() || this.getUri()
58 }
Adrien Béraud0cb76c92021-04-07 19:59:08 -040059
Adrien Béraude74741b2021-04-19 13:22:54 -040060 getDisplayNameNoFallback() {
61 return this.details["Account.displayName"] || this.getRegisteredName()
62 }
63
Adrien Béraud0cb76c92021-04-07 19:59:08 -040064 getConversationIds() {
65 return Object.keys(this.conversations)
66 }
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040067 getConversations() {
68 return this.conversations
69 }
Adrien Béraud0cb76c92021-04-07 19:59:08 -040070
71 getConversation(conversationId) {
72 return this.conversations[conversationId]
73 }
74
75 addConversation(conversation) {
76 this.conversations[conversation.getId()] = conversation
77 }
78
79 removeConversation(conversationId) {
80 delete this.conversations[conversationId]
81 }
82
83 getContactFromCache(uri) {
84 let contact = this.contactCache[uri]
85 if (!contact) {
86 contact = new Contact(uri)
87 this.contactCache[uri] = contact
88 }
89 return contact
90 }
91
92 getContacts() {
93 return this.contacts
94 }
Adrien Béraud21c53cf2021-04-22 00:04:32 -040095
96 getDefaultModerators() {
97 return this.defaultModerators
98 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -040099}
100
101Account.TYPE_JAMI = "RING"
102Account.TYPE_SIP = "SIP"
103
104Account.BOOL_TRUE = "true"
105Account.BOOL_FALSE = "false"
106
Adrien Béraude74741b2021-04-19 13:22:54 -0400107export default Account