X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fvideo%2Fvideo-change-ownership.ts;h=2db4b523a654b6fda12274fed3f54dfe2325a7ff;hb=2f63f629add5d24f8c01f309c7cae43b667b0c2a;hp=48c07728f66b9b87af81be3fdf5dbc2d555e04bf;hpb=5cf84858d49f4231cc4efec5e3132f17f65f6cf6;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video/video-change-ownership.ts b/server/models/video/video-change-ownership.ts index 48c07728f..2db4b523a 100644 --- a/server/models/video/video-change-ownership.ts +++ b/server/models/video/video-change-ownership.ts @@ -1,52 +1,60 @@ import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' -import { AccountModel } from '../account/account' -import { VideoModel } from './video' +import { MVideoChangeOwnershipFormattable, MVideoChangeOwnershipFull } from '@server/types/models/video/video-change-ownership' +import { AttributesOnly } from '@shared/typescript-utils' import { VideoChangeOwnership, VideoChangeOwnershipStatus } from '../../../shared/models/videos' -import { getSort } from '../utils' -import { VideoFileModel } from './video-file' +import { AccountModel } from '../account/account' +import { getSort } from '../shared' +import { ScopeNames as VideoScopeNames, VideoModel } from './video' 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]: { +@Scopes(() => ({ + [ScopeNames.WITH_ACCOUNTS]: { include: [ { - model: () => AccountModel, + model: AccountModel, as: 'Initiator', required: true }, { - model: () => AccountModel, + 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 } ] } -}) -export class VideoChangeOwnershipModel extends Model { +})) +export class VideoChangeOwnershipModel extends Model>> { @CreatedAt createdAt: Date @@ -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).findById(id) + static load (id: number): Promise { + 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 } }