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