From baea858180240d968588e1c994a9fa4efd59188d Mon Sep 17 00:00:00 2001 From: "Jibon L. Costa" Date: Tue, 26 Jul 2022 11:29:47 +0600 Subject: [PATCH] reuse db row instated of showing error (#73) --- pkg/models/room.go | 18 +++++++++++++++--- pkg/models/room_auth.go | 23 +++++++++++++++-------- pkg/models/room_service.go | 1 + pkg/models/webhook.go | 2 +- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/pkg/models/room.go b/pkg/models/room.go index a9ceafd3..8c03c8b5 100644 --- a/pkg/models/room.go +++ b/pkg/models/room.go @@ -40,7 +40,7 @@ func NewRoomModel() *roomModel { } } -func (rm *roomModel) InsertRoomData(r *RoomInfo) (int64, error) { +func (rm *roomModel) InsertOrUpdateRoomData(r *RoomInfo, update bool) (int64, error) { db := rm.db ctx, cancel := context.WithTimeout(rm.ctx, 3*time.Second) defer cancel() @@ -50,12 +50,24 @@ func (rm *roomModel) InsertRoomData(r *RoomInfo) (int64, error) { return 0, err } defer tx.Rollback() - stmt, err := tx.Prepare("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) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE is_running = ?") + + 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) 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.Prepare(query) if err != nil { return 0, err } - res, err := stmt.Exec(r.RoomTitle, r.RoomId, r.Sid, r.JoinedParticipants, r.IsRunning, r.WebhookUrl, r.IsBreakoutRoom, r.ParentRoomId, r.CreationTime, 1) + lastVal := 1 + if update { + lastVal = r.Id + } + + res, err := stmt.Exec(r.RoomTitle, r.RoomId, r.Sid, r.JoinedParticipants, r.IsRunning, r.WebhookUrl, r.IsBreakoutRoom, r.ParentRoomId, r.CreationTime, lastVal) if err != nil { return 0, err } diff --git a/pkg/models/room_auth.go b/pkg/models/room_auth.go index d8f8014b..fab27811 100644 --- a/pkg/models/room_auth.go +++ b/pkg/models/room_auth.go @@ -133,15 +133,16 @@ func (am *roomAuthModel) CreateRoom(r *RoomCreateReq) (bool, string, *livekit.Ro if roomDbInfo.Id > 0 { rf, err := am.rs.LoadRoomInfoFromRedis(r.RoomId) - if err != nil { - _, err = am.rm.UpdateRoomStatus(&RoomInfo{ - RoomId: r.RoomId, - IsRunning: 0, - Ended: time.Now().Format("2006-01-02 15:04:05"), - }) + if err != nil && err.Error() != "requested room does not exist" { return false, "can't create room. try again", nil } - return true, "room already exists", rf + + if err == nil && rf.Sid == roomDbInfo.Sid { + return true, "room already exists", rf + } + + // we'll allow to create room again & use the same DB row + // we can just update the DB row. No need to create new one } // we'll disable if SharedNotePad isn't enable in config @@ -201,6 +202,7 @@ func (am *roomAuthModel) CreateRoom(r *RoomCreateReq) (bool, string, *livekit.Ro isBreakoutRoom = 1 } + updateTable := false ri := &RoomInfo{ RoomTitle: r.RoomMetadata.RoomTitle, RoomId: room.Name, @@ -213,7 +215,12 @@ func (am *roomAuthModel) CreateRoom(r *RoomCreateReq) (bool, string, *livekit.Ro ParentRoomId: r.RoomMetadata.ParentRoomId, } - _, err = am.rm.InsertRoomData(ri) + 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 } diff --git a/pkg/models/room_service.go b/pkg/models/room_service.go index 60e8fae2..472bfb5e 100644 --- a/pkg/models/room_service.go +++ b/pkg/models/room_service.go @@ -42,6 +42,7 @@ func (r *RoomService) LoadRoomInfoFromRedis(roomId string) (*livekit.Room, error if err == redis.Nil { // if you change this text then make sure // you also update: scheduler.go activeRoomChecker() + // also room_auth.go CreateRoom() err = errors.New("requested room does not exist") } return nil, err diff --git a/pkg/models/webhook.go b/pkg/models/webhook.go index d8932868..daf9942a 100644 --- a/pkg/models/webhook.go +++ b/pkg/models/webhook.go @@ -61,7 +61,7 @@ func (w *webhookEvent) roomStarted() int64 { IsRunning: 1, CreationTime: event.Room.CreationTime, } - lastId, err := w.roomModel.InsertRoomData(room) + lastId, err := w.roomModel.InsertOrUpdateRoomData(room, false) if err != nil { log.Errorln(err)