-
Notifications
You must be signed in to change notification settings - Fork 491
/
store.go
158 lines (123 loc) · 2.58 KB
/
store.go
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
146
147
148
149
150
151
152
153
154
155
156
157
158
package whatsapp
import (
"github.com/Rhymen/go-whatsapp/binary"
"strings"
"sync"
"errors"
)
type Store struct {
Contacts map[string]Contact
Chats map[string]Chat
sync.RWMutex
}
type Contact struct {
Jid string
Notify string
Name string
Short string
}
type Chat struct {
Jid string
Name string
Unread string
LastMessageTime string
IsMuted string
IsMarkedSpam string
}
func newStore() *Store {
return &Store{
make(map[string]Contact),
make(map[string]Chat),
sync.RWMutex{},
}
}
func (sr *Store) updateContacts(contacts interface{}) {
c, ok := contacts.([]interface{})
if !ok {
return
}
defer sr.Unlock()
sr.Lock()
for _, contact := range c {
contactNode, ok := contact.(binary.Node)
if !ok {
continue
}
jid := strings.Replace(contactNode.Attributes["jid"], "@c.us", "@s.whatsapp.net", 1)
sr.Contacts[jid] = Contact{
jid,
contactNode.Attributes["notify"],
contactNode.Attributes["name"],
contactNode.Attributes["short"],
}
}
}
func (sr *Store) GetContacts() map[string]Contact {
defer sr.RUnlock()
sr.RLock()
return sr.Contacts
}
func (sr *Store) GetContact(jid string) (Contact, bool) {
defer sr.RUnlock()
sr.RLock()
if contact, ok := sr.Contacts[jid]; ok {
return contact, ok
}
return Contact{}, false
}
func (sr *Store) AddContact(contact Contact) error {
if contact.Jid == "" {
return errors.New("jit cannot be empty ")
}
defer sr.Unlock()
sr.Lock()
jid := strings.Replace(contact.Jid, "@c.us", "@s.whatsapp.net", 1)
sr.Contacts[jid] = contact
return nil
}
func (sr *Store) updateChats(chats interface{}) {
c, ok := chats.([]interface{})
if !ok {
return
}
defer sr.Unlock()
sr.Lock()
for _, chat := range c {
chatNode, ok := chat.(binary.Node)
if !ok {
continue
}
jid := strings.Replace(chatNode.Attributes["jid"], "@c.us", "@s.whatsapp.net", 1)
sr.Chats[jid] = Chat{
jid,
chatNode.Attributes["name"],
chatNode.Attributes["count"],
chatNode.Attributes["t"],
chatNode.Attributes["mute"],
chatNode.Attributes["spam"],
}
}
}
func (sr *Store) GetChats() map[string]Chat {
defer sr.RUnlock()
sr.RLock()
return sr.Chats
}
func (sr *Store) GetChat(jid string) (Chat, bool) {
defer sr.RUnlock()
sr.RLock()
if chat, ok := sr.Chats[jid]; ok {
return chat, ok
}
return Chat{}, false
}
func (sr *Store) AddChat(chat Chat) error {
if chat.Jid == "" {
return errors.New("jit cannot be empty ")
}
defer sr.RUnlock()
sr.RLock()
jid := strings.Replace(chat.Jid, "@c.us", "@s.whatsapp.net", 1)
sr.Chats[jid] = chat
return nil
}