16 } from 'sequelize-typescript'
17 import { Account, AccountSummary } from '../../../shared/models/actors'
18 import { isAccountDescriptionValid } from '../../helpers/custom-validators/accounts'
19 import { sendDeleteActor } from '../../lib/activitypub/send'
20 import { ActorModel } from '../activitypub/actor'
21 import { ApplicationModel } from '../application/application'
22 import { ServerModel } from '../server/server'
23 import { getSort, throwIfNotValid } from '../utils'
24 import { VideoChannelModel } from '../video/video-channel'
25 import { VideoCommentModel } from '../video/video-comment'
26 import { UserModel } from './user'
27 import { AvatarModel } from '../avatar/avatar'
28 import { VideoPlaylistModel } from '../video/video-playlist'
29 import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants'
30 import { FindOptions, IncludeOptions, Op, Transaction, WhereOptions } from 'sequelize'
31 import { AccountBlocklistModel } from './account-blocklist'
32 import { ServerBlocklistModel } from '../server/server-blocklist'
33 import { ActorFollowModel } from '../activitypub/actor-follow'
35 export enum ScopeNames {
39 export type SummaryOptions = {
40 whereActor?: WhereOptions
41 withAccountBlockerIds?: number[]
44 @DefaultScope(() => ({
47 model: ActorModel, // Default scope includes avatar and server
53 [ ScopeNames.SUMMARY ]: (options: SummaryOptions = {}) => {
54 const whereActor = options.whereActor || undefined
56 const serverInclude: IncludeOptions = {
57 attributes: [ 'host' ],
58 model: ServerModel.unscoped(),
62 const query: FindOptions = {
63 attributes: [ 'id', 'name' ],
66 attributes: [ 'id', 'preferredUsername', 'url', 'serverId', 'avatarId' ],
67 model: ActorModel.unscoped(),
74 model: AvatarModel.unscoped(),
82 if (options.withAccountBlockerIds) {
85 model: AccountBlocklistModel.unscoped(),
86 as: 'BlockedAccounts',
90 [Op.in]: options.withAccountBlockerIds
95 serverInclude.include = [
98 model: ServerBlocklistModel.unscoped(),
102 [Op.in]: options.withAccountBlockerIds
113 tableName: 'account',
116 fields: [ 'actorId' ],
120 fields: [ 'applicationId' ]
127 export class AccountModel extends Model<AccountModel> {
135 @Is('AccountDescription', value => throwIfNotValid(value, isAccountDescriptionValid, 'description', true))
136 @Column(DataType.STRING(CONSTRAINTS_FIELDS.USERS.DESCRIPTION.max))
145 @ForeignKey(() => ActorModel)
149 @BelongsTo(() => ActorModel, {
157 @ForeignKey(() => UserModel)
161 @BelongsTo(() => UserModel, {
169 @ForeignKey(() => ApplicationModel)
171 applicationId: number
173 @BelongsTo(() => ApplicationModel, {
179 Application: ApplicationModel
181 @HasMany(() => VideoChannelModel, {
188 VideoChannels: VideoChannelModel[]
190 @HasMany(() => VideoPlaylistModel, {
197 VideoPlaylists: VideoPlaylistModel[]
199 @HasMany(() => VideoCommentModel, {
206 VideoComments: VideoCommentModel[]
208 @HasMany(() => AccountBlocklistModel, {
210 name: 'targetAccountId',
213 as: 'BlockedAccounts',
216 BlockedAccounts: AccountBlocklistModel[]
219 static async sendDeleteIfOwned (instance: AccountModel, options) {
220 if (!instance.Actor) {
221 instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) as ActorModel
224 await ActorFollowModel.removeFollowsOf(instance.Actor.id, options.transaction)
225 if (instance.isOwned()) {
226 return sendDeleteActor(instance.Actor, options.transaction)
232 static load (id: number, transaction?: Transaction) {
233 return AccountModel.findByPk(id, { transaction })
236 static loadByNameWithHost (nameWithHost: string) {
237 const [ accountName, host ] = nameWithHost.split('@')
239 if (!host || host === WEBSERVER.HOST) return AccountModel.loadLocalByName(accountName)
241 return AccountModel.loadByNameAndHost(accountName, host)
244 static loadLocalByName (name: string) {
265 preferredUsername: name
271 return AccountModel.findOne(query)
274 static loadByNameAndHost (name: string, host: string) {
281 preferredUsername: name
296 return AccountModel.findOne(query)
299 static loadByUrl (url: string, transaction?: Transaction) {
313 return AccountModel.findOne(query)
316 static listForApi (start: number, count: number, sort: string) {
323 return AccountModel.findAndCountAll(query)
324 .then(({ rows, count }) => {
332 static listLocalsForSitemap (sort: string) {
336 order: getSort(sort),
339 attributes: [ 'preferredUsername', 'serverId' ],
340 model: ActorModel.unscoped(),
353 toFormattedJSON (): Account {
354 const actor = this.Actor.toFormattedJSON()
357 displayName: this.getDisplayName(),
358 description: this.description,
359 createdAt: this.createdAt,
360 updatedAt: this.updatedAt,
361 userId: this.userId ? this.userId : undefined
364 return Object.assign(actor, account)
367 toFormattedSummaryJSON (): AccountSummary {
368 const actor = this.Actor.toFormattedJSON()
373 displayName: this.getDisplayName(),
380 toActivityPubObject () {
381 const obj = this.Actor.toActivityPubObject(this.name, 'Account')
383 return Object.assign(obj, {
384 summary: this.description
389 return this.Actor.isOwned()
393 return this.Actor.isOutdated()
401 return this.BlockedAccounts && this.BlockedAccounts.length !== 0