-
Notifications
You must be signed in to change notification settings - Fork 63
/
room_auth.go
486 lines (408 loc) · 13.7 KB
/
room_auth.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
package models
import (
"context"
"database/sql"
"fmt"
"github.com/cavaliergopher/grab/v3"
"github.com/gabriel-vasile/mimetype"
"github.com/livekit/protocol/livekit"
"github.com/mynaparrot/plugnmeet-protocol/plugnmeet"
"github.com/mynaparrot/plugnmeet-protocol/utils"
"github.com/mynaparrot/plugnmeet-server/pkg/config"
log "github.com/sirupsen/logrus"
"net/http"
"os"
"strings"
"time"
)
type RoomAuthModel struct {
rs *RoomService
rm *RoomModel
}
func NewRoomAuthModel() *RoomAuthModel {
return &RoomAuthModel{
rs: NewRoomService(),
rm: NewRoomModel(),
}
}
func (am *RoomAuthModel) CreateRoom(r *plugnmeet.CreateRoomReq) (bool, string, *livekit.Room) {
// some pre creation tasks
am.preRoomCreationTasks(r)
// in preRoomCreationTasks we've added this room in progress list
// so, we'll just use defer to clean this room at the end of this function
defer am.rs.RoomCreationProgressList(r.RoomId, "del")
roomDbInfo, _ := am.rm.GetRoomInfo(r.RoomId, "", 1)
if roomDbInfo.Id > 0 {
rf, err := am.rs.LoadRoomInfo(r.RoomId)
if err != nil && err.Error() != config.RequestedRoomNotExist {
return false, "can't create room. try again", nil
}
if err == nil && rf.Sid == roomDbInfo.Sid {
return true, "room already exists", rf
}
// we'll allow creating room again & use the same DB row
// we can just update the DB row. No need to create a new one
}
// we'll set default values otherwise client got confused if data is missing
utils.PrepareDefaultRoomFeatures(r)
utils.SetCreateRoomDefaultValues(r, config.AppCnf.UploadFileSettings.MaxSize, config.AppCnf.UploadFileSettings.AllowedTypes, config.AppCnf.SharedNotePad.Enabled)
utils.SetRoomDefaultLockSettings(r)
// set default room settings
utils.SetDefaultRoomSettings(config.AppCnf.RoomDefaultSettings, r)
// copyright
copyrightConf := config.AppCnf.Client.CopyrightConf
if copyrightConf == nil {
r.Metadata.CopyrightConf = &plugnmeet.CopyrightConf{
Display: true,
Text: "Powered by <a href=\"https://www.plugnmeet.org\" target=\"_blank\">plugNmeet</a>",
}
} else {
d := &plugnmeet.CopyrightConf{
Display: copyrightConf.Display,
Text: copyrightConf.Text,
}
// this mean user has set copyright info by API
if r.Metadata.CopyrightConf != nil {
// if not allow to override then we will simply use default
if !copyrightConf.AllowOverride {
r.Metadata.CopyrightConf = d
}
} else {
r.Metadata.CopyrightConf = d
}
}
// Azure cognitive services
azu := config.AppCnf.AzureCognitiveServicesSpeech
if !azu.Enabled {
r.Metadata.RoomFeatures.SpeechToTextTranslationFeatures.IsAllow = false
} else {
var maxAllow int32 = 2
if azu.MaxNumTranLangsAllowSelecting > 0 {
maxAllow = azu.MaxNumTranLangsAllowSelecting
}
r.Metadata.RoomFeatures.SpeechToTextTranslationFeatures.MaxNumTranLangsAllowSelecting = maxAllow
}
if r.Metadata.IsBreakoutRoom && r.Metadata.RoomFeatures.EnableAnalytics {
// at present, we'll disable an analytic report for breakout rooms
r.Metadata.RoomFeatures.EnableAnalytics = false
}
meta, err := am.rs.MarshalRoomMetadata(r.Metadata)
if err != nil {
return false, "Error: " + err.Error(), nil
}
room, err := am.rs.CreateRoom(r.RoomId, r.EmptyTimeout, r.MaxParticipants, meta)
if err != nil {
return false, "Error: " + err.Error(), nil
}
if room.Sid == "" {
// without SID, it is hard to manage, if empty then we won't continue
// in this case we'll end the room to clean up
_, _ = am.rs.EndRoom(r.RoomId)
return false, "Error: can't create room with empty SID", nil
}
isBreakoutRoom := 0
if r.Metadata.IsBreakoutRoom {
isBreakoutRoom = 1
} else {
// at present, we'll fetch file for main room only
go am.prepareWhiteboardPreloadFile(r, room)
}
updateTable := false
ri := &RoomInfo{
RoomTitle: r.Metadata.RoomTitle,
RoomId: room.Name,
Sid: room.Sid,
JoinedParticipants: 0,
IsRunning: 1,
CreationTime: room.CreationTime,
Created: time.Now().UTC().Format("2006-01-02 15:04:05"),
WebhookUrl: "",
IsBreakoutRoom: int64(isBreakoutRoom),
ParentRoomId: r.Metadata.ParentRoomId,
}
if r.Metadata.WebhookUrl != nil {
ri.WebhookUrl = *r.Metadata.WebhookUrl
}
if roomDbInfo.Id > 0 {
updateTable = true
ri.Id = roomDbInfo.Id
}
_, err = am.rm.InsertOrUpdateRoomData(ri, updateTable)
if err != nil {
return false, "Error: " + err.Error(), nil
}
// we'll silently add metadata into our redis
// we can avoid errors (if occur) because it will update from webhook too
_, _ = am.rs.ManageActiveRoomsWithMetadata(r.RoomId, "add", meta)
return true, "room created", room
}
func (am *RoomAuthModel) preRoomCreationTasks(r *plugnmeet.CreateRoomReq) {
exist, err := am.rs.ManageActiveRoomsWithMetadata(r.GetRoomId(), "get", "")
if err == nil && exist != nil {
// maybe this room was ended just now, so we'll wait until clean up done
waitFor := config.WaitBeforeTriggerOnAfterRoomEnded + (1 * time.Second)
log.Infoln("this room:", r.GetRoomId(), "still active, we'll wait for:", waitFor, "before recreating it again.")
time.Sleep(waitFor)
}
// check & wait
am.rs.CheckAndWaitUntilRoomCreationInProgress(r.GetRoomId())
// we'll add this room in processing list
_, err = am.rs.RoomCreationProgressList(r.GetRoomId(), "add")
if err != nil {
log.Errorln(err)
}
}
func (am *RoomAuthModel) IsRoomActive(r *plugnmeet.IsRoomActiveReq) (*plugnmeet.IsRoomActiveRes, *plugnmeet.RoomMetadata) {
// check first
am.rs.CheckAndWaitUntilRoomCreationInProgress(r.GetRoomId())
res := &plugnmeet.IsRoomActiveRes{
Status: true,
Msg: "room is not active",
}
roomDbInfo, _ := am.rm.GetRoomInfo(r.RoomId, "", 1)
if roomDbInfo.Id == 0 {
return res, nil
}
// let's make sure room actually active
_, meta, err := am.rs.LoadRoomWithMetadata(r.RoomId)
if err != nil {
// room isn't active. Change status
_, _ = am.rm.UpdateRoomStatus(&RoomInfo{
RoomId: r.RoomId,
IsRunning: 0,
Ended: time.Now().UTC().Format("2006-01-02 15:04:05"),
})
return res, nil
}
res.IsActive = true
res.Msg = "room is active"
return res, meta
}
func (am *RoomAuthModel) GetActiveRoomInfo(r *plugnmeet.GetActiveRoomInfoReq) (bool, string, *plugnmeet.ActiveRoomWithParticipant) {
// check first
am.rs.CheckAndWaitUntilRoomCreationInProgress(r.GetRoomId())
roomDbInfo, _ := am.rm.GetRoomInfo(r.RoomId, "", 1)
if roomDbInfo.Id == 0 {
return false, "no room found", nil
}
rrr, err := am.rs.LoadRoomInfo(r.RoomId)
if err != nil {
return false, err.Error(), nil
}
res := new(plugnmeet.ActiveRoomWithParticipant)
res.RoomInfo = &plugnmeet.ActiveRoomInfo{
RoomTitle: roomDbInfo.RoomTitle,
RoomId: roomDbInfo.RoomId,
Sid: roomDbInfo.Sid,
JoinedParticipants: roomDbInfo.JoinedParticipants,
IsRunning: int32(roomDbInfo.IsRunning),
IsRecording: int32(roomDbInfo.IsRecording),
IsActiveRtmp: int32(roomDbInfo.IsActiveRTMP),
WebhookUrl: roomDbInfo.WebhookUrl,
IsBreakoutRoom: int32(roomDbInfo.IsBreakoutRoom),
ParentRoomId: roomDbInfo.ParentRoomId,
CreationTime: roomDbInfo.CreationTime,
Metadata: rrr.Metadata,
}
res.ParticipantsInfo, _ = am.rs.LoadParticipants(roomDbInfo.RoomId)
return true, "success", res
}
func (am *RoomAuthModel) GetActiveRoomsInfo() (bool, string, []*plugnmeet.ActiveRoomWithParticipant) {
roomsInfo, err := am.rm.GetActiveRoomsInfo()
if err != nil {
return false, "no active room found", nil
}
if len(roomsInfo) == 0 {
return false, "no active room found", nil
}
var res []*plugnmeet.ActiveRoomWithParticipant
for _, r := range roomsInfo {
roomInfo := r
i := new(plugnmeet.ActiveRoomWithParticipant)
i.RoomInfo = roomInfo
participants, err := am.rs.LoadParticipants(r.RoomId)
if err == nil {
i.ParticipantsInfo = participants
}
rri, err := am.rs.LoadRoomInfo(r.RoomId)
if err == nil {
i.RoomInfo.Metadata = rri.Metadata
}
res = append(res, i)
}
return true, "success", res
}
func (am *RoomAuthModel) EndRoom(r *plugnmeet.RoomEndReq) (bool, string) {
// check first
am.rs.CheckAndWaitUntilRoomCreationInProgress(r.GetRoomId())
roomDbInfo, _ := am.rm.GetRoomInfo(r.GetRoomId(), "", 1)
if roomDbInfo.Id == 0 {
return false, "room not active"
}
_, err := am.rs.EndRoom(r.GetRoomId())
if err != nil {
return false, "can't end room"
}
_, _ = am.rm.UpdateRoomStatus(&RoomInfo{
RoomId: r.GetRoomId(),
IsRunning: 0,
Ended: time.Now().UTC().Format("2006-01-02 15:04:05"),
})
return true, "success"
}
func (am *RoomAuthModel) FetchPastRooms(r *plugnmeet.FetchPastRoomsReq) (*plugnmeet.FetchPastRoomsResult, error) {
db := config.AppCnf.DB
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
limit := r.Limit
orderBy := "DESC"
if limit == 0 {
limit = 20
}
if r.OrderBy == "ASC" {
orderBy = "ASC"
}
var rows *sql.Rows
var err error
switch {
case len(r.RoomIds) > 0:
var args []interface{}
for _, rd := range r.RoomIds {
args = append(args, rd)
}
args = append(args, r.From)
args = append(args, limit)
query := "SELECT a.room_title, a.roomId, a.sid, a.joined_participants, a.webhook_url, a.created, a.ended, b.file_id FROM " + config.AppCnf.FormatDBTable("room_info") + " AS a LEFT JOIN " + config.AppCnf.FormatDBTable("room_analytics") + " AS b ON a.id = b.room_table_id WHERE a.roomId IN (?" + strings.Repeat(",?", len(r.RoomIds)-1) + ") AND a.is_running = '0' ORDER BY a.id " + orderBy + " LIMIT ?,?"
rows, err = db.QueryContext(ctx, query, args...)
default:
rows, err = db.QueryContext(ctx, "SELECT a.room_title, a.roomId, a.sid, a.joined_participants, a.webhook_url, a.created, a.ended, b.file_id FROM "+config.AppCnf.FormatDBTable("room_info")+" AS a LEFT JOIN "+config.AppCnf.FormatDBTable("room_analytics")+" AS b ON a.id = b.room_table_id WHERE a.is_running = '0' ORDER BY a.id "+orderBy+" LIMIT ?,?", r.From, limit)
}
if err != nil {
return nil, err
}
defer rows.Close()
var rooms []*plugnmeet.PastRoomInfo
for rows.Next() {
var room plugnmeet.PastRoomInfo
var rSid, analyticsFileId sql.NullString
err = rows.Scan(&room.RoomTitle, &room.RoomId, &rSid, &room.JoinedParticipants, &room.WebhookUrl, &room.Created, &room.Ended, &analyticsFileId)
if err != nil {
log.Errorln(err)
}
room.RoomSid = rSid.String
room.AnalyticsFileId = analyticsFileId.String
rooms = append(rooms, &room)
}
// get total number of recordings
var row *sql.Row
switch {
case len(r.RoomIds) > 0:
var args []interface{}
for _, rd := range r.RoomIds {
args = append(args, rd)
}
query := "SELECT COUNT(*) AS total FROM " + config.AppCnf.FormatDBTable("room_info") + " WHERE roomId IN (?" + strings.Repeat(",?", len(r.RoomIds)-1) + ") AND is_running = '0'"
row = db.QueryRowContext(ctx, query, args...)
default:
row = db.QueryRowContext(ctx, "SELECT COUNT(*) AS total FROM "+config.AppCnf.FormatDBTable("room_info")+" WHERE is_running = '0'")
}
var total int64
_ = row.Scan(&total)
result := &plugnmeet.FetchPastRoomsResult{
TotalRooms: total,
From: r.From,
Limit: limit,
OrderBy: orderBy,
RoomsList: rooms,
}
return result, nil
}
func (am *RoomAuthModel) ChangeVisibility(r *plugnmeet.ChangeVisibilityRes) (bool, string) {
_, roomMeta, err := am.rs.LoadRoomWithMetadata(r.RoomId)
if err != nil {
return false, err.Error()
}
if r.VisibleWhiteBoard != nil {
roomMeta.RoomFeatures.WhiteboardFeatures.Visible = *r.VisibleWhiteBoard
}
if r.VisibleNotepad != nil {
roomMeta.RoomFeatures.SharedNotePadFeatures.Visible = *r.VisibleNotepad
}
_, err = am.rs.UpdateRoomMetadataByStruct(r.RoomId, roomMeta)
if err != nil {
return false, err.Error()
}
return true, "success"
}
func (am *RoomAuthModel) prepareWhiteboardPreloadFile(req *plugnmeet.CreateRoomReq, room *livekit.Room) {
if !req.Metadata.RoomFeatures.WhiteboardFeatures.AllowedWhiteboard || req.Metadata.RoomFeatures.WhiteboardFeatures.PreloadFile == nil {
return
}
// get file info
httpClient := &http.Client{Timeout: 5 * time.Second}
resp, err := httpClient.Head(*req.Metadata.RoomFeatures.WhiteboardFeatures.PreloadFile)
if err != nil {
log.Errorln(err)
return
}
if resp.ContentLength < 1 {
log.Errorf("invalid file type")
return
} else if resp.ContentLength > config.MaxPreloadedWhiteboardFileSize {
log.Errorf("Allowd %d but given %d", config.MaxPreloadedWhiteboardFileSize, resp.ContentLength)
return
}
fm := NewManageFileModel(&ManageFile{
Sid: room.Sid,
RoomId: room.Name,
})
cType := resp.Header.Get("Content-Type")
if cType == "" {
log.Errorln("invalid Content-Type")
return
}
// validate file type
mtype := mimetype.Lookup(cType)
err = fm.validateMimeType(mtype)
if err != nil {
log.Errorln(err)
return
}
downloadDir := fmt.Sprintf("%s/%s", config.AppCnf.UploadFileSettings.Path, room.Sid)
if _, err = os.Stat(downloadDir); os.IsNotExist(err) {
err = os.MkdirAll(downloadDir, os.ModePerm)
if err != nil {
log.Errorln(err)
return
}
}
// now download the file
gres, err := grab.Get(downloadDir, *req.Metadata.RoomFeatures.WhiteboardFeatures.PreloadFile)
if err != nil {
log.Errorln(err)
return
}
// double check to make sure that dangerous file wasn't uploaded
mtype, err = mimetype.DetectFile(gres.Filename)
if err != nil {
log.Errorln(err)
// remove the file if have problem
_ = os.RemoveAll(gres.Filename)
return
}
err = fm.validateMimeType(mtype)
if err != nil {
log.Errorln(err)
// remove the file if validation not passed
_ = os.RemoveAll(gres.Filename)
return
}
ms := strings.SplitN(gres.Filename, "/", -1)
fm.FilePath = fmt.Sprintf("%s/%s", room.Sid, ms[len(ms)-1])
_, err = fm.ConvertWhiteboardFile()
if err != nil {
log.Errorln(err)
}
// finally delete the file
_ = os.RemoveAll(gres.Filename)
}