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/core-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 withAccountBlockerIds?: number[]
58 @DefaultScope(() => ({
61 model: ActorModel, // Default scope includes avatar and server
67 [ScopeNames.SUMMARY]: (options: SummaryOptions = {}) => {
68 const whereActor = options.whereActor || undefined
70 const serverInclude: IncludeOptions = {
71 attributes: [ 'host' ],
72 model: ServerModel.unscoped(),
76 const queryInclude: Includeable[] = [
78 attributes: [ 'id', 'preferredUsername', 'url', 'serverId', 'avatarId' ],
79 model: ActorModel.unscoped(),
80 required: options.actorRequired ?? true,
86 model: ActorImageModel.unscoped(),
94 const query: FindOptions = {
95 attributes: [ 'id', 'name', 'actorId' ]
98 if (options.withAccountBlockerIds) {
100 attributes: [ 'id' ],
101 model: AccountBlocklistModel.unscoped(),
102 as: 'BlockedAccounts',
106 [Op.in]: options.withAccountBlockerIds
111 serverInclude.include = [
113 attributes: [ 'id' ],
114 model: ServerBlocklistModel.unscoped(),
118 [Op.in]: options.withAccountBlockerIds
125 query.include = queryInclude
131 tableName: 'account',
134 fields: [ 'actorId' ],
138 fields: [ 'applicationId' ]
145 export class AccountModel extends Model<Partial<AttributesOnly<AccountModel>>> {
153 @Is('AccountDescription', value => throwIfNotValid(value, isAccountDescriptionValid, 'description', true))
154 @Column(DataType.STRING(CONSTRAINTS_FIELDS.USERS.DESCRIPTION.max))
163 @ForeignKey(() => ActorModel)
167 @BelongsTo(() => ActorModel, {
175 @ForeignKey(() => UserModel)
179 @BelongsTo(() => UserModel, {
187 @ForeignKey(() => ApplicationModel)
189 applicationId: number
191 @BelongsTo(() => ApplicationModel, {
197 Application: ApplicationModel
199 @HasMany(() => VideoChannelModel, {
206 VideoChannels: VideoChannelModel[]
208 @HasMany(() => VideoPlaylistModel, {
215 VideoPlaylists: VideoPlaylistModel[]
217 @HasMany(() => VideoCommentModel, {
224 VideoComments: VideoCommentModel[]
226 @HasMany(() => AccountBlocklistModel, {
228 name: 'targetAccountId',
231 as: 'BlockedAccounts',
234 BlockedAccounts: AccountBlocklistModel[]
237 static async sendDeleteIfOwned (instance: AccountModel, options) {
238 if (!instance.Actor) {
239 instance.Actor = await instance.$get('Actor', { transaction: options.transaction })
242 await ActorFollowModel.removeFollowsOf(instance.Actor.id, options.transaction)
244 if (instance.isOwned()) {
245 return sendDeleteActor(instance.Actor, options.transaction)
251 static load (id: number, transaction?: Transaction): Promise<MAccountDefault> {
252 return AccountModel.findByPk(id, { transaction })
255 static loadByNameWithHost (nameWithHost: string): Promise<MAccountDefault> {
256 const [ accountName, host ] = nameWithHost.split('@')
258 if (!host || host === WEBSERVER.HOST) return AccountModel.loadLocalByName(accountName)
260 return AccountModel.loadByNameAndHost(accountName, host)
263 static loadLocalByName (name: string): Promise<MAccountDefault> {
285 preferredUsername: name
291 return AccountModel.findOne(query)
294 return ModelCache.Instance.doCache({
295 cacheType: 'local-account-name',
298 // The server actor never change, so we can easily cache it
299 whitelist: () => name === SERVER_ACTOR_NAME
303 static loadByNameAndHost (name: string, host: string): Promise<MAccountDefault> {
310 preferredUsername: name
325 return AccountModel.findOne(query)
328 static loadByUrl (url: string, transaction?: Transaction): Promise<MAccountDefault> {
342 return AccountModel.findOne(query)
345 static listForApi (start: number, count: number, sort: string) {
352 return AccountModel.findAndCountAll(query)
353 .then(({ rows, count }) => {
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 {
410 const actor = this.Actor.toFormattedJSON()
413 displayName: this.getDisplayName(),
414 description: this.description,
415 updatedAt: this.updatedAt,
416 userId: this.userId ? this.userId : undefined
419 return Object.assign(actor, account)
422 toFormattedSummaryJSON (this: MAccountSummaryFormattable): AccountSummary {
423 const actor = this.Actor.toFormattedSummaryJSON()
428 displayName: this.getDisplayName(),
435 toActivityPubObject (this: MAccountAP) {
436 const obj = this.Actor.toActivityPubObject(this.name)
438 return Object.assign(obj, {
439 summary: this.description
444 return this.Actor.isOwned()
448 return this.Actor.isOutdated()
455 getLocalUrl (this: MAccountActor | MChannelActor) {
456 return WEBSERVER.URL + `/accounts/` + this.Actor.preferredUsername
460 return this.BlockedAccounts && this.BlockedAccounts.length !== 0