Skip to content

Commit

Permalink
Start recordings when bot posts status update
Browse files Browse the repository at this point in the history
  • Loading branch information
streamer45 committed Oct 4, 2023
1 parent ea6fac7 commit 1b8a805
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
21 changes: 18 additions & 3 deletions server/bot_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ func (p *Plugin) handleBotPostJobsStatus(w http.ResponseWriter, r *http.Request,
return
}

if status.JobType == public.JobTypeRecording && status.Status == public.JobStatusTypeFailed {
if status.JobType == public.JobTypeRecording {
if state.Call.Recording == nil {
res.Err = "no recording ongoing"
res.Code = http.StatusBadRequest
Expand All @@ -292,8 +292,23 @@ func (p *Plugin) handleBotPostJobsStatus(w http.ResponseWriter, r *http.Request,
return
}

state.Call.Recording.EndAt = time.Now().UnixMilli()
state.Call.Recording.Err = status.Error
if status.Status == public.JobStatusTypeFailed {
p.LogDebug("recording has failed", "jobID", jobID)
state.Call.Recording.EndAt = time.Now().UnixMilli()
state.Call.Recording.Err = status.Error
} else if status.Status == public.JobStatusTypeStarted {
if state.Call.Recording.StartAt > 0 {
res.Err = "recording has already started"
res.Code = http.StatusBadRequest
return
}
p.LogDebug("recording has started", "jobID", jobID)
state.Call.Recording.StartAt = time.Now().UnixMilli()
} else {
res.Err = "unsupported status type"
res.Code = http.StatusBadRequest
return
}

if err := p.kvSetChannelState(callID, state); err != nil {
res.Err = fmt.Errorf("failed to set channel state: %w", err).Error()
Expand Down
9 changes: 5 additions & 4 deletions server/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,16 @@ func (p *Plugin) addUserSession(state *channelState, userID, connID, channelID,
return nil, fmt.Errorf("user cannot join because of limits")
}

// When the bot joins the call it means the recording has started.
// When the bot joins the call it means the recording is starting. The actual
// start time is when the bot sends the status update through the API.
if userID == p.getBotID() {
if state.Call.Recording != nil && state.Call.Recording.StartAt == 0 {
if state.Call.Recording != nil && state.Call.Recording.StartAt == 0 && state.Call.Recording.BotConnID == "" {
if state.Call.Recording.ID != jobID {
return nil, fmt.Errorf("invalid job ID for recording")
}
state.Call.Recording.StartAt = time.Now().UnixMilli()
p.LogDebug("bot joined, recording is starting", "jobID", jobID)
state.Call.Recording.BotConnID = connID
} else if state.Call.Recording == nil || state.Call.Recording.StartAt > 0 {
} else if state.Call.Recording == nil || state.Call.Recording.StartAt > 0 || state.Call.Recording.BotConnID != "" {
// In this case we should fail to prevent the bot from recording
// without consent.
return nil, fmt.Errorf("recording not in progress or already started")
Expand Down

0 comments on commit 1b8a805

Please sign in to comment.