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