-
Notifications
You must be signed in to change notification settings - Fork 62
/
room.go
355 lines (288 loc) · 10.6 KB
/
room.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
package models
import (
"context"
"database/sql"
"errors"
"fmt"
"github.com/mynaparrot/plugnmeet-protocol/plugnmeet"
"github.com/mynaparrot/plugnmeet-server/pkg/config"
log "github.com/sirupsen/logrus"
"time"
)
type RoomInfo struct {
Id int64
RoomTitle string `json:"room_title"`
RoomId string `json:"room_id"`
Sid string `json:"sid"`
JoinedParticipants int64 `json:"joined_participants"`
IsRunning int `json:"is_running"`
IsRecording int `json:"is_recording"`
RecorderId string `json:"recorder_id"`
IsActiveRTMP int `json:"is_active_rtmp"`
NodeIdRTMP string `json:"rtmp_node_id"`
WebhookUrl string `json:"webhook_url"`
IsBreakoutRoom int64 `json:"is_breakout_room"`
ParentRoomId string `json:"parent_room_id"`
CreationTime int64 `json:"creation_time"`
Created string
Ended string
}
type RoomModel struct {
app *config.AppConfig
db *sql.DB
ctx context.Context
}
func NewRoomModel() *RoomModel {
return &RoomModel{
app: config.AppCnf,
db: config.AppCnf.DB,
ctx: context.Background(),
}
}
func (rm *RoomModel) InsertOrUpdateRoomData(r *RoomInfo, update bool) (int64, error) {
db := rm.db
ctx, cancel := context.WithTimeout(rm.ctx, 3*time.Second)
defer cancel()
tx, err := db.BeginTx(ctx, nil)
if err != nil {
return 0, err
}
defer tx.Rollback()
query := "INSERT INTO " + rm.app.FormatDBTable("room_info") + " (room_title, roomId, sid, joined_participants, is_running, webhook_url, is_breakout_room, parent_room_id, creation_time, created) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE is_running = ?"
if update {
query = "UPDATE " + rm.app.FormatDBTable("room_info") + " SET room_title = ?, roomId = ?, sid = ?, joined_participants = ?, is_running = ?, webhook_url = ?, is_breakout_room = ?, parent_room_id = ?, creation_time = ? WHERE id = ?"
}
stmt, err := tx.PrepareContext(ctx, query)
if err != nil {
return 0, err
}
var values []interface{}
if update {
values = append(values, r.RoomTitle, r.RoomId, r.Sid, r.JoinedParticipants, r.IsRunning, r.WebhookUrl, r.IsBreakoutRoom, r.ParentRoomId, r.CreationTime, r.Id)
} else {
values = append(values, r.RoomTitle, r.RoomId, r.Sid, r.JoinedParticipants, r.IsRunning, r.WebhookUrl, r.IsBreakoutRoom, r.ParentRoomId, r.CreationTime, r.Created, 1)
}
res, err := stmt.ExecContext(ctx, values...)
if err != nil {
return 0, err
}
lastId, err := res.LastInsertId()
if err != nil {
return 0, err
}
err = tx.Commit()
if err != nil {
return 0, err
}
err = stmt.Close()
if err != nil {
return 0, err
}
return lastId, nil
}
// UpdateRoomStatus will change the room status based on `is_running` value
// most of the place this method used to change status to close
func (rm *RoomModel) UpdateRoomStatus(r *RoomInfo) (int64, error) {
db := rm.db
ctx, cancel := context.WithTimeout(rm.ctx, 3*time.Second)
defer cancel()
tx, err := db.BeginTx(ctx, nil)
if err != nil {
return 0, err
}
defer tx.Rollback()
var query string
var args []interface{}
switch {
case len(r.RoomId) > 0:
query = "UPDATE " + rm.app.FormatDBTable("room_info") + " SET is_running = ? WHERE roomId = ?"
args = append(args, r.IsRunning)
if r.IsRunning == 0 {
// when the meeting will be ended,
// we can change sid to make sure that sid always unique
// so, here we'll use sid-id
query = "UPDATE " + rm.app.FormatDBTable("room_info") + " SET sid = CONCAT(sid, '-', id), is_running = ?, is_recording = 0, is_active_rtmp = 0, ended = ? WHERE roomId = ? AND is_running <> 0"
args = append(args, r.Ended)
}
args = append(args, r.RoomId)
default:
query = "UPDATE " + rm.app.FormatDBTable("room_info") +
" SET is_running = ? WHERE sid = ?"
args = append(args, r.IsRunning)
if r.IsRunning == 0 {
// when the meeting will be ended,
// we can change sid to make sure that sid always unique
// so, here we'll use sid-id
query = "UPDATE " + rm.app.FormatDBTable("room_info") +
" SET sid = CONCAT(sid, '-', id), is_running = ?, is_recording = 0, is_active_rtmp = 0, ended = ? WHERE sid = ? AND is_running <> 0"
args = append(args, r.Ended)
}
args = append(args, r.Sid)
}
stmt, err := tx.PrepareContext(ctx, query)
if err != nil {
return 0, err
}
res, err := stmt.ExecContext(ctx, args...)
if err != nil {
return 0, err
}
affectedId, err := res.RowsAffected()
if err != nil {
return 0, err
}
err = tx.Commit()
if err != nil {
return 0, err
}
err = stmt.Close()
if err != nil {
return 0, err
}
return affectedId, nil
}
// UpdateRoomParticipants will increment or decrement number of Participants
func (rm *RoomModel) UpdateRoomParticipants(r *RoomInfo, operator string) (int64, error) {
db := rm.db
ctx, cancel := context.WithTimeout(rm.ctx, 3*time.Second)
defer cancel()
tx, err := db.BeginTx(ctx, nil)
if err != nil {
return 0, err
}
defer tx.Rollback()
stmt, err := tx.PrepareContext(ctx, "UPDATE "+rm.app.FormatDBTable("room_info")+
" SET joined_participants = joined_participants "+operator+" 1 WHERE sid = ? OR sid = CONCAT(?, '-', id)")
if err != nil {
return 0, err
}
res, err := stmt.ExecContext(ctx, r.Sid, r.Sid)
if err != nil {
return 0, err
}
affectedId, err := res.RowsAffected()
if err != nil {
return 0, err
}
err = tx.Commit()
if err != nil {
return 0, err
}
err = stmt.Close()
if err != nil {
return 0, err
}
return affectedId, nil
}
// UpdateNumParticipants will update total number of Participants
func (rm *RoomModel) UpdateNumParticipants(roomSid string, num int64) (int64, error) {
db := rm.db
ctx, cancel := context.WithTimeout(rm.ctx, 3*time.Second)
defer cancel()
tx, err := db.BeginTx(ctx, nil)
if err != nil {
return 0, err
}
defer tx.Rollback()
stmt, err := tx.PrepareContext(ctx, "UPDATE "+rm.app.FormatDBTable("room_info")+
" SET joined_participants = ? WHERE sid = ? OR sid = CONCAT(?, '-', id)")
if err != nil {
return 0, err
}
res, err := stmt.ExecContext(ctx, num, roomSid, roomSid)
if err != nil {
return 0, err
}
affectedId, err := res.RowsAffected()
if err != nil {
return 0, err
}
err = tx.Commit()
if err != nil {
return 0, err
}
err = stmt.Close()
if err != nil {
return 0, err
}
return affectedId, nil
}
// GetRoomInfo can be used to get room information
// Note: this won't send nil value, so always need to check if table Id is 0 or not
func (rm *RoomModel) GetRoomInfo(roomId string, sid string, isRunning int) (*RoomInfo, string) {
db := rm.db
ctx, cancel := context.WithTimeout(rm.ctx, 3*time.Second)
defer cancel()
var query *sql.Row
switch {
case len(roomId) > 0 && isRunning == 1 && len(sid) == 0:
// for roomId + isRunning
query = db.QueryRowContext(ctx, "SELECT id, room_title, roomId, sid, joined_participants, is_running, is_recording, is_active_rtmp, webhook_url, is_breakout_room, parent_room_id, creation_time, ended FROM "+rm.app.FormatDBTable("room_info")+" WHERE roomId = ? AND is_running = 1", roomId)
case len(sid) > 0 && isRunning == 1 && len(roomId) == 0:
// for sid + isRunning
query = db.QueryRowContext(ctx, "SELECT id, room_title, roomId, sid, joined_participants, is_running, is_recording, is_active_rtmp, webhook_url, is_breakout_room, parent_room_id, creation_time, ended FROM "+rm.app.FormatDBTable("room_info")+" WHERE (sid = ? OR sid = CONCAT(?, '-', id)) AND is_running = 1", sid, sid)
case len(roomId) > 0 && len(sid) > 0 && isRunning == 1:
// for sid + roomId + isRunning
query = db.QueryRowContext(ctx, "SELECT id, room_title, roomId, sid, joined_participants, is_running, is_recording, is_active_rtmp, webhook_url, is_breakout_room, parent_room_id, creation_time, ended FROM "+rm.app.FormatDBTable("room_info")+" WHERE roomId = ? AND (sid = ? OR sid = CONCAT(?, '-', id)) AND is_running = 1", roomId, sid, sid)
default:
// for only sid
query = db.QueryRowContext(ctx, "SELECT id, room_title, roomId, sid, joined_participants, is_running, is_recording, is_active_rtmp, webhook_url, is_breakout_room, parent_room_id, creation_time, ended FROM "+rm.app.FormatDBTable("room_info")+" WHERE sid = ? OR sid = CONCAT(?, '-', id)", sid, sid)
}
var room RoomInfo
var msg string
err := query.Scan(&room.Id, &room.RoomTitle, &room.RoomId, &room.Sid, &room.JoinedParticipants, &room.IsRunning, &room.IsRecording, &room.IsActiveRTMP, &room.WebhookUrl, &room.IsBreakoutRoom, &room.ParentRoomId, &room.CreationTime, &room.Ended)
switch {
case errors.Is(err, sql.ErrNoRows):
msg = "no info found"
case err != nil:
msg = fmt.Sprintf("query error: %s", err.Error())
default:
msg = "success"
}
return &room, msg
}
// GetRoomInfoByTableId can be used to get room info by table id
// will be needed when recorder will send response
func (rm *RoomModel) GetRoomInfoByTableId(tableId int64) (*RoomInfo, string) {
db := rm.db
ctx, cancel := context.WithTimeout(rm.ctx, 3*time.Second)
defer cancel()
var query *sql.Row
query = db.QueryRowContext(ctx, "SELECT id, room_title, roomId, sid, joined_participants, is_running, is_recording, is_active_rtmp, webhook_url, is_breakout_room, parent_room_id, creation_time, ended FROM "+rm.app.FormatDBTable("room_info")+" WHERE id = ?", tableId)
var room RoomInfo
var msg string
err := query.Scan(&room.Id, &room.RoomTitle, &room.RoomId, &room.Sid, &room.JoinedParticipants, &room.IsRunning, &room.IsRecording, &room.IsActiveRTMP, &room.WebhookUrl, &room.IsBreakoutRoom, &room.ParentRoomId, &room.CreationTime, &room.Ended)
switch {
case errors.Is(err, sql.ErrNoRows):
msg = "no info found"
case err != nil:
msg = fmt.Sprintf("query error: %s", err.Error())
default:
msg = "success"
}
return &room, msg
}
func (rm *RoomModel) GetActiveRoomsInfo() ([]*plugnmeet.ActiveRoomInfo, error) {
db := rm.db
ctx, cancel := context.WithTimeout(rm.ctx, 3*time.Second)
defer cancel()
rows, err := db.QueryContext(ctx, "select room_title, roomId, sid, joined_participants, is_running, is_recording, is_active_rtmp, webhook_url, is_breakout_room, parent_room_id, creation_time from "+rm.app.FormatDBTable("room_info")+" where is_running = ?", 1)
if err != nil {
return nil, err
}
defer rows.Close()
var rooms []*plugnmeet.ActiveRoomInfo
for rows.Next() {
room := new(plugnmeet.ActiveRoomInfo)
err := rows.Scan(&room.RoomTitle, &room.RoomId, &room.Sid, &room.JoinedParticipants, &room.IsRunning, &room.IsRecording, &room.IsActiveRtmp, &room.WebhookUrl, &room.IsBreakoutRoom, &room.ParentRoomId, &room.CreationTime)
if err != nil {
log.Errorln(err)
continue
}
rooms = append(rooms, room)
}
if len(rooms) == 0 {
return nil, errors.New("no active room found")
}
return rooms, nil
}