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 { AvatarModel } from '../avatar/avatar'
22 import { ServerModel } from '../server/server'
23 import { getSort, throwIfNotValid } from '../utils'
24 import { VideoChannelModel } from '../video/video-channel'
25 import { UserModel } from './user'
30 model: () => ActorModel,
34 model: () => ServerModel,
38 model: () => AvatarModel,
48 export class AccountModel extends Model<AccountModel> {
51 @Is('AccountName', value => throwIfNotValid(value, isUserUsernameValid, 'account name'))
61 @ForeignKey(() => ActorModel)
65 @BelongsTo(() => ActorModel, {
73 @ForeignKey(() => UserModel)
77 @BelongsTo(() => UserModel, {
85 @ForeignKey(() => ApplicationModel)
89 @BelongsTo(() => ApplicationModel, {
95 Account: ApplicationModel
97 @HasMany(() => VideoChannelModel, {
104 VideoChannels: VideoChannelModel[]
107 static sendDeleteIfOwned (instance: AccountModel) {
108 if (instance.isOwned()) {
109 return sendDeleteActor(instance.Actor, undefined)
115 static load (id: number) {
116 return AccountModel.findById(id)
119 static loadByUUID (uuid: string) {
132 return AccountModel.findOne(query)
135 static loadLocalByName (name: string) {
139 [ Sequelize.Op.or ]: [
142 [ Sequelize.Op.ne ]: null
147 [ Sequelize.Op.ne ]: null
154 return AccountModel.findOne(query)
157 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
171 return AccountModel.findOne(query)
174 static listForApi (start: number, count: number, sort: string) {
178 order: [ getSort(sort) ]
181 return AccountModel.findAndCountAll(query)
182 .then(({ rows, count }) => {
190 toFormattedJSON (): Account {
191 const actor = this.Actor.toFormattedJSON()
194 name: this.Actor.preferredUsername,
195 displayName: this.name,
196 createdAt: this.createdAt,
197 updatedAt: this.updatedAt
200 return Object.assign(actor, account)
203 toActivityPubObject () {
204 return this.Actor.toActivityPubObject(this.name, 'Account')
208 return this.Actor.isOwned()