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, 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, MAccountDefault } from '../../typings/models'
36 import * as Bluebird from 'bluebird'
38 export enum ScopeNames {
42 export type SummaryOptions = {
43 whereActor?: WhereOptions
44 withAccountBlockerIds?: number[]
47 @DefaultScope(() => ({
50 model: ActorModel, // Default scope includes avatar and server
56 [ ScopeNames.SUMMARY ]: (options: SummaryOptions = {}) => {
57 const whereActor = options.whereActor || undefined
59 const serverInclude: IncludeOptions = {
60 attributes: [ 'host' ],
61 model: ServerModel.unscoped(),
65 const query: FindOptions = {
66 attributes: [ 'id', 'name' ],
69 attributes: [ 'id', 'preferredUsername', 'url', 'serverId', 'avatarId' ],
70 model: ActorModel.unscoped(),
77 model: AvatarModel.unscoped(),
85 if (options.withAccountBlockerIds) {
88 model: AccountBlocklistModel.unscoped(),
89 as: 'BlockedAccounts',
93 [Op.in]: options.withAccountBlockerIds
98 serverInclude.include = [
100 attributes: [ 'id' ],
101 model: ServerBlocklistModel.unscoped(),
105 [Op.in]: options.withAccountBlockerIds
116 tableName: 'account',
119 fields: [ 'actorId' ],
123 fields: [ 'applicationId' ]
130 export class AccountModel extends Model<AccountModel> {
138 @Is('AccountDescription', value => throwIfNotValid(value, isAccountDescriptionValid, 'description', true))
139 @Column(DataType.STRING(CONSTRAINTS_FIELDS.USERS.DESCRIPTION.max))
148 @ForeignKey(() => ActorModel)
152 @BelongsTo(() => ActorModel, {
160 @ForeignKey(() => UserModel)
164 @BelongsTo(() => UserModel, {
172 @ForeignKey(() => ApplicationModel)
174 applicationId: number
176 @BelongsTo(() => ApplicationModel, {
182 Application: ApplicationModel
184 @HasMany(() => VideoChannelModel, {
191 VideoChannels: VideoChannelModel[]
193 @HasMany(() => VideoPlaylistModel, {
200 VideoPlaylists: VideoPlaylistModel[]
202 @HasMany(() => VideoCommentModel, {
209 VideoComments: VideoCommentModel[]
211 @HasMany(() => AccountBlocklistModel, {
213 name: 'targetAccountId',
216 as: 'BlockedAccounts',
219 BlockedAccounts: AccountBlocklistModel[]
222 static async sendDeleteIfOwned (instance: AccountModel, options) {
223 if (!instance.Actor) {
224 instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) as ActorModel
227 await ActorFollowModel.removeFollowsOf(instance.Actor.id, options.transaction)
228 if (instance.isOwned()) {
229 return sendDeleteActor(instance.Actor, options.transaction)
235 static load (id: number, transaction?: Transaction): Bluebird<MAccountDefault> {
236 return AccountModel.findByPk(id, { transaction })
239 static loadByNameWithHost (nameWithHost: string): Bluebird<MAccountDefault> {
240 const [ accountName, host ] = nameWithHost.split('@')
242 if (!host || host === WEBSERVER.HOST) return AccountModel.loadLocalByName(accountName)
244 return AccountModel.loadByNameAndHost(accountName, host)
247 static loadLocalByName (name: string): Bluebird<MAccountDefault> {
268 preferredUsername: name
274 return AccountModel.findOne(query)
277 static loadByNameAndHost (name: string, host: string): Bluebird<MAccountDefault> {
284 preferredUsername: name
299 return AccountModel.findOne(query)
302 static loadByUrl (url: string, transaction?: Transaction): Bluebird<MAccountDefault> {
316 return AccountModel.findOne(query)
319 static listForApi (start: number, count: number, sort: string) {
326 return AccountModel.findAndCountAll(query)
327 .then(({ rows, count }) => {
335 static listLocalsForSitemap (sort: string): Bluebird<MAccountActor[]> {
339 order: getSort(sort),
342 attributes: [ 'preferredUsername', 'serverId' ],
343 model: ActorModel.unscoped(),
356 toFormattedJSON (): Account {
357 const actor = this.Actor.toFormattedJSON()
360 displayName: this.getDisplayName(),
361 description: this.description,
362 createdAt: this.createdAt,
363 updatedAt: this.updatedAt,
364 userId: this.userId ? this.userId : undefined
367 return Object.assign(actor, account)
370 toFormattedSummaryJSON (): AccountSummary {
371 const actor = this.Actor.toFormattedJSON()
376 displayName: this.getDisplayName(),
383 toActivityPubObject () {
384 const obj = this.Actor.toActivityPubObject(this.name, 'Account')
386 return Object.assign(obj, {
387 summary: this.description
392 return this.Actor.isOwned()
396 return this.Actor.isOutdated()
404 return this.BlockedAccounts && this.BlockedAccounts.length !== 0