blob: 06854113f0dcb6b039d97eddf6c80f0368509b78 [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 {
ervinanohf1758a42022-09-14 14:52:51 -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 = {};
13 }
14
15 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 }
27
28 update(data) {
29 this.details = data.details;
30 this.volatileDetails = data.volatileDetails;
31 }
32
33 async getObject() {
34 const hasModerators =
35 this.defaultModerators && this.defaultModerators.length;
36 return {
37 id: this.id,
38 details: this.details,
39 defaultModerators: hasModerators
40 ? await Promise.all(
41 this.defaultModerators.map(async (c) => await c.getObject())
42 )
43 : undefined,
44 volatileDetails: this.volatileDetails,
45 };
46 }
47
48 getId() {
49 return this.id;
50 }
51
52 getType() {
53 return this.details["Account.type"];
54 }
55
56 getUri() {
57 return this.details["Account.username"];
58 }
59
60 getRegisteredName() {
61 return this.volatileDetails["Account.registeredName"];
62 }
63
64 isRendezVous() {
65 return this.details["Account.rendezVous"] === Account.BOOL_TRUE;
66 }
67
68 isPublicIn() {
69 return this.details["DHT.PublicInCalls"] === Account.BOOL_TRUE;
70 }
71
72 setDetail(detail, value) {
73 this.details[detail] = value;
74 }
75
76 updateDetails(details) {
77 return Object.assign(this.details, details);
78 }
79
80 getDetails() {
81 return this.details;
82 }
83
84 getSummary() {
85 return this.getObject();
86 }
87
88 getDisplayName() {
89 return this.details["Account.displayName"] || this.getDisplayUri();
90 }
91
92 getDisplayUri() {
93 return this.getRegisteredName() || this.getUri();
94 }
95
96 getDisplayNameNoFallback() {
97 return this.details["Account.displayName"] || this.getRegisteredName();
98 }
99
100 getConversationIds() {
101 return Object.keys(this.conversations);
102 }
103 getConversations() {
104 return this.conversations;
105 }
106
107 getConversation(conversationId) {
108 return this.conversations[conversationId];
109 }
110
111 addConversation(conversation) {
112 this.conversations[conversation.getId()] = conversation;
113 }
114
115 removeConversation(conversationId) {
116 delete this.conversations[conversationId];
117 }
118
119 getContactFromCache(uri) {
120 let contact = this.contactCache[uri];
121 if (!contact) {
122 contact = new Contact(uri);
123 this.contactCache[uri] = contact;
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400124 }
ervinanohf1758a42022-09-14 14:52:51 -0400125 return contact;
126 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400127
ervinanohf1758a42022-09-14 14:52:51 -0400128 getContacts() {
129 return this.contacts;
130 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400131
ervinanohf1758a42022-09-14 14:52:51 -0400132 getDefaultModerators() {
133 return this.defaultModerators;
134 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400135
ervinanohf1758a42022-09-14 14:52:51 -0400136 setDevices(devices) {
137 this.devices = { ...devices };
138 }
139 getDevices() {
140 return this.devices;
141 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400142}
143
144Account.TYPE_JAMI = "RING"
145Account.TYPE_SIP = "SIP"
146
147Account.BOOL_TRUE = "true"
148Account.BOOL_FALSE = "false"
149
Adrien Béraude74741b2021-04-19 13:22:54 -0400150export default Account