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