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 { 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 { VideoCommentModel } from '../video/video-comment'
26 import { UserModel } from './user'
31 model: () => ActorModel,
35 model: () => ServerModel,
39 model: () => AvatarModel,
50 fields: [ 'actorId' ],
54 fields: [ 'applicationId' ]
61 export class AccountModel extends Model<AccountModel> {
69 @Is('AccountDescription', value => throwIfNotValid(value, isAccountDescriptionValid, 'description'))
79 @ForeignKey(() => ActorModel)
83 @BelongsTo(() => ActorModel, {
91 @ForeignKey(() => UserModel)
95 @BelongsTo(() => UserModel, {
103 @ForeignKey(() => ApplicationModel)
105 applicationId: number
107 @BelongsTo(() => ApplicationModel, {
113 Application: ApplicationModel
115 @HasMany(() => VideoChannelModel, {
122 VideoChannels: VideoChannelModel[]
124 @HasMany(() => VideoCommentModel, {
131 VideoComments: VideoCommentModel[]
134 static async sendDeleteIfOwned (instance: AccountModel, options) {
135 if (!instance.Actor) {
136 instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) as ActorModel
139 return instance.Actor.destroy({ transaction: options.transaction })
142 static load (id: number) {
143 return AccountModel.findById(id)
146 static loadByUUID (uuid: string) {
159 return AccountModel.findOne(query)
162 static loadLocalByName (name: string) {
165 [ Sequelize.Op.or ]: [
168 [ Sequelize.Op.ne ]: null
173 [ Sequelize.Op.ne ]: null
183 preferredUsername: name
189 return AccountModel.findOne(query)
192 static loadLocalByNameAndHost (name: string, host: string) {
199 preferredUsername: name
214 return AccountModel.findOne(query)
217 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
231 return AccountModel.findOne(query)
234 static listForApi (start: number, count: number, sort: string) {
241 return AccountModel.findAndCountAll(query)
242 .then(({ rows, count }) => {
250 toFormattedJSON (): Account {
251 const actor = this.Actor.toFormattedJSON()
254 displayName: this.getDisplayName(),
255 description: this.description,
256 createdAt: this.createdAt,
257 updatedAt: this.updatedAt
260 return Object.assign(actor, account)
263 toActivityPubObject () {
264 const obj = this.Actor.toActivityPubObject(this.name, 'Account')
266 return Object.assign(obj, {
267 summary: this.description
272 return this.Actor.isOwned()