1 import * as Sequelize from 'sequelize'
3 AfterDestroy, AllowNull, BelongsTo, Column, CreatedAt, DefaultScope, ForeignKey, HasMany, Model, Table,
5 } from 'sequelize-typescript'
6 import { Account } from '../../../shared/models/actors'
7 import { sendDeleteActor } from '../../lib/activitypub/send'
8 import { ActorModel } from '../activitypub/actor'
9 import { ApplicationModel } from '../application/application'
10 import { AvatarModel } from '../avatar/avatar'
11 import { ServerModel } from '../server/server'
12 import { getSort } from '../utils'
13 import { VideoChannelModel } from '../video/video-channel'
14 import { UserModel } from './user'
19 model: () => ActorModel,
23 model: () => ServerModel,
27 model: () => AvatarModel,
37 export class AccountModel extends Model<AccountModel> {
49 @ForeignKey(() => ActorModel)
53 @BelongsTo(() => ActorModel, {
61 @ForeignKey(() => UserModel)
65 @BelongsTo(() => UserModel, {
73 @ForeignKey(() => ApplicationModel)
77 @BelongsTo(() => ApplicationModel, {
83 Account: ApplicationModel
85 @HasMany(() => VideoChannelModel, {
92 VideoChannels: VideoChannelModel[]
95 static sendDeleteIfOwned (instance: AccountModel) {
96 if (instance.isOwned()) {
97 return sendDeleteActor(instance.Actor, undefined)
103 static load (id: number) {
104 return AccountModel.findById(id)
107 static loadByUUID (uuid: string) {
120 return AccountModel.findOne(query)
123 static loadLocalByName (name: string) {
127 [ Sequelize.Op.or ]: [
130 [ Sequelize.Op.ne ]: null
135 [ Sequelize.Op.ne ]: null
142 return AccountModel.findOne(query)
145 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
159 return AccountModel.findOne(query)
162 static listForApi (start: number, count: number, sort: string) {
166 order: [ getSort(sort) ]
169 return AccountModel.findAndCountAll(query)
170 .then(({ rows, count }) => {
178 toFormattedJSON (): Account {
179 const actor = this.Actor.toFormattedJSON()
182 name: this.Actor.preferredUsername,
183 displayName: this.name,
184 createdAt: this.createdAt,
185 updatedAt: this.updatedAt
188 return Object.assign(actor, account)
191 toActivityPubObject () {
192 return this.Actor.toActivityPubObject(this.name, 'Account')
196 return this.Actor.isOwned()