]>
Commit | Line | Data |
---|---|---|
1 | import { | |
2 | AllowNull, | |
3 | BeforeDestroy, | |
4 | BelongsTo, | |
5 | Column, | |
6 | CreatedAt, | |
7 | DataType, | |
8 | Default, | |
9 | DefaultScope, | |
10 | ForeignKey, | |
11 | HasMany, | |
12 | Is, | |
13 | Model, | |
14 | Scopes, | |
15 | Table, | |
16 | UpdatedAt | |
17 | } from 'sequelize-typescript' | |
18 | import { Account, AccountSummary } from '../../../shared/models/actors' | |
19 | import { isAccountDescriptionValid } from '../../helpers/custom-validators/accounts' | |
20 | import { sendDeleteActor } from '../../lib/activitypub/send' | |
21 | import { ActorModel } from '../activitypub/actor' | |
22 | import { ApplicationModel } from '../application/application' | |
23 | import { ServerModel } from '../server/server' | |
24 | import { getSort, throwIfNotValid } from '../utils' | |
25 | import { VideoChannelModel } from '../video/video-channel' | |
26 | import { VideoCommentModel } from '../video/video-comment' | |
27 | import { UserModel } from './user' | |
28 | import { AvatarModel } from '../avatar/avatar' | |
29 | import { VideoPlaylistModel } from '../video/video-playlist' | |
30 | import { CONSTRAINTS_FIELDS, SERVER_ACTOR_NAME, WEBSERVER } from '../../initializers/constants' | |
31 | import { FindOptions, IncludeOptions, Op, Transaction, WhereOptions } from 'sequelize' | |
32 | import { AccountBlocklistModel } from './account-blocklist' | |
33 | import { ServerBlocklistModel } from '../server/server-blocklist' | |
34 | import { ActorFollowModel } from '../activitypub/actor-follow' | |
35 | import { MAccountActor, MAccountAP, MAccountDefault, MAccountFormattable, MAccountSummaryFormattable, MAccount } from '../../typings/models' | |
36 | import * as Bluebird from 'bluebird' | |
37 | import { ModelCache } from '@server/models/model-cache' | |
38 | import { VideoModel } from '../video/video' | |
39 | ||
40 | export enum ScopeNames { | |
41 | SUMMARY = 'SUMMARY' | |
42 | } | |
43 | ||
44 | export type SummaryOptions = { | |
45 | whereActor?: WhereOptions | |
46 | withAccountBlockerIds?: number[] | |
47 | } | |
48 | ||
49 | @DefaultScope(() => ({ | |
50 | include: [ | |
51 | { | |
52 | model: ActorModel, // Default scope includes avatar and server | |
53 | required: true | |
54 | } | |
55 | ] | |
56 | })) | |
57 | @Scopes(() => ({ | |
58 | [ScopeNames.SUMMARY]: (options: SummaryOptions = {}) => { | |
59 | const whereActor = options.whereActor || undefined | |
60 | ||
61 | const serverInclude: IncludeOptions = { | |
62 | attributes: [ 'host' ], | |
63 | model: ServerModel.unscoped(), | |
64 | required: false | |
65 | } | |
66 | ||
67 | const query: FindOptions = { | |
68 | attributes: [ 'id', 'name' ], | |
69 | include: [ | |
70 | { | |
71 | attributes: [ 'id', 'preferredUsername', 'url', 'serverId', 'avatarId' ], | |
72 | model: ActorModel.unscoped(), | |
73 | required: true, | |
74 | where: whereActor, | |
75 | include: [ | |
76 | serverInclude, | |
77 | ||
78 | { | |
79 | model: AvatarModel.unscoped(), | |
80 | required: false | |
81 | } | |
82 | ] | |
83 | } | |
84 | ] | |
85 | } | |
86 | ||
87 | if (options.withAccountBlockerIds) { | |
88 | query.include.push({ | |
89 | attributes: [ 'id' ], | |
90 | model: AccountBlocklistModel.unscoped(), | |
91 | as: 'BlockedAccounts', | |
92 | required: false, | |
93 | where: { | |
94 | accountId: { | |
95 | [Op.in]: options.withAccountBlockerIds | |
96 | } | |
97 | } | |
98 | }) | |
99 | ||
100 | serverInclude.include = [ | |
101 | { | |
102 | attributes: [ 'id' ], | |
103 | model: ServerBlocklistModel.unscoped(), | |
104 | required: false, | |
105 | where: { | |
106 | accountId: { | |
107 | [Op.in]: options.withAccountBlockerIds | |
108 | } | |
109 | } | |
110 | } | |
111 | ] | |
112 | } | |
113 | ||
114 | return query | |
115 | } | |
116 | })) | |
117 | @Table({ | |
118 | tableName: 'account', | |
119 | indexes: [ | |
120 | { | |
121 | fields: [ 'actorId' ], | |
122 | unique: true | |
123 | }, | |
124 | { | |
125 | fields: [ 'applicationId' ] | |
126 | }, | |
127 | { | |
128 | fields: [ 'userId' ] | |
129 | } | |
130 | ] | |
131 | }) | |
132 | export class AccountModel extends Model<AccountModel> { | |
133 | ||
134 | @AllowNull(false) | |
135 | @Column | |
136 | name: string | |
137 | ||
138 | @AllowNull(true) | |
139 | @Default(null) | |
140 | @Is('AccountDescription', value => throwIfNotValid(value, isAccountDescriptionValid, 'description', true)) | |
141 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.USERS.DESCRIPTION.max)) | |
142 | description: string | |
143 | ||
144 | @CreatedAt | |
145 | createdAt: Date | |
146 | ||
147 | @UpdatedAt | |
148 | updatedAt: Date | |
149 | ||
150 | @ForeignKey(() => ActorModel) | |
151 | @Column | |
152 | actorId: number | |
153 | ||
154 | @BelongsTo(() => ActorModel, { | |
155 | foreignKey: { | |
156 | allowNull: false | |
157 | }, | |
158 | onDelete: 'cascade' | |
159 | }) | |
160 | Actor: ActorModel | |
161 | ||
162 | @ForeignKey(() => UserModel) | |
163 | @Column | |
164 | userId: number | |
165 | ||
166 | @BelongsTo(() => UserModel, { | |
167 | foreignKey: { | |
168 | allowNull: true | |
169 | }, | |
170 | onDelete: 'cascade' | |
171 | }) | |
172 | User: UserModel | |
173 | ||
174 | @ForeignKey(() => ApplicationModel) | |
175 | @Column | |
176 | applicationId: number | |
177 | ||
178 | @BelongsTo(() => ApplicationModel, { | |
179 | foreignKey: { | |
180 | allowNull: true | |
181 | }, | |
182 | onDelete: 'cascade' | |
183 | }) | |
184 | Application: ApplicationModel | |
185 | ||
186 | @HasMany(() => VideoChannelModel, { | |
187 | foreignKey: { | |
188 | allowNull: false | |
189 | }, | |
190 | onDelete: 'cascade', | |
191 | hooks: true | |
192 | }) | |
193 | VideoChannels: VideoChannelModel[] | |
194 | ||
195 | @HasMany(() => VideoPlaylistModel, { | |
196 | foreignKey: { | |
197 | allowNull: false | |
198 | }, | |
199 | onDelete: 'cascade', | |
200 | hooks: true | |
201 | }) | |
202 | VideoPlaylists: VideoPlaylistModel[] | |
203 | ||
204 | @HasMany(() => VideoCommentModel, { | |
205 | foreignKey: { | |
206 | allowNull: true | |
207 | }, | |
208 | onDelete: 'cascade', | |
209 | hooks: true | |
210 | }) | |
211 | VideoComments: VideoCommentModel[] | |
212 | ||
213 | @HasMany(() => AccountBlocklistModel, { | |
214 | foreignKey: { | |
215 | name: 'targetAccountId', | |
216 | allowNull: false | |
217 | }, | |
218 | as: 'BlockedAccounts', | |
219 | onDelete: 'CASCADE' | |
220 | }) | |
221 | BlockedAccounts: AccountBlocklistModel[] | |
222 | ||
223 | @BeforeDestroy | |
224 | static async sendDeleteIfOwned (instance: AccountModel, options) { | |
225 | if (!instance.Actor) { | |
226 | instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) | |
227 | } | |
228 | ||
229 | await ActorFollowModel.removeFollowsOf(instance.Actor.id, options.transaction) | |
230 | if (instance.isOwned()) { | |
231 | return sendDeleteActor(instance.Actor, options.transaction) | |
232 | } | |
233 | ||
234 | return undefined | |
235 | } | |
236 | ||
237 | static load (id: number, transaction?: Transaction): Bluebird<MAccountDefault> { | |
238 | return AccountModel.findByPk(id, { transaction }) | |
239 | } | |
240 | ||
241 | static loadByNameWithHost (nameWithHost: string): Bluebird<MAccountDefault> { | |
242 | const [ accountName, host ] = nameWithHost.split('@') | |
243 | ||
244 | if (!host || host === WEBSERVER.HOST) return AccountModel.loadLocalByName(accountName) | |
245 | ||
246 | return AccountModel.loadByNameAndHost(accountName, host) | |
247 | } | |
248 | ||
249 | static loadLocalByName (name: string): Bluebird<MAccountDefault> { | |
250 | const fun = () => { | |
251 | const query = { | |
252 | where: { | |
253 | [Op.or]: [ | |
254 | { | |
255 | userId: { | |
256 | [Op.ne]: null | |
257 | } | |
258 | }, | |
259 | { | |
260 | applicationId: { | |
261 | [Op.ne]: null | |
262 | } | |
263 | } | |
264 | ] | |
265 | }, | |
266 | include: [ | |
267 | { | |
268 | model: ActorModel, | |
269 | required: true, | |
270 | where: { | |
271 | preferredUsername: name | |
272 | } | |
273 | } | |
274 | ] | |
275 | } | |
276 | ||
277 | return AccountModel.findOne(query) | |
278 | } | |
279 | ||
280 | return ModelCache.Instance.doCache({ | |
281 | cacheType: 'local-account-name', | |
282 | key: name, | |
283 | fun, | |
284 | // The server actor never change, so we can easily cache it | |
285 | whitelist: () => name === SERVER_ACTOR_NAME | |
286 | }) | |
287 | } | |
288 | ||
289 | static loadByNameAndHost (name: string, host: string): Bluebird<MAccountDefault> { | |
290 | const query = { | |
291 | include: [ | |
292 | { | |
293 | model: ActorModel, | |
294 | required: true, | |
295 | where: { | |
296 | preferredUsername: name | |
297 | }, | |
298 | include: [ | |
299 | { | |
300 | model: ServerModel, | |
301 | required: true, | |
302 | where: { | |
303 | host | |
304 | } | |
305 | } | |
306 | ] | |
307 | } | |
308 | ] | |
309 | } | |
310 | ||
311 | return AccountModel.findOne(query) | |
312 | } | |
313 | ||
314 | static loadByUrl (url: string, transaction?: Transaction): Bluebird<MAccountDefault> { | |
315 | const query = { | |
316 | include: [ | |
317 | { | |
318 | model: ActorModel, | |
319 | required: true, | |
320 | where: { | |
321 | url | |
322 | } | |
323 | } | |
324 | ], | |
325 | transaction | |
326 | } | |
327 | ||
328 | return AccountModel.findOne(query) | |
329 | } | |
330 | ||
331 | static listForApi (start: number, count: number, sort: string) { | |
332 | const query = { | |
333 | offset: start, | |
334 | limit: count, | |
335 | order: getSort(sort) | |
336 | } | |
337 | ||
338 | return AccountModel.findAndCountAll(query) | |
339 | .then(({ rows, count }) => { | |
340 | return { | |
341 | data: rows, | |
342 | total: count | |
343 | } | |
344 | }) | |
345 | } | |
346 | ||
347 | static loadAccountIdFromVideo (videoId: number): Bluebird<MAccount> { | |
348 | const query = { | |
349 | include: [ | |
350 | { | |
351 | attributes: [ 'id', 'accountId' ], | |
352 | model: VideoChannelModel.unscoped(), | |
353 | required: true, | |
354 | include: [ | |
355 | { | |
356 | attributes: [ 'id', 'channelId' ], | |
357 | model: VideoModel.unscoped(), | |
358 | where: { | |
359 | id: videoId | |
360 | } | |
361 | } | |
362 | ] | |
363 | } | |
364 | ] | |
365 | } | |
366 | ||
367 | return AccountModel.findOne(query) | |
368 | } | |
369 | ||
370 | static listLocalsForSitemap (sort: string): Bluebird<MAccountActor[]> { | |
371 | const query = { | |
372 | attributes: [ ], | |
373 | offset: 0, | |
374 | order: getSort(sort), | |
375 | include: [ | |
376 | { | |
377 | attributes: [ 'preferredUsername', 'serverId' ], | |
378 | model: ActorModel.unscoped(), | |
379 | where: { | |
380 | serverId: null | |
381 | } | |
382 | } | |
383 | ] | |
384 | } | |
385 | ||
386 | return AccountModel | |
387 | .unscoped() | |
388 | .findAll(query) | |
389 | } | |
390 | ||
391 | toFormattedJSON (this: MAccountFormattable): Account { | |
392 | const actor = this.Actor.toFormattedJSON() | |
393 | const account = { | |
394 | id: this.id, | |
395 | displayName: this.getDisplayName(), | |
396 | description: this.description, | |
397 | createdAt: this.createdAt, | |
398 | updatedAt: this.updatedAt, | |
399 | userId: this.userId ? this.userId : undefined | |
400 | } | |
401 | ||
402 | return Object.assign(actor, account) | |
403 | } | |
404 | ||
405 | toFormattedSummaryJSON (this: MAccountSummaryFormattable): AccountSummary { | |
406 | const actor = this.Actor.toFormattedSummaryJSON() | |
407 | ||
408 | return { | |
409 | id: this.id, | |
410 | name: actor.name, | |
411 | displayName: this.getDisplayName(), | |
412 | url: actor.url, | |
413 | host: actor.host, | |
414 | avatar: actor.avatar | |
415 | } | |
416 | } | |
417 | ||
418 | toActivityPubObject (this: MAccountAP) { | |
419 | const obj = this.Actor.toActivityPubObject(this.name) | |
420 | ||
421 | return Object.assign(obj, { | |
422 | summary: this.description | |
423 | }) | |
424 | } | |
425 | ||
426 | isOwned () { | |
427 | return this.Actor.isOwned() | |
428 | } | |
429 | ||
430 | isOutdated () { | |
431 | return this.Actor.isOutdated() | |
432 | } | |
433 | ||
434 | getDisplayName () { | |
435 | return this.name | |
436 | } | |
437 | ||
438 | isBlocked () { | |
439 | return this.BlockedAccounts && this.BlockedAccounts.length !== 0 | |
440 | } | |
441 | } |