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 { 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', '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))
94 @Column(DataType.STRING(CONSTRAINTS_FIELDS.USERS.DESCRIPTION.max))
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 loadByNameWithHost (nameWithHost: string) {
184 const [ accountName, host ] = nameWithHost.split('@')
186 if (!host || host === WEBSERVER.HOST) return AccountModel.loadLocalByName(accountName)
188 return AccountModel.loadByNameAndHost(accountName, host)
191 static loadLocalByName (name: string) {
212 preferredUsername: name
218 return AccountModel.findOne(query)
221 static loadByNameAndHost (name: string, host: string) {
228 preferredUsername: name
243 return AccountModel.findOne(query)
246 static loadByUrl (url: string, transaction?: Transaction) {
260 return AccountModel.findOne(query)
263 static listForApi (start: number, count: number, sort: string) {
270 return AccountModel.findAndCountAll(query)
271 .then(({ rows, count }) => {
279 static listLocalsForSitemap (sort: string) {
283 order: getSort(sort),
286 attributes: [ 'preferredUsername', 'serverId' ],
287 model: ActorModel.unscoped(),
300 toFormattedJSON (): Account {
301 const actor = this.Actor.toFormattedJSON()
304 displayName: this.getDisplayName(),
305 description: this.description,
306 createdAt: this.createdAt,
307 updatedAt: this.updatedAt,
308 userId: this.userId ? this.userId : undefined
311 return Object.assign(actor, account)
314 toFormattedSummaryJSON (): AccountSummary {
315 const actor = this.Actor.toFormattedJSON()
320 displayName: this.getDisplayName(),
327 toActivityPubObject () {
328 const obj = this.Actor.toActivityPubObject(this.name, 'Account')
330 return Object.assign(obj, {
331 summary: this.description
336 return this.Actor.isOwned()
340 return this.Actor.isOutdated()