]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/video-change-ownership.ts
Add federation to ownership change
[github/Chocobozzz/PeerTube.git] / server / models / video / video-change-ownership.ts
CommitLineData
74d63469
GR
1import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
2import { AccountModel } from '../account/account'
3import { VideoModel } from './video'
4import { VideoChangeOwnership, VideoChangeOwnershipStatus } from '../../../shared/models/videos'
5import { getSort } from '../utils'
6import { VideoFileModel } from './video-file'
7
8enum ScopeNames {
9 FULL = 'FULL'
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.FULL]: {
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 model: () => VideoModel,
41 required: true,
5cf84858
C
42 include: [
43 { model: () => VideoFileModel }
44 ]
74d63469
GR
45 }
46 ]
47 }
48})
49export class VideoChangeOwnershipModel extends Model<VideoChangeOwnershipModel> {
50 @CreatedAt
51 createdAt: Date
52
53 @UpdatedAt
54 updatedAt: Date
55
56 @AllowNull(false)
57 @Column
58 status: VideoChangeOwnershipStatus
59
60 @ForeignKey(() => AccountModel)
61 @Column
62 initiatorAccountId: number
63
64 @BelongsTo(() => AccountModel, {
65 foreignKey: {
66 name: 'initiatorAccountId',
67 allowNull: false
68 },
69 onDelete: 'cascade'
70 })
71 Initiator: AccountModel
72
73 @ForeignKey(() => AccountModel)
74 @Column
75 nextOwnerAccountId: number
76
77 @BelongsTo(() => AccountModel, {
78 foreignKey: {
79 name: 'nextOwnerAccountId',
80 allowNull: false
81 },
82 onDelete: 'cascade'
83 })
84 NextOwner: AccountModel
85
86 @ForeignKey(() => VideoModel)
87 @Column
88 videoId: number
89
90 @BelongsTo(() => VideoModel, {
91 foreignKey: {
92 allowNull: false
93 },
94 onDelete: 'cascade'
95 })
96 Video: VideoModel
97
98 static listForApi (nextOwnerId: number, start: number, count: number, sort: string) {
5cf84858 99 const query = {
74d63469
GR
100 offset: start,
101 limit: count,
102 order: getSort(sort),
103 where: {
104 nextOwnerAccountId: nextOwnerId
105 }
5cf84858
C
106 }
107
108 return VideoChangeOwnershipModel.scope(ScopeNames.FULL).findAndCountAll(query)
109 .then(({ rows, count }) => ({ total: count, data: rows }))
74d63469
GR
110 }
111
112 static load (id: number) {
113 return VideoChangeOwnershipModel.scope(ScopeNames.FULL).findById(id)
114 }
115
116 toFormattedJSON (): VideoChangeOwnership {
117 return {
118 id: this.id,
119 status: this.status,
120 initiatorAccount: this.Initiator.toFormattedJSON(),
121 nextOwnerAccount: this.NextOwner.toFormattedJSON(),
122 video: {
123 id: this.Video.id,
124 uuid: this.Video.uuid,
125 url: this.Video.url,
126 name: this.Video.name
127 },
128 createdAt: this.createdAt
129 }
130 }
131}