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, MAccountDefault, MAccountSummaryFormattable, MAccountFormattable, MAccountAP } 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[]
221 private static cache: { [ id: string ]: any } = {}
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> {
250 // The server actor never change, so we can easily cache it
251 if (name === SERVER_ACTOR_NAME && AccountModel.cache[name]) {
252 return Bluebird.resolve(AccountModel.cache[name])
275 preferredUsername: name
281 return AccountModel.findOne(query)
283 if (name === SERVER_ACTOR_NAME) {
284 AccountModel.cache[name] = account
291 static loadByNameAndHost (name: string, host: string): Bluebird<MAccountDefault> {
298 preferredUsername: name
313 return AccountModel.findOne(query)
316 static loadByUrl (url: string, transaction?: Transaction): Bluebird<MAccountDefault> {
330 return AccountModel.findOne(query)
333 static listForApi (start: number, count: number, sort: string) {
340 return AccountModel.findAndCountAll(query)
341 .then(({ rows, count }) => {
349 static listLocalsForSitemap (sort: string): Bluebird<MAccountActor[]> {
353 order: getSort(sort),
356 attributes: [ 'preferredUsername', 'serverId' ],
357 model: ActorModel.unscoped(),
370 toFormattedJSON (this: MAccountFormattable): Account {
371 const actor = this.Actor.toFormattedJSON()
374 displayName: this.getDisplayName(),
375 description: this.description,
376 createdAt: this.createdAt,
377 updatedAt: this.updatedAt,
378 userId: this.userId ? this.userId : undefined
381 return Object.assign(actor, account)
384 toFormattedSummaryJSON (this: MAccountSummaryFormattable): AccountSummary {
385 const actor = this.Actor.toFormattedSummaryJSON()
390 displayName: this.getDisplayName(),
397 toActivityPubObject (this: MAccountAP) {
398 const obj = this.Actor.toActivityPubObject(this.name)
400 return Object.assign(obj, {
401 summary: this.description
406 return this.Actor.isOwned()
410 return this.Actor.isOutdated()
418 return this.BlockedAccounts && this.BlockedAccounts.length !== 0