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