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'
34 export enum ScopeNames {
38 export type SummaryOptions = {
39 whereActor?: WhereOptions
40 withAccountBlockerIds?: number[]
43 @DefaultScope(() => ({
46 model: ActorModel, // Default scope includes avatar and server
52 [ ScopeNames.SUMMARY ]: (options: SummaryOptions = {}) => {
53 const whereActor = options.whereActor || undefined
55 const serverInclude: IncludeOptions = {
56 attributes: [ 'host' ],
57 model: ServerModel.unscoped(),
61 const query: FindOptions = {
62 attributes: [ 'id', 'name' ],
65 attributes: [ 'id', 'preferredUsername', 'url', 'serverId', 'avatarId' ],
66 model: ActorModel.unscoped(),
73 model: AvatarModel.unscoped(),
81 if (options.withAccountBlockerIds) {
84 model: AccountBlocklistModel.unscoped(),
85 as: 'BlockedAccounts',
89 [Op.in]: options.withAccountBlockerIds
94 serverInclude.include = [
97 model: ServerBlocklistModel.unscoped(),
101 [Op.in]: options.withAccountBlockerIds
112 tableName: 'account',
115 fields: [ 'actorId' ],
119 fields: [ 'applicationId' ]
126 export class AccountModel extends Model<AccountModel> {
134 @Is('AccountDescription', value => throwIfNotValid(value, isAccountDescriptionValid, 'description', true))
135 @Column(DataType.STRING(CONSTRAINTS_FIELDS.USERS.DESCRIPTION.max))
144 @ForeignKey(() => ActorModel)
148 @BelongsTo(() => ActorModel, {
156 @ForeignKey(() => UserModel)
160 @BelongsTo(() => UserModel, {
168 @ForeignKey(() => ApplicationModel)
170 applicationId: number
172 @BelongsTo(() => ApplicationModel, {
178 Application: ApplicationModel
180 @HasMany(() => VideoChannelModel, {
187 VideoChannels: VideoChannelModel[]
189 @HasMany(() => VideoPlaylistModel, {
196 VideoPlaylists: VideoPlaylistModel[]
198 @HasMany(() => VideoCommentModel, {
205 VideoComments: VideoCommentModel[]
207 @HasMany(() => AccountBlocklistModel, {
209 name: 'targetAccountId',
212 as: 'BlockedAccounts',
215 BlockedAccounts: AccountBlocklistModel[]
218 static async sendDeleteIfOwned (instance: AccountModel, options) {
219 if (!instance.Actor) {
220 instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) as ActorModel
223 if (instance.isOwned()) {
224 return sendDeleteActor(instance.Actor, options.transaction)
230 static load (id: number, transaction?: Transaction) {
231 return AccountModel.findByPk(id, { transaction })
234 static loadByNameWithHost (nameWithHost: string) {
235 const [ accountName, host ] = nameWithHost.split('@')
237 if (!host || host === WEBSERVER.HOST) return AccountModel.loadLocalByName(accountName)
239 return AccountModel.loadByNameAndHost(accountName, host)
242 static loadLocalByName (name: string) {
263 preferredUsername: name
269 return AccountModel.findOne(query)
272 static loadByNameAndHost (name: string, host: string) {
279 preferredUsername: name
294 return AccountModel.findOne(query)
297 static loadByUrl (url: string, transaction?: Transaction) {
311 return AccountModel.findOne(query)
314 static listForApi (start: number, count: number, sort: string) {
321 return AccountModel.findAndCountAll(query)
322 .then(({ rows, count }) => {
330 static listLocalsForSitemap (sort: string) {
334 order: getSort(sort),
337 attributes: [ 'preferredUsername', 'serverId' ],
338 model: ActorModel.unscoped(),
351 toFormattedJSON (): Account {
352 const actor = this.Actor.toFormattedJSON()
355 displayName: this.getDisplayName(),
356 description: this.description,
357 createdAt: this.createdAt,
358 updatedAt: this.updatedAt,
359 userId: this.userId ? this.userId : undefined
362 return Object.assign(actor, account)
365 toFormattedSummaryJSON (): AccountSummary {
366 const actor = this.Actor.toFormattedJSON()
371 displayName: this.getDisplayName(),
378 toActivityPubObject () {
379 const obj = this.Actor.toActivityPubObject(this.name, 'Account')
381 return Object.assign(obj, {
382 summary: this.description
387 return this.Actor.isOwned()
391 return this.Actor.isOutdated()
399 return this.BlockedAccounts && this.BlockedAccounts.length !== 0