blob: e5d3ffef64a5495de240e9fffc051113c8d8ffb6 [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 {
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -04004 constructor(id, details, volatileDetails) {
5 this.id = id
6 this.details = details
7 this.volatileDetails = volatileDetails
8 this.contactCache = {}
9 this.contacts = {}
10 this.conversations = {}
11 this.lookups = []
12 this.devices = {}
Adrien Béraud6ecaa402021-04-06 17:37:25 -040013 }
14
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -040015 static from(object) {
16 const account = new Account(
17 object.id,
18 object.details,
19 object.volatileDetails
20 )
21 if (object.defaultModerators)
22 account.defaultModerators = object.defaultModerators.map((m) =>
23 Contact.from(m)
24 )
25 return account
26 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -040027
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -040028 update(data) {
29 this.details = data.details
30 this.volatileDetails = data.volatileDetails
31 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -040032
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -040033 async getObject() {
34 const hasModerators = this.defaultModerators && this.defaultModerators.length
35 return {
36 id: this.id,
37 details: this.details,
38 defaultModerators: hasModerators
39 ? await Promise.all(
40 this.defaultModerators.map(async (c) => await c.getObject())
41 )
42 : undefined,
43 volatileDetails: this.volatileDetails,
44 }
45 }
46
47 getId() {
48 return this.id
49 }
50
51 getType() {
52 return this.details['Account.type']
53 }
54
55 getUri() {
56 return this.details['Account.username']
57 }
58
59 getRegisteredName() {
60 return this.volatileDetails['Account.registeredName']
61 }
62
63 isRendezVous() {
64 return this.details['Account.rendezVous'] === Account.BOOL_TRUE
65 }
66
67 isPublicIn() {
68 return this.details['DHT.PublicInCalls'] === Account.BOOL_TRUE
69 }
70
71 setDetail(detail, value) {
72 this.details[detail] = value
73 }
74
75 updateDetails(details) {
76 return Object.assign(this.details, details)
77 }
78
79 getDetails() {
80 return this.details
81 }
82
83 getSummary() {
84 return this.getObject()
85 }
86
87 getDisplayName() {
88 return this.details['Account.displayName'] || this.getDisplayUri()
89 }
90
91 getDisplayUri() {
92 return this.getRegisteredName() || this.getUri()
93 }
94
95 getDisplayNameNoFallback() {
96 return this.details['Account.displayName'] || this.getRegisteredName()
97 }
98
99 getConversationIds() {
100 return Object.keys(this.conversations)
101 }
102 getConversations() {
103 return this.conversations
104 }
105
106 getConversation(conversationId) {
107 return this.conversations[conversationId]
108 }
109
110 addConversation(conversation) {
111 this.conversations[conversation.getId()] = conversation
112 }
113
114 removeConversation(conversationId) {
115 delete this.conversations[conversationId]
116 }
117
118 getContactFromCache(uri) {
119 let contact = this.contactCache[uri]
120 if (!contact) {
121 contact = new Contact(uri)
122 this.contactCache[uri] = contact
123 }
124 return contact
125 }
126
127 getContacts() {
128 return this.contacts
129 }
130
131 getDefaultModerators() {
132 return this.defaultModerators
133 }
134
135 setDevices(devices) {
136 this.devices = { ...devices }
137 }
138 getDevices() {
139 return this.devices
140 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400141}
142
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -0400143Account.TYPE_JAMI = 'RING'
144Account.TYPE_SIP = 'SIP'
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400145
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -0400146Account.BOOL_TRUE = 'true'
147Account.BOOL_FALSE = 'false'
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400148
Adrien Béraude74741b2021-04-19 13:22:54 -0400149export default Account