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> {
60 @ForeignKey(() => ActorModel)
64 @BelongsTo(() => ActorModel, {
72 @ForeignKey(() => UserModel)
76 @BelongsTo(() => UserModel, {
84 @ForeignKey(() => ApplicationModel)
88 @BelongsTo(() => ApplicationModel, {
94 Account: ApplicationModel
96 @HasMany(() => VideoChannelModel, {
103 VideoChannels: VideoChannelModel[]
106 static sendDeleteIfOwned (instance: AccountModel) {
107 if (instance.isOwned()) {
108 return sendDeleteActor(instance.Actor, undefined)
114 static load (id: number) {
115 return AccountModel.findById(id)
118 static loadByUUID (uuid: string) {
131 return AccountModel.findOne(query)
134 static loadLocalByName (name: string) {
138 [ Sequelize.Op.or ]: [
141 [ Sequelize.Op.ne ]: null
146 [ Sequelize.Op.ne ]: null
153 return AccountModel.findOne(query)
156 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
170 return AccountModel.findOne(query)
173 static listForApi (start: number, count: number, sort: string) {
177 order: [ getSort(sort) ]
180 return AccountModel.findAndCountAll(query)
181 .then(({ rows, count }) => {
189 toFormattedJSON (): Account {
190 const actor = this.Actor.toFormattedJSON()
193 name: this.Actor.preferredUsername,
194 displayName: this.name,
195 createdAt: this.createdAt,
196 updatedAt: this.updatedAt
199 return Object.assign(actor, account)
202 toActivityPubObject () {
203 return this.Actor.toActivityPubObject(this.name, 'Account')
207 return this.Actor.isOwned()