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, MAccount } from '../../types/models'
36 import * as Bluebird from 'bluebird'
37 import { ModelCache } from '@server/models/model-cache'
38 import { VideoModel } from '../video/video'
40 export enum ScopeNames {
44 export type SummaryOptions = {
45 actorRequired?: boolean // Default: true
46 whereActor?: WhereOptions
47 withAccountBlockerIds?: number[]
50 @DefaultScope(() => ({
53 model: ActorModel, // Default scope includes avatar and server
59 [ScopeNames.SUMMARY]: (options: SummaryOptions = {}) => {
60 const whereActor = options.whereActor || undefined
62 const serverInclude: IncludeOptions = {
63 attributes: [ 'host' ],
64 model: ServerModel.unscoped(),
68 const query: FindOptions = {
69 attributes: [ 'id', 'name', 'actorId' ],
72 attributes: [ 'id', 'preferredUsername', 'url', 'serverId', 'avatarId' ],
73 model: ActorModel.unscoped(),
74 required: options.actorRequired ?? true,
80 model: AvatarModel.unscoped(),
88 if (options.withAccountBlockerIds) {
91 model: AccountBlocklistModel.unscoped(),
92 as: 'BlockedAccounts',
96 [Op.in]: options.withAccountBlockerIds
101 serverInclude.include = [
103 attributes: [ 'id' ],
104 model: ServerBlocklistModel.unscoped(),
108 [Op.in]: options.withAccountBlockerIds
119 tableName: 'account',
122 fields: [ 'actorId' ],
126 fields: [ 'applicationId' ]
133 export class AccountModel extends Model<AccountModel> {
141 @Is('AccountDescription', value => throwIfNotValid(value, isAccountDescriptionValid, 'description', true))
142 @Column(DataType.STRING(CONSTRAINTS_FIELDS.USERS.DESCRIPTION.max))
151 @ForeignKey(() => ActorModel)
155 @BelongsTo(() => ActorModel, {
163 @ForeignKey(() => UserModel)
167 @BelongsTo(() => UserModel, {
175 @ForeignKey(() => ApplicationModel)
177 applicationId: number
179 @BelongsTo(() => ApplicationModel, {
185 Application: ApplicationModel
187 @HasMany(() => VideoChannelModel, {
194 VideoChannels: VideoChannelModel[]
196 @HasMany(() => VideoPlaylistModel, {
203 VideoPlaylists: VideoPlaylistModel[]
205 @HasMany(() => VideoCommentModel, {
212 VideoComments: VideoCommentModel[]
214 @HasMany(() => AccountBlocklistModel, {
216 name: 'targetAccountId',
219 as: 'BlockedAccounts',
222 BlockedAccounts: AccountBlocklistModel[]
225 static async sendDeleteIfOwned (instance: AccountModel, options) {
226 if (!instance.Actor) {
227 instance.Actor = await instance.$get('Actor', { transaction: options.transaction })
230 await ActorFollowModel.removeFollowsOf(instance.Actor.id, options.transaction)
231 if (instance.isOwned()) {
232 return sendDeleteActor(instance.Actor, options.transaction)
238 static load (id: number, transaction?: Transaction): Bluebird<MAccountDefault> {
239 return AccountModel.findByPk(id, { transaction })
242 static loadByNameWithHost (nameWithHost: string): Bluebird<MAccountDefault> {
243 const [ accountName, host ] = nameWithHost.split('@')
245 if (!host || host === WEBSERVER.HOST) return AccountModel.loadLocalByName(accountName)
247 return AccountModel.loadByNameAndHost(accountName, host)
250 static loadLocalByName (name: string): Bluebird<MAccountDefault> {
272 preferredUsername: name
278 return AccountModel.findOne(query)
281 return ModelCache.Instance.doCache({
282 cacheType: 'local-account-name',
285 // The server actor never change, so we can easily cache it
286 whitelist: () => name === SERVER_ACTOR_NAME
290 static loadByNameAndHost (name: string, host: string): Bluebird<MAccountDefault> {
297 preferredUsername: name
312 return AccountModel.findOne(query)
315 static loadByUrl (url: string, transaction?: Transaction): Bluebird<MAccountDefault> {
329 return AccountModel.findOne(query)
332 static listForApi (start: number, count: number, sort: string) {
339 return AccountModel.findAndCountAll(query)
340 .then(({ rows, count }) => {
348 static loadAccountIdFromVideo (videoId: number): Bluebird<MAccount> {
352 attributes: [ 'id', 'accountId' ],
353 model: VideoChannelModel.unscoped(),
357 attributes: [ 'id', 'channelId' ],
358 model: VideoModel.unscoped(),
368 return AccountModel.findOne(query)
371 static listLocalsForSitemap (sort: string): Bluebird<MAccountActor[]> {
375 order: getSort(sort),
378 attributes: [ 'preferredUsername', 'serverId' ],
379 model: ActorModel.unscoped(),
393 return WEBSERVER.URL + '/accounts/' + this.Actor.getIdentifier()
396 toFormattedJSON (this: MAccountFormattable): Account {
397 const actor = this.Actor.toFormattedJSON()
400 displayName: this.getDisplayName(),
401 description: this.description,
402 createdAt: this.createdAt,
403 updatedAt: this.updatedAt,
404 userId: this.userId ? this.userId : undefined
407 return Object.assign(actor, account)
410 toFormattedSummaryJSON (this: MAccountSummaryFormattable): AccountSummary {
411 const actor = this.Actor.toFormattedSummaryJSON()
416 displayName: this.getDisplayName(),
423 toActivityPubObject (this: MAccountAP) {
424 const obj = this.Actor.toActivityPubObject(this.name)
426 return Object.assign(obj, {
427 summary: this.description
432 return this.Actor.isOwned()
436 return this.Actor.isOutdated()
444 return this.BlockedAccounts && this.BlockedAccounts.length !== 0