1 import * as Sequelize from 'sequelize'
17 } from 'sequelize-typescript'
18 import { isUserUsernameValid } from '../../helpers/custom-validators/users'
19 import { sendDeleteAccount } from '../../lib/activitypub/send'
20 import { ActorModel } from '../activitypub/actor'
21 import { ApplicationModel } from '../application/application'
22 import { ServerModel } from '../server/server'
23 import { throwIfNotValid } from '../utils'
24 import { VideoChannelModel } from '../video/video-channel'
25 import { UserModel } from './user'
34 fields: [ 'serverId' ]
41 fields: [ 'applicationId' ],
45 fields: [ 'name', 'serverId', 'applicationId' ],
50 export class AccountModel extends Model<AccountModel> {
58 @ForeignKey(() => ActorModel)
62 @BelongsTo(() => ActorModel, {
70 @ForeignKey(() => UserModel)
74 @BelongsTo(() => UserModel, {
82 @ForeignKey(() => ApplicationModel)
86 @BelongsTo(() => ApplicationModel, {
92 Application: ApplicationModel
94 @HasMany(() => VideoChannelModel, {
101 VideoChannels: VideoChannelModel[]
104 static sendDeleteIfOwned (instance: AccountModel) {
105 if (instance.isOwned()) {
106 return sendDeleteAccount(instance, undefined)
112 static loadApplication () {
113 return AccountModel.findOne({
116 model: ApplicationModel,
123 static load (id: number) {
124 return AccountModel.findById(id)
127 static loadByUUID (uuid: string) {
134 return AccountModel.findOne(query)
137 static loadLocalByName (name: string) {
141 [ Sequelize.Op.or ]: [
144 [ Sequelize.Op.ne ]: null
149 [ Sequelize.Op.ne ]: null
156 return AccountModel.findOne(query)
159 static loadByNameAndHost (name: string, host: string) {
175 return AccountModel.findOne(query)
178 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
192 return AccountModel.findOne(query)
195 static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction) {
203 [ Sequelize.Op.in ]: followersUrls
211 return AccountModel.findAll(query)
215 const actor = this.Actor.toFormattedJSON()
218 createdAt: this.createdAt,
219 updatedAt: this.updatedAt
222 return Object.assign(actor, account)
225 toActivityPubObject () {
226 return this.Actor.toActivityPubObject(this.name, this.uuid, 'Account')
230 return this.Actor.isOwned()