Skip to content

Commit

Permalink
reuse db row instated of showing error (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
jibon57 authored Jul 26, 2022
1 parent 017734e commit baea858
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
18 changes: 15 additions & 3 deletions pkg/models/room.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
}
Expand Down
23 changes: 15 additions & 8 deletions pkg/models/room_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions pkg/models/room_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/models/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit baea858

Please sign in to comment.