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 { 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, // Default scope includes avatar and server
40 fields: [ 'actorId' ],
44 fields: [ 'applicationId' ]
51 export class AccountModel extends Model<AccountModel> {
59 @Is('AccountDescription', value => throwIfNotValid(value, isAccountDescriptionValid, 'description'))
69 @ForeignKey(() => ActorModel)
73 @BelongsTo(() => ActorModel, {
81 @ForeignKey(() => UserModel)
85 @BelongsTo(() => UserModel, {
93 @ForeignKey(() => ApplicationModel)
97 @BelongsTo(() => ApplicationModel, {
103 Application: ApplicationModel
105 @HasMany(() => VideoChannelModel, {
112 VideoChannels: VideoChannelModel[]
114 @HasMany(() => VideoCommentModel, {
121 VideoComments: VideoCommentModel[]
124 static async sendDeleteIfOwned (instance: AccountModel, options) {
125 if (!instance.Actor) {
126 instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) as ActorModel
129 if (instance.isOwned()) {
130 return sendDeleteActor(instance.Actor, options.transaction)
136 static load (id: number, transaction?: Sequelize.Transaction) {
137 return AccountModel.findById(id, { transaction })
140 static loadByUUID (uuid: string) {
153 return AccountModel.findOne(query)
156 static loadLocalByName (name: string) {
159 [ Sequelize.Op.or ]: [
162 [ Sequelize.Op.ne ]: null
167 [ Sequelize.Op.ne ]: null
177 preferredUsername: name
183 return AccountModel.findOne(query)
186 static loadByNameAndHost (name: string, host: string) {
193 preferredUsername: name
208 return AccountModel.findOne(query)
211 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
225 return AccountModel.findOne(query)
228 static listForApi (start: number, count: number, sort: string) {
235 return AccountModel.findAndCountAll(query)
236 .then(({ rows, count }) => {
244 static listLocalsForSitemap (sort: string) {
248 order: getSort(sort),
251 attributes: [ 'preferredUsername', 'serverId' ],
252 model: ActorModel.unscoped(),
265 toFormattedJSON (): Account {
266 const actor = this.Actor.toFormattedJSON()
269 displayName: this.getDisplayName(),
270 description: this.description,
271 createdAt: this.createdAt,
272 updatedAt: this.updatedAt,
273 userId: this.userId ? this.userId : undefined
276 return Object.assign(actor, account)
279 toActivityPubObject () {
280 const obj = this.Actor.toActivityPubObject(this.name, 'Account')
282 return Object.assign(obj, {
283 summary: this.description
288 return this.Actor.isOwned()