/
comment.go
166 lines (155 loc) · 4.87 KB
/
comment.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
159
160
161
162
163
164
165
166
package media
import (
"bytes"
"encoding/json"
"fmt"
"github.com/dreamer2q/go_wechat/request"
"github.com/pkg/errors"
"strings"
)
//TODO test comment related apis
const (
reqCommentOpen = "cgi-bin/comment/open"
reqCommentClose = "cgi-bin/comment/close"
reqCommentList = "cgi-bin/comment/list"
reqCommentMarkElect = "cgi-bin/comment/markelect"
reqCommentUnmarkElect = "cgi-bin/comment/unmarkelect"
reqCommentDel = "cgi-bin/comment/delete"
reqCommentDelReply = "cgi-bin/comment/reply/delete"
reqCommentAddReply = "cgi-bin/comment/reply/add"
)
type GetComment struct {
MsgID uint32 `json:"msg_data_id"`
Index uint32 `json:"index,omitempty"`
Begin uint32 `json:"begin"`
Count uint32 `json:"count"`
Type CommentType `json:"type"`
}
type CommentType uint32
const (
CTypeBoth CommentType = 0
CTypeCommon CommentType = 1
CTypeSelected CommentType = 2
)
type CommentResult struct {
request.CommonError
Total uint32 `json:"total"`
Comments []struct {
UserCommentID string `json:"user_comment_id"`
OpenID string `json:"openid"`
CreateTime int64 `json:"create_time"`
Content string `json:"content"`
Reply *struct {
Content string `json:"content"`
CreateTime int64 `json:"create_time"`
} `json:"reply,omitempty"`
} `json:"comment"`
}
func (m *Media) CommentList(req *GetComment) (*CommentResult, error) {
postBytes, err := json.Marshal(req)
if err != nil {
return nil, errors.Wrap(err, "marshal")
}
_, body, err := m.req.Post(reqCommentList, nil, request.TypeJSON, bytes.NewReader(postBytes))
if err != nil {
return nil, errors.Wrap(err, "post")
}
ret := &CommentResult{}
err = json.Unmarshal(body, ret)
if err != nil {
return nil, errors.Wrap(err, "unmarshal")
}
if ret.ErrCode != 0 {
return nil, fmt.Errorf("%d %v", ret.ErrCode, ret.ErrMsg)
}
return ret, nil
}
func (m *Media) CommentMark(MsgID uint32, UserID uint32, index ...uint32) error {
return m.doCommentSelect(true, MsgID, UserID, index...)
}
func (m *Media) CommentUnmark(MsgID uint32, UserID uint32, index ...uint32) error {
return m.doCommentSelect(false, MsgID, UserID, index...)
}
func (m *Media) doCommentSelect(mark bool, MsgID uint32, UserID uint32, index ...uint32) error {
var (
i uint32
body []byte
err error
)
if index != nil {
i = index[0]
}
postJson := fmt.Sprintf(`{"msg_data_id":%d ,"index": %d, "user_comment_id": %d}`, MsgID, i, UserID)
if mark {
_, body, err = m.req.Post(reqCommentMarkElect, nil, request.TypeJSON, strings.NewReader(postJson))
} else {
_, body, err = m.req.Post(reqCommentUnmarkElect, nil, request.TypeJSON, strings.NewReader(postJson))
}
if err != nil {
return errors.Wrap(err, "post")
}
return request.CheckCommonError(body)
}
func (m *Media) CommentOpen(mediaID string, index ...int) error {
return m.doCommentSwitch(true, mediaID, index...)
}
func (m *Media) CommentClose(mediaID string, index ...int) error {
return m.doCommentSwitch(false, mediaID, index...)
}
func (m *Media) doCommentSwitch(open bool, mediaID string, index ...int) error {
var (
postJson string
body []byte
err error
)
if index != nil {
postJson = fmt.Sprintf(`{"msg_data_id":%q, "index":%q }`, mediaID, index[0])
} else {
postJson = fmt.Sprintf(`{"msg_data_id":%q}`, mediaID)
}
if open {
_, body, err = m.req.Post(reqCommentOpen, nil, request.TypeJSON, strings.NewReader(postJson))
} else {
_, body, err = m.req.Post(reqCommentClose, nil, request.TypeJSON, strings.NewReader(postJson))
}
if err != nil {
return errors.Wrap(err, "post")
}
return request.CheckCommonError(body)
}
func (m *Media) CommentDel(isReply bool, MsgID uint32, CommentID uint32, index ...uint32) error {
var (
postJson string
body []byte
err error
)
if index != nil {
postJson = fmt.Sprintf(`{"msg_data_id":%d ,"index":%d ,"user_comment_id":%d }`, MsgID, index[0], CommentID)
} else {
postJson = fmt.Sprintf(`{"msg_data_id":%d ,"user_comment_id":%d }`, MsgID, CommentID)
}
if isReply {
_, body, err = m.req.Post(reqCommentDelReply, nil, request.TypeJSON, strings.NewReader(postJson))
} else {
_, body, err = m.req.Post(reqCommentDel, nil, request.TypeJSON, strings.NewReader(postJson))
}
if err != nil {
return errors.Wrap(err, "post")
}
return request.CheckCommonError(body)
}
func (m *Media) CommentReply(msgID uint32, commentID uint32, content string, index ...uint32) error {
var (
postJson string
)
if index != nil {
postJson = fmt.Sprintf(`{"msg_data_id": %d, "index":%d, "user_comment_id": %d, "content":%q }`, msgID, index[0], commentID, content)
} else {
postJson = fmt.Sprintf(`{"msg_data_id": %d, "user_comment_id": %d, "content":%q }`, msgID, commentID, content)
}
_, body, err := m.req.Post(reqCommentAddReply, nil, request.TypeJSON, strings.NewReader(postJson))
if err != nil {
return errors.Wrap(err, "post")
}
return request.CheckCommonError(body)
}