1 import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
2 import { AccountModel } from '../account/account'
3 import { ScopeNames as VideoScopeNames, VideoModel } from './video'
4 import { VideoChangeOwnership, VideoChangeOwnershipStatus } from '../../../shared/models/videos'
5 import { getSort } from '../utils'
6 import { MVideoChangeOwnershipFormattable, MVideoChangeOwnershipFull } from '@server/typings/models/video/video-change-ownership'
7 import * as Bluebird from 'bluebird'
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
56 export class VideoChangeOwnershipModel extends Model<VideoChangeOwnershipModel> {
65 status: VideoChangeOwnershipStatus
67 @ForeignKey(() => AccountModel)
69 initiatorAccountId: number
71 @BelongsTo(() => AccountModel, {
73 name: 'initiatorAccountId',
78 Initiator: AccountModel
80 @ForeignKey(() => AccountModel)
82 nextOwnerAccountId: number
84 @BelongsTo(() => AccountModel, {
86 name: 'nextOwnerAccountId',
91 NextOwner: AccountModel
93 @ForeignKey(() => VideoModel)
97 @BelongsTo(() => VideoModel, {
105 static listForApi (nextOwnerId: number, start: number, count: number, sort: string) {
109 order: getSort(sort),
111 nextOwnerAccountId: nextOwnerId
116 VideoChangeOwnershipModel.scope(ScopeNames.WITH_ACCOUNTS).count(query),
117 VideoChangeOwnershipModel.scope([ ScopeNames.WITH_ACCOUNTS, ScopeNames.WITH_VIDEO ]).findAll<MVideoChangeOwnershipFull>(query)
118 ]).then(([ count, rows ]) => ({ total: count, data: rows }))
121 static load (id: number): Bluebird<MVideoChangeOwnershipFull> {
122 return VideoChangeOwnershipModel.scope([ ScopeNames.WITH_ACCOUNTS, ScopeNames.WITH_VIDEO ])
126 toFormattedJSON (this: MVideoChangeOwnershipFormattable): VideoChangeOwnership {
130 initiatorAccount: this.Initiator.toFormattedJSON(),
131 nextOwnerAccount: this.NextOwner.toFormattedJSON(),
134 uuid: this.Video.uuid,
136 name: this.Video.name
138 createdAt: this.createdAt