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 return sendDeleteActor(instance.Actor, options.transaction)
148 static load (id: number) {
149 return AccountModel.findById(id)
152 static loadByUUID (uuid: string) {
165 return AccountModel.findOne(query)
168 static loadLocalByName (name: string) {
171 [ Sequelize.Op.or ]: [
174 [ Sequelize.Op.ne ]: null
179 [ Sequelize.Op.ne ]: null
189 preferredUsername: name
195 return AccountModel.findOne(query)
198 static loadLocalByNameAndHost (name: string, host: string) {
205 preferredUsername: name
220 return AccountModel.findOne(query)
223 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
237 return AccountModel.findOne(query)
240 static listForApi (start: number, count: number, sort: string) {
247 return AccountModel.findAndCountAll(query)
248 .then(({ rows, count }) => {
256 toFormattedJSON (): Account {
257 const actor = this.Actor.toFormattedJSON()
260 displayName: this.getDisplayName(),
261 description: this.description,
262 createdAt: this.createdAt,
263 updatedAt: this.updatedAt
266 return Object.assign(actor, account)
269 toActivityPubObject () {
270 const obj = this.Actor.toActivityPubObject(this.name, 'Account')
272 return Object.assign(obj, {
273 summary: this.description
278 return this.Actor.isOwned()