X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Faccount%2Faccount.ts;h=27c75d8861203c3be8c6e8e27b9d134e3dbb334c;hb=e724fa93c71d76d709e819a05e5e2904a3c4205b;hp=b26395fd4baeb7a58ee69c3601ee0147e924f10e;hpb=fadf619ad61a016c1c7fc53de5a8f398a4f77519;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/account/account.ts b/server/models/account/account.ts index b26395fd4..27c75d886 100644 --- a/server/models/account/account.ts +++ b/server/models/account/account.ts @@ -1,54 +1,65 @@ import * as Sequelize from 'sequelize' import { - AfterDestroy, AllowNull, + BeforeDestroy, 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 { Account } from '../../../shared/models/actors' +import { isAccountDescriptionValid } from '../../helpers/custom-validators/accounts' +import { sendDeleteActor } from '../../lib/activitypub/send' import { ActorModel } from '../activitypub/actor' import { ApplicationModel } from '../application/application' import { ServerModel } from '../server/server' -import { throwIfNotValid } from '../utils' +import { getSort, throwIfNotValid } from '../utils' import { VideoChannelModel } from '../video/video-channel' +import { VideoCommentModel } from '../video/video-comment' import { UserModel } from './user' +@DefaultScope({ + include: [ + { + model: () => ActorModel, // Default scope includes avatar and server + required: true + } + ] +}) @Table({ tableName: 'account', indexes: [ { - fields: [ 'name' ] - }, - { - fields: [ 'serverId' ] - }, - { - fields: [ 'userId' ], + fields: [ 'actorId' ], unique: true }, { - fields: [ 'applicationId' ], - unique: true + fields: [ 'applicationId' ] }, { - fields: [ 'name', 'serverId', 'applicationId' ], - unique: true + fields: [ 'userId' ] } ] }) export class AccountModel extends Model { + @AllowNull(false) + @Column + name: string + + @AllowNull(true) + @Default(null) + @Is('AccountDescription', value => throwIfNotValid(value, isAccountDescriptionValid, 'description')) + @Column + description: string + @CreatedAt createdAt: Date @@ -100,35 +111,43 @@ export class AccountModel extends Model { }) VideoChannels: VideoChannelModel[] - @AfterDestroy - static sendDeleteIfOwned (instance: AccountModel) { + @HasMany(() => VideoCommentModel, { + foreignKey: { + allowNull: false + }, + onDelete: 'cascade', + hooks: true + }) + VideoComments: VideoCommentModel[] + + @BeforeDestroy + static async sendDeleteIfOwned (instance: AccountModel, options) { + if (!instance.Actor) { + instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) as ActorModel + } + if (instance.isOwned()) { - return sendDeleteAccount(instance, undefined) + return sendDeleteActor(instance.Actor, options.transaction) } return undefined } - static loadApplication () { - return AccountModel.findOne({ - include: [ - { - model: ApplicationModel, - required: true - } - ] - }) - } - - static load (id: number) { - return AccountModel.findById(id) + static load (id: number, transaction?: Sequelize.Transaction) { + return AccountModel.findById(id, { transaction }) } static loadByUUID (uuid: string) { const query = { - where: { - uuid - } + include: [ + { + model: ActorModel, + required: true, + where: { + uuid + } + } + ] } return AccountModel.findOne(query) @@ -137,7 +156,6 @@ export class AccountModel extends Model { static loadLocalByName (name: string) { const query = { where: { - name, [ Sequelize.Op.or ]: [ { userId: { @@ -150,7 +168,16 @@ export class AccountModel extends Model { } } ] - } + }, + include: [ + { + model: ActorModel, + required: true, + where: { + preferredUsername: name + } + } + ] } return AccountModel.findOne(query) @@ -158,16 +185,22 @@ export class AccountModel extends Model { static loadByNameAndHost (name: string, host: string) { const query = { - where: { - name - }, include: [ { - model: ServerModel, + model: ActorModel, required: true, where: { - host - } + preferredUsername: name + }, + include: [ + { + model: ServerModel, + required: true, + where: { + host + } + } + ] } ] } @@ -192,29 +225,28 @@ export class AccountModel extends Model { return AccountModel.findOne(query) } - static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction) { + static listForApi (start: number, count: number, sort: string) { const query = { - include: [ - { - model: ActorModel, - required: true, - where: { - followersUrl: { - [ Sequelize.Op.in ]: followersUrls - } - } - } - ], - transaction + offset: start, + limit: count, + order: getSort(sort) } - return AccountModel.findAll(query) + return AccountModel.findAndCountAll(query) + .then(({ rows, count }) => { + return { + data: rows, + total: count + } + }) } - toFormattedJSON () { + toFormattedJSON (): Account { const actor = this.Actor.toFormattedJSON() const account = { id: this.id, + displayName: this.getDisplayName(), + description: this.description, createdAt: this.createdAt, updatedAt: this.updatedAt } @@ -223,10 +255,18 @@ export class AccountModel extends Model { } toActivityPubObject () { - return this.Actor.toActivityPubObject(this.name, this.uuid, 'Account') + const obj = this.Actor.toActivityPubObject(this.name, 'Account') + + return Object.assign(obj, { + summary: this.description + }) } isOwned () { return this.Actor.isOwned() } + + getDisplayName () { + return this.name + } }