blob: 669381e53574d8f1415216eeab5ceff8225f790e [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éraud0cb76c92021-04-07 19:59:08 -040026 getObject() {
27 return {
28 id: this.id,
29 details: this.details,
Adrien Béraud21c53cf2021-04-22 00:04:32 -040030 defaultModerators: this.defaultModerators ? this.defaultModerators.map(c => c.getObject()) : undefined,
Adrien Béraud0cb76c92021-04-07 19:59:08 -040031 volatileDetails: this.volatileDetails
32 }
33 }
34
Adrien Béraud6ecaa402021-04-06 17:37:25 -040035 getId() { return this.id }
36
37 getType() { return this.details["Account.type"] }
38
39 getUri() { return this.details["Account.username"] }
40
41 getRegisteredName() { return this.volatileDetails["Account.registeredName"] }
42
43 isRendezVous() { return this.details["Account.rendezVous"] === Account.BOOL_TRUE }
Adrien Béraud6ecaa402021-04-06 17:37:25 -040044
Adrien Béraud0cb76c92021-04-07 19:59:08 -040045 isPublicIn() { return this.details["DHT.PublicInCalls"] === Account.BOOL_TRUE }
Adrien Béraud6ecaa402021-04-06 17:37:25 -040046
47 getSummary() {
48 return this.getObject()
49 }
50
51 getDisplayName() {
52 return this.details["Account.displayName"] || this.getDisplayUri()
53 }
54
55 getDisplayUri() {
56 return this.getRegisteredName() || this.getUri()
57 }
Adrien Béraud0cb76c92021-04-07 19:59:08 -040058
Adrien Béraude74741b2021-04-19 13:22:54 -040059 getDisplayNameNoFallback() {
60 return this.details["Account.displayName"] || this.getRegisteredName()
61 }
62
Adrien Béraud0cb76c92021-04-07 19:59:08 -040063 getConversationIds() {
64 return Object.keys(this.conversations)
65 }
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040066 getConversations() {
67 return this.conversations
68 }
Adrien Béraud0cb76c92021-04-07 19:59:08 -040069
70 getConversation(conversationId) {
71 return this.conversations[conversationId]
72 }
73
74 addConversation(conversation) {
75 this.conversations[conversation.getId()] = conversation
76 }
77
78 removeConversation(conversationId) {
79 delete this.conversations[conversationId]
80 }
81
82 getContactFromCache(uri) {
83 let contact = this.contactCache[uri]
84 if (!contact) {
85 contact = new Contact(uri)
86 this.contactCache[uri] = contact
87 }
88 return contact
89 }
90
91 getContacts() {
92 return this.contacts
93 }
Adrien Béraud21c53cf2021-04-22 00:04:32 -040094
95 getDefaultModerators() {
96 return this.defaultModerators
97 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -040098}
99
100Account.TYPE_JAMI = "RING"
101Account.TYPE_SIP = "SIP"
102
103Account.BOOL_TRUE = "true"
104Account.BOOL_FALSE = "false"
105
Adrien Béraude74741b2021-04-19 13:22:54 -0400106export default Account