1 import * as Sequelize from 'sequelize'
16 } from 'sequelize-typescript'
17 import { Account } from '../../../shared/models/actors'
18 import { isAccountDescriptionValid } from '../../helpers/custom-validators/accounts'
19 import { logger } from '../../helpers/logger'
20 import { sendDeleteActor } from '../../lib/activitypub/send'
21 import { ActorModel } from '../activitypub/actor'
22 import { ApplicationModel } from '../application/application'
23 import { AvatarModel } from '../avatar/avatar'
24 import { ServerModel } from '../server/server'
25 import { getSort, throwIfNotValid } from '../utils'
26 import { VideoChannelModel } from '../video/video-channel'
27 import { VideoCommentModel } from '../video/video-comment'
28 import { UserModel } from './user'
33 model: () => ActorModel,
37 model: () => ServerModel,
41 model: () => AvatarModel,
51 export class AccountModel extends Model<AccountModel> {
59 @Is('AccountDescription', value => throwIfNotValid(value, isAccountDescriptionValid, 'description'))
69 @ForeignKey(() => ActorModel)
73 @BelongsTo(() => ActorModel, {
81 @ForeignKey(() => UserModel)
85 @BelongsTo(() => UserModel, {
93 @ForeignKey(() => ApplicationModel)
97 @BelongsTo(() => ApplicationModel, {
103 Application: ApplicationModel
105 @HasMany(() => VideoChannelModel, {
112 VideoChannels: VideoChannelModel[]
114 @HasMany(() => VideoCommentModel, {
121 VideoComments: VideoCommentModel[]
124 static async sendDeleteIfOwned (instance: AccountModel, options) {
125 if (!instance.Actor) {
126 instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) as ActorModel
129 if (instance.isOwned()) {
130 logger.debug('Sending delete of actor of account %s.', instance.Actor.url)
131 return sendDeleteActor(instance.Actor, options.transaction)
137 static load (id: number) {
138 return AccountModel.findById(id)
141 static loadByUUID (uuid: string) {
154 return AccountModel.findOne(query)
157 static loadLocalByName (name: string) {
161 [ Sequelize.Op.or ]: [
164 [ Sequelize.Op.ne ]: null
169 [ Sequelize.Op.ne ]: null
176 return AccountModel.findOne(query)
179 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
193 return AccountModel.findOne(query)
196 static listForApi (start: number, count: number, sort: string) {
203 return AccountModel.findAndCountAll(query)
204 .then(({ rows, count }) => {
212 toFormattedJSON (): Account {
213 const actor = this.Actor.toFormattedJSON()
216 displayName: this.name,
217 description: this.description,
218 createdAt: this.createdAt,
219 updatedAt: this.updatedAt
222 return Object.assign(actor, account)
225 toActivityPubObject () {
226 const obj = this.Actor.toActivityPubObject(this.name, 'Account')
228 return Object.assign(obj, {
229 summary: this.description
234 return this.Actor.isOwned()