]>
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' | |
a59f210f | 19 | import { ModelCache } from '@server/models/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, | |
31 | MAccountSummaryFormattable, | |
32 | MChannelActor | |
33 | } from '../../types/models' | |
7d9ba5c0 C |
34 | import { ActorModel } from '../actor/actor' |
35 | import { ActorFollowModel } from '../actor/actor-follow' | |
36 | import { ActorImageModel } from '../actor/actor-image' | |
3fd3ab2d | 37 | import { ApplicationModel } from '../application/application' |
3fd3ab2d | 38 | import { ServerModel } from '../server/server' |
a59f210f | 39 | import { ServerBlocklistModel } from '../server/server-blocklist' |
7d9ba5c0 | 40 | import { UserModel } from '../user/user' |
2422c46b | 41 | import { getSort, throwIfNotValid } from '../utils' |
a59f210f | 42 | import { VideoModel } from '../video/video' |
3fd3ab2d | 43 | import { VideoChannelModel } from '../video/video-channel' |
f05a1c30 | 44 | import { VideoCommentModel } from '../video/video-comment' |
418d092a | 45 | import { VideoPlaylistModel } from '../video/video-playlist' |
bfbd9128 | 46 | import { AccountBlocklistModel } from './account-blocklist' |
418d092a C |
47 | |
48 | export enum ScopeNames { | |
49 | SUMMARY = 'SUMMARY' | |
50 | } | |
3fd3ab2d | 51 | |
bfbd9128 | 52 | export type SummaryOptions = { |
4f32032f | 53 | actorRequired?: boolean // Default: true |
bfbd9128 | 54 | whereActor?: WhereOptions |
fa47956e | 55 | whereServer?: WhereOptions |
bfbd9128 | 56 | withAccountBlockerIds?: number[] |
d0800f76 | 57 | forCount?: boolean |
bfbd9128 C |
58 | } |
59 | ||
3acc5084 | 60 | @DefaultScope(() => ({ |
50d6de9c | 61 | include: [ |
3fd3ab2d | 62 | { |
3acc5084 | 63 | model: ActorModel, // Default scope includes avatar and server |
f37dc0dd | 64 | required: true |
e4f97bab | 65 | } |
e4f97bab | 66 | ] |
3acc5084 C |
67 | })) |
68 | @Scopes(() => ({ | |
a1587156 | 69 | [ScopeNames.SUMMARY]: (options: SummaryOptions = {}) => { |
bfbd9128 C |
70 | const serverInclude: IncludeOptions = { |
71 | attributes: [ 'host' ], | |
72 | model: ServerModel.unscoped(), | |
fa47956e C |
73 | required: !!options.whereServer, |
74 | where: options.whereServer | |
bfbd9128 C |
75 | } |
76 | ||
d0800f76 | 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 | } | |
bfbd9128 | 84 | |
d0800f76 | 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 | |
b49f22d8 C |
95 | ] |
96 | ||
97 | const query: FindOptions = { | |
98 | attributes: [ 'id', 'name', 'actorId' ] | |
418d092a | 99 | } |
bfbd9128 C |
100 | |
101 | if (options.withAccountBlockerIds) { | |
b49f22d8 | 102 | queryInclude.push({ |
bfbd9128 C |
103 | attributes: [ 'id' ], |
104 | model: AccountBlocklistModel.unscoped(), | |
3c10840f | 105 | as: 'BlockedBy', |
bfbd9128 C |
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 | ||
b49f22d8 C |
128 | query.include = queryInclude |
129 | ||
bfbd9128 | 130 | return query |
418d092a | 131 | } |
3acc5084 | 132 | })) |
50d6de9c | 133 | @Table({ |
8cd72bd3 C |
134 | tableName: 'account', |
135 | indexes: [ | |
136 | { | |
137 | fields: [ 'actorId' ], | |
138 | unique: true | |
139 | }, | |
140 | { | |
141 | fields: [ 'applicationId' ] | |
142 | }, | |
143 | { | |
144 | fields: [ 'userId' ] | |
145 | } | |
146 | ] | |
50d6de9c | 147 | }) |
16c016e8 | 148 | export class AccountModel extends Model<Partial<AttributesOnly<AccountModel>>> { |
3fd3ab2d | 149 | |
50d6de9c | 150 | @AllowNull(false) |
50d6de9c C |
151 | @Column |
152 | name: string | |
153 | ||
2422c46b C |
154 | @AllowNull(true) |
155 | @Default(null) | |
1735c825 | 156 | @Is('AccountDescription', value => throwIfNotValid(value, isAccountDescriptionValid, 'description', true)) |
241c3357 | 157 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.USERS.DESCRIPTION.max)) |
2422c46b C |
158 | description: string |
159 | ||
3fd3ab2d C |
160 | @CreatedAt |
161 | createdAt: Date | |
162 | ||
163 | @UpdatedAt | |
164 | updatedAt: Date | |
165 | ||
fadf619a | 166 | @ForeignKey(() => ActorModel) |
3fd3ab2d | 167 | @Column |
fadf619a | 168 | actorId: number |
e4f97bab | 169 | |
fadf619a | 170 | @BelongsTo(() => ActorModel, { |
e4f97bab | 171 | foreignKey: { |
fadf619a | 172 | allowNull: false |
e4f97bab C |
173 | }, |
174 | onDelete: 'cascade' | |
175 | }) | |
fadf619a | 176 | Actor: ActorModel |
e4f97bab | 177 | |
3fd3ab2d C |
178 | @ForeignKey(() => UserModel) |
179 | @Column | |
180 | userId: number | |
181 | ||
182 | @BelongsTo(() => UserModel, { | |
e4f97bab | 183 | foreignKey: { |
e4f97bab C |
184 | allowNull: true |
185 | }, | |
186 | onDelete: 'cascade' | |
187 | }) | |
3fd3ab2d C |
188 | User: UserModel |
189 | ||
190 | @ForeignKey(() => ApplicationModel) | |
191 | @Column | |
192 | applicationId: number | |
e4f97bab | 193 | |
3fd3ab2d | 194 | @BelongsTo(() => ApplicationModel, { |
e4f97bab | 195 | foreignKey: { |
e4f97bab C |
196 | allowNull: true |
197 | }, | |
198 | onDelete: 'cascade' | |
199 | }) | |
f05a1c30 | 200 | Application: ApplicationModel |
e4f97bab | 201 | |
3fd3ab2d | 202 | @HasMany(() => VideoChannelModel, { |
e4f97bab | 203 | foreignKey: { |
e4f97bab C |
204 | allowNull: false |
205 | }, | |
206 | onDelete: 'cascade', | |
207 | hooks: true | |
208 | }) | |
3fd3ab2d | 209 | VideoChannels: VideoChannelModel[] |
e4f97bab | 210 | |
418d092a C |
211 | @HasMany(() => VideoPlaylistModel, { |
212 | foreignKey: { | |
213 | allowNull: false | |
214 | }, | |
215 | onDelete: 'cascade', | |
216 | hooks: true | |
217 | }) | |
218 | VideoPlaylists: VideoPlaylistModel[] | |
219 | ||
f05a1c30 C |
220 | @HasMany(() => VideoCommentModel, { |
221 | foreignKey: { | |
69222afa | 222 | allowNull: true |
f05a1c30 C |
223 | }, |
224 | onDelete: 'cascade', | |
225 | hooks: true | |
226 | }) | |
227 | VideoComments: VideoCommentModel[] | |
228 | ||
bfbd9128 C |
229 | @HasMany(() => AccountBlocklistModel, { |
230 | foreignKey: { | |
231 | name: 'targetAccountId', | |
232 | allowNull: false | |
233 | }, | |
2760b454 | 234 | as: 'BlockedBy', |
bfbd9128 C |
235 | onDelete: 'CASCADE' |
236 | }) | |
2760b454 | 237 | BlockedBy: AccountBlocklistModel[] |
bfbd9128 | 238 | |
f05a1c30 C |
239 | @BeforeDestroy |
240 | static async sendDeleteIfOwned (instance: AccountModel, options) { | |
241 | if (!instance.Actor) { | |
e6122097 | 242 | instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) |
f05a1c30 C |
243 | } |
244 | ||
44b88f18 | 245 | await ActorFollowModel.removeFollowsOf(instance.Actor.id, options.transaction) |
2af337c8 | 246 | |
c5a893d5 | 247 | if (instance.isOwned()) { |
c5a893d5 C |
248 | return sendDeleteActor(instance.Actor, options.transaction) |
249 | } | |
250 | ||
251 | return undefined | |
e4f97bab C |
252 | } |
253 | ||
b49f22d8 | 254 | static load (id: number, transaction?: Transaction): Promise<MAccountDefault> { |
9b39106d | 255 | return AccountModel.findByPk(id, { transaction }) |
3fd3ab2d | 256 | } |
2295ce6c | 257 | |
b49f22d8 | 258 | static loadByNameWithHost (nameWithHost: string): Promise<MAccountDefault> { |
92bf2f62 C |
259 | const [ accountName, host ] = nameWithHost.split('@') |
260 | ||
6dd9de95 | 261 | if (!host || host === WEBSERVER.HOST) return AccountModel.loadLocalByName(accountName) |
92bf2f62 C |
262 | |
263 | return AccountModel.loadByNameAndHost(accountName, host) | |
264 | } | |
265 | ||
b49f22d8 | 266 | static loadLocalByName (name: string): Promise<MAccountDefault> { |
0ffd6d32 C |
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 | } | |
3fd3ab2d | 280 | } |
0ffd6d32 C |
281 | ] |
282 | }, | |
283 | include: [ | |
3fd3ab2d | 284 | { |
0ffd6d32 C |
285 | model: ActorModel, |
286 | required: true, | |
287 | where: { | |
288 | preferredUsername: name | |
3fd3ab2d C |
289 | } |
290 | } | |
291 | ] | |
0ffd6d32 | 292 | } |
e8cb4409 | 293 | |
0ffd6d32 C |
294 | return AccountModel.findOne(query) |
295 | } | |
e4a686b4 | 296 | |
0ffd6d32 C |
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 | }) | |
e8cb4409 C |
304 | } |
305 | ||
b49f22d8 | 306 | static loadByNameAndHost (name: string, host: string): Promise<MAccountDefault> { |
e8cb4409 C |
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 | ] | |
3fd3ab2d | 326 | } |
7a7724e6 | 327 | |
3fd3ab2d C |
328 | return AccountModel.findOne(query) |
329 | } | |
7a7724e6 | 330 | |
b49f22d8 | 331 | static loadByUrl (url: string, transaction?: Transaction): Promise<MAccountDefault> { |
3fd3ab2d | 332 | const query = { |
fadf619a C |
333 | include: [ |
334 | { | |
335 | model: ActorModel, | |
336 | required: true, | |
337 | where: { | |
338 | url | |
339 | } | |
340 | } | |
341 | ], | |
3fd3ab2d C |
342 | transaction |
343 | } | |
e4f97bab | 344 | |
3fd3ab2d C |
345 | return AccountModel.findOne(query) |
346 | } | |
e4f97bab | 347 | |
265ba139 C |
348 | static listForApi (start: number, count: number, sort: string) { |
349 | const query = { | |
350 | offset: start, | |
351 | limit: count, | |
6ff9c676 | 352 | order: getSort(sort) |
265ba139 C |
353 | } |
354 | ||
d0800f76 | 355 | return Promise.all([ |
356 | AccountModel.count(), | |
357 | AccountModel.findAll(query) | |
358 | ]).then(([ total, data ]) => ({ total, data })) | |
265ba139 C |
359 | } |
360 | ||
b49f22d8 | 361 | static loadAccountIdFromVideo (videoId: number): Promise<MAccount> { |
696d83fd C |
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 | ||
b49f22d8 | 384 | static listLocalsForSitemap (sort: string): Promise<MAccountActor[]> { |
2feebf3e C |
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 | ||
d95d1559 C |
405 | getClientUrl () { |
406 | return WEBSERVER.URL + '/accounts/' + this.Actor.getIdentifier() | |
407 | } | |
408 | ||
1ca9f7c3 | 409 | toFormattedJSON (this: MAccountFormattable): Account { |
d0800f76 | 410 | return { |
411 | ...this.Actor.toFormattedJSON(), | |
412 | ||
3fd3ab2d | 413 | id: this.id, |
244e76a5 | 414 | displayName: this.getDisplayName(), |
2422c46b | 415 | description: this.description, |
e024fd6a | 416 | updatedAt: this.updatedAt, |
d0800f76 | 417 | userId: this.userId ?? undefined |
3fd3ab2d C |
418 | } |
419 | } | |
e4f97bab | 420 | |
1ca9f7c3 C |
421 | toFormattedSummaryJSON (this: MAccountSummaryFormattable): AccountSummary { |
422 | const actor = this.Actor.toFormattedSummaryJSON() | |
418d092a C |
423 | |
424 | return { | |
425 | id: this.id, | |
418d092a | 426 | displayName: this.getDisplayName(), |
d0800f76 | 427 | |
428 | name: actor.name, | |
418d092a C |
429 | url: actor.url, |
430 | host: actor.host, | |
d0800f76 | 431 | avatars: actor.avatars, |
432 | ||
433 | // TODO: remove, deprecated in 4.2 | |
418d092a C |
434 | avatar: actor.avatar |
435 | } | |
436 | } | |
437 | ||
b5fecbf4 | 438 | toActivityPubObject (this: MAccountAP) { |
8424c402 | 439 | const obj = this.Actor.toActivityPubObject(this.name) |
2422c46b C |
440 | |
441 | return Object.assign(obj, { | |
442 | summary: this.description | |
443 | }) | |
e4f97bab C |
444 | } |
445 | ||
3fd3ab2d | 446 | isOwned () { |
fadf619a | 447 | return this.Actor.isOwned() |
3fd3ab2d | 448 | } |
244e76a5 | 449 | |
744d0eca C |
450 | isOutdated () { |
451 | return this.Actor.isOutdated() | |
452 | } | |
453 | ||
244e76a5 RK |
454 | getDisplayName () { |
455 | return this.name | |
456 | } | |
bfbd9128 | 457 | |
a59f210f C |
458 | getLocalUrl (this: MAccountActor | MChannelActor) { |
459 | return WEBSERVER.URL + `/accounts/` + this.Actor.preferredUsername | |
460 | } | |
461 | ||
bfbd9128 | 462 | isBlocked () { |
2760b454 | 463 | return this.BlockedBy && this.BlockedBy.length !== 0 |
bfbd9128 | 464 | } |
63c93323 | 465 | } |