diff --git a/web/app/assets/javascripts/recordingModel.js b/web/app/assets/javascripts/recordingModel.js index 6c7dd32a8..3355b9e30 100644 --- a/web/app/assets/javascripts/recordingModel.js +++ b/web/app/assets/javascripts/recordingModel.js @@ -463,17 +463,129 @@ return { isRecording: isRecording, recordingClientId: recordingClientId }; } + async function getCurrentRecordingState() { + var recording = null; + var recordingId = await context.jamClient.GetCurrentRecordingId(); + if (recordingId && recordingId != "") { + recording = await rest.getRecordingPromise({ id: recordingId }) + } + return { + isRecording: recording !== null, + recordingOwnerId: recording ? recording.owner.id : null, + } + } + + function handleRecordingStarted(recordingId, result, clientId) { + var success = result.success; + var reason = result.reason; + var detail = result.detail; + + // in this scenario, we don't know all the tracks of the user. + // we need to ask sessionModel to populate us with the recording data ASAP + currentRecording = rest.getRecordingPromise({ id: recordingId }) + currentRecording + .then(function (recording) { + currentRecordingId = recording.id; + currentOrLastRecordingId = recording.id; + }) + .catch(function () { + abortRecording(recordingId, 'populate-recording-info', app.clientId); + }); + + var details = { recordingId: recordingId, isRecording: false } + $self.triggerHandler('startingRecording', details); + context.RecordingActions.startingRecording(details) + currentlyRecording = true; + + details = { clientId: clientId, recordingId: recordingId, isRecording: true } + $self.triggerHandler('startedRecording', details); + context.RecordingActions.startedRecording(details) + } + + function handleRecordingStopped(recordingId, result) { + var session = context.SessionStore.getCurrentOrLastSession(); + if (!session) { + logger.debug("no session, so no recording"); + return; + } + + context.SessionStore.updateSessionInfo(session) + + getCurrentRecordingState().then(function (recordingState) { + if (recordingState.isRecording) { + // we are still recording, so don't transition to stopped + logger.debug("recording is still running, so don't transition to stopped"); + return; + } + + if (recordingId == "video") { + // comes from VideoRecordingStopped + return; + } + + logger.debug("handleRecordingStopped " + recordingId, result) + + var success = result.success; + var reason = result.reason; + var detail = result.detail; + + var details = { recordingId: recordingId, reason: reason, detail: detail, isRecording: true } + $self.triggerHandler('stoppingRecording', details); + context.RecordingActions.stoppingRecording(details) + + // the backend says the recording must be stopped. + // tell the server to stop it too + rest.stopRecording({ + id: recordingId + }) + .always(function () { + transitionToStopped(); + }) + .fail(function (jqXHR, textStatus, errorMessage) { + if (jqXHR.status == 422) { + logger.debug("recording already stopped %o", arguments); + var details = { recordingId: recordingId, reason: reason, detail: detail, isRecording: false } + $self.triggerHandler('stoppedRecording', details); + context.RecordingActions.stoppedRecording(details) + } + else if (jqXHR.status == 404) { + logger.debug("recording is already deleted %o", arguments); + var details = { recordingId: recordingId, reason: reason, detail: detail, isRecording: false } + $self.triggerHandler('stoppedRecording', details); + context.RecordingActions.stoppedRecording(details) + } + else { + var details = { recordingId: recordingId, reason: textStatus, detail: errorMessage, isRecording: false } + $self.triggerHandler('stoppedRecording', details); + context.RecordingActions.stoppedRecording(details) + } + }) + .done(function () { + var details = { recordingId: recordingId, reason: reason, detail: detail, isRecording: false } + $self.triggerHandler('stoppedRecording', details); + context.RecordingActions.stoppedRecording(details) + }) + }).catch(function (error) { + console.error("Error reading recording state:", error); + }); + + } + + this.initialize = function() { }; this.startRecording = startRecording; this.stopRecording = stopRecording; this.onServerStopRecording = onServerStopRecording; + this.onServerStartRecording = onServerStartRecording; this.isRecording = isRecording; this.reset = reset; this.stopRecordingIfNeeded = stopRecordingIfNeeded; this.currentOrLastRecordingId = function () { return currentOrLastRecordingId; }; this.getRecordingState = getRecordingState; + + this.getCurrentRecordingState = getCurrentRecordingState; context.JK.HandleRecordingStartResult = handleRecordingStartResult; context.JK.HandleRecordingStopResult = handleRecordingStopResult;