Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement editor locking so only one person can edit a recording a time #1102

Merged
merged 3 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/mock/editor/ID-dual-stream-demo/edit.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,6 @@
"id": null,
"title": null
},
"workflow_active": false
"workflow_active": false,
"locking_active": false
}
7 changes: 6 additions & 1 deletion src/main/Body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import MainContent from './MainContent';
import TheEnd from './TheEnd';
import Error from './Error';
import Landing from "./Landing";
import Lock from "./Lock";

import { css } from '@emotion/react'

Expand All @@ -27,7 +28,10 @@ const Body: React.FC = () => {
)
} else if (isEnd) {
return (
<TheEnd />
<div>
<Lock />
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lock component appears here so that any lock can be freed on discarding of edits

<TheEnd />
</div>
);
} else if (isError) {
return (
Expand All @@ -36,6 +40,7 @@ const Body: React.FC = () => {
} else {
return (
<div css={bodyStyle}>
<Lock />
<MainMenu />
<MainContent />
</div>
Expand Down
80 changes: 80 additions & 0 deletions src/main/Lock.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import React, { useEffect } from "react";
import { LuLock } from "react-icons/lu";

import { useDispatch, useSelector } from "react-redux";
import { settings } from "../config";
import { setLock, video } from "../redux/videoSlice";
import { selectIsEnd } from '../redux/endSlice'
import { setError } from "../redux/errorSlice";
import { client } from "../util/client";
import { useInterval } from "../util/utilityFunctions";
import { useBeforeunload } from 'react-beforeunload';

const Lock: React.FC = () => {
const endpoint = `${settings.opencast.url}/editor/${settings.id}/lock`

const dispatch = useDispatch();
const lockingActive = useSelector((state: { videoState: { lockingActive: video["lockingActive"] } }) => state.videoState.lockingActive);
const lockRefresh = useSelector((state: { videoState: { lockRefresh: video["lockRefresh"] } }) => state.videoState.lockRefresh);
const lockState = useSelector((state: { videoState: { lockState: video["lockState"] } }) => state.videoState.lockState);
const lock = useSelector((state: { videoState: { lock: video["lock"] } }) => state.videoState.lock);
const isEnd = useSelector(selectIsEnd)

function requestLock() {
const form = `user=${lock.user}&uuid=${lock.uuid}`;
client.post(endpoint, form, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
}
})
.then(() => dispatch(setLock(true)))
.catch((error: string) => {
dispatch(setLock(false))
dispatch(setError({
error: true,
errorDetails: error,
errorIcon: LuLock,
errorTitle: 'Video editing locked',
errorMessage: 'This video is currently being edited by another user'
}))
})
}

function releaseLock() {
if (lockingActive && lockState) {
client.delete(endpoint + '/' + lock.uuid)
.then(() => {
console.info('Lock released')
dispatch(setLock(false));
})
}
}

// Request lock
useEffect(() => {
if (lockingActive) {
requestLock()
}
}, [lockingActive])

// Refresh lock
useInterval(async () => {
requestLock()
}, lockingActive ? lockRefresh : null);

// Release lock on leaving page
useBeforeunload((_event: { preventDefault: () => void; }) => {
releaseLock()
})

// Release lock on discard
useEffect(() => {
if (isEnd) {
releaseLock()
}
}, [isEnd])

return (<></>)
}

export default Lock;
24 changes: 23 additions & 1 deletion src/redux/videoSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ export interface video {
title: string,
presenters: string[],
workflows: Workflow[],

lockingActive: boolean, // Whether locking event editing is enabled
lockRefresh: number | null, // Lock refresh period
lockState: boolean, // Whether lock has been obtained
lock: lockData
}

export interface lockData {
uuid: string,
user: string
}

export const initialState: video & httpRequestState = {
Expand All @@ -52,6 +62,11 @@ export const initialState: video & httpRequestState = {
presenters: [],
workflows: [],

lockingActive: false,
lockRefresh: null,
lockState: false,
lock: {uuid: '', user: ''},

status: 'idle',
error: undefined,
errorReason: 'unknown',
Expand Down Expand Up @@ -141,6 +156,9 @@ const videoSlice = createSlice({
const index = state.tracks.findIndex(t => t.id === action.payload)
state.tracks[index].thumbnailUri = undefined
},
setLock: (state, action: PayloadAction<video["lockState"]>) => {
state.lockState = action.payload;
},
cut: state => {
// If we're exactly between two segments, we can't split the current segment
if (state.segments[state.activeSegmentIndex].start === state.currentlyAt ||
Expand Down Expand Up @@ -224,6 +242,10 @@ const videoSlice = createSlice({
state.originalThumbnails = state.tracks.map((track: Track) => { return {id: track.id, uri: track.thumbnailUri} })

state.aspectRatios = new Array(state.videoCount)
state.lockingActive = action.payload.locking_active
state.lockRefresh = action.payload.lock_refresh
state.lock.uuid = action.payload.lock_uuid;
state.lock.user = action.payload.lock_user;
})
builder.addCase(
fetchVideoInformation.rejected, (state, action) => {
Expand Down Expand Up @@ -350,7 +372,7 @@ const setThumbnailHelper = (state: video, id: Track["id"], uri: Track["thumbnail

export const { setTrackEnabled, setIsPlaying, setIsPlayPreview, setCurrentlyAt, setCurrentlyAtInSeconds,
addSegment, setAspectRatio, setHasChanges, setWaveformImages, setThumbnails, setThumbnail, removeThumbnail,
cut, markAsDeletedOrAlive, setSelectedWorkflowIndex, mergeLeft, mergeRight, mergeAll, setPreviewTriggered,
setLock, cut, markAsDeletedOrAlive, setSelectedWorkflowIndex, mergeLeft, mergeRight, mergeAll, setPreviewTriggered,
setClickTriggered } = videoSlice.actions

// Export selectors
Expand Down
12 changes: 10 additions & 2 deletions src/util/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { settings } from '../config';

/**
* Client I stole this form a react tutorial
* Client I stole this from a react tutorial
*/
export async function client(endpoint, { body, ...customConfig } = {}) {
const headers = { 'Content-Type': 'application/json' }
Expand All @@ -29,7 +29,11 @@ export async function client(endpoint, { body, ...customConfig } = {}) {
}

if (body) {
config.body = JSON.stringify(body)
if (config.headers['Content-Type'].includes("urlencoded")) {
config.body = body
} else {
config.body = JSON.stringify(body)
}
}

let data
Expand Down Expand Up @@ -63,3 +67,7 @@ client.get = function (endpoint, customConfig = {}) {
client.post = function (endpoint, body, customConfig = {}) {
return client(endpoint, { ...customConfig, body })
}

client.delete = function (endpoint, customConfig = {}) {
return client(endpoint, { ...customConfig, method: 'DELETE' })
}
26 changes: 26 additions & 0 deletions src/util/utilityFunctions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { nanoid } from '@reduxjs/toolkit';
import { WebVTTParser, WebVTTSerializer } from 'webvtt-parser';
import { ExtendedSubtitleCue, SubtitleCue } from '../types';
import { useEffect, useRef } from 'react';

export const roundToDecimalPlace = (num: number, decimalPlace: number) => {
const decimalFactor = Math.pow(10, decimalPlace)
Expand Down Expand Up @@ -180,3 +181,28 @@ export function languageCodeToName(lang: string | undefined): string | undefined
return undefined
}
}

// Runs a callback every delay milliseconds
// Pass delay = null to stop
// Based off: https://overreacted.io/making-setinterval-declarative-with-react-hooks/
type IntervalFunction = () => (unknown | void)
export function useInterval(callback: IntervalFunction, delay: number | null) {

const savedCallback = useRef<IntervalFunction | null>(null)

useEffect(() => {
savedCallback.current = callback
})

useEffect(() => {
function tick() {
if (savedCallback.current !== null) {
savedCallback.current()
}
}
if (delay !== null) {
const id = setInterval(tick, delay)
return () => { clearInterval(id) }
}
}, [callback, delay]);
}
Loading