1 import * as Sequelize from 'sequelize'
3 AllowNull, BeforeDestroy, BelongsTo, Column, CreatedAt, DefaultScope, ForeignKey, HasMany, Model, Table,
5 } from 'sequelize-typescript'
6 import { Account } from '../../../shared/models/actors'
7 import { logger } from '../../helpers/logger'
8 import { sendDeleteActor } from '../../lib/activitypub/send'
9 import { ActorModel } from '../activitypub/actor'
10 import { ApplicationModel } from '../application/application'
11 import { AvatarModel } from '../avatar/avatar'
12 import { ServerModel } from '../server/server'
13 import { getSort } from '../utils'
14 import { VideoChannelModel } from '../video/video-channel'
15 import { VideoCommentModel } from '../video/video-comment'
16 import { UserModel } from './user'
21 model: () => ActorModel,
25 model: () => ServerModel,
29 model: () => AvatarModel,
39 export class AccountModel extends Model<AccountModel> {
51 @ForeignKey(() => ActorModel)
55 @BelongsTo(() => ActorModel, {
63 @ForeignKey(() => UserModel)
67 @BelongsTo(() => UserModel, {
75 @ForeignKey(() => ApplicationModel)
79 @BelongsTo(() => ApplicationModel, {
85 Application: ApplicationModel
87 @HasMany(() => VideoChannelModel, {
94 VideoChannels: VideoChannelModel[]
96 @HasMany(() => VideoCommentModel, {
103 VideoComments: VideoCommentModel[]
106 static async sendDeleteIfOwned (instance: AccountModel, options) {
107 if (!instance.Actor) {
108 instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) as ActorModel
111 if (instance.isOwned()) {
112 logger.debug('Sending delete of actor of account %s.', instance.Actor.url)
113 return sendDeleteActor(instance.Actor, options.transaction)
119 static load (id: number) {
120 return AccountModel.findById(id)
123 static loadByUUID (uuid: string) {
136 return AccountModel.findOne(query)
139 static loadLocalByName (name: string) {
143 [ Sequelize.Op.or ]: [
146 [ Sequelize.Op.ne ]: null
151 [ Sequelize.Op.ne ]: null
158 return AccountModel.findOne(query)
161 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
175 return AccountModel.findOne(query)
178 static listForApi (start: number, count: number, sort: string) {
182 order: [ getSort(sort) ]
185 return AccountModel.findAndCountAll(query)
186 .then(({ rows, count }) => {
194 toFormattedJSON (): Account {
195 const actor = this.Actor.toFormattedJSON()
198 displayName: this.name,
199 createdAt: this.createdAt,
200 updatedAt: this.updatedAt
203 return Object.assign(actor, account)
206 toActivityPubObject () {
207 return this.Actor.toActivityPubObject(this.name, 'Account')
211 return this.Actor.isOwned()