]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/video-change-ownership.ts
Upgrade sequelize to v6
[github/Chocobozzz/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 { MVideoChangeOwnershipFormattable, MVideoChangeOwnershipFull } from '@server/types/models/video/video-change-ownership'
3 import { VideoChangeOwnership, VideoChangeOwnershipStatus } from '../../../shared/models/videos'
4 import { AccountModel } from '../account/account'
5 import { getSort } from '../utils'
6 import { ScopeNames as VideoScopeNames, VideoModel } from './video'
7
8 enum ScopeNames {
9 WITH_ACCOUNTS = 'WITH_ACCOUNTS',
10 WITH_VIDEO = 'WITH_VIDEO'
11 }
12
13 @Table({
14 tableName: 'videoChangeOwnership',
15 indexes: [
16 {
17 fields: [ 'videoId' ]
18 },
19 {
20 fields: [ 'initiatorAccountId' ]
21 },
22 {
23 fields: [ 'nextOwnerAccountId' ]
24 }
25 ]
26 })
27 @Scopes(() => ({
28 [ScopeNames.WITH_ACCOUNTS]: {
29 include: [
30 {
31 model: AccountModel,
32 as: 'Initiator',
33 required: true
34 },
35 {
36 model: AccountModel,
37 as: 'NextOwner',
38 required: true
39 }
40 ]
41 },
42 [ScopeNames.WITH_VIDEO]: {
43 include: [
44 {
45 model: VideoModel.scope([
46 VideoScopeNames.WITH_THUMBNAILS,
47 VideoScopeNames.WITH_WEBTORRENT_FILES,
48 VideoScopeNames.WITH_STREAMING_PLAYLISTS,
49 VideoScopeNames.WITH_ACCOUNT_DETAILS
50 ]),
51 required: true
52 }
53 ]
54 }
55 }))
56 export class VideoChangeOwnershipModel extends Model {
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): Promise<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: this.Video.toFormattedJSON(),
133 createdAt: this.createdAt
134 }
135 }
136 }