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
|
package realtime
import (
"fmt"
"strconv"
"time"
"github.com/Jeffail/gabs"
"github.com/gopackage/ddp"
"github.com/matterbridge/Rocket.Chat.Go.SDK/models"
)
const (
// RocketChat doesn't send the `added` event for new messages by default, only `changed`.
send_added_event = true
default_buffer_size = 100
)
// LoadHistory loads history
// Takes roomId
//
// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/load-history
func (c *Client) LoadHistory(roomId string) error {
_, err := c.ddp.Call("loadHistory", roomId)
if err != nil {
return err
}
return nil
}
// SendMessage sends message to channel
// takes channel and message
//
// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/send-message
func (c *Client) SendMessage(m *models.Message) (*models.Message, error) {
m.ID = c.newRandomId()
rawResponse, err := c.ddp.Call("sendMessage", m)
if err != nil {
return nil, err
}
if rawResponse == nil {
return nil, fmt.Errorf("rawResponse is %#v", rawResponse)
}
return getMessageFromData(rawResponse.(map[string]interface{})), nil
}
// EditMessage edits a message
// takes message object
//
// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/update-message
func (c *Client) EditMessage(message *models.Message) error {
_, err := c.ddp.Call("updateMessage", message)
if err != nil {
return err
}
return nil
}
// DeleteMessage deletes a message
// takes a message object
//
// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/delete-message
func (c *Client) DeleteMessage(message *models.Message) error {
_, err := c.ddp.Call("deleteMessage", map[string]string{
"_id": message.ID,
})
if err != nil {
return err
}
return nil
}
// ReactToMessage adds a reaction to a message
// takes a message and emoji
//
// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/set-reaction
func (c *Client) ReactToMessage(message *models.Message, reaction string) error {
_, err := c.ddp.Call("setReaction", reaction, message.ID)
if err != nil {
return err
}
return nil
}
// StarMessage stars message
// takes a message object
//
// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/star-message
func (c *Client) StarMessage(message *models.Message) error {
_, err := c.ddp.Call("starMessage", map[string]interface{}{
"_id": message.ID,
"rid": message.RoomID,
"starred": true,
})
if err != nil {
return err
}
return nil
}
// UnStarMessage unstars message
// takes message object
//
// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/star-message
func (c *Client) UnStarMessage(message *models.Message) error {
_, err := c.ddp.Call("starMessage", map[string]interface{}{
"_id": message.ID,
"rid": message.RoomID,
"starred": false,
})
if err != nil {
return err
}
return nil
}
// PinMessage pins a message
// takes a message object
//
// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/pin-message
func (c *Client) PinMessage(message *models.Message) error {
_, err := c.ddp.Call("pinMessage", message)
if err != nil {
return err
}
return nil
}
// UnPinMessage unpins message
// takes a message object
//
// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/unpin-messages
func (c *Client) UnPinMessage(message *models.Message) error {
_, err := c.ddp.Call("unpinMessage", message)
if err != nil {
return err
}
return nil
}
// SubscribeToMessageStream Subscribes to the message updates of a channel
// Returns a buffered channel
//
// https://rocket.chat/docs/developer-guides/realtime-api/subscriptions/stream-room-messages/
func (c *Client) SubscribeToMessageStream(channel *models.Channel, msgChannel chan models.Message) error {
if err := c.ddp.Sub("stream-room-messages", channel.ID, send_added_event); err != nil {
return err
}
// msgChannel := make(chan models.Message, default_buffer_size)
c.ddp.CollectionByName("stream-room-messages").AddUpdateListener(messageExtractor{msgChannel, "update"})
return nil
}
func getMessagesFromUpdateEvent(update ddp.Update) []models.Message {
document, _ := gabs.Consume(update["args"])
args, err := document.Children()
if err != nil {
// log.Printf("Event arguments are in an unexpected format: %v", err)
return make([]models.Message, 0)
}
messages := make([]models.Message, len(args))
for i, arg := range args {
messages[i] = *getMessageFromDocument(arg)
}
return messages
}
func getMessageFromData(data interface{}) *models.Message {
// TODO: We should know what this will look like, we shouldn't need to use gabs
document, _ := gabs.Consume(data)
return getMessageFromDocument(document)
}
func getMessageFromDocument(arg *gabs.Container) *models.Message {
var ts *time.Time
var attachments []models.Attachment
attachmentSrc, err := arg.Path("attachments").Children()
if err != nil {
attachments = make([]models.Attachment, 0)
} else {
attachments = make([]models.Attachment, len(attachmentSrc))
for i, attachment := range attachmentSrc {
attachments[i] = models.Attachment{
Timestamp: stringOrZero(attachment.Path("ts").Data()),
Title: stringOrZero(attachment.Path("title").Data()),
TitleLink: stringOrZero(attachment.Path("title_link").Data()),
TitleLinkDownload: stringOrZero(attachment.Path("title_link_download").Data()),
ImageURL: stringOrZero(attachment.Path("image_url").Data()),
AuthorName: stringOrZero(arg.Path("u.name").Data()),
}
}
}
date := stringOrZero(arg.Path("ts.$date").Data())
if len(date) > 0 {
if ti, err := strconv.ParseFloat(date, 64); err == nil {
t := time.Unix(int64(ti)/1e3, int64(ti)%1e3)
ts = &t
}
}
return &models.Message{
ID: stringOrZero(arg.Path("_id").Data()),
RoomID: stringOrZero(arg.Path("rid").Data()),
Msg: stringOrZero(arg.Path("msg").Data()),
Type: stringOrZero(arg.Path("t").Data()),
Timestamp: ts,
User: &models.User{
ID: stringOrZero(arg.Path("u._id").Data()),
UserName: stringOrZero(arg.Path("u.username").Data()),
},
Attachments: attachments,
}
}
func stringOrZero(i interface{}) string {
if i == nil {
return ""
}
switch i.(type) {
case string:
return i.(string)
case float64:
return fmt.Sprintf("%f", i.(float64))
default:
return ""
}
}
type messageExtractor struct {
messageChannel chan models.Message
operation string
}
func (u messageExtractor) CollectionUpdate(collection, operation, id string, doc ddp.Update) {
if operation == u.operation {
msgs := getMessagesFromUpdateEvent(doc)
for _, m := range msgs {
u.messageChannel <- m
}
}
}
|