blob: aa1e95770c77d14d9b2c4833f6e8131df2b10e14 [file] [log] [blame]
simond47ef9e2022-09-28 22:24:28 -04001import Contact from './Contact.js';
Adrien Béraud0cb76c92021-04-07 19:59:08 -04002
Adrien Béraud6ecaa402021-04-06 17:37:25 -04003class Account {
simond47ef9e2022-09-28 22:24:28 -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 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -040014
simond47ef9e2022-09-28 22:24:28 -040015 static from(object) {
16 const account = new Account(object.id, object.details, object.volatileDetails);
17 if (object.defaultModerators) account.defaultModerators = object.defaultModerators.map((m) => Contact.from(m));
18 return account;
19 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -040020
simond47ef9e2022-09-28 22:24:28 -040021 update(data) {
22 this.details = data.details;
23 this.volatileDetails = data.volatileDetails;
24 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -040025
simond47ef9e2022-09-28 22:24:28 -040026 async getObject() {
27 const hasModerators = this.defaultModerators && this.defaultModerators.length;
28 return {
29 id: this.id,
30 details: this.details,
31 defaultModerators: hasModerators
32 ? await Promise.all(this.defaultModerators.map(async (c) => await c.getObject()))
33 : undefined,
34 volatileDetails: this.volatileDetails,
35 };
36 }
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -040037
simond47ef9e2022-09-28 22:24:28 -040038 getId() {
39 return this.id;
40 }
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -040041
simond47ef9e2022-09-28 22:24:28 -040042 getType() {
43 return this.details['Account.type'];
44 }
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -040045
simond47ef9e2022-09-28 22:24:28 -040046 getUri() {
47 return this.details['Account.username'];
48 }
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -040049
simond47ef9e2022-09-28 22:24:28 -040050 getRegisteredName() {
51 return this.volatileDetails['Account.registeredName'];
52 }
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -040053
simond47ef9e2022-09-28 22:24:28 -040054 isRendezVous() {
55 return this.details['Account.rendezVous'] === Account.BOOL_TRUE;
56 }
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -040057
simond47ef9e2022-09-28 22:24:28 -040058 isPublicIn() {
59 return this.details['DHT.PublicInCalls'] === Account.BOOL_TRUE;
60 }
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -040061
simond47ef9e2022-09-28 22:24:28 -040062 setDetail(detail, value) {
63 this.details[detail] = value;
64 }
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -040065
simond47ef9e2022-09-28 22:24:28 -040066 updateDetails(details) {
67 return Object.assign(this.details, details);
68 }
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -040069
simond47ef9e2022-09-28 22:24:28 -040070 getDetails() {
71 return this.details;
72 }
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -040073
simond47ef9e2022-09-28 22:24:28 -040074 getSummary() {
75 return this.getObject();
76 }
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -040077
simond47ef9e2022-09-28 22:24:28 -040078 getDisplayName() {
79 return this.details['Account.displayName'] || this.getDisplayUri();
80 }
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -040081
simond47ef9e2022-09-28 22:24:28 -040082 getDisplayUri() {
83 return this.getRegisteredName() || this.getUri();
84 }
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -040085
simond47ef9e2022-09-28 22:24:28 -040086 getDisplayNameNoFallback() {
87 return this.details['Account.displayName'] || this.getRegisteredName();
88 }
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -040089
simond47ef9e2022-09-28 22:24:28 -040090 getConversationIds() {
91 return Object.keys(this.conversations);
92 }
93 getConversations() {
94 return this.conversations;
95 }
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -040096
simond47ef9e2022-09-28 22:24:28 -040097 getConversation(conversationId) {
98 return this.conversations[conversationId];
99 }
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -0400100
simond47ef9e2022-09-28 22:24:28 -0400101 addConversation(conversation) {
102 this.conversations[conversation.getId()] = conversation;
103 }
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -0400104
simond47ef9e2022-09-28 22:24:28 -0400105 removeConversation(conversationId) {
106 delete this.conversations[conversationId];
107 }
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -0400108
simond47ef9e2022-09-28 22:24:28 -0400109 getContactFromCache(uri) {
110 let contact = this.contactCache[uri];
111 if (!contact) {
112 contact = new Contact(uri);
113 this.contactCache[uri] = contact;
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -0400114 }
simond47ef9e2022-09-28 22:24:28 -0400115 return contact;
116 }
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -0400117
simond47ef9e2022-09-28 22:24:28 -0400118 getContacts() {
119 return this.contacts;
120 }
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -0400121
simond47ef9e2022-09-28 22:24:28 -0400122 getDefaultModerators() {
123 return this.defaultModerators;
124 }
Adrien Béraud2b3c2cd2022-09-18 14:24:33 -0400125
simond47ef9e2022-09-28 22:24:28 -0400126 setDevices(devices) {
127 this.devices = { ...devices };
128 }
129 getDevices() {
130 return this.devices;
131 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400132}
133
simond47ef9e2022-09-28 22:24:28 -0400134Account.TYPE_JAMI = 'RING';
135Account.TYPE_SIP = 'SIP';
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400136
simond47ef9e2022-09-28 22:24:28 -0400137Account.BOOL_TRUE = 'true';
138Account.BOOL_FALSE = 'false';
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400139
simond47ef9e2022-09-28 22:24:28 -0400140export default Account;