feature: initial syndication feeds support
[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 })
51 export class AccountModel extends Model<AccountModel> {
52
53   @AllowNull(false)
54   @Column
55   name: string
56
57   @AllowNull(true)
58   @Default(null)
59   @Is('AccountDescription', value => throwIfNotValid(value, isAccountDescriptionValid, 'description'))
60   @Column
61   description: string
62
63   @CreatedAt
64   createdAt: Date
65
66   @UpdatedAt
67   updatedAt: Date
68
69   @ForeignKey(() => ActorModel)
70   @Column
71   actorId: number
72
73   @BelongsTo(() => ActorModel, {
74     foreignKey: {
75       allowNull: false
76     },
77     onDelete: 'cascade'
78   })
79   Actor: ActorModel
80
81   @ForeignKey(() => UserModel)
82   @Column
83   userId: number
84
85   @BelongsTo(() => UserModel, {
86     foreignKey: {
87       allowNull: true
88     },
89     onDelete: 'cascade'
90   })
91   User: UserModel
92
93   @ForeignKey(() => ApplicationModel)
94   @Column
95   applicationId: number
96
97   @BelongsTo(() => ApplicationModel, {
98     foreignKey: {
99       allowNull: true
100     },
101     onDelete: 'cascade'
102   })
103   Application: ApplicationModel
104
105   @HasMany(() => VideoChannelModel, {
106     foreignKey: {
107       allowNull: false
108     },
109     onDelete: 'cascade',
110     hooks: true
111   })
112   VideoChannels: VideoChannelModel[]
113
114   @HasMany(() => VideoCommentModel, {
115     foreignKey: {
116       allowNull: false
117     },
118     onDelete: 'cascade',
119     hooks: true
120   })
121   VideoComments: VideoCommentModel[]
122
123   @BeforeDestroy
124   static async sendDeleteIfOwned (instance: AccountModel, options) {
125     if (!instance.Actor) {
126       instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) as ActorModel
127     }
128
129     if (instance.isOwned()) {
130       logger.debug('Sending delete of actor of account %s.', instance.Actor.url)
131       return sendDeleteActor(instance.Actor, options.transaction)
132     }
133
134     return undefined
135   }
136
137   static load (id: number) {
138     return AccountModel.findById(id)
139   }
140
141   static loadByUUID (uuid: string) {
142     const query = {
143       include: [
144         {
145           model: ActorModel,
146           required: true,
147           where: {
148             uuid
149           }
150         }
151       ]
152     }
153
154     return AccountModel.findOne(query)
155   }
156
157   static loadLocalByName (name: string) {
158     const query = {
159       where: {
160         [ Sequelize.Op.or ]: [
161           {
162             userId: {
163               [ Sequelize.Op.ne ]: null
164             }
165           },
166           {
167             applicationId: {
168               [ Sequelize.Op.ne ]: null
169             }
170           }
171         ]
172       },
173       include: [
174         {
175           model: ActorModel,
176           required: true,
177           where: {
178             preferredUsername: name
179           }
180         }
181       ]
182     }
183
184     return AccountModel.findOne(query)
185   }
186
187   static loadLocalByNameAndHost (name: string, host: string) {
188     const query = {
189       include: [
190         {
191           model: ActorModel,
192           required: true,
193           where: {
194             preferredUsername: name
195           },
196           include: [
197             {
198               model: ServerModel,
199               required: true,
200               where: {
201                 host
202               }
203             }
204           ]
205         }
206       ]
207     }
208
209     return AccountModel.findOne(query)
210   }
211
212   static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
213     const query = {
214       include: [
215         {
216           model: ActorModel,
217           required: true,
218           where: {
219             url
220           }
221         }
222       ],
223       transaction
224     }
225
226     return AccountModel.findOne(query)
227   }
228
229   static listForApi (start: number, count: number, sort: string) {
230     const query = {
231       offset: start,
232       limit: count,
233       order: getSort(sort)
234     }
235
236     return AccountModel.findAndCountAll(query)
237       .then(({ rows, count }) => {
238         return {
239           data: rows,
240           total: count
241         }
242       })
243   }
244
245   toFormattedJSON (): Account {
246     const actor = this.Actor.toFormattedJSON()
247     const account = {
248       id: this.id,
249       displayName: this.getDisplayName(),
250       description: this.description,
251       createdAt: this.createdAt,
252       updatedAt: this.updatedAt
253     }
254
255     return Object.assign(actor, account)
256   }
257
258   toActivityPubObject () {
259     const obj = this.Actor.toActivityPubObject(this.name, 'Account')
260
261     return Object.assign(obj, {
262       summary: this.description
263     })
264   }
265
266   isOwned () {
267     return this.Actor.isOwned()
268   }
269
270   getDisplayName () {
271     return this.name
272   }
273 }