Add new follow, mention and user registered notifs
[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 { VideoModel } from './video'
4 import { VideoChangeOwnership, VideoChangeOwnershipStatus } from '../../../shared/models/videos'
5 import { getSort } from '../utils'
6 import { VideoFileModel } from './video-file'
7
8 enum 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,
42         include: [
43           { model: () => VideoFileModel }
44         ]
45       }
46     ]
47   }
48 })
49 export 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) {
99     const query = {
100       offset: start,
101       limit: count,
102       order: getSort(sort),
103       where: {
104         nextOwnerAccountId: nextOwnerId
105       }
106     }
107
108     return VideoChangeOwnershipModel.scope(ScopeNames.FULL).findAndCountAll(query)
109                                     .then(({ rows, count }) => ({ total: count, data: rows }))
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 }