]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/account/account.ts
Merge branch 'release/v1.3.0' into develop
[github/Chocobozzz/PeerTube.git] / server / models / account / account.ts
1 import {
2 AllowNull,
3 BeforeDestroy,
4 BelongsTo,
5 Column,
6 CreatedAt, DataType,
7 Default,
8 DefaultScope,
9 ForeignKey,
10 HasMany,
11 Is,
12 Model,
13 Scopes,
14 Table,
15 UpdatedAt
16 } from 'sequelize-typescript'
17 import { Account, AccountSummary } from '../../../shared/models/actors'
18 import { isAccountDescriptionValid } from '../../helpers/custom-validators/accounts'
19 import { sendDeleteActor } from '../../lib/activitypub/send'
20 import { ActorModel } from '../activitypub/actor'
21 import { ApplicationModel } from '../application/application'
22 import { ServerModel } from '../server/server'
23 import { getSort, throwIfNotValid } from '../utils'
24 import { VideoChannelModel } from '../video/video-channel'
25 import { VideoCommentModel } from '../video/video-comment'
26 import { UserModel } from './user'
27 import { AvatarModel } from '../avatar/avatar'
28 import { VideoPlaylistModel } from '../video/video-playlist'
29 import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants'
30 import { Op, Transaction, WhereOptions } from 'sequelize'
31
32 export enum ScopeNames {
33 SUMMARY = 'SUMMARY'
34 }
35
36 @DefaultScope(() => ({
37 include: [
38 {
39 model: ActorModel, // Default scope includes avatar and server
40 required: true
41 }
42 ]
43 }))
44 @Scopes(() => ({
45 [ ScopeNames.SUMMARY ]: (whereActor?: WhereOptions) => {
46 return {
47 attributes: [ 'id', 'name' ],
48 include: [
49 {
50 attributes: [ 'id', 'preferredUsername', 'url', 'serverId', 'avatarId' ],
51 model: ActorModel.unscoped(),
52 required: true,
53 where: whereActor,
54 include: [
55 {
56 attributes: [ 'host' ],
57 model: ServerModel.unscoped(),
58 required: false
59 },
60 {
61 model: AvatarModel.unscoped(),
62 required: false
63 }
64 ]
65 }
66 ]
67 }
68 }
69 }))
70 @Table({
71 tableName: 'account',
72 indexes: [
73 {
74 fields: [ 'actorId' ],
75 unique: true
76 },
77 {
78 fields: [ 'applicationId' ]
79 },
80 {
81 fields: [ 'userId' ]
82 }
83 ]
84 })
85 export class AccountModel extends Model<AccountModel> {
86
87 @AllowNull(false)
88 @Column
89 name: string
90
91 @AllowNull(true)
92 @Default(null)
93 @Is('AccountDescription', value => throwIfNotValid(value, isAccountDescriptionValid, 'description', true))
94 @Column(DataType.STRING(CONSTRAINTS_FIELDS.USERS.DESCRIPTION.max))
95 description: string
96
97 @CreatedAt
98 createdAt: Date
99
100 @UpdatedAt
101 updatedAt: Date
102
103 @ForeignKey(() => ActorModel)
104 @Column
105 actorId: number
106
107 @BelongsTo(() => ActorModel, {
108 foreignKey: {
109 allowNull: false
110 },
111 onDelete: 'cascade'
112 })
113 Actor: ActorModel
114
115 @ForeignKey(() => UserModel)
116 @Column
117 userId: number
118
119 @BelongsTo(() => UserModel, {
120 foreignKey: {
121 allowNull: true
122 },
123 onDelete: 'cascade'
124 })
125 User: UserModel
126
127 @ForeignKey(() => ApplicationModel)
128 @Column
129 applicationId: number
130
131 @BelongsTo(() => ApplicationModel, {
132 foreignKey: {
133 allowNull: true
134 },
135 onDelete: 'cascade'
136 })
137 Application: ApplicationModel
138
139 @HasMany(() => VideoChannelModel, {
140 foreignKey: {
141 allowNull: false
142 },
143 onDelete: 'cascade',
144 hooks: true
145 })
146 VideoChannels: VideoChannelModel[]
147
148 @HasMany(() => VideoPlaylistModel, {
149 foreignKey: {
150 allowNull: false
151 },
152 onDelete: 'cascade',
153 hooks: true
154 })
155 VideoPlaylists: VideoPlaylistModel[]
156
157 @HasMany(() => VideoCommentModel, {
158 foreignKey: {
159 allowNull: false
160 },
161 onDelete: 'cascade',
162 hooks: true
163 })
164 VideoComments: VideoCommentModel[]
165
166 @BeforeDestroy
167 static async sendDeleteIfOwned (instance: AccountModel, options) {
168 if (!instance.Actor) {
169 instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) as ActorModel
170 }
171
172 if (instance.isOwned()) {
173 return sendDeleteActor(instance.Actor, options.transaction)
174 }
175
176 return undefined
177 }
178
179 static load (id: number, transaction?: Transaction) {
180 return AccountModel.findByPk(id, { transaction })
181 }
182
183 static loadByNameWithHost (nameWithHost: string) {
184 const [ accountName, host ] = nameWithHost.split('@')
185
186 if (!host || host === WEBSERVER.HOST) return AccountModel.loadLocalByName(accountName)
187
188 return AccountModel.loadByNameAndHost(accountName, host)
189 }
190
191 static loadLocalByName (name: string) {
192 const query = {
193 where: {
194 [ Op.or ]: [
195 {
196 userId: {
197 [ Op.ne ]: null
198 }
199 },
200 {
201 applicationId: {
202 [ Op.ne ]: null
203 }
204 }
205 ]
206 },
207 include: [
208 {
209 model: ActorModel,
210 required: true,
211 where: {
212 preferredUsername: name
213 }
214 }
215 ]
216 }
217
218 return AccountModel.findOne(query)
219 }
220
221 static loadByNameAndHost (name: string, host: string) {
222 const query = {
223 include: [
224 {
225 model: ActorModel,
226 required: true,
227 where: {
228 preferredUsername: name
229 },
230 include: [
231 {
232 model: ServerModel,
233 required: true,
234 where: {
235 host
236 }
237 }
238 ]
239 }
240 ]
241 }
242
243 return AccountModel.findOne(query)
244 }
245
246 static loadByUrl (url: string, transaction?: Transaction) {
247 const query = {
248 include: [
249 {
250 model: ActorModel,
251 required: true,
252 where: {
253 url
254 }
255 }
256 ],
257 transaction
258 }
259
260 return AccountModel.findOne(query)
261 }
262
263 static listForApi (start: number, count: number, sort: string) {
264 const query = {
265 offset: start,
266 limit: count,
267 order: getSort(sort)
268 }
269
270 return AccountModel.findAndCountAll(query)
271 .then(({ rows, count }) => {
272 return {
273 data: rows,
274 total: count
275 }
276 })
277 }
278
279 static listLocalsForSitemap (sort: string) {
280 const query = {
281 attributes: [ ],
282 offset: 0,
283 order: getSort(sort),
284 include: [
285 {
286 attributes: [ 'preferredUsername', 'serverId' ],
287 model: ActorModel.unscoped(),
288 where: {
289 serverId: null
290 }
291 }
292 ]
293 }
294
295 return AccountModel
296 .unscoped()
297 .findAll(query)
298 }
299
300 toFormattedJSON (): Account {
301 const actor = this.Actor.toFormattedJSON()
302 const account = {
303 id: this.id,
304 displayName: this.getDisplayName(),
305 description: this.description,
306 createdAt: this.createdAt,
307 updatedAt: this.updatedAt,
308 userId: this.userId ? this.userId : undefined
309 }
310
311 return Object.assign(actor, account)
312 }
313
314 toFormattedSummaryJSON (): AccountSummary {
315 const actor = this.Actor.toFormattedJSON()
316
317 return {
318 id: this.id,
319 name: actor.name,
320 displayName: this.getDisplayName(),
321 url: actor.url,
322 host: actor.host,
323 avatar: actor.avatar
324 }
325 }
326
327 toActivityPubObject () {
328 const obj = this.Actor.toActivityPubObject(this.name, 'Account')
329
330 return Object.assign(obj, {
331 summary: this.description
332 })
333 }
334
335 isOwned () {
336 return this.Actor.isOwned()
337 }
338
339 isOutdated () {
340 return this.Actor.isOutdated()
341 }
342
343 getDisplayName () {
344 return this.name
345 }
346 }