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