forked from GetStream/stream-chat-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
262 lines (198 loc) · 6.76 KB
/
message.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package stream_chat // nolint: golint
import (
"errors"
"net/http"
"net/url"
"path"
"time"
)
type MessageType string
const (
MessageTypeRegular MessageType = "regular"
MessageTypeError MessageType = "error"
MessageTypeReply MessageType = "reply"
MessageTypeSystem MessageType = "system"
MessageTypeEphemeral MessageType = "ephemeral"
)
type Message struct {
ID string `json:"id"`
Text string `json:"text"`
HTML string `json:"html"`
Type MessageType `json:"type,omitempty"` // one of MessageType* constants
User *User `json:"user"`
Attachments []*Attachment `json:"attachments"`
LatestReactions []*Reaction `json:"latest_reactions"` // last reactions
OwnReactions []*Reaction `json:"own_reactions"`
ReactionCounts map[string]int `json:"reaction_counts"`
ParentID string `json:"parent_id"` // id of parent message if it's reply
ShowInChannel bool `json:"show_in_channel"` // show reply message also in channel
ReplyCount int `json:"reply_count,omitempty"`
MentionedUsers []*User `json:"mentioned_users"`
CreatedAt *time.Time `json:"created_at,omitempty"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
// any other fields the user wants to attach a message
ExtraData map[string]interface{}
}
func (m *Message) toRequest() messageRequest {
var req messageRequest
req.Message = messageRequestMessage{
Text: m.Text,
Attachments: m.Attachments,
User: messageRequestUser{ID: m.User.ID},
ExtraData: m.ExtraData,
ParentID: m.ParentID,
ShowInChannel: m.ShowInChannel,
}
if len(m.MentionedUsers) > 0 {
req.Message.MentionedUsers = make([]string, 0, len(m.MentionedUsers))
for _, u := range m.MentionedUsers {
req.Message.MentionedUsers = append(req.Message.MentionedUsers, u.ID)
}
}
return req
}
type messageRequest struct {
Message messageRequestMessage `json:"message"`
}
type messageRequestMessage struct {
Text string `json:"text"`
Attachments []*Attachment `json:"attachments"`
User messageRequestUser `json:"user"`
MentionedUsers []string `json:"mentioned_users"`
ParentID string `json:"parent_id"`
ShowInChannel bool `json:"show_in_channel"`
ExtraData map[string]interface{} `json:"-,extra"` //nolint: staticcheck
}
type messageRequestUser struct {
ID string `json:"id"`
}
type messageResponse struct {
Message *Message `json:"message"`
}
type Attachment struct {
Type string `json:"type,omitempty"` // text, image, audio, video
AuthorName string `json:"author_name,omitempty"`
Title string `json:"title,omitempty"`
TitleLink string `json:"title_link,omitempty"`
Text string `json:"text,omitempty"`
ImageURL string `json:"image_url,omitempty"`
ThumbURL string `json:"thumb_url,omitempty"`
AssetURL string `json:"asset_url,omitempty"`
OGScrapeURL string `json:"og_scrape_url,omitempty"`
ExtraData map[string]interface{} `json:"-,extra"` //nolint: staticcheck
}
// SendMessage sends a message to the channel. Returns full message details from server
func (ch *Channel) SendMessage(message *Message, userID string) (*Message, error) {
switch {
case message == nil:
return nil, errors.New("message is nil")
case userID == "":
return nil, errors.New("user ID must be not empty")
}
var resp messageResponse
message.User = &User{ID: userID}
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID), "message")
err := ch.client.makeRequest(http.MethodPost, p, nil, message.toRequest(), &resp)
if err != nil {
return nil, err
}
return resp.Message, nil
}
// MarkAllRead marks all messages as read for userID
func (c *Client) MarkAllRead(userID string) error {
if userID == "" {
return errors.New("user ID must be not empty")
}
data := map[string]interface{}{
"user": map[string]string{
"id": userID,
},
}
return c.makeRequest(http.MethodPost, "channels/read", nil, data, nil)
}
// GetMessage returns message by ID
func (c *Client) GetMessage(msgID string) (*Message, error) {
if msgID == "" {
return nil, errors.New("message ID must be not empty")
}
var resp messageResponse
p := path.Join("messages", url.PathEscape(msgID))
err := c.makeRequest(http.MethodGet, p, nil, nil, &resp)
if err != nil {
return nil, err
}
return resp.Message, nil
}
// UpdateMessage updates message with given msgID
func (c *Client) UpdateMessage(msg *Message, msgID string) (*Message, error) {
switch {
case msg == nil:
return nil, errors.New("message is nil")
case msgID == "":
return nil, errors.New("message ID must be not empty")
}
var resp messageResponse
p := path.Join("messages", url.PathEscape(msgID))
err := c.makeRequest(http.MethodPost, p, nil, msg.toRequest(), &resp)
if err != nil {
return nil, err
}
return resp.Message, nil
}
func (c *Client) DeleteMessage(msgID string) error {
if msgID == "" {
return errors.New("message ID must be not empty")
}
p := path.Join("messages", url.PathEscape(msgID))
return c.makeRequest(http.MethodDelete, p, nil, nil, nil)
}
func (c *Client) FlagMessage(msgID string) error {
if msgID == "" {
return errors.New("message ID is empty")
}
options := map[string]interface{}{
"target_message_id": msgID,
}
return c.makeRequest(http.MethodPost, "moderation/flag", nil, options, nil)
}
func (c *Client) UnflagMessage(msgID string) error {
if msgID == "" {
return errors.New("message ID is empty")
}
options := map[string]interface{}{
"target_message_id": msgID,
}
return c.makeRequest(http.MethodPost, "moderation/unflag", nil, options, nil)
}
type repliesResponse struct {
Messages []*Message `json:"messages"`
}
// GetReplies returns list of the message replies for a parent message
// options: Pagination params, ie {limit:10, idlte: 10}
func (ch *Channel) GetReplies(parentID string, options map[string][]string) ([]*Message, error) {
if parentID == "" {
return nil, errors.New("parent ID is empty")
}
p := path.Join("messages", url.PathEscape(parentID), "replies")
var resp repliesResponse
err := ch.client.makeRequest(http.MethodGet, p, options, nil, &resp)
return resp.Messages, err
}
type sendActionRequest struct {
MessageID string `json:"message_id"`
FormData map[string]string `json:"form_data"`
}
// SendAction for message
func (ch *Channel) SendAction(msgID string, formData map[string]string) (*Message, error) {
switch {
case msgID == "":
return nil, errors.New("message ID is empty")
case len(formData) == 0:
return nil, errors.New("form data is empty")
}
p := path.Join("messages", url.PathEscape(msgID), "action")
data := sendActionRequest{MessageID: msgID, FormData: formData}
var resp messageResponse
err := ch.client.makeRequest(http.MethodPost, p, nil, data, &resp)
return resp.Message, err
}