mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-06 03:50:26 +02:00
Add ability for users to block an account/instance on server side
This commit is contained in:
parent
dffd5d127f
commit
7ad9b9846c
33 changed files with 1344 additions and 56 deletions
94
server/middlewares/validators/blocklist.ts
Normal file
94
server/middlewares/validators/blocklist.ts
Normal file
|
@ -0,0 +1,94 @@
|
|||
import { param, body } from 'express-validator/check'
|
||||
import * as express from 'express'
|
||||
import { logger } from '../../helpers/logger'
|
||||
import { areValidationErrors } from './utils'
|
||||
import { isAccountNameWithHostExist } from '../../helpers/custom-validators/accounts'
|
||||
import { UserModel } from '../../models/account/user'
|
||||
import { AccountBlocklistModel } from '../../models/account/account-blocklist'
|
||||
import { isHostValid } from '../../helpers/custom-validators/servers'
|
||||
import { ServerBlocklistModel } from '../../models/server/server-blocklist'
|
||||
|
||||
const blockAccountByAccountValidator = [
|
||||
body('accountName').exists().withMessage('Should have an account name with host'),
|
||||
|
||||
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
||||
logger.debug('Checking blockAccountByAccountValidator parameters', { parameters: req.body })
|
||||
|
||||
if (areValidationErrors(req, res)) return
|
||||
if (!await isAccountNameWithHostExist(req.body.accountName, res)) return
|
||||
|
||||
return next()
|
||||
}
|
||||
]
|
||||
|
||||
const unblockAccountByAccountValidator = [
|
||||
param('accountName').exists().withMessage('Should have an account name with host'),
|
||||
|
||||
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
||||
logger.debug('Checking unblockAccountByAccountValidator parameters', { parameters: req.params })
|
||||
|
||||
if (areValidationErrors(req, res)) return
|
||||
if (!await isAccountNameWithHostExist(req.params.accountName, res)) return
|
||||
|
||||
const user = res.locals.oauth.token.User as UserModel
|
||||
const targetAccount = res.locals.account
|
||||
if (!await isUnblockAccountExists(user.Account.id, targetAccount.id, res)) return
|
||||
|
||||
return next()
|
||||
}
|
||||
]
|
||||
|
||||
const unblockServerByAccountValidator = [
|
||||
param('host').custom(isHostValid).withMessage('Should have an account name with host'),
|
||||
|
||||
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
||||
logger.debug('Checking unblockServerByAccountValidator parameters', { parameters: req.params })
|
||||
|
||||
if (areValidationErrors(req, res)) return
|
||||
|
||||
const user = res.locals.oauth.token.User as UserModel
|
||||
if (!await isUnblockServerExists(user.Account.id, req.params.host, res)) return
|
||||
|
||||
return next()
|
||||
}
|
||||
]
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export {
|
||||
blockAccountByAccountValidator,
|
||||
unblockAccountByAccountValidator,
|
||||
unblockServerByAccountValidator
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
async function isUnblockAccountExists (accountId: number, targetAccountId: number, res: express.Response) {
|
||||
const accountBlock = await AccountBlocklistModel.loadByAccountAndTarget(accountId, targetAccountId)
|
||||
if (!accountBlock) {
|
||||
res.status(404)
|
||||
.send({ error: 'Account block entry not found.' })
|
||||
.end()
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
res.locals.accountBlock = accountBlock
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
async function isUnblockServerExists (accountId: number, host: string, res: express.Response) {
|
||||
const serverBlock = await ServerBlocklistModel.loadByAccountAndHost(accountId, host)
|
||||
if (!serverBlock) {
|
||||
res.status(404)
|
||||
.send({ error: 'Server block entry not found.' })
|
||||
.end()
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
res.locals.serverBlock = serverBlock
|
||||
|
||||
return true
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
export * from './account'
|
||||
export * from './blocklist'
|
||||
export * from './oembed'
|
||||
export * from './activitypub'
|
||||
export * from './pagination'
|
||||
|
@ -10,3 +11,4 @@ export * from './user-subscriptions'
|
|||
export * from './videos'
|
||||
export * from './webfinger'
|
||||
export * from './search'
|
||||
export * from './server'
|
||||
|
|
33
server/middlewares/validators/server.ts
Normal file
33
server/middlewares/validators/server.ts
Normal file
|
@ -0,0 +1,33 @@
|
|||
import * as express from 'express'
|
||||
import { logger } from '../../helpers/logger'
|
||||
import { areValidationErrors } from './utils'
|
||||
import { isHostValid } from '../../helpers/custom-validators/servers'
|
||||
import { ServerModel } from '../../models/server/server'
|
||||
import { body } from 'express-validator/check'
|
||||
|
||||
const serverGetValidator = [
|
||||
body('host').custom(isHostValid).withMessage('Should have a valid host'),
|
||||
|
||||
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
||||
logger.debug('Checking serverGetValidator parameters', { parameters: req.body })
|
||||
|
||||
if (areValidationErrors(req, res)) return
|
||||
|
||||
const server = await ServerModel.loadByHost(req.body.host)
|
||||
if (!server) {
|
||||
return res.status(404)
|
||||
.send({ error: 'Server host not found.' })
|
||||
.end()
|
||||
}
|
||||
|
||||
res.locals.server = server
|
||||
|
||||
return next()
|
||||
}
|
||||
]
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export {
|
||||
serverGetValidator
|
||||
}
|
|
@ -16,6 +16,8 @@ const SORTABLE_VIDEO_CHANNELS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.V
|
|||
const SORTABLE_FOLLOWERS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.FOLLOWERS)
|
||||
const SORTABLE_FOLLOWING_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.FOLLOWING)
|
||||
const SORTABLE_USER_SUBSCRIPTIONS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USER_SUBSCRIPTIONS)
|
||||
const SORTABLE_ACCOUNTS_BLOCKLIST_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.ACCOUNTS_BLOCKLIST)
|
||||
const SORTABLE_SERVERS_BLOCKLIST_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.SERVERS_BLOCKLIST)
|
||||
|
||||
const usersSortValidator = checkSort(SORTABLE_USERS_COLUMNS)
|
||||
const accountsSortValidator = checkSort(SORTABLE_ACCOUNTS_COLUMNS)
|
||||
|
@ -31,6 +33,8 @@ const videoChannelsSortValidator = checkSort(SORTABLE_VIDEO_CHANNELS_COLUMNS)
|
|||
const followersSortValidator = checkSort(SORTABLE_FOLLOWERS_COLUMNS)
|
||||
const followingSortValidator = checkSort(SORTABLE_FOLLOWING_COLUMNS)
|
||||
const userSubscriptionsSortValidator = checkSort(SORTABLE_USER_SUBSCRIPTIONS_COLUMNS)
|
||||
const accountsBlocklistSortValidator = checkSort(SORTABLE_ACCOUNTS_BLOCKLIST_COLUMNS)
|
||||
const serversBlocklistSortValidator = checkSort(SORTABLE_SERVERS_BLOCKLIST_COLUMNS)
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
|
@ -48,5 +52,7 @@ export {
|
|||
jobsSortValidator,
|
||||
videoCommentThreadsSortValidator,
|
||||
userSubscriptionsSortValidator,
|
||||
videoChannelsSearchSortValidator
|
||||
videoChannelsSearchSortValidator,
|
||||
accountsBlocklistSortValidator,
|
||||
serversBlocklistSortValidator
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue