]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/videos/live.ts
Feature/Add replay privacy (#5692)
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / live.ts
index ec4c073b58f6e9beb3e0ecdaaa13ffa481320f75..de047d4ec3c803658aacc2a326cb6d557433b832 100644 (file)
@@ -16,7 +16,7 @@ import {
 } from '@server/middlewares/validators/videos/video-live'
 import { VideoLiveModel } from '@server/models/video/video-live'
 import { VideoLiveSessionModel } from '@server/models/video/video-live-session'
-import { MVideoDetails, MVideoFullLight } from '@server/types/models'
+import { MVideoDetails, MVideoFullLight, MVideoLive } from '@server/types/models'
 import { buildUUID, uuidToShort } from '@shared/extra-utils'
 import { HttpStatusCode, LiveVideoCreate, LiveVideoLatencyMode, LiveVideoUpdate, UserRight, VideoState } from '@shared/models'
 import { logger } from '../../../helpers/logger'
@@ -24,6 +24,7 @@ import { sequelizeTypescript } from '../../../initializers/database'
 import { updateVideoMiniatureFromExisting } from '../../../lib/thumbnail'
 import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, optionalAuthenticate } from '../../../middlewares'
 import { VideoModel } from '../../../models/video/video'
+import { VideoLiveReplaySettingModel } from '@server/models/video/video-live-replay-setting'
 
 const liveRouter = express.Router()
 
@@ -105,7 +106,10 @@ async function updateLiveVideo (req: express.Request, res: express.Response) {
   const video = res.locals.videoAll
   const videoLive = res.locals.videoLive
 
-  if (exists(body.saveReplay)) videoLive.saveReplay = body.saveReplay
+  const newReplaySettingModel = await updateReplaySettings(videoLive, body)
+  if (newReplaySettingModel) videoLive.replaySettingId = newReplaySettingModel.id
+  else videoLive.replaySettingId = null
+
   if (exists(body.permanentLive)) videoLive.permanentLive = body.permanentLive
   if (exists(body.latencyMode)) videoLive.latencyMode = body.latencyMode
 
@@ -116,6 +120,27 @@ async function updateLiveVideo (req: express.Request, res: express.Response) {
   return res.status(HttpStatusCode.NO_CONTENT_204).end()
 }
 
+async function updateReplaySettings (videoLive: MVideoLive, body: LiveVideoUpdate) {
+  if (exists(body.saveReplay)) videoLive.saveReplay = body.saveReplay
+
+  // The live replay is not saved anymore, destroy the old model if it existed
+  if (!videoLive.saveReplay) {
+    if (videoLive.replaySettingId) {
+      await VideoLiveReplaySettingModel.removeSettings(videoLive.replaySettingId)
+    }
+
+    return undefined
+  }
+
+  const settingModel = videoLive.replaySettingId
+    ? await VideoLiveReplaySettingModel.load(videoLive.replaySettingId)
+    : new VideoLiveReplaySettingModel()
+
+  if (exists(body.replaySettings.privacy)) settingModel.privacy = body.replaySettings.privacy
+
+  return settingModel.save()
+}
+
 async function addLiveVideo (req: express.Request, res: express.Response) {
   const videoInfo: LiveVideoCreate = req.body
 
@@ -161,6 +186,15 @@ async function addLiveVideo (req: express.Request, res: express.Response) {
     // Do not forget to add video channel information to the created video
     videoCreated.VideoChannel = res.locals.videoChannel
 
+    if (videoLive.saveReplay) {
+      const replaySettings = new VideoLiveReplaySettingModel({
+        privacy: videoInfo.replaySettings.privacy
+      })
+      await replaySettings.save(sequelizeOptions)
+
+      videoLive.replaySettingId = replaySettings.id
+    }
+
     videoLive.videoId = videoCreated.id
     videoCreated.VideoLive = await videoLive.save(sequelizeOptions)