1 import { FindOptions, Includeable, IncludeOptions, Op, Transaction, WhereOptions } from 'sequelize'
18 } from 'sequelize-typescript'
19 import { ModelCache } from '@server/models/model-cache'
20 import { Account, AccountSummary } from '../../../shared/models/actors'
21 import { isAccountDescriptionValid } from '../../helpers/custom-validators/accounts'
22 import { CONSTRAINTS_FIELDS, SERVER_ACTOR_NAME, WEBSERVER } from '../../initializers/constants'
23 import { sendDeleteActor } from '../../lib/activitypub/send'
30 MAccountSummaryFormattable,
32 } from '../../types/models'
33 import { ActorModel } from '../activitypub/actor'
34 import { ActorFollowModel } from '../activitypub/actor-follow'
35 import { ApplicationModel } from '../application/application'
36 import { ActorImageModel } from './actor-image'
37 import { ServerModel } from '../server/server'
38 import { ServerBlocklistModel } from '../server/server-blocklist'
39 import { getSort, throwIfNotValid } from '../utils'
40 import { VideoModel } from '../video/video'
41 import { VideoChannelModel } from '../video/video-channel'
42 import { VideoCommentModel } from '../video/video-comment'
43 import { VideoPlaylistModel } from '../video/video-playlist'
44 import { AccountBlocklistModel } from './account-blocklist'
45 import { UserModel } from './user'
47 export enum ScopeNames {
51 export type SummaryOptions = {
52 actorRequired?: boolean // Default: true
53 whereActor?: WhereOptions
54 withAccountBlockerIds?: number[]
57 @DefaultScope(() => ({
60 model: ActorModel, // Default scope includes avatar and server
66 [ScopeNames.SUMMARY]: (options: SummaryOptions = {}) => {
67 const whereActor = options.whereActor || undefined
69 const serverInclude: IncludeOptions = {
70 attributes: [ 'host' ],
71 model: ServerModel.unscoped(),
75 const queryInclude: Includeable[] = [
77 attributes: [ 'id', 'preferredUsername', 'url', 'serverId', 'avatarId' ],
78 model: ActorModel.unscoped(),
79 required: options.actorRequired ?? true,
85 model: ActorImageModel.unscoped(),
93 const query: FindOptions = {
94 attributes: [ 'id', 'name', 'actorId' ]
97 if (options.withAccountBlockerIds) {
100 model: AccountBlocklistModel.unscoped(),
101 as: 'BlockedAccounts',
105 [Op.in]: options.withAccountBlockerIds
110 serverInclude.include = [
112 attributes: [ 'id' ],
113 model: ServerBlocklistModel.unscoped(),
117 [Op.in]: options.withAccountBlockerIds
124 query.include = queryInclude
130 tableName: 'account',
133 fields: [ 'actorId' ],
137 fields: [ 'applicationId' ]
144 export class AccountModel extends Model {
152 @Is('AccountDescription', value => throwIfNotValid(value, isAccountDescriptionValid, 'description', true))
153 @Column(DataType.STRING(CONSTRAINTS_FIELDS.USERS.DESCRIPTION.max))
162 @ForeignKey(() => ActorModel)
166 @BelongsTo(() => ActorModel, {
174 @ForeignKey(() => UserModel)
178 @BelongsTo(() => UserModel, {
186 @ForeignKey(() => ApplicationModel)
188 applicationId: number
190 @BelongsTo(() => ApplicationModel, {
196 Application: ApplicationModel
198 @HasMany(() => VideoChannelModel, {
205 VideoChannels: VideoChannelModel[]
207 @HasMany(() => VideoPlaylistModel, {
214 VideoPlaylists: VideoPlaylistModel[]
216 @HasMany(() => VideoCommentModel, {
223 VideoComments: VideoCommentModel[]
225 @HasMany(() => AccountBlocklistModel, {
227 name: 'targetAccountId',
230 as: 'BlockedAccounts',
233 BlockedAccounts: AccountBlocklistModel[]
236 static async sendDeleteIfOwned (instance: AccountModel, options) {
237 if (!instance.Actor) {
238 instance.Actor = await instance.$get('Actor', { transaction: options.transaction })
241 await ActorFollowModel.removeFollowsOf(instance.Actor.id, options.transaction)
243 if (instance.isOwned()) {
244 return sendDeleteActor(instance.Actor, options.transaction)
250 static load (id: number, transaction?: Transaction): Promise<MAccountDefault> {
251 return AccountModel.findByPk(id, { transaction })
254 static loadByNameWithHost (nameWithHost: string): Promise<MAccountDefault> {
255 const [ accountName, host ] = nameWithHost.split('@')
257 if (!host || host === WEBSERVER.HOST) return AccountModel.loadLocalByName(accountName)
259 return AccountModel.loadByNameAndHost(accountName, host)
262 static loadLocalByName (name: string): Promise<MAccountDefault> {
284 preferredUsername: name
290 return AccountModel.findOne(query)
293 return ModelCache.Instance.doCache({
294 cacheType: 'local-account-name',
297 // The server actor never change, so we can easily cache it
298 whitelist: () => name === SERVER_ACTOR_NAME
302 static loadByNameAndHost (name: string, host: string): Promise<MAccountDefault> {
309 preferredUsername: name
324 return AccountModel.findOne(query)
327 static loadByUrl (url: string, transaction?: Transaction): Promise<MAccountDefault> {
341 return AccountModel.findOne(query)
344 static listForApi (start: number, count: number, sort: string) {
351 return AccountModel.findAndCountAll(query)
352 .then(({ rows, count }) => {
360 static loadAccountIdFromVideo (videoId: number): Promise<MAccount> {
364 attributes: [ 'id', 'accountId' ],
365 model: VideoChannelModel.unscoped(),
369 attributes: [ 'id', 'channelId' ],
370 model: VideoModel.unscoped(),
380 return AccountModel.findOne(query)
383 static listLocalsForSitemap (sort: string): Promise<MAccountActor[]> {
387 order: getSort(sort),
390 attributes: [ 'preferredUsername', 'serverId' ],
391 model: ActorModel.unscoped(),
405 return WEBSERVER.URL + '/accounts/' + this.Actor.getIdentifier()
408 toFormattedJSON (this: MAccountFormattable): Account {
409 const actor = this.Actor.toFormattedJSON()
412 displayName: this.getDisplayName(),
413 description: this.description,
414 createdAt: this.createdAt,
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