blob: e40be7c9d7c5aface8d42df0ac6721d5ef819c79 [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
Adrien Béraud86986032021-04-25 12:04:53 -040048 setDetail(detail, value) {
49 this.details[detail] = value
50 }
51
52 updateDetails(details) {
53 return Object.assign(this.details, details)
54 }
55
56 getDetails() {
57 return this.details
58 }
59
Adrien Béraud6ecaa402021-04-06 17:37:25 -040060 getSummary() {
61 return this.getObject()
62 }
63
64 getDisplayName() {
65 return this.details["Account.displayName"] || this.getDisplayUri()
66 }
67
68 getDisplayUri() {
69 return this.getRegisteredName() || this.getUri()
70 }
Adrien Béraud0cb76c92021-04-07 19:59:08 -040071
Adrien Béraude74741b2021-04-19 13:22:54 -040072 getDisplayNameNoFallback() {
73 return this.details["Account.displayName"] || this.getRegisteredName()
74 }
75
Adrien Béraud0cb76c92021-04-07 19:59:08 -040076 getConversationIds() {
77 return Object.keys(this.conversations)
78 }
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040079 getConversations() {
80 return this.conversations
81 }
Adrien Béraud0cb76c92021-04-07 19:59:08 -040082
83 getConversation(conversationId) {
84 return this.conversations[conversationId]
85 }
86
87 addConversation(conversation) {
88 this.conversations[conversation.getId()] = conversation
89 }
90
91 removeConversation(conversationId) {
92 delete this.conversations[conversationId]
93 }
94
95 getContactFromCache(uri) {
96 let contact = this.contactCache[uri]
97 if (!contact) {
98 contact = new Contact(uri)
99 this.contactCache[uri] = contact
100 }
101 return contact
102 }
103
104 getContacts() {
105 return this.contacts
106 }
Adrien Béraud21c53cf2021-04-22 00:04:32 -0400107
108 getDefaultModerators() {
109 return this.defaultModerators
110 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400111}
112
113Account.TYPE_JAMI = "RING"
114Account.TYPE_SIP = "SIP"
115
116Account.BOOL_TRUE = "true"
117Account.BOOL_FALSE = "false"
118
Adrien Béraude74741b2021-04-19 13:22:54 -0400119export default Account