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