Cleanup req files on bad request
[oweals/peertube.git] / server / models / account / account.ts
1 import * as Sequelize from 'sequelize'
2 import {
3   AllowNull,
4   BeforeDestroy,
5   BelongsTo,
6   Column,
7   CreatedAt,
8   Default,
9   DefaultScope,
10   ForeignKey,
11   HasMany,
12   Is,
13   Model,
14   Table,
15   UpdatedAt
16 } from 'sequelize-typescript'
17 import { Account } from '../../../shared/models/actors'
18 import { isAccountDescriptionValid } from '../../helpers/custom-validators/accounts'
19 import { logger } from '../../helpers/logger'
20 import { sendDeleteActor } from '../../lib/activitypub/send'
21 import { ActorModel } from '../activitypub/actor'
22 import { ApplicationModel } from '../application/application'
23 import { AvatarModel } from '../avatar/avatar'
24 import { ServerModel } from '../server/server'
25 import { getSort, throwIfNotValid } from '../utils'
26 import { VideoChannelModel } from '../video/video-channel'
27 import { VideoCommentModel } from '../video/video-comment'
28 import { UserModel } from './user'
29
30 @DefaultScope({
31   include: [
32     {
33       model: () => ActorModel,
34       required: true,
35       include: [
36         {
37           model: () => ServerModel,
38           required: false
39         },
40         {
41           model: () => AvatarModel,
42           required: false
43         }
44       ]
45     }
46   ]
47 })
48 @Table({
49   tableName: 'account',
50   indexes: [
51     {
52       fields: [ 'actorId' ],
53       unique: true
54     },
55     {
56       fields: [ 'applicationId' ]
57     },
58     {
59       fields: [ 'userId' ]
60     }
61   ]
62 })
63 export class AccountModel extends Model<AccountModel> {
64
65   @AllowNull(false)
66   @Column
67   name: string
68
69   @AllowNull(true)
70   @Default(null)
71   @Is('AccountDescription', value => throwIfNotValid(value, isAccountDescriptionValid, 'description'))
72   @Column
73   description: string
74
75   @CreatedAt
76   createdAt: Date
77
78   @UpdatedAt
79   updatedAt: Date
80
81   @ForeignKey(() => ActorModel)
82   @Column
83   actorId: number
84
85   @BelongsTo(() => ActorModel, {
86     foreignKey: {
87       allowNull: false
88     },
89     onDelete: 'cascade'
90   })
91   Actor: ActorModel
92
93   @ForeignKey(() => UserModel)
94   @Column
95   userId: number
96
97   @BelongsTo(() => UserModel, {
98     foreignKey: {
99       allowNull: true
100     },
101     onDelete: 'cascade'
102   })
103   User: UserModel
104
105   @ForeignKey(() => ApplicationModel)
106   @Column
107   applicationId: number
108
109   @BelongsTo(() => ApplicationModel, {
110     foreignKey: {
111       allowNull: true
112     },
113     onDelete: 'cascade'
114   })
115   Application: ApplicationModel
116
117   @HasMany(() => VideoChannelModel, {
118     foreignKey: {
119       allowNull: false
120     },
121     onDelete: 'cascade',
122     hooks: true
123   })
124   VideoChannels: VideoChannelModel[]
125
126   @HasMany(() => VideoCommentModel, {
127     foreignKey: {
128       allowNull: false
129     },
130     onDelete: 'cascade',
131     hooks: true
132   })
133   VideoComments: VideoCommentModel[]
134
135   @BeforeDestroy
136   static async sendDeleteIfOwned (instance: AccountModel, options) {
137     if (!instance.Actor) {
138       instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) as ActorModel
139     }
140
141     if (instance.isOwned()) {
142       return sendDeleteActor(instance.Actor, options.transaction)
143     }
144
145     return undefined
146   }
147
148   static load (id: number) {
149     return AccountModel.findById(id)
150   }
151
152   static loadByUUID (uuid: string) {
153     const query = {
154       include: [
155         {
156           model: ActorModel,
157           required: true,
158           where: {
159             uuid
160           }
161         }
162       ]
163     }
164
165     return AccountModel.findOne(query)
166   }
167
168   static loadLocalByName (name: string) {
169     const query = {
170       where: {
171         [ Sequelize.Op.or ]: [
172           {
173             userId: {
174               [ Sequelize.Op.ne ]: null
175             }
176           },
177           {
178             applicationId: {
179               [ Sequelize.Op.ne ]: null
180             }
181           }
182         ]
183       },
184       include: [
185         {
186           model: ActorModel,
187           required: true,
188           where: {
189             preferredUsername: name
190           }
191         }
192       ]
193     }
194
195     return AccountModel.findOne(query)
196   }
197
198   static loadLocalByNameAndHost (name: string, host: string) {
199     const query = {
200       include: [
201         {
202           model: ActorModel,
203           required: true,
204           where: {
205             preferredUsername: name
206           },
207           include: [
208             {
209               model: ServerModel,
210               required: true,
211               where: {
212                 host
213               }
214             }
215           ]
216         }
217       ]
218     }
219
220     return AccountModel.findOne(query)
221   }
222
223   static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
224     const query = {
225       include: [
226         {
227           model: ActorModel,
228           required: true,
229           where: {
230             url
231           }
232         }
233       ],
234       transaction
235     }
236
237     return AccountModel.findOne(query)
238   }
239
240   static listForApi (start: number, count: number, sort: string) {
241     const query = {
242       offset: start,
243       limit: count,
244       order: getSort(sort)
245     }
246
247     return AccountModel.findAndCountAll(query)
248       .then(({ rows, count }) => {
249         return {
250           data: rows,
251           total: count
252         }
253       })
254   }
255
256   toFormattedJSON (): Account {
257     const actor = this.Actor.toFormattedJSON()
258     const account = {
259       id: this.id,
260       displayName: this.getDisplayName(),
261       description: this.description,
262       createdAt: this.createdAt,
263       updatedAt: this.updatedAt
264     }
265
266     return Object.assign(actor, account)
267   }
268
269   toActivityPubObject () {
270     const obj = this.Actor.toActivityPubObject(this.name, 'Account')
271
272     return Object.assign(obj, {
273       summary: this.description
274     })
275   }
276
277   isOwned () {
278     return this.Actor.isOwned()
279   }
280
281   getDisplayName () {
282     return this.name
283   }
284 }