]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/videos/ownership.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / ownership.ts
index bc247c4eeb418509d681857c4864e5082bef44a1..88355b289501e3a7434958d4c449511d0a420128 100644 (file)
@@ -1,6 +1,12 @@
-import * as express from 'express'
+import express from 'express'
+import { MVideoFullLight } from '@server/types/models'
+import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
+import { VideoChangeOwnershipStatus, VideoState } from '../../../../shared/models/videos'
 import { logger } from '../../../helpers/logger'
-import { sequelizeTypescript } from '../../../initializers'
+import { getFormattedObjects } from '../../../helpers/utils'
+import { sequelizeTypescript } from '../../../initializers/database'
+import { sendUpdateVideo } from '../../../lib/activitypub/send'
+import { changeVideoChannelShare } from '../../../lib/activitypub/share'
 import {
   asyncMiddleware,
   asyncRetryTransactionMiddleware,
@@ -11,12 +17,9 @@ import {
   videosChangeOwnershipValidator,
   videosTerminateChangeOwnershipValidator
 } from '../../../middlewares'
+import { VideoModel } from '../../../models/video/video'
 import { VideoChangeOwnershipModel } from '../../../models/video/video-change-ownership'
-import { VideoChangeOwnershipStatus, VideoPrivacy, VideoState } from '../../../../shared/models/videos'
 import { VideoChannelModel } from '../../../models/video/video-channel'
-import { getFormattedObjects } from '../../../helpers/utils'
-import { changeVideoChannelShare } from '../../../lib/activitypub'
-import { sendUpdateVideo } from '../../../lib/activitypub/send'
 
 const ownershipVideoRouter = express.Router()
 
@@ -55,7 +58,7 @@ export {
 // ---------------------------------------------------------------------------
 
 async function giveVideoOwnership (req: express.Request, res: express.Response) {
-  const videoInstance = res.locals.video
+  const videoInstance = res.locals.videoAll
   const initiatorAccountId = res.locals.oauth.token.User.Account.id
   const nextOwner = res.locals.nextOwner
 
@@ -78,7 +81,9 @@ async function giveVideoOwnership (req: express.Request, res: express.Response)
   })
 
   logger.info('Ownership change for video %s created.', videoInstance.name)
-  return res.type('json').status(204).end()
+  return res.type('json')
+            .status(HttpStatusCode.NO_CONTENT_204)
+            .end()
 }
 
 async function listVideoOwnership (req: express.Request, res: express.Response) {
@@ -94,37 +99,40 @@ async function listVideoOwnership (req: express.Request, res: express.Response)
   return res.json(getFormattedObjects(resultList.data, resultList.total))
 }
 
-async function acceptOwnership (req: express.Request, res: express.Response) {
+function acceptOwnership (req: express.Request, res: express.Response) {
   return sequelizeTypescript.transaction(async t => {
     const videoChangeOwnership = res.locals.videoChangeOwnership
-    const targetVideo = videoChangeOwnership.Video
     const channel = res.locals.videoChannel
 
-    const oldVideoChannel = await VideoChannelModel.loadByIdAndPopulateAccount(targetVideo.channelId)
+    // We need more attributes for federation
+    const targetVideo = await VideoModel.loadFull(videoChangeOwnership.Video.id, t)
+
+    const oldVideoChannel = await VideoChannelModel.loadAndPopulateAccount(targetVideo.channelId, t)
+
+    targetVideo.channelId = channel.id
 
-    targetVideo.set('channelId', channel.id)
-    const targetVideoUpdated = await targetVideo.save({ transaction: t })
+    const targetVideoUpdated = await targetVideo.save({ transaction: t }) as MVideoFullLight
     targetVideoUpdated.VideoChannel = channel
 
-    if (targetVideoUpdated.privacy !== VideoPrivacy.PRIVATE && targetVideoUpdated.state === VideoState.PUBLISHED) {
+    if (targetVideoUpdated.hasPrivacyForFederation() && targetVideoUpdated.state === VideoState.PUBLISHED) {
       await changeVideoChannelShare(targetVideoUpdated, oldVideoChannel, t)
       await sendUpdateVideo(targetVideoUpdated, t, oldVideoChannel.Account.Actor)
     }
 
-    videoChangeOwnership.set('status', VideoChangeOwnershipStatus.ACCEPTED)
+    videoChangeOwnership.status = VideoChangeOwnershipStatus.ACCEPTED
     await videoChangeOwnership.save({ transaction: t })
 
-    return res.sendStatus(204)
+    return res.status(HttpStatusCode.NO_CONTENT_204).end()
   })
 }
 
-async function refuseOwnership (req: express.Request, res: express.Response) {
+function refuseOwnership (req: express.Request, res: express.Response) {
   return sequelizeTypescript.transaction(async t => {
     const videoChangeOwnership = res.locals.videoChangeOwnership
 
-    videoChangeOwnership.set('status', VideoChangeOwnershipStatus.REFUSED)
+    videoChangeOwnership.status = VideoChangeOwnershipStatus.REFUSED
     await videoChangeOwnership.save({ transaction: t })
 
-    return res.sendStatus(204)
+    return res.status(HttpStatusCode.NO_CONTENT_204).end()
   })
 }