1 import * as Sequelize from 'sequelize'
15 } from 'sequelize-typescript'
16 import { Account } from '../../../shared/models/actors'
17 import { isUserUsernameValid } from '../../helpers/custom-validators/users'
18 import { sendDeleteActor } from '../../lib/activitypub/send'
19 import { ActorModel } from '../activitypub/actor'
20 import { ApplicationModel } from '../application/application'
21 import { ServerModel } from '../server/server'
22 import { throwIfNotValid } from '../utils'
23 import { VideoChannelModel } from '../video/video-channel'
24 import { UserModel } from './user'
29 model: () => ActorModel,
33 model: () => ServerModel,
43 export class AccountModel extends Model<AccountModel> {
46 @Is('AccountName', value => throwIfNotValid(value, isUserUsernameValid, 'account name'))
56 @ForeignKey(() => ActorModel)
60 @BelongsTo(() => ActorModel, {
68 @ForeignKey(() => UserModel)
72 @BelongsTo(() => UserModel, {
80 @ForeignKey(() => ApplicationModel)
84 @BelongsTo(() => ApplicationModel, {
90 Account: ApplicationModel
92 @HasMany(() => VideoChannelModel, {
99 VideoChannels: VideoChannelModel[]
102 static sendDeleteIfOwned (instance: AccountModel) {
103 if (instance.isOwned()) {
104 return sendDeleteActor(instance.Actor, undefined)
110 static load (id: number) {
111 return AccountModel.findById(id)
114 static loadByUUID (uuid: string) {
127 return AccountModel.findOne(query)
130 static loadLocalByName (name: string) {
134 [ Sequelize.Op.or ]: [
137 [ Sequelize.Op.ne ]: null
142 [ Sequelize.Op.ne ]: null
149 return AccountModel.findOne(query)
152 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
166 return AccountModel.findOne(query)
169 toFormattedJSON (): Account {
170 const actor = this.Actor.toFormattedJSON()
173 name: this.Actor.preferredUsername,
174 displayName: this.name,
175 createdAt: this.createdAt,
176 updatedAt: this.updatedAt
179 return Object.assign(actor, account)
182 toActivityPubObject () {
183 return this.Actor.toActivityPubObject(this.name, 'Account')
187 return this.Actor.isOwned()