/
cache.go
146 lines (128 loc) · 3.05 KB
/
cache.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
package wechat
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"sync"
)
type cache contactCache
type contactCache struct {
sync.Mutex
contacts map[string]*Contact
}
// Contact is wx Account struct
type Contact struct {
UserName string
NickName string
HeadImgURL string `json:"HeadImgUrl"`
HeadHash string
RemarkName string
DisplayName string
StarFriend float64
Sex float64
Signature string
VerifyFlag float64
ContactFlag float64
HeadImgFlag float64
Province string
City string
Alias string
EncryChatRoomID string `json:"EncryChatRoomId"`
Type int
MemberList []*Contact
}
const (
// Offical 公众号 ...
Offical = 0
// Friend 好友 ...
Friend = 1
// Group 群组 ...
Group = 2
// Member 群组成员 ...
Member = 3
// FriendAndMember 即是好友也是群成员 ...
FriendAndMember = 4
)
func newCache() *cache {
return &cache{
contacts: make(map[string]*Contact),
}
}
func (c *cache) updateContact(v map[string]interface{}) {
nc, err := newContact(v)
if err != nil {
logger.Errorf(`create contact failed error: %v`, err)
} else {
if len(nc.UserName) > 0 {
oc := c.contacts[nc.NickName]
if oc != nil {
logger.Info(`old contact: %v will be replaced by %v`, oc, nc)
}
c.contacts[nc.UserName] = nc
} else {
logger.Warningf(`bad contact %v`, v)
}
}
}
func (c *cache) clearContactBy(username string) {
delete(c.contacts, username)
}
func (c *cache) clear() {
c.contacts = make(map[string]*Contact)
}
func (wechat *WeChat) syncContacts(cts []map[string]interface{}) {
wechat.cache.Lock()
defer wechat.cache.Unlock()
logger.Debugf(`count of contact [contain group member]: [%d]`, len(cts))
for _, v := range cts {
wechat.cache.updateContact(v)
}
var buffer bytes.Buffer
for _, c := range wechat.cache.contacts {
des := "[群成员]"
if c.Type == FriendAndMember {
des = "[群友兼容好友]"
} else if c.Type == Friend {
des = "[好友]"
} else if c.Type == Offical {
des = "[公众号]"
}
buffer.WriteString(des)
buffer.WriteString(fmt.Sprintf(" %v", replaceEmoji(c.NickName)))
buffer.WriteString(fmt.Sprintf(" [Sex: %v", c.Sex))
buffer.WriteString(fmt.Sprintf(" City: %v]", c.City))
buffer.WriteString("\n")
}
logger.Debug(buffer.String())
}
// 修改在这里处理
func (wechat *WeChat) appendContacts(cts []map[string]interface{}) {
wechat.cache.Lock()
defer wechat.cache.Unlock()
for _, v := range cts {
wechat.cache.updateContact(v)
}
}
func (wechat *WeChat) removeContact(username string) {
wechat.cache.Lock()
wechat.cache.clearContactBy(username)
wechat.cache.Unlock()
}
func (c *cache) contactByUserName(un string) (*Contact, error) {
c.Lock()
defer c.Unlock()
if contact, found := c.contacts[un]; found {
return contact, nil
}
return nil, errors.New(`not Found`)
}
func newContact(m map[string]interface{}) (*Contact, error) {
data, err := json.Marshal(m)
if err != nil {
return nil, err
}
var c *Contact
err = json.Unmarshal(data, &c)
return c, err
}