]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/videos/ownership.ts
Check activities host
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / ownership.ts
index fc42f5fff16d748cc9557cc1dd80b67e264682ce..5ea7d7c6a3d72428be0f968581bfd9ef710eff8c 100644 (file)
@@ -14,9 +14,12 @@ import {
 import { AccountModel } from '../../../models/account/account'
 import { VideoModel } from '../../../models/video/video'
 import { VideoChangeOwnershipModel } from '../../../models/video/video-change-ownership'
-import { VideoChangeOwnershipStatus } from '../../../../shared/models/videos'
+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'
+import { UserModel } from '../../../models/account/user'
 
 const ownershipVideoRouter = express.Router()
 
@@ -56,33 +59,36 @@ export {
 
 async function giveVideoOwnership (req: express.Request, res: express.Response) {
   const videoInstance = res.locals.video as VideoModel
-  const initiatorAccount = res.locals.oauth.token.User.Account as AccountModel
+  const initiatorAccountId = (res.locals.oauth.token.User as UserModel).Account.id
   const nextOwner = res.locals.nextOwner as AccountModel
 
-  await sequelizeTypescript.transaction(async t => {
-    await VideoChangeOwnershipModel.findOrCreate({
+  await sequelizeTypescript.transaction(t => {
+    return VideoChangeOwnershipModel.findOrCreate({
       where: {
-        initiatorAccountId: initiatorAccount.id,
+        initiatorAccountId,
         nextOwnerAccountId: nextOwner.id,
         videoId: videoInstance.id,
         status: VideoChangeOwnershipStatus.WAITING
       },
       defaults: {
-        initiatorAccountId: initiatorAccount.id,
+        initiatorAccountId,
         nextOwnerAccountId: nextOwner.id,
         videoId: videoInstance.id,
         status: VideoChangeOwnershipStatus.WAITING
-      }
+      },
+      transaction: t
     })
-    logger.info('Ownership change for video %s created.', videoInstance.name)
-    return res.type('json').status(204).end()
   })
+
+  logger.info('Ownership change for video %s created.', videoInstance.name)
+  return res.type('json').status(204).end()
 }
 
 async function listVideoOwnership (req: express.Request, res: express.Response) {
-  const currentAccount = res.locals.oauth.token.User.Account as AccountModel
+  const currentAccountId = (res.locals.oauth.token.User as UserModel).Account.id
+
   const resultList = await VideoChangeOwnershipModel.listForApi(
-    currentAccount.id,
+    currentAccountId,
     req.query.start || 0,
     req.query.count || 10,
     req.query.sort || 'createdAt'
@@ -97,11 +103,19 @@ async function acceptOwnership (req: express.Request, res: express.Response) {
     const targetVideo = videoChangeOwnership.Video
     const channel = res.locals.videoChannel as VideoChannelModel
 
+    const oldVideoChannel = await VideoChannelModel.loadByIdAndPopulateAccount(targetVideo.channelId)
+
     targetVideo.set('channelId', channel.id)
+    const targetVideoUpdated = await targetVideo.save({ transaction: t })
+    targetVideoUpdated.VideoChannel = channel
+
+    if (targetVideoUpdated.privacy !== VideoPrivacy.PRIVATE && targetVideoUpdated.state === VideoState.PUBLISHED) {
+      await changeVideoChannelShare(targetVideoUpdated, oldVideoChannel, t)
+      await sendUpdateVideo(targetVideoUpdated, t, oldVideoChannel.Account.Actor)
+    }
 
-    await targetVideo.save()
     videoChangeOwnership.set('status', VideoChangeOwnershipStatus.ACCEPTED)
-    await videoChangeOwnership.save()
+    await videoChangeOwnership.save({ transaction: t })
 
     return res.sendStatus(204)
   })
@@ -110,8 +124,10 @@ async function acceptOwnership (req: express.Request, res: express.Response) {
 async function refuseOwnership (req: express.Request, res: express.Response) {
   return sequelizeTypescript.transaction(async t => {
     const videoChangeOwnership = res.locals.videoChangeOwnership as VideoChangeOwnershipModel
+
     videoChangeOwnership.set('status', VideoChangeOwnershipStatus.REFUSED)
-    await videoChangeOwnership.save()
+    await videoChangeOwnership.save({ transaction: t })
+
     return res.sendStatus(204)
   })
 }