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 { WEBSERVER } from '../../initializers/constants'
30 import { Op, Transaction, WhereOptions } from 'sequelize'
32 export enum ScopeNames {
36 @DefaultScope(() => ({
39 model: ActorModel, // Default scope includes avatar and server
45 [ ScopeNames.SUMMARY ]: (whereActor?: WhereOptions) => {
47 attributes: [ 'id', 'name' ],
50 attributes: [ 'id', 'uuid', 'preferredUsername', 'url', 'serverId', 'avatarId' ],
51 model: ActorModel.unscoped(),
56 attributes: [ 'host' ],
57 model: ServerModel.unscoped(),
61 model: AvatarModel.unscoped(),
74 fields: [ 'actorId' ],
78 fields: [ 'applicationId' ]
85 export class AccountModel extends Model<AccountModel> {
93 @Is('AccountDescription', value => throwIfNotValid(value, isAccountDescriptionValid, 'description', true))
103 @ForeignKey(() => ActorModel)
107 @BelongsTo(() => ActorModel, {
115 @ForeignKey(() => UserModel)
119 @BelongsTo(() => UserModel, {
127 @ForeignKey(() => ApplicationModel)
129 applicationId: number
131 @BelongsTo(() => ApplicationModel, {
137 Application: ApplicationModel
139 @HasMany(() => VideoChannelModel, {
146 VideoChannels: VideoChannelModel[]
148 @HasMany(() => VideoPlaylistModel, {
155 VideoPlaylists: VideoPlaylistModel[]
157 @HasMany(() => VideoCommentModel, {
164 VideoComments: VideoCommentModel[]
167 static async sendDeleteIfOwned (instance: AccountModel, options) {
168 if (!instance.Actor) {
169 instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) as ActorModel
172 if (instance.isOwned()) {
173 return sendDeleteActor(instance.Actor, options.transaction)
179 static load (id: number, transaction?: Transaction) {
180 return AccountModel.findByPk(id, { transaction })
183 static loadByUUID (uuid: string) {
196 return AccountModel.findOne(query)
199 static loadByNameWithHost (nameWithHost: string) {
200 const [ accountName, host ] = nameWithHost.split('@')
202 if (!host || host === WEBSERVER.HOST) return AccountModel.loadLocalByName(accountName)
204 return AccountModel.loadByNameAndHost(accountName, host)
207 static loadLocalByName (name: string) {
228 preferredUsername: name
234 return AccountModel.findOne(query)
237 static loadByNameAndHost (name: string, host: string) {
244 preferredUsername: name
259 return AccountModel.findOne(query)
262 static loadByUrl (url: string, transaction?: Transaction) {
276 return AccountModel.findOne(query)
279 static listForApi (start: number, count: number, sort: string) {
286 return AccountModel.findAndCountAll(query)
287 .then(({ rows, count }) => {
295 static listLocalsForSitemap (sort: string) {
299 order: getSort(sort),
302 attributes: [ 'preferredUsername', 'serverId' ],
303 model: ActorModel.unscoped(),
316 toFormattedJSON (): Account {
317 const actor = this.Actor.toFormattedJSON()
320 displayName: this.getDisplayName(),
321 description: this.description,
322 createdAt: this.createdAt,
323 updatedAt: this.updatedAt,
324 userId: this.userId ? this.userId : undefined
327 return Object.assign(actor, account)
330 toFormattedSummaryJSON (): AccountSummary {
331 const actor = this.Actor.toFormattedJSON()
337 displayName: this.getDisplayName(),
344 toActivityPubObject () {
345 const obj = this.Actor.toActivityPubObject(this.name, 'Account')
347 return Object.assign(obj, {
348 summary: this.description
353 return this.Actor.isOwned()
357 return this.Actor.isOutdated()