From b876eaf11a1ed9683664d94767ca684ba5b77753 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 24 Apr 2019 09:44:36 +0200 Subject: [PATCH] Fix ownership changes --- server/controllers/api/videos/ownership.ts | 12 ++++--- server/initializers/database.ts | 2 +- server/models/video/video-change-ownership.ts | 36 ++++++++++--------- 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/server/controllers/api/videos/ownership.ts b/server/controllers/api/videos/ownership.ts index bc247c4ee..5272c1385 100644 --- a/server/controllers/api/videos/ownership.ts +++ b/server/controllers/api/videos/ownership.ts @@ -17,6 +17,7 @@ import { VideoChannelModel } from '../../../models/video/video-channel' import { getFormattedObjects } from '../../../helpers/utils' import { changeVideoChannelShare } from '../../../lib/activitypub' import { sendUpdateVideo } from '../../../lib/activitypub/send' +import { VideoModel } from '../../../models/video/video' const ownershipVideoRouter = express.Router() @@ -97,12 +98,15 @@ 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 - const targetVideo = videoChangeOwnership.Video const channel = res.locals.videoChannel + // We need more attributes for federation + const targetVideo = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoChangeOwnership.Video.id) + const oldVideoChannel = await VideoChannelModel.loadByIdAndPopulateAccount(targetVideo.channelId) - targetVideo.set('channelId', channel.id) + targetVideo.channelId = channel.id + const targetVideoUpdated = await targetVideo.save({ transaction: t }) targetVideoUpdated.VideoChannel = channel @@ -111,7 +115,7 @@ async function acceptOwnership (req: express.Request, res: express.Response) { 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) @@ -122,7 +126,7 @@ async 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) diff --git a/server/initializers/database.ts b/server/initializers/database.ts index d9a265e7a..142063a99 100644 --- a/server/initializers/database.ts +++ b/server/initializers/database.ts @@ -86,6 +86,7 @@ async function initDatabaseModels (silent: boolean) { AccountVideoRateModel, UserModel, VideoAbuseModel, + VideoModel, VideoChangeOwnershipModel, VideoChannelModel, VideoShareModel, @@ -93,7 +94,6 @@ async function initDatabaseModels (silent: boolean) { VideoCaptionModel, VideoBlacklistModel, VideoTagModel, - VideoModel, VideoCommentModel, ScheduleVideoUpdateModel, VideoImportModel, diff --git a/server/models/video/video-change-ownership.ts b/server/models/video/video-change-ownership.ts index 171d4574d..b545a2f8c 100644 --- a/server/models/video/video-change-ownership.ts +++ b/server/models/video/video-change-ownership.ts @@ -1,30 +1,30 @@ import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' import { AccountModel } from '../account/account' -import { VideoModel } from './video' +import { ScopeNames as VideoScopeNames, VideoModel } from './video' import { VideoChangeOwnership, VideoChangeOwnershipStatus } from '../../../shared/models/videos' import { getSort } from '../utils' -import { VideoFileModel } from './video-file' enum ScopeNames { - FULL = 'FULL' + WITH_ACCOUNTS = 'WITH_ACCOUNTS', + WITH_VIDEO = 'WITH_VIDEO' } @Table({ tableName: 'videoChangeOwnership', indexes: [ { - fields: ['videoId'] + fields: [ 'videoId' ] }, { - fields: ['initiatorAccountId'] + fields: [ 'initiatorAccountId' ] }, { - fields: ['nextOwnerAccountId'] + fields: [ 'nextOwnerAccountId' ] } ] }) @Scopes(() => ({ - [ScopeNames.FULL]: { + [ScopeNames.WITH_ACCOUNTS]: { include: [ { model: AccountModel, @@ -35,13 +35,14 @@ enum ScopeNames { model: AccountModel, as: 'NextOwner', required: true - }, + } + ] + }, + [ScopeNames.WITH_VIDEO]: { + include: [ { - model: VideoModel, - required: true, - include: [ - { model: VideoFileModel } - ] + model: VideoModel.scope([ VideoScopeNames.WITH_THUMBNAILS, VideoScopeNames.WITH_FILES ]), + required: true } ] } @@ -105,12 +106,15 @@ export class VideoChangeOwnershipModel extends Model } } - return VideoChangeOwnershipModel.scope(ScopeNames.FULL).findAndCountAll(query) - .then(({ rows, count }) => ({ total: count, data: rows })) + return Promise.all([ + VideoChangeOwnershipModel.scope(ScopeNames.WITH_ACCOUNTS).count(query), + VideoChangeOwnershipModel.scope([ ScopeNames.WITH_ACCOUNTS, ScopeNames.WITH_VIDEO ]).findAll(query) + ]).then(([ count, rows ]) => ({ total: count, data: rows })) } static load (id: number) { - return VideoChangeOwnershipModel.scope(ScopeNames.FULL).findByPk(id) + return VideoChangeOwnershipModel.scope([ ScopeNames.WITH_ACCOUNTS, ScopeNames.WITH_VIDEO ]) + .findByPk(id) } toFormattedJSON (): VideoChangeOwnership { -- 2.25.1