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 { AvatarModel } from '../avatar/avatar'
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: AvatarModel.unscoped(),
92 const query: FindOptions = {
93 attributes: [ 'id', 'name', 'actorId' ]
96 if (options.withAccountBlockerIds) {
99 model: AccountBlocklistModel.unscoped(),
100 as: 'BlockedAccounts',
104 [Op.in]: options.withAccountBlockerIds
109 serverInclude.include = [
111 attributes: [ 'id' ],
112 model: ServerBlocklistModel.unscoped(),
116 [Op.in]: options.withAccountBlockerIds
123 query.include = queryInclude
129 tableName: 'account',
132 fields: [ 'actorId' ],
136 fields: [ 'applicationId' ]
143 export class AccountModel extends Model {
151 @Is('AccountDescription', value => throwIfNotValid(value, isAccountDescriptionValid, 'description', true))
152 @Column(DataType.STRING(CONSTRAINTS_FIELDS.USERS.DESCRIPTION.max))
161 @ForeignKey(() => ActorModel)
165 @BelongsTo(() => ActorModel, {
173 @ForeignKey(() => UserModel)
177 @BelongsTo(() => UserModel, {
185 @ForeignKey(() => ApplicationModel)
187 applicationId: number
189 @BelongsTo(() => ApplicationModel, {
195 Application: ApplicationModel
197 @HasMany(() => VideoChannelModel, {
204 VideoChannels: VideoChannelModel[]
206 @HasMany(() => VideoPlaylistModel, {
213 VideoPlaylists: VideoPlaylistModel[]
215 @HasMany(() => VideoCommentModel, {
222 VideoComments: VideoCommentModel[]
224 @HasMany(() => AccountBlocklistModel, {
226 name: 'targetAccountId',
229 as: 'BlockedAccounts',
232 BlockedAccounts: AccountBlocklistModel[]
235 static async sendDeleteIfOwned (instance: AccountModel, options) {
236 if (!instance.Actor) {
237 instance.Actor = await instance.$get('Actor', { transaction: options.transaction })
240 await ActorFollowModel.removeFollowsOf(instance.Actor.id, options.transaction)
242 if (instance.isOwned()) {
243 return sendDeleteActor(instance.Actor, options.transaction)
249 static load (id: number, transaction?: Transaction): Promise<MAccountDefault> {
250 return AccountModel.findByPk(id, { transaction })
253 static loadByNameWithHost (nameWithHost: string): Promise<MAccountDefault> {
254 const [ accountName, host ] = nameWithHost.split('@')
256 if (!host || host === WEBSERVER.HOST) return AccountModel.loadLocalByName(accountName)
258 return AccountModel.loadByNameAndHost(accountName, host)
261 static loadLocalByName (name: string): Promise<MAccountDefault> {
283 preferredUsername: name
289 return AccountModel.findOne(query)
292 return ModelCache.Instance.doCache({
293 cacheType: 'local-account-name',
296 // The server actor never change, so we can easily cache it
297 whitelist: () => name === SERVER_ACTOR_NAME
301 static loadByNameAndHost (name: string, host: string): Promise<MAccountDefault> {
308 preferredUsername: name
323 return AccountModel.findOne(query)
326 static loadByUrl (url: string, transaction?: Transaction): Promise<MAccountDefault> {
340 return AccountModel.findOne(query)
343 static listForApi (start: number, count: number, sort: string) {
350 return AccountModel.findAndCountAll(query)
351 .then(({ rows, count }) => {
359 static loadAccountIdFromVideo (videoId: number): Promise<MAccount> {
363 attributes: [ 'id', 'accountId' ],
364 model: VideoChannelModel.unscoped(),
368 attributes: [ 'id', 'channelId' ],
369 model: VideoModel.unscoped(),
379 return AccountModel.findOne(query)
382 static listLocalsForSitemap (sort: string): Promise<MAccountActor[]> {
386 order: getSort(sort),
389 attributes: [ 'preferredUsername', 'serverId' ],
390 model: ActorModel.unscoped(),
404 return WEBSERVER.URL + '/accounts/' + this.Actor.getIdentifier()
407 toFormattedJSON (this: MAccountFormattable): Account {
408 const actor = this.Actor.toFormattedJSON()
411 displayName: this.getDisplayName(),
412 description: this.description,
413 createdAt: this.createdAt,
414 updatedAt: this.updatedAt,
415 userId: this.userId ? this.userId : undefined
418 return Object.assign(actor, account)
421 toFormattedSummaryJSON (this: MAccountSummaryFormattable): AccountSummary {
422 const actor = this.Actor.toFormattedSummaryJSON()
427 displayName: this.getDisplayName(),
434 toActivityPubObject (this: MAccountAP) {
435 const obj = this.Actor.toActivityPubObject(this.name)
437 return Object.assign(obj, {
438 summary: this.description
443 return this.Actor.isOwned()
447 return this.Actor.isOutdated()
454 getLocalUrl (this: MAccountActor | MChannelActor) {
455 return WEBSERVER.URL + `/accounts/` + this.Actor.preferredUsername
459 return this.BlockedAccounts && this.BlockedAccounts.length !== 0