From e0a929179a9dc76e035ca7fda2b61d5ff46afbc5 Mon Sep 17 00:00:00 2001 From: Rigel Kent Date: Sun, 19 Apr 2020 14:11:40 +0200 Subject: Add filter inputs for blacklisted videos and muted accounts/servers --- server/controllers/api/server/server-blocklist.ts | 16 +++++++++-- server/controllers/api/users/my-blocklist.ts | 16 +++++++++-- server/controllers/api/videos/blacklist.ts | 8 +++++- .../validators/videos/video-blacklist.ts | 4 +++ server/models/account/account-blocklist.ts | 32 ++++++++++++++++++---- server/models/server/server-blocklist.ts | 17 ++++++++++-- server/models/utils.ts | 15 ++++++++-- server/models/video/video-abuse.ts | 18 ++++-------- server/models/video/video-blacklist.ts | 13 +++++++-- 9 files changed, 109 insertions(+), 30 deletions(-) (limited to 'server') diff --git a/server/controllers/api/server/server-blocklist.ts b/server/controllers/api/server/server-blocklist.ts index d165db191..ffb7814fa 100644 --- a/server/controllers/api/server/server-blocklist.ts +++ b/server/controllers/api/server/server-blocklist.ts @@ -82,7 +82,13 @@ export { async function listBlockedAccounts (req: express.Request, res: express.Response) { const serverActor = await getServerActor() - const resultList = await AccountBlocklistModel.listForApi(serverActor.Account.id, req.query.start, req.query.count, req.query.sort) + const resultList = await AccountBlocklistModel.listForApi({ + start: req.query.start, + count: req.query.count, + sort: req.query.sort, + search: req.query.search, + accountId: serverActor.Account.id + }) return res.json(getFormattedObjects(resultList.data, resultList.total)) } @@ -107,7 +113,13 @@ async function unblockAccount (req: express.Request, res: express.Response) { async function listBlockedServers (req: express.Request, res: express.Response) { const serverActor = await getServerActor() - const resultList = await ServerBlocklistModel.listForApi(serverActor.Account.id, req.query.start, req.query.count, req.query.sort) + const resultList = await ServerBlocklistModel.listForApi({ + start: req.query.start, + count: req.query.count, + sort: req.query.sort, + search: req.query.search, + accountId: serverActor.Account.id + }) return res.json(getFormattedObjects(resultList.data, resultList.total)) } diff --git a/server/controllers/api/users/my-blocklist.ts b/server/controllers/api/users/my-blocklist.ts index 713c16022..3a44376f2 100644 --- a/server/controllers/api/users/my-blocklist.ts +++ b/server/controllers/api/users/my-blocklist.ts @@ -74,7 +74,13 @@ export { async function listBlockedAccounts (req: express.Request, res: express.Response) { const user = res.locals.oauth.token.User - const resultList = await AccountBlocklistModel.listForApi(user.Account.id, req.query.start, req.query.count, req.query.sort) + const resultList = await AccountBlocklistModel.listForApi({ + start: req.query.start, + count: req.query.count, + sort: req.query.sort, + search: req.query.search, + accountId: user.Account.id + }) return res.json(getFormattedObjects(resultList.data, resultList.total)) } @@ -99,7 +105,13 @@ async function unblockAccount (req: express.Request, res: express.Response) { async function listBlockedServers (req: express.Request, res: express.Response) { const user = res.locals.oauth.token.User - const resultList = await ServerBlocklistModel.listForApi(user.Account.id, req.query.start, req.query.count, req.query.sort) + const resultList = await ServerBlocklistModel.listForApi({ + start: req.query.start, + count: req.query.count, + sort: req.query.sort, + search: req.query.search, + accountId: user.Account.id + }) return res.json(getFormattedObjects(resultList.data, resultList.total)) } diff --git a/server/controllers/api/videos/blacklist.ts b/server/controllers/api/videos/blacklist.ts index 2a667480d..c4aa79cd2 100644 --- a/server/controllers/api/videos/blacklist.ts +++ b/server/controllers/api/videos/blacklist.ts @@ -102,7 +102,13 @@ async function updateVideoBlacklistController (req: express.Request, res: expres } async function listBlacklist (req: express.Request, res: express.Response) { - const resultList = await VideoBlacklistModel.listForApi(req.query.start, req.query.count, req.query.sort, req.query.type) + const resultList = await VideoBlacklistModel.listForApi({ + start: req.query.start, + count: req.query.count, + sort: req.query.sort, + search: req.query.search, + type: req.query.type + }) return res.json(getFormattedObjects(resultList.data, resultList.total)) } diff --git a/server/middlewares/validators/videos/video-blacklist.ts b/server/middlewares/validators/videos/video-blacklist.ts index 5440e57e7..4bd6a8333 100644 --- a/server/middlewares/validators/videos/video-blacklist.ts +++ b/server/middlewares/validators/videos/video-blacklist.ts @@ -69,6 +69,10 @@ const videosBlacklistFiltersValidator = [ query('type') .optional() .custom(isVideoBlacklistTypeValid).withMessage('Should have a valid video blacklist type attribute'), + query('search') + .optional() + .not() + .isEmpty().withMessage('Should have a valid search'), (req: express.Request, res: express.Response, next: express.NextFunction) => { logger.debug('Checking videos blacklist filters query', { parameters: req.query }) diff --git a/server/models/account/account-blocklist.ts b/server/models/account/account-blocklist.ts index e2f66d733..fe2d5d010 100644 --- a/server/models/account/account-blocklist.ts +++ b/server/models/account/account-blocklist.ts @@ -1,6 +1,6 @@ import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' import { AccountModel } from './account' -import { getSort } from '../utils' +import { getSort, searchAttribute } from '../utils' import { AccountBlock } from '../../../shared/models/blocklist' import { Op } from 'sequelize' import * as Bluebird from 'bluebird' @@ -111,16 +111,36 @@ export class AccountBlocklistModel extends Model { return AccountBlocklistModel.findOne(query) } - static listForApi (accountId: number, start: number, count: number, sort: string) { + static listForApi (parameters: { + start: number + count: number + sort: string + search?: string + accountId: number + }) { + const { start, count, sort, search, accountId } = parameters + const query = { offset: start, limit: count, - order: getSort(sort), - where: { - accountId - } + order: getSort(sort) + } + + const where = { + accountId } + if (search) { + Object.assign(where, { + [Op.or]: [ + { ...searchAttribute(search, '$BlockedAccount.name$') }, + { ...searchAttribute(search, '$BlockedAccount.Actor.url$') } + ] + }) + } + + Object.assign(query, { where }) + return AccountBlocklistModel .scope([ ScopeNames.WITH_ACCOUNTS ]) .findAndCountAll(query) diff --git a/server/models/server/server-blocklist.ts b/server/models/server/server-blocklist.ts index 883ae47ab..764203d2c 100644 --- a/server/models/server/server-blocklist.ts +++ b/server/models/server/server-blocklist.ts @@ -2,7 +2,7 @@ import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, Updated import { AccountModel } from '../account/account' import { ServerModel } from './server' import { ServerBlock } from '../../../shared/models/blocklist' -import { getSort } from '../utils' +import { getSort, searchAttribute } from '../utils' import * as Bluebird from 'bluebird' import { MServerBlocklist, MServerBlocklistAccountServer, MServerBlocklistFormattable } from '@server/typings/models' import { Op } from 'sequelize' @@ -120,16 +120,27 @@ export class ServerBlocklistModel extends Model { return ServerBlocklistModel.findOne(query) } - static listForApi (accountId: number, start: number, count: number, sort: string) { + static listForApi (parameters: { + start: number + count: number + sort: string + search?: string + accountId: number + }) { + const { start, count, sort, search, accountId } = parameters + const query = { offset: start, limit: count, order: getSort(sort), where: { - accountId + accountId, + ...searchAttribute(search, '$BlockedServer.host$') } } + console.log(search) + return ServerBlocklistModel .scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_SERVER ]) .findAndCountAll(query) diff --git a/server/models/utils.ts b/server/models/utils.ts index 06ff05864..7137419a2 100644 --- a/server/models/utils.ts +++ b/server/models/utils.ts @@ -1,7 +1,7 @@ import { Model, Sequelize } from 'sequelize-typescript' import validator from 'validator' import { Col } from 'sequelize/types/lib/utils' -import { literal, OrderItem } from 'sequelize' +import { literal, OrderItem, Op } from 'sequelize' type Primitive = string | Function | number | boolean | Symbol | undefined | null type DeepOmitHelper = { @@ -207,6 +207,16 @@ function buildDirectionAndField (value: string) { return { direction, field } } +function searchAttribute (sourceField, targetField) { + return sourceField + ? { + [targetField]: { + [Op.iLike]: `%${sourceField}%` + } + } + : {} +} + // --------------------------------------------------------------------------- export { @@ -228,7 +238,8 @@ export { parseAggregateResult, getFollowsSort, buildDirectionAndField, - createSafeIn + createSafeIn, + searchAttribute } // --------------------------------------------------------------------------- diff --git a/server/models/video/video-abuse.ts b/server/models/video/video-abuse.ts index d68608ca6..e8c3bd823 100644 --- a/server/models/video/video-abuse.ts +++ b/server/models/video/video-abuse.ts @@ -9,7 +9,7 @@ import { isVideoAbuseStateValid } from '../../helpers/custom-validators/video-abuses' import { AccountModel } from '../account/account' -import { buildBlockedAccountSQL, getSort, throwIfNotValid } from '../utils' +import { buildBlockedAccountSQL, getSort, throwIfNotValid, searchAttribute } from '../utils' import { VideoModel } from './video' import { VideoAbuseState, VideoDetails } from '../../../shared' import { CONSTRAINTS_FIELDS, VIDEO_ABUSE_STATES } from '../../initializers/constants' @@ -17,8 +17,8 @@ import { MUserAccountId, MVideoAbuse, MVideoAbuseFormattable, MVideoAbuseVideo } import * as Bluebird from 'bluebird' import { literal, Op } from 'sequelize' import { ThumbnailModel } from './thumbnail' -import { VideoChannelModel } from './video-channel' import { VideoBlacklistModel } from './video-blacklist' +import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from './video-channel' export enum ScopeNames { FOR_API = 'FOR_API' @@ -33,12 +33,6 @@ export enum ScopeNames { serverAccountId: number userAccountId: any }) => { - const search = (sourceField, targetField) => sourceField ? ({ - [targetField]: { - [Op.iLike]: `%${sourceField}%` - } - }) : {} - let where = { reporterAccountId: { [Op.notIn]: literal('(' + buildBlockedAccountSQL(options.serverAccountId, options.userAccountId) + ')') @@ -148,19 +142,19 @@ export enum ScopeNames { { model: AccountModel, required: true, - where: { ...search(options.searchReporter, 'name') } + where: { ...searchAttribute(options.searchReporter, 'name') } }, { model: VideoModel, required: false, - where: { ...search(options.searchVideo, 'name') }, + where: { ...searchAttribute(options.searchVideo, 'name') }, include: [ { model: ThumbnailModel }, { - model: VideoChannelModel.scope([ 'WITH_ACTOR', 'WITH_ACCOUNT' ]), - where: { ...search(options.searchVideoChannel, 'name') } + model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }), + where: { ...searchAttribute(options.searchVideoChannel, 'name') } }, { attributes: [ 'id', 'reason', 'unfederated' ], diff --git a/server/models/video/video-blacklist.ts b/server/models/video/video-blacklist.ts index 694983cb3..680eba471 100644 --- a/server/models/video/video-blacklist.ts +++ b/server/models/video/video-blacklist.ts @@ -1,5 +1,5 @@ import { AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' -import { getBlacklistSort, SortType, throwIfNotValid } from '../utils' +import { getBlacklistSort, SortType, throwIfNotValid, searchAttribute } from '../utils' import { VideoModel } from './video' import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from './video-channel' import { isVideoBlacklistReasonValid, isVideoBlacklistTypeValid } from '../../helpers/custom-validators/video-blacklist' @@ -54,7 +54,15 @@ export class VideoBlacklistModel extends Model { }) Video: VideoModel - static listForApi (start: number, count: number, sort: SortType, type?: VideoBlacklistType) { + static listForApi (parameters: { + start: number + count: number + sort: SortType + search?: string + type?: VideoBlacklistType + }) { + const { start, count, sort, search, type } = parameters + function buildBaseQuery (): FindOptions { return { offset: start, @@ -70,6 +78,7 @@ export class VideoBlacklistModel extends Model { { model: VideoModel, required: true, + where: { ...searchAttribute(search, 'name') }, include: [ { model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }), -- cgit v1.2.3