-
Notifications
You must be signed in to change notification settings - Fork 6
/
ContactsStore.ts
145 lines (131 loc) · 5.46 KB
/
ContactsStore.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import {
Instance,
SnapshotOut,
types,
flow,
destroy,
isStateTreeNode,
detach,
} from 'mobx-state-tree'
import {withSetPropAction} from './helpers/withSetPropAction'
import {
ContactModel,
Contact,
} from './Contact'
import {log} from '../services/logService'
// export const maxContactsInModel = 10
export const ContactsStoreModel = types
.model('ContactsStore', {
contacts: types.array(ContactModel),
publicPubkey: types.maybe(types.string),
lastPendingReceivedCheck: types.maybe(types.number), // UNIX timestamp
receivedEventIds: types.optional(types.array(types.string), [])
})
.actions(withSetPropAction)
.views(self => ({
findByPubkey: (pubkey: string) => {
const c = self.contacts.find(c => c.pubkey === pubkey)
return c ? c : undefined
},
findByNpub: (npub: string) => {
const c = self.contacts.find(c => c.npub === npub)
return c ? c : undefined
},
alreadyExists(pubkey: string) {
return self.contacts.some(m => m.pubkey === pubkey) ? true : false
},
nip05AlreadyExists(nip05: string) {
return self.contacts.some(m => m.nip05 === nip05) ? true : false
},
}))
.actions(self => ({
addContact(newContact: Contact) {
if(self.alreadyExists(newContact.pubkey)) {
log.warn('[addContact]', 'Contact already exists', newContact)
return
}
if(self.nip05AlreadyExists(newContact.nip05 as string)) {
log.warn('[addContact]', 'Contact NIP05 already exists with different pubkey', newContact)
return
}
newContact.createdAt = Math.floor(Date.now() / 1000)
const contactInstance = ContactModel.create(newContact)
self.contacts.push(contactInstance)
log.debug('[addContact]', 'New contact added to the ContactsStore', newContact)
return contactInstance
},
refreshPicture(pubkey: string) {
const contactInstance = self.findByPubkey(pubkey)
if (contactInstance) {
contactInstance.refreshPicture()
log.debug('[refreshPicture]', 'Contact picture refreshed in ContactsStore')
}
return contactInstance
},
saveNote (pubkey: string, note: string) {
const contactInstance = self.findByPubkey(pubkey)
if (contactInstance) {
contactInstance.setNoteToSelf(note)
log.debug('[saveNote]', 'Contact note updated in ContactsStore')
}
},
removeContact(contactToBeRemoved: Contact) {
let contactInstance: Contact | undefined
if (isStateTreeNode(contactToBeRemoved)) {
contactInstance = contactToBeRemoved
} else {
contactInstance = self.findByNpub((contactToBeRemoved as Contact).npub)
}
if (contactInstance) {
detach(contactInstance) // needed
destroy(contactInstance)
log.debug('[removeContact]', 'Contact removed from MintsStore')
}
},
removeAllContacts() {
self.contacts.clear()
log.debug('[removeAllContacts]', 'Removed all Contacts from ContactsStore')
},
setPublicPubkey(publicPubkey: string) {
self.publicPubkey = publicPubkey
log.debug('[setPublicPubkey]', publicPubkey)
},
setLastPendingReceivedCheck(ts?: number) {
if(ts) {
self.lastPendingReceivedCheck = ts
log.trace('[setLastPendingReceivedCheck]', {ts})
return
}
const ts2: number = Math.floor(Date.now() / 1000)
self.lastPendingReceivedCheck = ts2
log.trace('[setLastPendingReceivedCheck]', {ts2})
},
addReceivedEventId(id: string) {
self.receivedEventIds.push(id)
},
eventAlreadyReceived(id: string) {
return self.receivedEventIds.includes(id)
},
}))
.views(self => ({
get count() {
return self.contacts.length
},
/* get recent(): Contact[] {
return this.all.slice(0, maxContactsInModel) // Return the first 3 Contacts
},*/
get all() {
return self.contacts
.slice()
.sort((a, b) => {
// Sort by createdAt timestamp
if (a.createdAt && b.createdAt) {
return b.createdAt - a.createdAt
}
})
}
}))
export interface ContactsStore
extends Instance<typeof ContactsStoreModel> {}
export interface ContactsStoreSnapshot
extends SnapshotOut<typeof ContactsStoreModel> {}