-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
threads.go
376 lines (314 loc) · 11.5 KB
/
threads.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package socket
import (
"fmt"
"sort"
"strconv"
"strings"
"go.mau.fi/mautrix-meta/messagix/table"
)
type SendMessageTask struct {
// If you are forwarding a message, you set the ThreadId to the thread you would like to forward it to
ThreadId int64 `json:"thread_id"`
Otid int64 `json:"otid,string"`
Source table.ThreadSourceType `json:"source"`
SendType table.SendType `json:"send_type"`
AttachmentFBIds []int64 `json:"attachment_fbids,omitempty"`
SyncGroup int64 `json:"sync_group"`
ReplyMetaData *ReplyMetaData `json:"reply_metadata,omitempty"`
MentionData *MentionData `json:"mention_data,omitempty"`
Text string `json:"text,omitempty"`
HotEmojiSize int32 `json:"hot_emoji_size,omitempty"`
StickerId int64 `json:"sticker_id,omitempty"`
InitiatingSource table.InitiatingSource `json:"initiating_source,omitempty"` // usually FACEBOOK_INBOX
SkipUrlPreviewGen int32 `json:"skip_url_preview_gen"` // 0 or 1
TextHasLinks int32 `json:"text_has_links"` // 0 or 1
StripForwardedMsgCaption int32 `json:"strip_forwarded_msg_caption,omitempty"` // 0 or 1
ForwardedMsgId string `json:"forwarded_msg_id,omitempty"`
MultiTabEnv int32 `json:"multitab_env"` // 0 ?
// url to external media
// for example:
//
// https://media2.giphy.com/media/fItgT774J3nWw/giphy.gif?cid=999aceaclonctzck6x9rte211fb3l24m2poepsdchan17ryd&ep=v1_gifs_trending&rid=giphy.gif&ct=g
Url string `json:"url,omitempty"`
// attribution app id, returned in the graphql query CometAnimatedImagePickerSearchResultsRootQuery
AttributionAppId int64 `json:"attribution_app_id,omitempty"`
}
type ReplyMetaData struct {
ReplyMessageId string `json:"reply_source_id"`
ReplySourceType int64 `json:"reply_source_type"` // 1 ?
ReplyType int64 `json:"reply_type"` // ?
ReplySender int64 `json:"-"`
}
type MentionData struct {
// All fields here are comma-separated lists
MentionIDs string `json:"mention_ids"`
MentionOffsets string `json:"mention_offsets"`
MentionLengths string `json:"mention_lengths"`
MentionTypes string `json:"mention_types"`
}
func (md *MentionData) Parse() (Mentions, error) {
if len(md.MentionIDs) == 0 {
return nil, nil
}
mentionIDs := strings.Split(md.MentionIDs, ",")
mentionOffsets := strings.Split(md.MentionOffsets, ",")
mentionLengths := strings.Split(md.MentionLengths, ",")
mentionTypes := strings.Split(md.MentionTypes, ",")
if len(mentionIDs) != len(mentionOffsets) || len(mentionOffsets) != len(mentionLengths) || len(mentionLengths) != len(mentionTypes) {
return nil, fmt.Errorf("mismatching mention data lengths: %d, %d, %d, %d", len(mentionIDs), len(mentionOffsets), len(mentionLengths), len(mentionTypes))
}
mentions := make(Mentions, len(mentionIDs))
for i := range mentionIDs {
userID, err := strconv.ParseInt(mentionIDs[i], 10, 64)
if err != nil {
return nil, fmt.Errorf("failed to parse mention #%d user ID: %w", i+1, err)
}
offset, err := strconv.Atoi(mentionOffsets[i])
if err != nil {
return nil, fmt.Errorf("failed to parse mention #%d offset: %w", i+1, err)
}
length, err := strconv.Atoi(mentionLengths[i])
if err != nil {
return nil, fmt.Errorf("failed to parse mention #%d length: %w", i+1, err)
}
mentions[i] = Mention{
ID: userID,
Offset: offset,
Length: length,
Type: MentionType(mentionTypes[i]),
}
}
sort.Sort(mentions)
return mentions, nil
}
type MentionType string
const (
MentionTypePerson MentionType = "p"
MentionTypeSilent MentionType = "s"
MentionTypeThread MentionType = "t"
)
type Mention struct {
ID int64
Offset int
Length int
Type MentionType
}
type Mentions []Mention
func (m Mentions) Len() int {
return len(m)
}
func (m Mentions) Less(i, j int) bool {
return m[i].Offset < m[j].Offset
}
func (m Mentions) Swap(i, j int) {
m[i], m[j] = m[j], m[i]
}
func (m Mentions) ToData() MentionData {
mentionIDs := make([]string, len(m))
mentionOffsets := make([]string, len(m))
mentionLengths := make([]string, len(m))
mentionTypes := make([]string, len(m))
for i, mention := range m {
mentionIDs[i] = strconv.FormatInt(mention.ID, 10)
mentionOffsets[i] = strconv.Itoa(mention.Offset)
mentionLengths[i] = strconv.Itoa(mention.Length)
mentionTypes[i] = string(mention.Type)
}
return MentionData{
MentionIDs: strings.Join(mentionIDs, ","),
MentionOffsets: strings.Join(mentionOffsets, ","),
MentionLengths: strings.Join(mentionLengths, ","),
MentionTypes: strings.Join(mentionTypes, ","),
}
}
func (t *SendMessageTask) GetLabel() string {
return TaskLabels["SendMessageTask"]
}
func (t *SendMessageTask) Create() (interface{}, interface{}, bool) {
queueName := strconv.FormatInt(t.ThreadId, 10)
return t, queueName, false
}
type CreatePollTask struct {
QuestionText string `json:"question_text"`
ThreadKey int64 `json:"thread_key"`
Options []string `json:"options"`
SyncGroup int64 `json:"sync_group"`
}
func (t *CreatePollTask) GetLabel() string {
return TaskLabels["CreatePollTask"]
}
func (t *CreatePollTask) Create() (interface{}, interface{}, bool) {
return t, "poll_creation", false
}
type UpdatePollTask struct {
ThreadKey int64 `json:"thread_key"`
PollID int64 `json:"poll_id"`
AddedOptions []map[string]int `json:"added_options"`
SelectedOptions []int64 `json:"selected_options"`
SyncGroup int64 `json:"sync_group"`
}
func (t *UpdatePollTask) GetLabel() string {
return TaskLabels["UpdatePollTask"]
}
func (t *UpdatePollTask) Create() (interface{}, interface{}, bool) {
return t, "poll_update", false
}
type ThreadMarkReadTask struct {
ThreadId int64 `json:"thread_id"`
LastReadWatermarkTs int64 `json:"last_read_watermark_ts"`
SyncGroup int64 `json:"sync_group"`
}
func (t *ThreadMarkReadTask) GetLabel() string {
return TaskLabels["ThreadMarkRead"]
}
func (t *ThreadMarkReadTask) Create() (interface{}, interface{}, bool) {
queueName := strconv.FormatInt(t.ThreadId, 10)
return t, queueName, false
}
type FetchMessagesTask struct {
ThreadKey int64 `json:"thread_key"`
Direction int64 `json:"direction"` // 0
ReferenceTimestampMs int64 `json:"reference_timestamp_ms"`
ReferenceMessageId string `json:"reference_message_id"`
SyncGroup int64 `json:"sync_group"` // 1
Cursor string `json:"cursor"`
}
func (t *FetchMessagesTask) GetLabel() string {
return TaskLabels["FetchMessagesTask"]
}
func (t *FetchMessagesTask) Create() (interface{}, interface{}, bool) {
threadStr := strconv.FormatInt(t.ThreadKey, 10)
queueName := "mrq." + threadStr
return t, queueName, false
}
type MuteThreadTask struct {
ThreadKey int64 `json:"thread_key"`
MailboxType int64 `json:"mailbox_type"` // 0
MuteExpireTimeMS int64 `json:"mute_expire_time_ms"`
SyncGroup int64 `json:"sync_group"` // 1
}
func (t *MuteThreadTask) GetLabel() string {
return TaskLabels["MuteThreadTask"]
}
func (t *MuteThreadTask) Create() (interface{}, interface{}, bool) {
return t, strconv.FormatInt(t.ThreadKey, 10), false
}
type RenameThreadTask struct {
ThreadKey int64 `json:"thread_key"`
ThreadName string `json:"thread_name"`
SyncGroup int64 `json:"sync_group"` // 1
}
func (t *RenameThreadTask) GetLabel() string {
return TaskLabels["RenameThreadTask"]
}
func (t *RenameThreadTask) Create() (interface{}, interface{}, bool) {
return t, strconv.FormatInt(t.ThreadKey, 10), false
}
type SetThreadImageTask struct {
ThreadKey int64 `json:"thread_key"`
ImageID int64 `json:"image_id"`
SyncGroup int64 `json:"sync_group"` // 1
}
func (t *SetThreadImageTask) GetLabel() string {
return TaskLabels["SetThreadImageTask"]
}
func (t *SetThreadImageTask) Create() (interface{}, interface{}, bool) {
return t, "thread_image", false
}
type EditMessageTask struct {
MessageID string `json:"message_id"`
Text string `json:"text"`
}
func (t *EditMessageTask) GetLabel() string {
return TaskLabels["EditMessageTask"]
}
func (t *EditMessageTask) Create() (interface{}, interface{}, bool) {
return t, "edit_message", false
}
type UpdateAdminTask struct {
ThreadKey int64 `json:"thread_key"`
ContactID int64 `json:"contact_id"`
IsAdmin int `json:"is_admin"`
}
func (t *UpdateAdminTask) GetLabel() string {
return TaskLabels["UpdateAdminTask"]
}
func (t *UpdateAdminTask) Create() (interface{}, interface{}, bool) {
return t, "admin_status", false
}
type RemoveParticipantTask struct {
ThreadID int64 `json:"thread_id"`
ContactID int64 `json:"contact_id"`
}
func (t *RemoveParticipantTask) GetLabel() string {
return TaskLabels["RemoveParticipantTask"]
}
func (t *RemoveParticipantTask) Create() (interface{}, interface{}, bool) {
return t, "remove_participant_v2", false
}
type AddParticipantsTask struct {
ThreadKey int64 `json:"thread_key"`
ContactIDs []int64 `json:"contact_ids"`
SyncGroup int64 `json:"sync_group"`
}
func (t *AddParticipantsTask) GetLabel() string {
return TaskLabels["AddParticipantsTask"]
}
func (t *AddParticipantsTask) Create() (interface{}, interface{}, bool) {
return t, strconv.FormatInt(t.ThreadKey, 10), false
}
type CreateThreadTask struct {
ThreadFBID int64 `json:"thread_fbid"`
ForceUpsert int `json:"force_upsert"`
UseOpenMessengerTransport int `json:"use_open_messenger_transport"`
SyncGroup int `json:"sync_group"`
MetadataOnly int `json:"metadata_only"`
PreviewOnly int `json:"preview_only"`
}
func (t *CreateThreadTask) GetLabel() string {
return TaskLabels["CreateThreadTask"]
}
func (t *CreateThreadTask) Create() (interface{}, interface{}, bool) {
return t, strconv.FormatInt(t.ThreadFBID, 10), false
}
type CreateGroupPayload struct {
ThreadID int64 `json:"thread_id"`
OTID string `json:"otid"`
Source int `json:"source"` // 0
SendType int `json:"send_type"` // 8
}
type CreateGroupTask struct {
Participants []int64 `json:"participants"`
SendPayload CreateGroupPayload `json:"send_payload"`
}
func (t *CreateGroupTask) GetLabel() string {
return TaskLabels["CreateGroupTask"]
}
func (t *CreateGroupTask) Create() (interface{}, interface{}, bool) {
return t, strconv.FormatInt(t.SendPayload.ThreadID, 10), false
}
type DeleteThreadTask struct {
ThreadKey int64 `json:"thread_key"`
RemoveType int64 `json:"remove_type"`
SyncGroup int64 `json:"sync_group"`
}
func (t *DeleteThreadTask) GetLabel() string {
return TaskLabels["DeleteThreadTask"]
}
func (t *DeleteThreadTask) Create() (interface{}, interface{}, bool) {
return t, strconv.FormatInt(t.ThreadKey, 10), false
}
type CreateWhatsAppThreadTask struct {
WAJID int64 `json:"wa_jid"`
OfflineThreadKey int64 `json:"offline_thread_key"`
ThreadType table.ThreadType `json:"thread_type"`
FolderType table.FolderType `json:"folder_type"`
BumpTimestampMS int64 `json:"bump_timestamp_ms"`
TAMThreadSubtype int64 `json:"tam_thread_subtype"` // 0
}
func (t *CreateWhatsAppThreadTask) GetLabel() string {
return TaskLabels["CreateWhatsAppThreadTask"]
}
func (t *CreateWhatsAppThreadTask) Create() (any, any, bool) {
return t, strconv.FormatInt(t.OfflineThreadKey, 10), false
}