-
Notifications
You must be signed in to change notification settings - Fork 47
/
bot_api.go
665 lines (573 loc) · 19.3 KB
/
bot_api.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
// Copyright (c) 2022-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"regexp"
"time"
"github.com/mattermost/mattermost-plugin-calls/server/public"
"github.com/mattermost/mattermost/server/public/model"
)
var (
botChRE = regexp.MustCompile(`^\/bot\/channels\/([a-z0-9]+)$`)
botUserImageRE = regexp.MustCompile(`^\/bot\/users\/([a-z0-9]+)\/image$`)
botUploadsRE = regexp.MustCompile(`^\/bot\/uploads\/?([a-z0-9]+)?$`)
botRecordingsRE = regexp.MustCompile(`^\/bot\/calls\/([a-z0-9]+)\/recordings$`)
botJobsStatusRE = regexp.MustCompile(`^\/bot\/calls\/([a-z0-9]+)\/jobs\/([a-z0-9]+)\/status$`)
botProfileForSessionRE = regexp.MustCompile(`^\/bot\/calls\/([a-z0-9]+)\/sessions\/([a-z0-9]+)\/profile$`)
botTranscriptionsRE = regexp.MustCompile(`^\/bot\/calls\/([a-z0-9]+)\/transcriptions$`)
botFilenameRE = regexp.MustCompile(`^\/bot\/calls\/([a-z0-9]+)\/filename$`)
)
func (p *Plugin) getBotID() string {
if p.botSession != nil {
return p.botSession.UserId
}
return ""
}
func (p *Plugin) isBot(userID string) bool {
if userID != "" && userID == p.getBotID() {
return true
}
return false
}
func (p *Plugin) isBotSession(r *http.Request) bool {
return p.isBot(r.Header.Get("Mattermost-User-Id"))
}
func (p *Plugin) handleBotGetChannel(w http.ResponseWriter, _ *http.Request, channelID string) {
channel, appErr := p.API.GetChannel(channelID)
if appErr != nil {
p.LogError(appErr.Error())
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(channel); err != nil {
p.LogError(err.Error())
}
}
func (p *Plugin) handleBotGetUserImage(w http.ResponseWriter, r *http.Request, userID string) {
data, appErr := p.API.GetProfileImage(userID)
if appErr != nil {
p.LogError(appErr.Error())
http.NotFound(w, r)
}
http.ServeContent(w, r, userID, time.Now(), bytes.NewReader(data))
}
func (p *Plugin) handleBotGetUpload(w http.ResponseWriter, _ *http.Request, uploadID string) {
var res httpResponse
defer p.httpResponseHandler(&res, w)
us, err := p.API.GetUploadSession(uploadID)
if err != nil {
res.Err = err.Error()
appErr, ok := err.(*model.AppError)
if ok {
res.Code = appErr.StatusCode
} else {
res.Code = http.StatusInternalServerError
}
return
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(us); err != nil {
p.LogError(err.Error())
}
}
func (p *Plugin) handleBotUploadData(w http.ResponseWriter, r *http.Request, uploadID string) {
var res httpResponse
defer p.httpAudit("handleBotUploadData", &res, w, r)
us, err := p.API.GetUploadSession(uploadID)
if err != nil {
res.Err = err.Error()
appErr, ok := err.(*model.AppError)
if ok {
res.Code = appErr.StatusCode
} else {
res.Code = http.StatusInternalServerError
}
return
}
serverCfg := p.API.GetConfig()
if serverCfg == nil {
res.Err = "failed to get server configuration"
res.Code = http.StatusInternalServerError
return
}
// We use FileSettings.MaxFileSize to keep the request body size bounded to
// a sensible value to avoid abuse. This doesn't limit the amount of data we
// can upload overall.
fi, err := p.API.UploadData(us, http.MaxBytesReader(w, r.Body, *serverCfg.FileSettings.MaxFileSize))
if err != nil {
res.Err = err.Error()
appErr, ok := err.(*model.AppError)
if ok {
res.Code = appErr.StatusCode
} else {
res.Code = http.StatusInternalServerError
}
return
}
// Upload is incomplete.
if fi == nil {
res.Code = http.StatusNoContent
return
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(fi); err != nil {
p.LogError(err.Error())
}
}
func (p *Plugin) handleBotCreateUpload(w http.ResponseWriter, r *http.Request) {
var res httpResponse
defer p.httpAudit("handleBotCreateUpload", &res, w, r)
var us *model.UploadSession
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, requestBodyMaxSizeBytes)).Decode(&us); err != nil {
res.Err = "failed to decode request body: " + err.Error()
res.Code = http.StatusBadRequest
return
}
us.Id = model.NewId()
us.Type = model.UploadTypeAttachment
us.UserId = p.getBotID()
us, err := p.API.CreateUploadSession(us)
if err != nil {
res.Err = err.Error()
appErr, ok := err.(*model.AppError)
if ok {
res.Code = appErr.StatusCode
} else {
res.Code = http.StatusInternalServerError
}
return
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(us); err != nil {
p.LogError(err.Error())
}
}
func (p *Plugin) handleBotPostRecordings(w http.ResponseWriter, r *http.Request, callID string) {
var res httpResponse
defer p.httpAudit("handleBotPostRecordings", &res, w, r)
var info public.RecordingJobInfo
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, requestBodyMaxSizeBytes)).Decode(&info); err != nil {
res.Err = "failed to decode request body: " + err.Error()
res.Code = http.StatusBadRequest
return
}
if err := info.IsValid(); err != nil {
res.Err = err.Error()
res.Code = http.StatusBadRequest
return
}
// Here we need to lock since we'll be reading and updating the call
// post, potentially concurrently with other events (e.g. call ending,
// transcribing job completing).
_, err := p.lockCall(callID)
if err != nil {
res.Err = fmt.Errorf("failed to lock call: %w", err).Error()
res.Code = http.StatusInternalServerError
return
}
defer p.unlockCall(callID)
// Update call post
post, err := p.GetPost(info.PostID)
if err != nil {
res.Err = "failed to get call post: " + err.Error()
res.Code = http.StatusInternalServerError
return
}
threadID := post.Id
// Post in thread
if post.RootId != "" {
threadID = post.RootId
}
// TODO: consider deprecating this in favour of the new recordings metadata
// object.
recordingFiles, ok := post.GetProp("recording_files").([]interface{})
if !ok {
recordingFiles = []interface{}{
info.FileIDs[0],
}
} else {
recordingFiles = append(recordingFiles, info.FileIDs[0])
}
post.AddProp("recording_files", recordingFiles)
startAt, _ := post.GetProp("start_at").(int64)
postMsg := "Here's the call recording"
if cfg := p.getConfiguration(); cfg.transcriptionsEnabled() {
postMsg = "Here's the call recording. Transcription is processing and will be posted when ready."
}
if title, _ := post.GetProp("title").(string); title != "" {
postMsg = fmt.Sprintf("%s of %s at %s UTC", postMsg, title, time.UnixMilli(startAt).Format("3:04PM"))
}
recPost := &model.Post{
UserId: p.getBotID(),
ChannelId: callID,
Message: postMsg,
Type: callRecordingPostType,
RootId: threadID,
FileIds: []string{info.FileIDs[0]},
}
recPost.AddProp("recording_id", info.JobID)
recPost.AddProp("call_post_id", info.PostID)
recPost, appErr := p.API.CreatePost(recPost)
if appErr != nil {
res.Err = "failed to create post: " + appErr.Error()
res.Code = http.StatusInternalServerError
return
}
// We update the metadata with the file and post IDs for the recording.
recordings, ok := post.GetProp("recordings").(map[string]any)
if ok {
var rm jobMetadata
rm.fromMap(recordings[info.JobID])
rm.FileID = info.FileIDs[0]
rm.PostID = recPost.Id
recordings[info.JobID] = rm.toMap()
post.AddProp("recordings", recordings)
} else {
p.LogError("unexpected data found in recordings post prop", "recID", info.JobID)
}
_, appErr = p.API.UpdatePost(post)
if appErr != nil {
res.Err = "failed to update call thread: " + appErr.Error()
res.Code = http.StatusInternalServerError
return
}
res.Code = http.StatusOK
res.Msg = "success"
}
func (p *Plugin) handleBotPostTranscriptions(w http.ResponseWriter, r *http.Request, callID string) {
var res httpResponse
defer p.httpAudit("handleBotPostTranscription", &res, w, r)
var info public.TranscribingJobInfo
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, requestBodyMaxSizeBytes)).Decode(&info); err != nil {
res.Err = "failed to decode request body: " + err.Error()
res.Code = http.StatusBadRequest
return
}
if err := info.IsValid(); err != nil {
res.Err = err.Error()
res.Code = http.StatusBadRequest
return
}
// Here we need to lock since we'll be reading and updating the call
// post, potentially concurrently with other events (e.g. call ending,
// recording job completing).
_, err := p.lockCall(callID)
if err != nil {
res.Err = fmt.Errorf("failed to lock call: %w", err).Error()
res.Code = http.StatusInternalServerError
return
}
defer p.unlockCall(callID)
// Update call post
post, err := p.GetPost(info.PostID)
if err != nil {
res.Err = "failed to get call post: " + err.Error()
res.Code = http.StatusInternalServerError
return
}
threadID := post.Id
// Post in thread
if post.RootId != "" {
threadID = post.RootId
}
// This is a bit hacky but the permissions system doesn't
// allow non admin users to access files that are not attached to
// a post, even if the channel id is set.
// Updating the file to point to the existing call post solves this problem
// without requiring us to expose a dedicated API nor attach the file which
// we don't want to show.
if err := p.updateFileInfoPostID(info.Transcriptions[0].FileIDs[0], callID, info.PostID); err != nil {
res.Err = "failed to update fileinfo post id: " + err.Error()
res.Code = http.StatusInternalServerError
}
startAt, _ := post.GetProp("start_at").(int64)
postMsg := "Here's the call transcription"
if title, _ := post.GetProp("title").(string); title != "" {
postMsg = fmt.Sprintf("%s of %s at %s UTC", postMsg, title, time.UnixMilli(startAt).Format("3:04PM"))
}
transcriptionPost := &model.Post{
UserId: p.getBotID(),
ChannelId: callID,
Message: postMsg,
Type: callTranscriptionType,
RootId: threadID,
FileIds: []string{info.Transcriptions[0].FileIDs[1]},
}
transcriptionPost.AddProp("call_post_id", info.PostID)
transcriptionPost.AddProp("transcription_id", info.JobID)
transcriptionPost.AddProp("captions", info.Transcriptions.ToClientCaptions())
trPost, appErr := p.API.CreatePost(transcriptionPost)
if appErr != nil {
res.Err = "failed to create post: " + appErr.Error()
res.Code = http.StatusInternalServerError
return
}
// We update the metadata with the file and post IDs for the transcription.
transcriptions, ok := post.GetProp("transcriptions").(map[string]any)
if !ok {
res.Err = "unexpected data found in transcriptions post prop"
res.Code = http.StatusInternalServerError
p.LogError(res.Err, "trID", info.JobID)
return
}
var tm jobMetadata
tm.fromMap(transcriptions[info.JobID])
tm.FileID = info.Transcriptions[0].FileIDs[0]
tm.PostID = trPost.Id
transcriptions[info.JobID] = tm.toMap()
post.AddProp("transcriptions", transcriptions)
// We retrieve the related recording info (if any) so that we can save the file id
// for the VTT captions in the props of the recording post that will
// eventually render them on top of the video player.
recordings, ok := post.GetProp("recordings").(map[string]any)
if !ok {
res.Err = "unexpected data found in recordings post prop"
res.Code = http.StatusInternalServerError
p.LogError(res.Err, "trID", info.JobID)
return
}
var rm jobMetadata
rm.fromMap(recordings[tm.RecID])
if rm.PostID != "" {
recPost, err := p.GetPost(rm.PostID)
if err != nil {
res.Err = "failed to get recording post: " + err.Error()
res.Code = http.StatusInternalServerError
p.LogError(res.Err, "trID", info.JobID)
return
}
recPost.AddProp("captions", info.Transcriptions.ToClientCaptions())
if _, appErr := p.API.UpdatePost(recPost); appErr != nil {
res.Err = "failed to update recording post: " + appErr.Error()
res.Code = http.StatusInternalServerError
p.LogError(res.Err, "trID", info.JobID)
return
}
} else {
p.LogWarn("unexpected missing recording post ID", "trID", info.JobID)
}
_, appErr = p.API.UpdatePost(post)
if appErr != nil {
res.Err = "failed to update call thread: " + appErr.Error()
res.Code = http.StatusInternalServerError
return
}
res.Code = http.StatusOK
res.Msg = "success"
}
func (p *Plugin) handleBotPostJobsStatus(w http.ResponseWriter, r *http.Request, callID, jobID string) {
var res httpResponse
defer p.httpAudit("handleBotPostJobsStatus", &res, w, r)
var status public.JobStatus
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, requestBodyMaxSizeBytes)).Decode(&status); err != nil {
res.Err = "failed to decode request body: " + err.Error()
res.Code = http.StatusBadRequest
return
}
state, err := p.lockCall(callID)
if err != nil {
res.Err = fmt.Errorf("failed to lock call: %w", err).Error()
res.Code = http.StatusInternalServerError
return
}
defer p.unlockCall(callID)
if state == nil || state.Call == nil {
res.Err = "no call ongoing"
res.Code = http.StatusBadRequest
return
}
var jb, lcState *jobState
switch status.JobType {
case public.JobTypeRecording:
jb = state.Call.Recording
case public.JobTypeTranscribing:
jb = state.Call.Transcription
if cfg := p.getConfiguration(); cfg != nil && cfg.liveCaptionsEnabled() {
if lcState, err = state.getLiveCaptions(); err != nil {
p.LogError("failed to get live captions job", "callID", callID,
"error", err.Error())
}
}
default:
res.Err = "invalid job type"
res.Code = http.StatusBadRequest
return
}
if jb == nil {
res.Err = "no job ongoing"
res.Code = http.StatusBadRequest
return
}
if jb.ID != jobID {
res.Err = "invalid job ID"
res.Code = http.StatusBadRequest
return
}
if status.Status != public.JobStatusTypeFailed && jb.EndAt > 0 {
res.Err = "job has ended"
res.Code = http.StatusBadRequest
return
}
if status.Status == public.JobStatusTypeFailed {
p.LogDebug("job has failed", "jobID", jobID, "jobType", status.JobType)
jb.EndAt = time.Now().UnixMilli()
jb.Err = status.Error
if status.JobType == public.JobTypeRecording && state.Call.Transcription != nil {
if err := p.stopTranscribingJob(state, callID); err != nil {
p.LogError("failed to stop transcribing job", "callID", callID, "err", err.Error())
}
} else if status.JobType == public.JobTypeTranscribing && state.Call.Recording != nil {
if _, _, err := p.stopRecordingJob(state, callID); err != nil {
p.LogError("failed to stop recording job", "callID", callID, "err", err.Error())
}
}
} else if status.Status == public.JobStatusTypeStarted {
if jb.StartAt > 0 {
res.Err = "job has already started"
res.Code = http.StatusBadRequest
return
}
p.LogDebug("job has started", "jobID", jobID)
jb.StartAt = time.Now().UnixMilli()
if lcState != nil {
// For now we are assuming that if transcriptions are on and live captions are enabled,
// then the live captioning has started. This can change in the future; if it does, we will
// only need to change the backend.
lcState.StartAt = time.Now().UnixMilli()
}
} else {
res.Err = "unsupported status type"
res.Code = http.StatusBadRequest
return
}
if err := p.kvSetChannelState(callID, state); err != nil {
res.Err = fmt.Errorf("failed to set channel state: %w", err).Error()
res.Code = http.StatusInternalServerError
return
}
if status.JobType == public.JobTypeRecording {
p.publishWebSocketEvent(wsEventCallJobState, map[string]interface{}{
"callID": callID,
"jobState": state.Call.Recording.getClientState().toMap(),
}, &model.WebsocketBroadcast{ChannelId: callID, ReliableClusterSend: true})
// MM-57224: deprecated, remove when not needed by mobile pre 2.14.0
p.publishWebSocketEvent(wsEventCallRecordingState, map[string]interface{}{
"callID": callID,
"recState": state.Call.Recording.getClientState().toMap(),
}, &model.WebsocketBroadcast{ChannelId: callID, ReliableClusterSend: true})
} else {
p.publishWebSocketEvent(wsEventCallJobState, map[string]interface{}{
"callID": callID,
"jobState": state.Call.Transcription.getClientState().toMap(),
}, &model.WebsocketBroadcast{ChannelId: callID, ReliableClusterSend: true})
if lcState != nil {
p.publishWebSocketEvent(wsEventCallJobState, map[string]interface{}{
"callID": callID,
"jobState": state.Call.LiveCaptions.getClientState().toMap(),
}, &model.WebsocketBroadcast{ChannelId: callID, ReliableClusterSend: true})
}
}
res.Code = http.StatusOK
res.Msg = "success"
}
func (p *Plugin) handleBotGetProfileForSession(w http.ResponseWriter, callID, sessionID string) {
var res httpResponse
defer p.httpResponseHandler(&res, w)
state, err := p.lockCall(callID)
if err != nil {
p.LogError("handleBotGetProfileForSession: failed to lock call", "err", err.Error())
res.Code = http.StatusInternalServerError
res.Err = err.Error()
return
}
defer p.unlockCall(callID)
if state == nil || state.Call == nil {
res.Code = http.StatusBadRequest
res.Err = "no call ongoing"
return
}
ust := state.Call.Sessions[sessionID]
if ust.UserID == "" {
res.Code = http.StatusNotFound
res.Err = "not found"
return
}
user, appErr := p.API.GetUser(ust.UserID)
if appErr != nil {
res.Code = http.StatusInternalServerError
res.Err = appErr.Error()
return
}
user.Sanitize(nil)
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(user); err != nil {
p.LogError(err.Error())
}
}
func (p *Plugin) handleBotGetFilenameForCall(w http.ResponseWriter, callID string) {
var res httpResponse
defer p.httpResponseHandler(&res, w)
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(map[string]string{
"filename": p.genFilenameForCall(callID),
}); err != nil {
p.LogError(err.Error())
}
}
func (p *Plugin) handleBotAPI(w http.ResponseWriter, r *http.Request) {
if !p.licenseChecker.RecordingsAllowed() {
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
if !p.isBotSession(r) {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
if r.Method == http.MethodGet {
if matches := botChRE.FindStringSubmatch(r.URL.Path); len(matches) == 2 {
p.handleBotGetChannel(w, r, matches[1])
return
}
if matches := botUserImageRE.FindStringSubmatch(r.URL.Path); len(matches) == 2 {
p.handleBotGetUserImage(w, r, matches[1])
return
}
if matches := botUploadsRE.FindStringSubmatch(r.URL.Path); len(matches) == 2 && matches[1] != "" {
p.handleBotGetUpload(w, r, matches[1])
return
}
if matches := botProfileForSessionRE.FindStringSubmatch(r.URL.Path); len(matches) == 3 {
p.handleBotGetProfileForSession(w, matches[1], matches[2])
return
}
if matches := botFilenameRE.FindStringSubmatch(r.URL.Path); len(matches) == 2 {
p.handleBotGetFilenameForCall(w, matches[1])
return
}
}
if r.Method == http.MethodPost {
if r.URL.Path == "/bot/uploads" {
p.handleBotCreateUpload(w, r)
return
} else if matches := botUploadsRE.FindStringSubmatch(r.URL.Path); len(matches) == 2 && matches[1] != "" {
p.handleBotUploadData(w, r, matches[1])
return
}
if matches := botRecordingsRE.FindStringSubmatch(r.URL.Path); len(matches) == 2 {
p.handleBotPostRecordings(w, r, matches[1])
return
}
if matches := botJobsStatusRE.FindStringSubmatch(r.URL.Path); len(matches) == 3 {
p.handleBotPostJobsStatus(w, r, matches[1], matches[2])
return
}
if matches := botTranscriptionsRE.FindStringSubmatch(r.URL.Path); len(matches) == 2 {
p.handleBotPostTranscriptions(w, r, matches[1])
return
}
}
http.NotFound(w, r)
}