1 import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
2 import { MVideoChangeOwnershipFormattable, MVideoChangeOwnershipFull } from '@server/types/models/video/video-change-ownership'
3 import { AttributesOnly } from '@shared/core-utils'
4 import { VideoChangeOwnership, VideoChangeOwnershipStatus } from '../../../shared/models/videos'
5 import { AccountModel } from '../account/account'
6 import { getSort } from '../utils'
7 import { ScopeNames as VideoScopeNames, VideoModel } from './video'
10 WITH_ACCOUNTS = 'WITH_ACCOUNTS',
11 WITH_VIDEO = 'WITH_VIDEO'
15 tableName: 'videoChangeOwnership',
21 fields: [ 'initiatorAccountId' ]
24 fields: [ 'nextOwnerAccountId' ]
29 [ScopeNames.WITH_ACCOUNTS]: {
43 [ScopeNames.WITH_VIDEO]: {
46 model: VideoModel.scope([
47 VideoScopeNames.WITH_THUMBNAILS,
48 VideoScopeNames.WITH_WEBTORRENT_FILES,
49 VideoScopeNames.WITH_STREAMING_PLAYLISTS,
50 VideoScopeNames.WITH_ACCOUNT_DETAILS
57 export class VideoChangeOwnershipModel extends Model<Partial<AttributesOnly<VideoChangeOwnershipModel>>> {
66 status: VideoChangeOwnershipStatus
68 @ForeignKey(() => AccountModel)
70 initiatorAccountId: number
72 @BelongsTo(() => AccountModel, {
74 name: 'initiatorAccountId',
79 Initiator: AccountModel
81 @ForeignKey(() => AccountModel)
83 nextOwnerAccountId: number
85 @BelongsTo(() => AccountModel, {
87 name: 'nextOwnerAccountId',
92 NextOwner: AccountModel
94 @ForeignKey(() => VideoModel)
98 @BelongsTo(() => VideoModel, {
106 static listForApi (nextOwnerId: number, start: number, count: number, sort: string) {
110 order: getSort(sort),
112 nextOwnerAccountId: nextOwnerId
117 VideoChangeOwnershipModel.scope(ScopeNames.WITH_ACCOUNTS).count(query),
118 VideoChangeOwnershipModel.scope([ ScopeNames.WITH_ACCOUNTS, ScopeNames.WITH_VIDEO ]).findAll<MVideoChangeOwnershipFull>(query)
119 ]).then(([ count, rows ]) => ({ total: count, data: rows }))
122 static load (id: number): Promise<MVideoChangeOwnershipFull> {
123 return VideoChangeOwnershipModel.scope([ ScopeNames.WITH_ACCOUNTS, ScopeNames.WITH_VIDEO ])
127 toFormattedJSON (this: MVideoChangeOwnershipFormattable): VideoChangeOwnership {
131 initiatorAccount: this.Initiator.toFormattedJSON(),
132 nextOwnerAccount: this.NextOwner.toFormattedJSON(),
133 video: this.Video.toFormattedJSON(),
134 createdAt: this.createdAt