From 50d6de9c286abcb34ff4234d56d9cbb803db7665 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 14 Dec 2017 17:38:41 +0100 Subject: Begin moving video channel to actor --- server/models/account/account.ts | 111 ++++++++++++--------------------------- 1 file changed, 33 insertions(+), 78 deletions(-) (limited to 'server/models/account/account.ts') diff --git a/server/models/account/account.ts b/server/models/account/account.ts index b26395fd4..1ee232537 100644 --- a/server/models/account/account.ts +++ b/server/models/account/account.ts @@ -5,18 +5,16 @@ import { BelongsTo, Column, CreatedAt, - DataType, - Default, + DefaultScope, ForeignKey, HasMany, Is, - IsUUID, Model, Table, UpdatedAt } from 'sequelize-typescript' import { isUserUsernameValid } from '../../helpers/custom-validators/users' -import { sendDeleteAccount } from '../../lib/activitypub/send' +import { sendDeleteActor } from '../../lib/activitypub/send' import { ActorModel } from '../activitypub/actor' import { ApplicationModel } from '../application/application' import { ServerModel } from '../server/server' @@ -24,31 +22,30 @@ import { throwIfNotValid } from '../utils' import { VideoChannelModel } from '../video/video-channel' import { UserModel } from './user' -@Table({ - tableName: 'account', - indexes: [ - { - fields: [ 'name' ] - }, - { - fields: [ 'serverId' ] - }, - { - fields: [ 'userId' ], - unique: true - }, - { - fields: [ 'applicationId' ], - unique: true - }, +@DefaultScope({ + include: [ { - fields: [ 'name', 'serverId', 'applicationId' ], - unique: true + model: () => ActorModel, + required: true, + include: [ + { + model: () => ServerModel, + required: false + } + ] } ] }) +@Table({ + tableName: 'account' +}) export class AccountModel extends Model { + @AllowNull(false) + @Is('AccountName', value => throwIfNotValid(value, isUserUsernameValid, 'account name')) + @Column + name: string + @CreatedAt createdAt: Date @@ -89,7 +86,7 @@ export class AccountModel extends Model { }, onDelete: 'cascade' }) - Application: ApplicationModel + Account: ApplicationModel @HasMany(() => VideoChannelModel, { foreignKey: { @@ -103,32 +100,27 @@ export class AccountModel extends Model { @AfterDestroy static sendDeleteIfOwned (instance: AccountModel) { if (instance.isOwned()) { - return sendDeleteAccount(instance, undefined) + return sendDeleteActor(instance.Actor, undefined) } return undefined } - static loadApplication () { - return AccountModel.findOne({ - include: [ - { - model: ApplicationModel, - required: true - } - ] - }) - } - static load (id: number) { return AccountModel.findById(id) } static loadByUUID (uuid: string) { const query = { - where: { - uuid - } + include: [ + { + model: ActorModel, + required: true, + where: { + uuid + } + } + ] } return AccountModel.findOne(query) @@ -156,25 +148,6 @@ export class AccountModel extends Model { return AccountModel.findOne(query) } - static loadByNameAndHost (name: string, host: string) { - const query = { - where: { - name - }, - include: [ - { - model: ServerModel, - required: true, - where: { - host - } - } - ] - } - - return AccountModel.findOne(query) - } - static loadByUrl (url: string, transaction?: Sequelize.Transaction) { const query = { include: [ @@ -192,29 +165,11 @@ export class AccountModel extends Model { return AccountModel.findOne(query) } - static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction) { - const query = { - include: [ - { - model: ActorModel, - required: true, - where: { - followersUrl: { - [ Sequelize.Op.in ]: followersUrls - } - } - } - ], - transaction - } - - return AccountModel.findAll(query) - } - toFormattedJSON () { const actor = this.Actor.toFormattedJSON() const account = { id: this.id, + name: this.name, createdAt: this.createdAt, updatedAt: this.updatedAt } @@ -223,7 +178,7 @@ export class AccountModel extends Model { } toActivityPubObject () { - return this.Actor.toActivityPubObject(this.name, this.uuid, 'Account') + return this.Actor.toActivityPubObject(this.name, 'Account') } isOwned () { -- cgit v1.2.3