1 import * as Bluebird from 'bluebird'
2 import { FindOptions, IncludeOptions, Op, Transaction, WhereOptions } from 'sequelize'
19 } from 'sequelize-typescript'
20 import { ModelCache } from '@server/models/model-cache'
21 import { Account, AccountSummary } from '../../../shared/models/actors'
22 import { isAccountDescriptionValid } from '../../helpers/custom-validators/accounts'
23 import { CONSTRAINTS_FIELDS, SERVER_ACTOR_NAME, WEBSERVER } from '../../initializers/constants'
24 import { sendDeleteActor } from '../../lib/activitypub/send'
31 MAccountSummaryFormattable,
33 } from '../../types/models'
34 import { ActorModel } from '../activitypub/actor'
35 import { ActorFollowModel } from '../activitypub/actor-follow'
36 import { ApplicationModel } from '../application/application'
37 import { AvatarModel } from '../avatar/avatar'
38 import { ServerModel } from '../server/server'
39 import { ServerBlocklistModel } from '../server/server-blocklist'
40 import { getSort, throwIfNotValid } from '../utils'
41 import { VideoModel } from '../video/video'
42 import { VideoChannelModel } from '../video/video-channel'
43 import { VideoCommentModel } from '../video/video-comment'
44 import { VideoPlaylistModel } from '../video/video-playlist'
45 import { AccountBlocklistModel } from './account-blocklist'
46 import { UserModel } from './user'
48 export enum ScopeNames {
52 export type SummaryOptions = {
53 actorRequired?: boolean // Default: true
54 whereActor?: WhereOptions
55 withAccountBlockerIds?: number[]
58 @DefaultScope(() => ({
61 model: ActorModel, // Default scope includes avatar and server
67 [ScopeNames.SUMMARY]: (options: SummaryOptions = {}) => {
68 const whereActor = options.whereActor || undefined
70 const serverInclude: IncludeOptions = {
71 attributes: [ 'host' ],
72 model: ServerModel.unscoped(),
76 const query: FindOptions = {
77 attributes: [ 'id', 'name', 'actorId' ],
80 attributes: [ 'id', 'preferredUsername', 'url', 'serverId', 'avatarId' ],
81 model: ActorModel.unscoped(),
82 required: options.actorRequired ?? true,
88 model: AvatarModel.unscoped(),
96 if (options.withAccountBlockerIds) {
99 model: AccountBlocklistModel.unscoped(),
100 as: 'BlockedAccounts',
104 [Op.in]: options.withAccountBlockerIds
109 serverInclude.include = [
111 attributes: [ 'id' ],
112 model: ServerBlocklistModel.unscoped(),
116 [Op.in]: options.withAccountBlockerIds
127 tableName: 'account',
130 fields: [ 'actorId' ],
134 fields: [ 'applicationId' ]
141 export class AccountModel extends Model<AccountModel> {
149 @Is('AccountDescription', value => throwIfNotValid(value, isAccountDescriptionValid, 'description', true))
150 @Column(DataType.STRING(CONSTRAINTS_FIELDS.USERS.DESCRIPTION.max))
159 @ForeignKey(() => ActorModel)
163 @BelongsTo(() => ActorModel, {
171 @ForeignKey(() => UserModel)
175 @BelongsTo(() => UserModel, {
183 @ForeignKey(() => ApplicationModel)
185 applicationId: number
187 @BelongsTo(() => ApplicationModel, {
193 Application: ApplicationModel
195 @HasMany(() => VideoChannelModel, {
202 VideoChannels: VideoChannelModel[]
204 @HasMany(() => VideoPlaylistModel, {
211 VideoPlaylists: VideoPlaylistModel[]
213 @HasMany(() => VideoCommentModel, {
220 VideoComments: VideoCommentModel[]
222 @HasMany(() => AccountBlocklistModel, {
224 name: 'targetAccountId',
227 as: 'BlockedAccounts',
230 BlockedAccounts: AccountBlocklistModel[]
233 static async sendDeleteIfOwned (instance: AccountModel, options) {
234 if (!instance.Actor) {
235 instance.Actor = await instance.$get('Actor', { transaction: options.transaction })
238 await ActorFollowModel.removeFollowsOf(instance.Actor.id, options.transaction)
240 if (instance.isOwned()) {
241 return sendDeleteActor(instance.Actor, options.transaction)
247 static load (id: number, transaction?: Transaction): Bluebird<MAccountDefault> {
248 return AccountModel.findByPk(id, { transaction })
251 static loadByNameWithHost (nameWithHost: string): Bluebird<MAccountDefault> {
252 const [ accountName, host ] = nameWithHost.split('@')
254 if (!host || host === WEBSERVER.HOST) return AccountModel.loadLocalByName(accountName)
256 return AccountModel.loadByNameAndHost(accountName, host)
259 static loadLocalByName (name: string): Bluebird<MAccountDefault> {
281 preferredUsername: name
287 return AccountModel.findOne(query)
290 return ModelCache.Instance.doCache({
291 cacheType: 'local-account-name',
294 // The server actor never change, so we can easily cache it
295 whitelist: () => name === SERVER_ACTOR_NAME
299 static loadByNameAndHost (name: string, host: string): Bluebird<MAccountDefault> {
306 preferredUsername: name
321 return AccountModel.findOne(query)
324 static loadByUrl (url: string, transaction?: Transaction): Bluebird<MAccountDefault> {
338 return AccountModel.findOne(query)
341 static listForApi (start: number, count: number, sort: string) {
348 return AccountModel.findAndCountAll(query)
349 .then(({ rows, count }) => {
357 static loadAccountIdFromVideo (videoId: number): Bluebird<MAccount> {
361 attributes: [ 'id', 'accountId' ],
362 model: VideoChannelModel.unscoped(),
366 attributes: [ 'id', 'channelId' ],
367 model: VideoModel.unscoped(),
377 return AccountModel.findOne(query)
380 static listLocalsForSitemap (sort: string): Bluebird<MAccountActor[]> {
384 order: getSort(sort),
387 attributes: [ 'preferredUsername', 'serverId' ],
388 model: ActorModel.unscoped(),
402 return WEBSERVER.URL + '/accounts/' + this.Actor.getIdentifier()
405 toFormattedJSON (this: MAccountFormattable): Account {
406 const actor = this.Actor.toFormattedJSON()
409 displayName: this.getDisplayName(),
410 description: this.description,
411 createdAt: this.createdAt,
412 updatedAt: this.updatedAt,
413 userId: this.userId ? this.userId : undefined
416 return Object.assign(actor, account)
419 toFormattedSummaryJSON (this: MAccountSummaryFormattable): AccountSummary {
420 const actor = this.Actor.toFormattedSummaryJSON()
425 displayName: this.getDisplayName(),
432 toActivityPubObject (this: MAccountAP) {
433 const obj = this.Actor.toActivityPubObject(this.name)
435 return Object.assign(obj, {
436 summary: this.description
441 return this.Actor.isOwned()
445 return this.Actor.isOutdated()
452 getLocalUrl (this: MAccountActor | MChannelActor) {
453 return WEBSERVER.URL + `/accounts/` + this.Actor.preferredUsername
457 return this.BlockedAccounts && this.BlockedAccounts.length !== 0