1package whatsapp
2
3import (
4	"github.com/Rhymen/go-whatsapp/binary"
5	"strings"
6)
7
8type Store struct {
9	Contacts map[string]Contact
10	Chats    map[string]Chat
11}
12
13type Contact struct {
14	Jid    string
15	Notify string
16	Name   string
17	Short  string
18}
19
20type Chat struct {
21	Jid             string
22	Name            string
23	Unread          string
24	LastMessageTime string
25	IsMuted         string
26	IsMarkedSpam    string
27}
28
29func newStore() *Store {
30	return &Store{
31		make(map[string]Contact),
32		make(map[string]Chat),
33	}
34}
35
36func (wac *Conn) updateContacts(contacts interface{}) {
37	c, ok := contacts.([]interface{})
38	if !ok {
39		return
40	}
41
42	for _, contact := range c {
43		contactNode, ok := contact.(binary.Node)
44		if !ok {
45			continue
46		}
47
48		jid := strings.Replace(contactNode.Attributes["jid"], "@c.us", "@s.whatsapp.net", 1)
49		wac.Store.Contacts[jid] = Contact{
50			jid,
51			contactNode.Attributes["notify"],
52			contactNode.Attributes["name"],
53			contactNode.Attributes["short"],
54		}
55	}
56}
57
58func (wac *Conn) updateChats(chats interface{}) {
59	c, ok := chats.([]interface{})
60	if !ok {
61		return
62	}
63
64	for _, chat := range c {
65		chatNode, ok := chat.(binary.Node)
66		if !ok {
67			continue
68		}
69
70		jid := strings.Replace(chatNode.Attributes["jid"], "@c.us", "@s.whatsapp.net", 1)
71		wac.Store.Chats[jid] = Chat{
72			jid,
73			chatNode.Attributes["name"],
74			chatNode.Attributes["count"],
75			chatNode.Attributes["t"],
76			chatNode.Attributes["mute"],
77			chatNode.Attributes["spam"],
78		}
79	}
80}
81