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,
52 fields: [ 'actorId' ],
56 fields: [ 'applicationId' ]
63 export class AccountModel extends Model<AccountModel> {
71 @Is('AccountDescription', value => throwIfNotValid(value, isAccountDescriptionValid, 'description'))
81 @ForeignKey(() => ActorModel)
85 @BelongsTo(() => ActorModel, {
93 @ForeignKey(() => UserModel)
97 @BelongsTo(() => UserModel, {
105 @ForeignKey(() => ApplicationModel)
107 applicationId: number
109 @BelongsTo(() => ApplicationModel, {
115 Application: ApplicationModel
117 @HasMany(() => VideoChannelModel, {
124 VideoChannels: VideoChannelModel[]
126 @HasMany(() => VideoCommentModel, {
133 VideoComments: VideoCommentModel[]
136 static async sendDeleteIfOwned (instance: AccountModel, options) {
137 if (!instance.Actor) {
138 instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) as ActorModel
141 if (instance.isOwned()) {
142 logger.debug('Sending delete of actor of account %s.', instance.Actor.url)
143 return sendDeleteActor(instance.Actor, options.transaction)
149 static load (id: number) {
150 return AccountModel.findById(id)
153 static loadByUUID (uuid: string) {
166 return AccountModel.findOne(query)
169 static loadLocalByName (name: string) {
172 [ Sequelize.Op.or ]: [
175 [ Sequelize.Op.ne ]: null
180 [ Sequelize.Op.ne ]: null
190 preferredUsername: name
196 return AccountModel.findOne(query)
199 static loadLocalByNameAndHost (name: string, host: string) {
206 preferredUsername: name
221 return AccountModel.findOne(query)
224 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
238 return AccountModel.findOne(query)
241 static listForApi (start: number, count: number, sort: string) {
248 return AccountModel.findAndCountAll(query)
249 .then(({ rows, count }) => {
257 toFormattedJSON (): Account {
258 const actor = this.Actor.toFormattedJSON()
261 displayName: this.getDisplayName(),
262 description: this.description,
263 createdAt: this.createdAt,
264 updatedAt: this.updatedAt
267 return Object.assign(actor, account)
270 toActivityPubObject () {
271 const obj = this.Actor.toActivityPubObject(this.name, 'Account')
273 return Object.assign(obj, {
274 summary: this.description
279 return this.Actor.isOwned()