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 whereActor?: WhereOptions
46 withAccountBlockerIds?: number[]
49 @DefaultScope(() => ({
52 model: ActorModel, // Default scope includes avatar and server
58 [ScopeNames.SUMMARY]: (options: SummaryOptions = {}) => {
59 const whereActor = options.whereActor || undefined
61 const serverInclude: IncludeOptions = {
62 attributes: [ 'host' ],
63 model: ServerModel.unscoped(),
67 const query: FindOptions = {
68 attributes: [ 'id', 'name' ],
71 attributes: [ 'id', 'preferredUsername', 'url', 'serverId', 'avatarId' ],
72 model: ActorModel.unscoped(),
79 model: AvatarModel.unscoped(),
87 if (options.withAccountBlockerIds) {
90 model: AccountBlocklistModel.unscoped(),
91 as: 'BlockedAccounts',
95 [Op.in]: options.withAccountBlockerIds
100 serverInclude.include = [
102 attributes: [ 'id' ],
103 model: ServerBlocklistModel.unscoped(),
107 [Op.in]: options.withAccountBlockerIds
118 tableName: 'account',
121 fields: [ 'actorId' ],
125 fields: [ 'applicationId' ]
132 export class AccountModel extends Model<AccountModel> {
140 @Is('AccountDescription', value => throwIfNotValid(value, isAccountDescriptionValid, 'description', true))
141 @Column(DataType.STRING(CONSTRAINTS_FIELDS.USERS.DESCRIPTION.max))
150 @ForeignKey(() => ActorModel)
154 @BelongsTo(() => ActorModel, {
162 @ForeignKey(() => UserModel)
166 @BelongsTo(() => UserModel, {
174 @ForeignKey(() => ApplicationModel)
176 applicationId: number
178 @BelongsTo(() => ApplicationModel, {
184 Application: ApplicationModel
186 @HasMany(() => VideoChannelModel, {
193 VideoChannels: VideoChannelModel[]
195 @HasMany(() => VideoPlaylistModel, {
202 VideoPlaylists: VideoPlaylistModel[]
204 @HasMany(() => VideoCommentModel, {
211 VideoComments: VideoCommentModel[]
213 @HasMany(() => AccountBlocklistModel, {
215 name: 'targetAccountId',
218 as: 'BlockedAccounts',
221 BlockedAccounts: AccountBlocklistModel[]
224 static async sendDeleteIfOwned (instance: AccountModel, options) {
225 if (!instance.Actor) {
226 instance.Actor = await instance.$get('Actor', { transaction: options.transaction })
229 await ActorFollowModel.removeFollowsOf(instance.Actor.id, options.transaction)
230 if (instance.isOwned()) {
231 return sendDeleteActor(instance.Actor, options.transaction)
237 static load (id: number, transaction?: Transaction): Bluebird<MAccountDefault> {
238 return AccountModel.findByPk(id, { transaction })
241 static loadByNameWithHost (nameWithHost: string): Bluebird<MAccountDefault> {
242 const [ accountName, host ] = nameWithHost.split('@')
244 if (!host || host === WEBSERVER.HOST) return AccountModel.loadLocalByName(accountName)
246 return AccountModel.loadByNameAndHost(accountName, host)
249 static loadLocalByName (name: string): Bluebird<MAccountDefault> {
271 preferredUsername: name
277 return AccountModel.findOne(query)
280 return ModelCache.Instance.doCache({
281 cacheType: 'local-account-name',
284 // The server actor never change, so we can easily cache it
285 whitelist: () => name === SERVER_ACTOR_NAME
289 static loadByNameAndHost (name: string, host: string): Bluebird<MAccountDefault> {
296 preferredUsername: name
311 return AccountModel.findOne(query)
314 static loadByUrl (url: string, transaction?: Transaction): Bluebird<MAccountDefault> {
328 return AccountModel.findOne(query)
331 static listForApi (start: number, count: number, sort: string) {
338 return AccountModel.findAndCountAll(query)
339 .then(({ rows, count }) => {
347 static loadAccountIdFromVideo (videoId: number): Bluebird<MAccount> {
351 attributes: [ 'id', 'accountId' ],
352 model: VideoChannelModel.unscoped(),
356 attributes: [ 'id', 'channelId' ],
357 model: VideoModel.unscoped(),
367 return AccountModel.findOne(query)
370 static listLocalsForSitemap (sort: string): Bluebird<MAccountActor[]> {
374 order: getSort(sort),
377 attributes: [ 'preferredUsername', 'serverId' ],
378 model: ActorModel.unscoped(),
391 toFormattedJSON (this: MAccountFormattable): Account {
392 const actor = this.Actor.toFormattedJSON()
395 displayName: this.getDisplayName(),
396 description: this.description,
397 createdAt: this.createdAt,
398 updatedAt: this.updatedAt,
399 userId: this.userId ? this.userId : undefined
402 return Object.assign(actor, account)
405 toFormattedSummaryJSON (this: MAccountSummaryFormattable): AccountSummary {
406 const actor = this.Actor.toFormattedSummaryJSON()
411 displayName: this.getDisplayName(),
418 toActivityPubObject (this: MAccountAP) {
419 const obj = this.Actor.toActivityPubObject(this.name)
421 return Object.assign(obj, {
422 summary: this.description
427 return this.Actor.isOwned()
431 return this.Actor.isOutdated()
439 return this.BlockedAccounts && this.BlockedAccounts.length !== 0