]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/video-change-ownership.ts
Merge branch 'release/3.2.0' into develop
[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 { 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'
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 VideoScopeNames.WITH_ACCOUNT_DETAILS
51 ]),
52 required: true
53 }
54 ]
55 }
56 }))
57 export class VideoChangeOwnershipModel extends Model<Partial<AttributesOnly<VideoChangeOwnershipModel>>> {
58 @CreatedAt
59 createdAt: Date
60
61 @UpdatedAt
62 updatedAt: Date
63
64 @AllowNull(false)
65 @Column
66 status: VideoChangeOwnershipStatus
67
68 @ForeignKey(() => AccountModel)
69 @Column
70 initiatorAccountId: number
71
72 @BelongsTo(() => AccountModel, {
73 foreignKey: {
74 name: 'initiatorAccountId',
75 allowNull: false
76 },
77 onDelete: 'cascade'
78 })
79 Initiator: AccountModel
80
81 @ForeignKey(() => AccountModel)
82 @Column
83 nextOwnerAccountId: number
84
85 @BelongsTo(() => AccountModel, {
86 foreignKey: {
87 name: 'nextOwnerAccountId',
88 allowNull: false
89 },
90 onDelete: 'cascade'
91 })
92 NextOwner: AccountModel
93
94 @ForeignKey(() => VideoModel)
95 @Column
96 videoId: number
97
98 @BelongsTo(() => VideoModel, {
99 foreignKey: {
100 allowNull: false
101 },
102 onDelete: 'cascade'
103 })
104 Video: VideoModel
105
106 static listForApi (nextOwnerId: number, start: number, count: number, sort: string) {
107 const query = {
108 offset: start,
109 limit: count,
110 order: getSort(sort),
111 where: {
112 nextOwnerAccountId: nextOwnerId
113 }
114 }
115
116 return Promise.all([
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 }))
120 }
121
122 static load (id: number): Promise<MVideoChangeOwnershipFull> {
123 return VideoChangeOwnershipModel.scope([ ScopeNames.WITH_ACCOUNTS, ScopeNames.WITH_VIDEO ])
124 .findByPk(id)
125 }
126
127 toFormattedJSON (this: MVideoChangeOwnershipFormattable): VideoChangeOwnership {
128 return {
129 id: this.id,
130 status: this.status,
131 initiatorAccount: this.Initiator.toFormattedJSON(),
132 nextOwnerAccount: this.NextOwner.toFormattedJSON(),
133 video: this.Video.toFormattedJSON(),
134 createdAt: this.createdAt
135 }
136 }
137 }