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) {
160 [ Sequelize.Op.or ]: [
163 [ Sequelize.Op.ne ]: null
168 [ Sequelize.Op.ne ]: null
178 preferredUsername: name
184 return AccountModel.findOne(query)
187 static loadLocalByNameAndHost (name: string, host: string) {
194 preferredUsername: name
209 return AccountModel.findOne(query)
212 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
226 return AccountModel.findOne(query)
229 static listForApi (start: number, count: number, sort: string) {
236 return AccountModel.findAndCountAll(query)
237 .then(({ rows, count }) => {
245 toFormattedJSON (): Account {
246 const actor = this.Actor.toFormattedJSON()
249 displayName: this.getDisplayName(),
250 description: this.description,
251 createdAt: this.createdAt,
252 updatedAt: this.updatedAt
255 return Object.assign(actor, account)
258 toActivityPubObject () {
259 const obj = this.Actor.toActivityPubObject(this.name, 'Account')
261 return Object.assign(obj, {
262 summary: this.description
267 return this.Actor.isOwned()