3259b6c0208d6b7b63d8739d3668c04257a99d8d
[oweals/peertube.git] / server / models / video / video-change-ownership.ts
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'
8
9 enum ScopeNames {
10   WITH_ACCOUNTS = 'WITH_ACCOUNTS',
11   WITH_VIDEO = 'WITH_VIDEO'
12 }
13
14 @Table({
15   tableName: 'videoChangeOwnership',
16   indexes: [
17     {
18       fields: [ 'videoId' ]
19     },
20     {
21       fields: [ 'initiatorAccountId' ]
22     },
23     {
24       fields: [ 'nextOwnerAccountId' ]
25     }
26   ]
27 })
28 @Scopes(() => ({
29   [ScopeNames.WITH_ACCOUNTS]: {
30     include: [
31       {
32         model: AccountModel,
33         as: 'Initiator',
34         required: true
35       },
36       {
37         model: AccountModel,
38         as: 'NextOwner',
39         required: true
40       }
41     ]
42   },
43   [ScopeNames.WITH_VIDEO]: {
44     include: [
45       {
46         model: VideoModel.scope([
47           VideoScopeNames.WITH_THUMBNAILS,
48           VideoScopeNames.WITH_WEBTORRENT_FILES,
49           VideoScopeNames.WITH_STREAMING_PLAYLISTS
50         ]),
51         required: true
52       }
53     ]
54   }
55 }))
56 export class VideoChangeOwnershipModel extends Model<VideoChangeOwnershipModel> {
57   @CreatedAt
58   createdAt: Date
59
60   @UpdatedAt
61   updatedAt: Date
62
63   @AllowNull(false)
64   @Column
65   status: VideoChangeOwnershipStatus
66
67   @ForeignKey(() => AccountModel)
68   @Column
69   initiatorAccountId: number
70
71   @BelongsTo(() => AccountModel, {
72     foreignKey: {
73       name: 'initiatorAccountId',
74       allowNull: false
75     },
76     onDelete: 'cascade'
77   })
78   Initiator: AccountModel
79
80   @ForeignKey(() => AccountModel)
81   @Column
82   nextOwnerAccountId: number
83
84   @BelongsTo(() => AccountModel, {
85     foreignKey: {
86       name: 'nextOwnerAccountId',
87       allowNull: false
88     },
89     onDelete: 'cascade'
90   })
91   NextOwner: AccountModel
92
93   @ForeignKey(() => VideoModel)
94   @Column
95   videoId: number
96
97   @BelongsTo(() => VideoModel, {
98     foreignKey: {
99       allowNull: false
100     },
101     onDelete: 'cascade'
102   })
103   Video: VideoModel
104
105   static listForApi (nextOwnerId: number, start: number, count: number, sort: string) {
106     const query = {
107       offset: start,
108       limit: count,
109       order: getSort(sort),
110       where: {
111         nextOwnerAccountId: nextOwnerId
112       }
113     }
114
115     return Promise.all([
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 }))
119   }
120
121   static load (id: number): Bluebird<MVideoChangeOwnershipFull> {
122     return VideoChangeOwnershipModel.scope([ ScopeNames.WITH_ACCOUNTS, ScopeNames.WITH_VIDEO ])
123                                     .findByPk(id)
124   }
125
126   toFormattedJSON (this: MVideoChangeOwnershipFormattable): VideoChangeOwnership {
127     return {
128       id: this.id,
129       status: this.status,
130       initiatorAccount: this.Initiator.toFormattedJSON(),
131       nextOwnerAccount: this.NextOwner.toFormattedJSON(),
132       video: {
133         id: this.Video.id,
134         uuid: this.Video.uuid,
135         url: this.Video.url,
136         name: this.Video.name
137       },
138       createdAt: this.createdAt
139     }
140   }
141 }