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