1 import * as Sequelize from 'sequelize'
16 } from 'sequelize-typescript'
17 import { Account } from '../../../shared/models/actors'
18 import { isAccountDescriptionValid } from '../../helpers/custom-validators/accounts'
19 import { sendDeleteActor } from '../../lib/activitypub/send'
20 import { ActorModel } from '../activitypub/actor'
21 import { ApplicationModel } from '../application/application'
22 import { AvatarModel } from '../avatar/avatar'
23 import { ServerModel } from '../server/server'
24 import { getSort, throwIfNotValid } from '../utils'
25 import { VideoChannelModel } from '../video/video-channel'
26 import { VideoCommentModel } from '../video/video-comment'
27 import { UserModel } from './user'
32 model: () => ActorModel,
36 model: () => ServerModel,
40 model: () => AvatarModel,
51 fields: [ 'actorId' ],
55 fields: [ 'applicationId' ]
62 export class AccountModel extends Model<AccountModel> {
70 @Is('AccountDescription', value => throwIfNotValid(value, isAccountDescriptionValid, 'description'))
80 @ForeignKey(() => ActorModel)
84 @BelongsTo(() => ActorModel, {
92 @ForeignKey(() => UserModel)
96 @BelongsTo(() => UserModel, {
104 @ForeignKey(() => ApplicationModel)
106 applicationId: number
108 @BelongsTo(() => ApplicationModel, {
114 Application: ApplicationModel
116 @HasMany(() => VideoChannelModel, {
123 VideoChannels: VideoChannelModel[]
125 @HasMany(() => VideoCommentModel, {
132 VideoComments: VideoCommentModel[]
135 static async sendDeleteIfOwned (instance: AccountModel, options) {
136 if (!instance.Actor) {
137 instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) as ActorModel
140 if (instance.isOwned()) {
141 return sendDeleteActor(instance.Actor, options.transaction)
147 static load (id: number) {
148 return AccountModel.findById(id)
151 static loadByUUID (uuid: string) {
164 return AccountModel.findOne(query)
167 static loadLocalByName (name: string) {
170 [ Sequelize.Op.or ]: [
173 [ Sequelize.Op.ne ]: null
178 [ Sequelize.Op.ne ]: null
188 preferredUsername: name
194 return AccountModel.findOne(query)
197 static loadLocalByNameAndHost (name: string, host: string) {
204 preferredUsername: name
219 return AccountModel.findOne(query)
222 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
236 return AccountModel.findOne(query)
239 static listForApi (start: number, count: number, sort: string) {
246 return AccountModel.findAndCountAll(query)
247 .then(({ rows, count }) => {
255 toFormattedJSON (): Account {
256 const actor = this.Actor.toFormattedJSON()
259 displayName: this.getDisplayName(),
260 description: this.description,
261 createdAt: this.createdAt,
262 updatedAt: this.updatedAt
265 return Object.assign(actor, account)
268 toActivityPubObject () {
269 const obj = this.Actor.toActivityPubObject(this.name, 'Account')
271 return Object.assign(obj, {
272 summary: this.description
277 return this.Actor.isOwned()