This repository has been archived by the owner on Oct 28, 2021. It is now read-only.
forked from Rhymen/go-whatsapp
-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
store.go
121 lines (106 loc) · 2.68 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
package whatsapp
import (
"strconv"
"strings"
"sync"
"time"
"github.com/Rhymen/go-whatsapp/binary"
)
type Store struct {
Contacts map[JID]Contact
ContactsLock sync.RWMutex
Chats map[JID]Chat
ChatsLock sync.RWMutex
}
type Contact struct {
JID JID
Notify string
Name string
Short string
IsEnterprise bool
IsVerified bool
VName string
Source map[string]string
ReceivedAt time.Time
}
type Chat struct {
JID JID
Name string
ModifyTag string
UnreadCount int
LastMessageTime int64
MutedUntil int64
IsMarkedSpam bool
IsArchived bool
IsPinned bool
Source map[string]string
ReceivedAt time.Time
}
func parseChat(attributes map[string]string) (out Chat) {
var err error
out.JID = strings.Replace(attributes["jid"], OldUserSuffix, NewUserSuffix, 1)
out.Name = attributes["name"]
out.ModifyTag = attributes["modify_tag"]
out.UnreadCount, err = strconv.Atoi(attributes["count"])
if err != nil {
out.UnreadCount = -1
}
out.LastMessageTime, _ = strconv.ParseInt(attributes["t"], 10, 64)
out.MutedUntil, _ = strconv.ParseInt(attributes["mute"], 10, 64)
out.IsMarkedSpam, _ = strconv.ParseBool(attributes["spam"])
out.IsArchived, _ = strconv.ParseBool(attributes["archive"])
_, out.IsPinned = attributes["pin"]
out.Source = attributes
out.ReceivedAt = time.Now()
return
}
func parseContact(attributes map[string]string) (out Contact) {
out.JID = strings.Replace(attributes["jid"], "@c.us", "@s.whatsapp.net", 1)
out.Notify = attributes["notify"]
out.Name = attributes["name"]
out.Short = attributes["short"]
out.VName = attributes["vname"]
out.IsEnterprise, _ = strconv.ParseBool(attributes["enterprise"])
out.IsVerified, _ = strconv.ParseBool(attributes["verify"])
out.Source = attributes
out.ReceivedAt = time.Now()
return
}
func newStore() *Store {
return &Store{
Contacts: make(map[string]Contact),
Chats: make(map[string]Chat),
}
}
func (wac *Conn) updateContacts(contacts interface{}) {
c, ok := contacts.([]interface{})
if !ok {
return
}
wac.Store.ContactsLock.Lock()
for _, contact := range c {
contactNode, ok := contact.(binary.Node)
if !ok {
continue
}
parsedContact := parseContact(contactNode.Attributes)
wac.Store.Contacts[parsedContact.JID] = parsedContact
}
wac.Store.ContactsLock.Unlock()
}
func (wac *Conn) updateChats(chats interface{}) {
c, ok := chats.([]interface{})
if !ok {
return
}
wac.Store.ChatsLock.Lock()
for _, chat := range c {
chatNode, ok := chat.(binary.Node)
if !ok {
continue
}
parsedChat := parseChat(chatNode.Attributes)
wac.Store.Chats[parsedChat.JID] = parsedChat
}
wac.Store.ChatsLock.Unlock()
}