forked from eatmoreapple/openwechat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync_check.go
54 lines (45 loc) · 1.27 KB
/
sync_check.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
package openwechat
import (
"errors"
"strconv"
)
type Selector string
const (
SelectorNormal Selector = "0" // 正常
SelectorNewMsg Selector = "2" // 有新消息
SelectorModContact Selector = "4" // 联系人信息变更
SelectorAddOrDelContact Selector = "6" // 添加或删除联系人
SelectorModChatRoom Selector = "7" // 进入或退出聊天室
)
type SyncCheckResponse struct {
RetCode string
Selector Selector
}
func (s SyncCheckResponse) Success() bool {
return s.RetCode == "0"
}
func (s SyncCheckResponse) NorMal() bool {
return s.Success() && s.Selector == SelectorNormal
}
func (s SyncCheckResponse) HasNewMessage() bool {
return s.Success() && s.Selector == SelectorNewMsg
}
func (s SyncCheckResponse) Err() error {
if s.Success() {
return nil
}
i, err := strconv.Atoi(s.RetCode)
if err != nil {
return errors.New("sync check unknown error")
}
return Ret(i)
}
func NewSyncCheckResponse(b []byte) (*SyncCheckResponse, error) {
results := syncCheckRegexp.FindSubmatch(b)
if len(results) != 3 {
return nil, errors.New("parse sync key failed")
}
retCode, selector := string(results[1]), Selector(results[2])
syncCheckResponse := &SyncCheckResponse{RetCode: retCode, Selector: selector}
return syncCheckResponse, nil
}