1 import { FindOptions, Includeable, IncludeOptions, Op, Transaction, WhereOptions } from 'sequelize'
18 } from 'sequelize-typescript'
19 import { ModelCache } from '@server/models/model-cache'
20 import { AttributesOnly } from '@shared/typescript-utils'
21 import { Account, AccountSummary } from '../../../shared/models/actors'
22 import { isAccountDescriptionValid } from '../../helpers/custom-validators/accounts'
23 import { CONSTRAINTS_FIELDS, SERVER_ACTOR_NAME, WEBSERVER } from '../../initializers/constants'
24 import { sendDeleteActor } from '../../lib/activitypub/send/send-delete'
31 MAccountSummaryFormattable,
33 } from '../../types/models'
34 import { ActorModel } from '../actor/actor'
35 import { ActorFollowModel } from '../actor/actor-follow'
36 import { ActorImageModel } from '../actor/actor-image'
37 import { ApplicationModel } from '../application/application'
38 import { ServerModel } from '../server/server'
39 import { ServerBlocklistModel } from '../server/server-blocklist'
40 import { UserModel } from '../user/user'
41 import { getSort, throwIfNotValid } from '../utils'
42 import { VideoModel } from '../video/video'
43 import { VideoChannelModel } from '../video/video-channel'
44 import { VideoCommentModel } from '../video/video-comment'
45 import { VideoPlaylistModel } from '../video/video-playlist'
46 import { AccountBlocklistModel } from './account-blocklist'
48 export enum ScopeNames {
52 export type SummaryOptions = {
53 actorRequired?: boolean // Default: true
54 whereActor?: WhereOptions
55 whereServer?: WhereOptions
56 withAccountBlockerIds?: number[]
60 @DefaultScope(() => ({
63 model: ActorModel, // Default scope includes avatar and server
69 [ScopeNames.SUMMARY]: (options: SummaryOptions = {}) => {
70 const serverInclude: IncludeOptions = {
71 attributes: [ 'host' ],
72 model: ServerModel.unscoped(),
73 required: !!options.whereServer,
74 where: options.whereServer
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 ]
85 if (options.forCount !== true) {
86 actorInclude.include.push({
87 model: ActorImageModel,
93 const queryInclude: Includeable[] = [
97 const query: FindOptions = {
98 attributes: [ 'id', 'name', 'actorId' ]
101 if (options.withAccountBlockerIds) {
103 attributes: [ 'id' ],
104 model: AccountBlocklistModel.unscoped(),
109 [Op.in]: options.withAccountBlockerIds
114 serverInclude.include = [
116 attributes: [ 'id' ],
117 model: ServerBlocklistModel.unscoped(),
121 [Op.in]: options.withAccountBlockerIds
128 query.include = queryInclude
134 tableName: 'account',
137 fields: [ 'actorId' ],
141 fields: [ 'applicationId' ]
148 export class AccountModel extends Model<Partial<AttributesOnly<AccountModel>>> {
156 @Is('AccountDescription', value => throwIfNotValid(value, isAccountDescriptionValid, 'description', true))
157 @Column(DataType.STRING(CONSTRAINTS_FIELDS.USERS.DESCRIPTION.max))
166 @ForeignKey(() => ActorModel)
170 @BelongsTo(() => ActorModel, {
178 @ForeignKey(() => UserModel)
182 @BelongsTo(() => UserModel, {
190 @ForeignKey(() => ApplicationModel)
192 applicationId: number
194 @BelongsTo(() => ApplicationModel, {
200 Application: ApplicationModel
202 @HasMany(() => VideoChannelModel, {
209 VideoChannels: VideoChannelModel[]
211 @HasMany(() => VideoPlaylistModel, {
218 VideoPlaylists: VideoPlaylistModel[]
220 @HasMany(() => VideoCommentModel, {
227 VideoComments: VideoCommentModel[]
229 @HasMany(() => AccountBlocklistModel, {
231 name: 'targetAccountId',
237 BlockedBy: AccountBlocklistModel[]
240 static async sendDeleteIfOwned (instance: AccountModel, options) {
241 if (!instance.Actor) {
242 instance.Actor = await instance.$get('Actor', { transaction: options.transaction })
245 await ActorFollowModel.removeFollowsOf(instance.Actor.id, options.transaction)
247 if (instance.isOwned()) {
248 return sendDeleteActor(instance.Actor, options.transaction)
254 static load (id: number, transaction?: Transaction): Promise<MAccountDefault> {
255 return AccountModel.findByPk(id, { transaction })
258 static loadByNameWithHost (nameWithHost: string): Promise<MAccountDefault> {
259 const [ accountName, host ] = nameWithHost.split('@')
261 if (!host || host === WEBSERVER.HOST) return AccountModel.loadLocalByName(accountName)
263 return AccountModel.loadByNameAndHost(accountName, host)
266 static loadLocalByName (name: string): Promise<MAccountDefault> {
288 preferredUsername: name
294 return AccountModel.findOne(query)
297 return ModelCache.Instance.doCache({
298 cacheType: 'local-account-name',
301 // The server actor never change, so we can easily cache it
302 whitelist: () => name === SERVER_ACTOR_NAME
306 static loadByNameAndHost (name: string, host: string): Promise<MAccountDefault> {
313 preferredUsername: name
328 return AccountModel.findOne(query)
331 static loadByUrl (url: string, transaction?: Transaction): Promise<MAccountDefault> {
345 return AccountModel.findOne(query)
348 static listForApi (start: number, count: number, sort: string) {
356 AccountModel.count(),
357 AccountModel.findAll(query)
358 ]).then(([ total, data ]) => ({ total, data }))
361 static loadAccountIdFromVideo (videoId: number): Promise<MAccount> {
365 attributes: [ 'id', 'accountId' ],
366 model: VideoChannelModel.unscoped(),
370 attributes: [ 'id', 'channelId' ],
371 model: VideoModel.unscoped(),
381 return AccountModel.findOne(query)
384 static listLocalsForSitemap (sort: string): Promise<MAccountActor[]> {
388 order: getSort(sort),
391 attributes: [ 'preferredUsername', 'serverId' ],
392 model: ActorModel.unscoped(),
406 return WEBSERVER.URL + '/accounts/' + this.Actor.getIdentifier()
409 toFormattedJSON (this: MAccountFormattable): Account {
411 ...this.Actor.toFormattedJSON(),
414 displayName: this.getDisplayName(),
415 description: this.description,
416 updatedAt: this.updatedAt,
417 userId: this.userId ?? undefined
421 toFormattedSummaryJSON (this: MAccountSummaryFormattable): AccountSummary {
422 const actor = this.Actor.toFormattedSummaryJSON()
426 displayName: this.getDisplayName(),
431 avatars: actor.avatars,
433 // TODO: remove, deprecated in 4.2
438 toActivityPubObject (this: MAccountAP) {
439 const obj = this.Actor.toActivityPubObject(this.name)
441 return Object.assign(obj, {
442 summary: this.description
447 return this.Actor.isOwned()
451 return this.Actor.isOutdated()
458 getLocalUrl (this: MAccountActor | MChannelActor) {
459 return WEBSERVER.URL + `/accounts/` + this.Actor.preferredUsername
463 return this.BlockedBy && this.BlockedBy.length !== 0