X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Faccount%2Faccount.ts;h=4dc4123018a5fc2dc6713df5afe447fa2786b9ff;hb=13176a07a95984a53cc59aec5217f2ce9806d1bc;hp=6f425024e8a5164392032fb647dce67729b6d5d2;hpb=6dd9de95dfa39bd5c1faed00d1dbd52cd112bae0;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/account/account.ts b/server/models/account/account.ts index 6f425024e..4dc412301 100644 --- a/server/models/account/account.ts +++ b/server/models/account/account.ts @@ -1,10 +1,9 @@ -import * as Sequelize from 'sequelize' import { AllowNull, BeforeDestroy, BelongsTo, Column, - CreatedAt, + CreatedAt, DataType, Default, DefaultScope, ForeignKey, @@ -27,36 +26,50 @@ import { VideoCommentModel } from '../video/video-comment' import { UserModel } from './user' import { AvatarModel } from '../avatar/avatar' import { VideoPlaylistModel } from '../video/video-playlist' -import { WEBSERVER } from '../../initializers/constants' +import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants' +import { FindOptions, IncludeOptions, Op, Transaction, WhereOptions } from 'sequelize' +import { AccountBlocklistModel } from './account-blocklist' +import { ServerBlocklistModel } from '../server/server-blocklist' +import { ActorFollowModel } from '../activitypub/actor-follow' export enum ScopeNames { SUMMARY = 'SUMMARY' } -@DefaultScope({ +export type SummaryOptions = { + whereActor?: WhereOptions + withAccountBlockerIds?: number[] +} + +@DefaultScope(() => ({ include: [ { - model: () => ActorModel, // Default scope includes avatar and server + model: ActorModel, // Default scope includes avatar and server required: true } ] -}) -@Scopes({ - [ ScopeNames.SUMMARY ]: (whereActor?: Sequelize.WhereOptions) => { - return { +})) +@Scopes(() => ({ + [ ScopeNames.SUMMARY ]: (options: SummaryOptions = {}) => { + const whereActor = options.whereActor || undefined + + const serverInclude: IncludeOptions = { + attributes: [ 'host' ], + model: ServerModel.unscoped(), + required: false + } + + const query: FindOptions = { attributes: [ 'id', 'name' ], include: [ { - attributes: [ 'id', 'uuid', 'preferredUsername', 'url', 'serverId', 'avatarId' ], + attributes: [ 'id', 'preferredUsername', 'url', 'serverId', 'avatarId' ], model: ActorModel.unscoped(), required: true, where: whereActor, include: [ - { - attributes: [ 'host' ], - model: ServerModel.unscoped(), - required: false - }, + serverInclude, + { model: AvatarModel.unscoped(), required: false @@ -65,8 +78,37 @@ export enum ScopeNames { } ] } + + if (options.withAccountBlockerIds) { + query.include.push({ + attributes: [ 'id' ], + model: AccountBlocklistModel.unscoped(), + as: 'BlockedAccounts', + required: false, + where: { + accountId: { + [Op.in]: options.withAccountBlockerIds + } + } + }) + + serverInclude.include = [ + { + attributes: [ 'id' ], + model: ServerBlocklistModel.unscoped(), + required: false, + where: { + accountId: { + [Op.in]: options.withAccountBlockerIds + } + } + } + ] + } + + return query } -}) +})) @Table({ tableName: 'account', indexes: [ @@ -90,8 +132,8 @@ export class AccountModel extends Model { @AllowNull(true) @Default(null) - @Is('AccountDescription', value => throwIfNotValid(value, isAccountDescriptionValid, 'description')) - @Column + @Is('AccountDescription', value => throwIfNotValid(value, isAccountDescriptionValid, 'description', true)) + @Column(DataType.STRING(CONSTRAINTS_FIELDS.USERS.DESCRIPTION.max)) description: string @CreatedAt @@ -163,12 +205,23 @@ export class AccountModel extends Model { }) VideoComments: VideoCommentModel[] + @HasMany(() => AccountBlocklistModel, { + foreignKey: { + name: 'targetAccountId', + allowNull: false + }, + as: 'BlockedAccounts', + onDelete: 'CASCADE' + }) + BlockedAccounts: AccountBlocklistModel[] + @BeforeDestroy static async sendDeleteIfOwned (instance: AccountModel, options) { if (!instance.Actor) { instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) as ActorModel } + await ActorFollowModel.removeFollowsOf(instance.Actor.id, options.transaction) if (instance.isOwned()) { return sendDeleteActor(instance.Actor, options.transaction) } @@ -176,26 +229,10 @@ export class AccountModel extends Model { return undefined } - static load (id: number, transaction?: Sequelize.Transaction) { + static load (id: number, transaction?: Transaction) { return AccountModel.findByPk(id, { transaction }) } - static loadByUUID (uuid: string) { - const query = { - include: [ - { - model: ActorModel, - required: true, - where: { - uuid - } - } - ] - } - - return AccountModel.findOne(query) - } - static loadByNameWithHost (nameWithHost: string) { const [ accountName, host ] = nameWithHost.split('@') @@ -207,15 +244,15 @@ export class AccountModel extends Model { static loadLocalByName (name: string) { const query = { where: { - [ Sequelize.Op.or ]: [ + [ Op.or ]: [ { userId: { - [ Sequelize.Op.ne ]: null + [ Op.ne ]: null } }, { applicationId: { - [ Sequelize.Op.ne ]: null + [ Op.ne ]: null } } ] @@ -259,7 +296,7 @@ export class AccountModel extends Model { return AccountModel.findOne(query) } - static loadByUrl (url: string, transaction?: Sequelize.Transaction) { + static loadByUrl (url: string, transaction?: Transaction) { const query = { include: [ { @@ -332,7 +369,6 @@ export class AccountModel extends Model { return { id: this.id, - uuid: actor.uuid, name: actor.name, displayName: this.getDisplayName(), url: actor.url, @@ -360,4 +396,8 @@ export class AccountModel extends Model { getDisplayName () { return this.name } + + isBlocked () { + return this.BlockedAccounts && this.BlockedAccounts.length !== 0 + } }