17 } from 'sequelize-typescript'
18 import { Account, AccountSummary } from '../../../shared/models/actors'
19 import { isAccountDescriptionValid } from '../../helpers/custom-validators/accounts'
20 import { sendDeleteActor } from '../../lib/activitypub/send'
21 import { ActorModel } from '../activitypub/actor'
22 import { ApplicationModel } from '../application/application'
23 import { ServerModel } from '../server/server'
24 import { getSort, throwIfNotValid } from '../utils'
25 import { VideoChannelModel } from '../video/video-channel'
26 import { VideoCommentModel } from '../video/video-comment'
27 import { UserModel } from './user'
28 import { AvatarModel } from '../avatar/avatar'
29 import { VideoPlaylistModel } from '../video/video-playlist'
30 import { CONSTRAINTS_FIELDS, SERVER_ACTOR_NAME, WEBSERVER } from '../../initializers/constants'
31 import { FindOptions, IncludeOptions, Op, Transaction, WhereOptions } from 'sequelize'
32 import { AccountBlocklistModel } from './account-blocklist'
33 import { ServerBlocklistModel } from '../server/server-blocklist'
34 import { ActorFollowModel } from '../activitypub/actor-follow'
35 import { MAccountActor, MAccountAP, MAccountDefault, MAccountFormattable, MAccountSummaryFormattable } from '../../typings/models'
36 import * as Bluebird from 'bluebird'
37 import { ModelCache } from '@server/models/model-cache'
39 export enum ScopeNames {
43 export type SummaryOptions = {
44 whereActor?: WhereOptions
45 withAccountBlockerIds?: number[]
48 @DefaultScope(() => ({
51 model: ActorModel, // Default scope includes avatar and server
57 [ScopeNames.SUMMARY]: (options: SummaryOptions = {}) => {
58 const whereActor = options.whereActor || undefined
60 const serverInclude: IncludeOptions = {
61 attributes: [ 'host' ],
62 model: ServerModel.unscoped(),
66 const query: FindOptions = {
67 attributes: [ 'id', 'name' ],
70 attributes: [ 'id', 'preferredUsername', 'url', 'serverId', 'avatarId' ],
71 model: ActorModel.unscoped(),
78 model: AvatarModel.unscoped(),
86 if (options.withAccountBlockerIds) {
89 model: AccountBlocklistModel.unscoped(),
90 as: 'BlockedAccounts',
94 [Op.in]: options.withAccountBlockerIds
99 serverInclude.include = [
101 attributes: [ 'id' ],
102 model: ServerBlocklistModel.unscoped(),
106 [Op.in]: options.withAccountBlockerIds
117 tableName: 'account',
120 fields: [ 'actorId' ],
124 fields: [ 'applicationId' ]
131 export class AccountModel extends Model<AccountModel> {
139 @Is('AccountDescription', value => throwIfNotValid(value, isAccountDescriptionValid, 'description', true))
140 @Column(DataType.STRING(CONSTRAINTS_FIELDS.USERS.DESCRIPTION.max))
149 @ForeignKey(() => ActorModel)
153 @BelongsTo(() => ActorModel, {
161 @ForeignKey(() => UserModel)
165 @BelongsTo(() => UserModel, {
173 @ForeignKey(() => ApplicationModel)
175 applicationId: number
177 @BelongsTo(() => ApplicationModel, {
183 Application: ApplicationModel
185 @HasMany(() => VideoChannelModel, {
192 VideoChannels: VideoChannelModel[]
194 @HasMany(() => VideoPlaylistModel, {
201 VideoPlaylists: VideoPlaylistModel[]
203 @HasMany(() => VideoCommentModel, {
210 VideoComments: VideoCommentModel[]
212 @HasMany(() => AccountBlocklistModel, {
214 name: 'targetAccountId',
217 as: 'BlockedAccounts',
220 BlockedAccounts: AccountBlocklistModel[]
223 static async sendDeleteIfOwned (instance: AccountModel, options) {
224 if (!instance.Actor) {
225 instance.Actor = await instance.$get('Actor', { transaction: options.transaction })
228 await ActorFollowModel.removeFollowsOf(instance.Actor.id, options.transaction)
229 if (instance.isOwned()) {
230 return sendDeleteActor(instance.Actor, options.transaction)
236 static load (id: number, transaction?: Transaction): Bluebird<MAccountDefault> {
237 return AccountModel.findByPk(id, { transaction })
240 static loadByNameWithHost (nameWithHost: string): Bluebird<MAccountDefault> {
241 const [ accountName, host ] = nameWithHost.split('@')
243 if (!host || host === WEBSERVER.HOST) return AccountModel.loadLocalByName(accountName)
245 return AccountModel.loadByNameAndHost(accountName, host)
248 static loadLocalByName (name: string): Bluebird<MAccountDefault> {
270 preferredUsername: name
276 return AccountModel.findOne(query)
279 return ModelCache.Instance.doCache({
280 cacheType: 'local-account-name',
283 // The server actor never change, so we can easily cache it
284 whitelist: () => name === SERVER_ACTOR_NAME
288 static loadByNameAndHost (name: string, host: string): Bluebird<MAccountDefault> {
295 preferredUsername: name
310 return AccountModel.findOne(query)
313 static loadByUrl (url: string, transaction?: Transaction): Bluebird<MAccountDefault> {
327 return AccountModel.findOne(query)
330 static listForApi (start: number, count: number, sort: string) {
337 return AccountModel.findAndCountAll(query)
338 .then(({ rows, count }) => {
346 static listLocalsForSitemap (sort: string): Bluebird<MAccountActor[]> {
350 order: getSort(sort),
353 attributes: [ 'preferredUsername', 'serverId' ],
354 model: ActorModel.unscoped(),
367 toFormattedJSON (this: MAccountFormattable): Account {
368 const actor = this.Actor.toFormattedJSON()
371 displayName: this.getDisplayName(),
372 description: this.description,
373 createdAt: this.createdAt,
374 updatedAt: this.updatedAt,
375 userId: this.userId ? this.userId : undefined
378 return Object.assign(actor, account)
381 toFormattedSummaryJSON (this: MAccountSummaryFormattable): AccountSummary {
382 const actor = this.Actor.toFormattedSummaryJSON()
387 displayName: this.getDisplayName(),
394 toActivityPubObject (this: MAccountAP) {
395 const obj = this.Actor.toActivityPubObject(this.name)
397 return Object.assign(obj, {
398 summary: this.description
403 return this.Actor.isOwned()
407 return this.Actor.isOutdated()
415 return this.BlockedAccounts && this.BlockedAccounts.length !== 0