X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fvideo%2Fvideo-change-ownership.ts;h=ac0ab7e8b9088c1a3158b4696cb5c2bc7dd6c3a0;hb=4c9e9d2ee9899ba48b86eda18d44638a78587ac5;hp=171d4574d8dbf218061cbc576b8286c7ad8e85f4;hpb=3acc50844047a37698f0618fa235c138e386a053;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video/video-change-ownership.ts b/server/models/video/video-change-ownership.ts index 171d4574d..ac0ab7e8b 100644 --- a/server/models/video/video-change-ownership.ts +++ b/server/models/video/video-change-ownership.ts @@ -1,30 +1,32 @@ 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' +import { MVideoChangeOwnershipFormattable, MVideoChangeOwnershipFull } from '@server/types/models/video/video-change-ownership' +import * as Bluebird from 'bluebird' 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 +37,19 @@ 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_WEBTORRENT_FILES, + VideoScopeNames.WITH_STREAMING_PLAYLISTS, + VideoScopeNames.WITH_ACCOUNT_DETAILS + ]), + required: true } ] } @@ -105,26 +113,24 @@ 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) + static load (id: number): Bluebird { + return VideoChangeOwnershipModel.scope([ ScopeNames.WITH_ACCOUNTS, ScopeNames.WITH_VIDEO ]) + .findByPk(id) } - toFormattedJSON (): VideoChangeOwnership { + toFormattedJSON (this: MVideoChangeOwnershipFormattable): VideoChangeOwnership { return { id: this.id, status: this.status, initiatorAccount: this.Initiator.toFormattedJSON(), nextOwnerAccount: this.NextOwner.toFormattedJSON(), - video: { - id: this.Video.id, - uuid: this.Video.uuid, - url: this.Video.url, - name: this.Video.name - }, + video: this.Video.toFormattedJSON(), createdAt: this.createdAt } }