-
Notifications
You must be signed in to change notification settings - Fork 43
/
recording_api.go
322 lines (267 loc) · 11 KB
/
recording_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
// Copyright (c) 2022-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package main
import (
"encoding/json"
"fmt"
"github.com/mattermost/mattermost-plugin-calls/server/public"
"net/http"
"regexp"
"time"
"github.com/mattermost/calls-offloader/public/job"
"github.com/mattermost/mattermost/server/public/model"
)
var callRecordingActionRE = regexp.MustCompile(`^\/calls\/([a-z0-9]+)/recording/(start|stop|publish)$`)
const recordingJobStartTimeout = time.Minute
func (p *Plugin) recJobTimeoutChecker(callID, jobID string) {
time.Sleep(recordingJobStartTimeout)
state, err := p.lockCall(callID)
if err != nil {
p.LogError("failed to lock call", "err", err.Error(), "callID", callID, "jobID", jobID)
return
}
defer p.unlockCall(callID)
recState, err := state.getRecording()
if err != nil {
p.LogWarn("failed to get recording state", "err", err.Error(), "callID", callID, "jobID", jobID)
return
}
// If the recording hasn't started (bot hasn't joined yet) we notify the
// client.
if recState.StartAt == 0 {
if recState.JobID != jobID {
p.LogInfo("a new job has started in between, exiting", "callID", callID, "jobID", jobID)
return
}
p.LogError("timed out waiting for recorder bot to join", "callID", callID, "jobID", jobID)
state.Call.Recording = nil
if err := p.kvSetChannelState(callID, state); err != nil {
p.LogError("failed to set channel state", "err", err.Error())
return
}
if state.Call.Transcription != nil && state.Call.Transcription.EndAt == 0 {
if err := p.stopTranscribingJob(state, callID); err != nil {
p.LogError("failed to stop transcribing job", "err", err.Error(), "callID", callID, "jobID", jobID)
}
}
clientState := recState.getClientState()
clientState.Err = "failed to start recording job: timed out waiting for bot to join call"
clientState.EndAt = time.Now().UnixMilli()
p.publishWebSocketEvent(wsEventCallJobState, map[string]interface{}{
"callID": callID,
"jobState": clientState.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": clientState.toMap(),
}, &model.WebsocketBroadcast{ChannelId: callID, ReliableClusterSend: true})
}
}
func (p *Plugin) startRecordingJob(state *channelState, callID, userID string) (rst *JobStateClient, rcode int, rerr error) {
if state.Call.Recording != nil && state.Call.Recording.EndAt == 0 {
return nil, http.StatusForbidden, fmt.Errorf("recording already in progress")
}
recState := new(jobState)
recState.Type = public.JobTypeRecording
recState.ID = model.NewId()
recState.CreatorID = userID
recState.InitAt = time.Now().UnixMilli()
state.Call.Recording = recState
if err := p.kvSetChannelState(callID, state); err != nil {
return nil, http.StatusInternalServerError, fmt.Errorf("failed to set channel state: %w", err)
}
defer func() {
// In case of any error we relay it to the client.
if rerr != nil && recState != nil {
recState.EndAt = time.Now().UnixMilli()
recState.Err = rerr.Error()
p.publishWebSocketEvent(wsEventCallJobState, map[string]interface{}{
"callID": callID,
"jobState": recState.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": recState.getClientState().toMap(),
}, &model.WebsocketBroadcast{ChannelId: callID, ReliableClusterSend: true})
}
}()
// Sending the event prior to making the API call to the job service
// since it could take a few seconds to complete and we want clients
// to get their local state updated as soon as it changes on the server.
p.publishWebSocketEvent(wsEventCallJobState, map[string]interface{}{
"callID": callID,
"jobState": recState.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": recState.getClientState().toMap(),
}, &model.WebsocketBroadcast{ChannelId: callID, ReliableClusterSend: true})
// We don't want to keep the lock while making the API call to the service since it
// could take a while to return. We lock again as soon as this returns.
p.unlockCall(callID)
recJobID, jobErr := p.getJobService().RunJob(job.TypeRecording, callID, state.Call.PostID, recState.ID, p.botSession.Token)
state, err := p.lockCall(callID)
if err != nil {
return nil, http.StatusInternalServerError, fmt.Errorf("failed to lock call: %w", err)
}
recState, err = state.getRecording()
if err != nil {
return nil, http.StatusInternalServerError, fmt.Errorf("failed to get recording state: %w", err)
}
if jobErr != nil {
state.Call.Recording = nil
if err := p.kvSetChannelState(callID, state); err != nil {
return nil, http.StatusInternalServerError, fmt.Errorf("failed to set channel state: %w", err)
}
return nil, http.StatusInternalServerError, fmt.Errorf("failed to create recording job: %w", jobErr)
}
if recState.JobID != "" {
return nil, http.StatusForbidden, fmt.Errorf("recording job already in progress")
}
recState.JobID = recJobID
if err := p.kvSetChannelState(callID, state); err != nil {
return nil, http.StatusInternalServerError, fmt.Errorf("failed to set channel state: %w", err)
}
p.LogDebug("recording job started successfully", "jobID", recJobID, "callID", callID)
var trID string
if cfg := p.getConfiguration(); cfg.transcriptionsEnabled() {
trID = model.NewId()
p.LogDebug("transcriptions enabled, starting job", "callID", callID)
if err := p.startTranscribingJob(state, callID, userID, trID); err != nil {
p.LogError("failed to start transcribing job", "callID", callID, "err", err.Error())
return nil, http.StatusInternalServerError, fmt.Errorf("failed to start transcribing job: %w", err)
}
}
if err := p.saveRecordingMetadata(state.Call.PostID, recState.ID, trID); err != nil {
p.LogError("failed to save recording metadata", "err", err.Error())
}
p.publishWebSocketEvent(wsEventCallJobState, map[string]interface{}{
"callID": callID,
"jobState": recState.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": recState.getClientState().toMap(),
}, &model.WebsocketBroadcast{ChannelId: callID, ReliableClusterSend: true})
go p.recJobTimeoutChecker(callID, recJobID)
return recState.getClientState(), http.StatusOK, nil
}
func (p *Plugin) stopRecordingJob(state *channelState, callID string) (rst *JobStateClient, rcode int, rerr error) {
if state.Call.Recording == nil || state.Call.Recording.EndAt != 0 {
return nil, http.StatusForbidden, fmt.Errorf("no recording in progress")
}
recState, err := state.getRecording()
if err != nil {
return nil, http.StatusInternalServerError, fmt.Errorf("failed to get recording state: %w", err)
}
recState.EndAt = time.Now().UnixMilli()
state.Call.Recording = nil
if err := p.kvSetChannelState(callID, state); err != nil {
return nil, http.StatusInternalServerError, fmt.Errorf("failed to set channel state: %w", err)
}
defer func() {
// In case of any error we relay it to the client.
if rerr != nil {
recState.Err = rerr.Error()
p.publishWebSocketEvent(wsEventCallJobState, map[string]interface{}{
"callID": callID,
"jobState": recState.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": recState.getClientState().toMap(),
}, &model.WebsocketBroadcast{ChannelId: callID, ReliableClusterSend: true})
}
}()
if state.Call.Transcription != nil && state.Call.Transcription.EndAt == 0 {
if err := p.stopTranscribingJob(state, callID); err != nil {
p.LogError("failed to stop transcribing job", "callID", callID, "err", err.Error())
}
}
if err := p.getJobService().StopJob(callID, recState.ID, p.getBotID(), recState.BotConnID); err != nil {
return nil, http.StatusInternalServerError, fmt.Errorf("failed to stop recording job: %w", err)
}
p.publishWebSocketEvent(wsEventCallJobState, map[string]interface{}{
"callID": callID,
"jobState": recState.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": recState.getClientState().toMap(),
}, &model.WebsocketBroadcast{ChannelId: callID, ReliableClusterSend: true})
return recState.getClientState(), http.StatusOK, nil
}
func (p *Plugin) handleRecordingAction(w http.ResponseWriter, r *http.Request, callID, action string) {
var res httpResponse
defer p.httpAudit("handleRecordingAction", &res, w, r)
userID := r.Header.Get("Mattermost-User-Id")
if !p.API.HasPermissionToChannel(userID, callID, model.PermissionReadChannel) {
res.Err = "Forbidden"
res.Code = http.StatusForbidden
return
}
if !p.licenseChecker.RecordingsAllowed() {
res.Err = "Recordings are not allowed by your license"
res.Code = http.StatusForbidden
return
}
if cfg := p.getConfiguration(); !cfg.recordingsEnabled() {
res.Err = "Recordings are not enabled"
res.Code = http.StatusForbidden
return
}
if p.getJobService() == nil {
res.Err = "Job service is not initialized"
res.Code = http.StatusForbidden
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 {
res.Err = "channel state is missing from store"
res.Code = http.StatusForbidden
return
}
if state.Call == nil {
res.Err = "no call ongoing"
res.Code = http.StatusForbidden
return
}
if state.Call.HostID != userID {
res.Err = "no permissions to record"
res.Code = http.StatusForbidden
return
}
var code int
var recState *JobStateClient
switch action {
case "start":
recState, code, err = p.startRecordingJob(state, callID, userID)
case "stop":
recState, code, err = p.stopRecordingJob(state, callID)
default:
res.Err = "unsupported recording action"
res.Code = http.StatusBadRequest
return
}
if err != nil {
res.Code = code
res.Err = err.Error()
return
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(recState); err != nil {
p.LogError(err.Error())
}
}