X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fcontrollers%2Fapi%2Fvideos%2Fownership.ts;h=a85d7c30b692fdc5f6c4b7ece9e87dbb29f2b7a4;hb=c00100b607ab97dfea690880434657b7ee11466d;hp=fc42f5fff16d748cc9557cc1dd80b67e264682ce;hpb=74d63469355bad731cdd32defdc85913df3cbd5c;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/controllers/api/videos/ownership.ts b/server/controllers/api/videos/ownership.ts index fc42f5fff..a85d7c30b 100644 --- a/server/controllers/api/videos/ownership.ts +++ b/server/controllers/api/videos/ownership.ts @@ -1,6 +1,6 @@ import * as express from 'express' import { logger } from '../../../helpers/logger' -import { sequelizeTypescript } from '../../../initializers' +import { sequelizeTypescript } from '../../../initializers/database' import { asyncMiddleware, asyncRetryTransactionMiddleware, @@ -11,12 +11,15 @@ import { videosChangeOwnershipValidator, videosTerminateChangeOwnershipValidator } from '../../../middlewares' -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, VideoState } from '../../../../shared/models/videos' import { VideoChannelModel } from '../../../models/video/video-channel' import { getFormattedObjects } from '../../../helpers/utils' +import { changeVideoChannelShare } from '../../../lib/activitypub/share' +import { sendUpdateVideo } from '../../../lib/activitypub/send' +import { VideoModel } from '../../../models/video/video' +import { MVideoFullLight } from '@server/types/models' +import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' const ownershipVideoRouter = express.Router() @@ -55,34 +58,39 @@ 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 nextOwner = res.locals.nextOwner as AccountModel + const videoInstance = res.locals.videoAll + const initiatorAccountId = res.locals.oauth.token.User.Account.id + const nextOwner = res.locals.nextOwner - 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(HttpStatusCode.NO_CONTENT_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.Account.id + const resultList = await VideoChangeOwnershipModel.listForApi( - currentAccount.id, + currentAccountId, req.query.start || 0, req.query.count || 10, req.query.sort || 'createdAt' @@ -93,25 +101,38 @@ async function listVideoOwnership (req: express.Request, res: express.Response) async function acceptOwnership (req: express.Request, res: express.Response) { return sequelizeTypescript.transaction(async t => { - const videoChangeOwnership = res.locals.videoChangeOwnership as VideoChangeOwnershipModel - const targetVideo = videoChangeOwnership.Video - const channel = res.locals.videoChannel as VideoChannelModel + const videoChangeOwnership = res.locals.videoChangeOwnership + const channel = res.locals.videoChannel + + // We need more attributes for federation + const targetVideo = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoChangeOwnership.Video.id) - targetVideo.set('channelId', channel.id) + const oldVideoChannel = await VideoChannelModel.loadAndPopulateAccount(targetVideo.channelId) - await targetVideo.save() - videoChangeOwnership.set('status', VideoChangeOwnershipStatus.ACCEPTED) - await videoChangeOwnership.save() + targetVideo.channelId = channel.id - return res.sendStatus(204) + const targetVideoUpdated = await targetVideo.save({ transaction: t }) as MVideoFullLight + targetVideoUpdated.VideoChannel = channel + + if (targetVideoUpdated.hasPrivacyForFederation() && targetVideoUpdated.state === VideoState.PUBLISHED) { + await changeVideoChannelShare(targetVideoUpdated, oldVideoChannel, t) + await sendUpdateVideo(targetVideoUpdated, t, oldVideoChannel.Account.Actor) + } + + videoChangeOwnership.status = VideoChangeOwnershipStatus.ACCEPTED + await videoChangeOwnership.save({ transaction: t }) + + return res.sendStatus(HttpStatusCode.NO_CONTENT_204) }) } 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() - return res.sendStatus(204) + const videoChangeOwnership = res.locals.videoChangeOwnership + + videoChangeOwnership.status = VideoChangeOwnershipStatus.REFUSED + await videoChangeOwnership.save({ transaction: t }) + + return res.sendStatus(HttpStatusCode.NO_CONTENT_204) }) }