-import { body, param } from 'express-validator/check'
-import * as express from 'express'
-import { logger } from '../../helpers/logger'
-import { areValidationErrors } from './utils'
-import { doesAccountNameWithHostExist } from '../../helpers/custom-validators/accounts'
-import { UserModel } from '../../models/account/user'
+import express from 'express'
+import { body, param, query } from 'express-validator'
+import { areValidActorHandles } from '@server/helpers/custom-validators/activitypub/actor'
+import { getServerActor } from '@server/models/application/application'
+import { arrayify } from '@shared/core-utils'
+import { HttpStatusCode } from '../../../shared/models/http/http-error-codes'
+import { isEachUniqueHostValid, isHostValid } from '../../helpers/custom-validators/servers'
+import { WEBSERVER } from '../../initializers/constants'
import { AccountBlocklistModel } from '../../models/account/account-blocklist'
-import { isHostValid } from '../../helpers/custom-validators/servers'
-import { ServerBlocklistModel } from '../../models/server/server-blocklist'
import { ServerModel } from '../../models/server/server'
-import { CONFIG } from '../../initializers'
-import { getServerActor } from '../../helpers/utils'
+import { ServerBlocklistModel } from '../../models/server/server-blocklist'
+import { areValidationErrors, doesAccountNameWithHostExist } from './shared'
const blockAccountValidator = [
- body('accountName').exists().withMessage('Should have an account name with host'),
+ body('accountName')
+ .exists(),
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 doesAccountNameWithHostExist(req.body.accountName, res)) return
const accountToBlock = res.locals.account
if (user.Account.id === accountToBlock.id) {
- res.status(409)
- .send({ error: 'You cannot block yourself.' })
- .end()
-
+ res.fail({
+ status: HttpStatusCode.CONFLICT_409,
+ message: 'You cannot block yourself.'
+ })
return
}
]
const unblockAccountByAccountValidator = [
- param('accountName').exists().withMessage('Should have an account name with host'),
+ param('accountName')
+ .exists(),
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 doesAccountNameWithHostExist(req.params.accountName, res)) return
]
const unblockAccountByServerValidator = [
- param('accountName').exists().withMessage('Should have an account name with host'),
+ param('accountName')
+ .exists(),
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
- logger.debug('Checking unblockAccountByServerValidator parameters', { parameters: req.params })
-
if (areValidationErrors(req, res)) return
if (!await doesAccountNameWithHostExist(req.params.accountName, res)) return
]
const blockServerValidator = [
- body('host').custom(isHostValid).withMessage('Should have a valid host'),
+ body('host')
+ .custom(isHostValid),
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
- logger.debug('Checking serverGetValidator parameters', { parameters: req.body })
-
if (areValidationErrors(req, res)) return
const host: string = req.body.host
- if (host === CONFIG.WEBSERVER.HOST) {
- return res.status(409)
- .send({ error: 'You cannot block your own server.' })
- .end()
+ if (host === WEBSERVER.HOST) {
+ return res.fail({
+ status: HttpStatusCode.CONFLICT_409,
+ message: 'You cannot block your own server.'
+ })
}
- const server = await ServerModel.loadByHost(host)
- if (!server) {
- return res.status(404)
- .send({ error: 'Server host not found.' })
- .end()
- }
+ const server = await ServerModel.loadOrCreateByHost(host)
res.locals.server = server
]
const unblockServerByAccountValidator = [
- param('host').custom(isHostValid).withMessage('Should have an account name with host'),
+ param('host')
+ .custom(isHostValid),
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
]
const unblockServerByServerValidator = [
- param('host').custom(isHostValid).withMessage('Should have an account name with host'),
+ param('host')
+ .custom(isHostValid),
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
- logger.debug('Checking unblockServerByServerValidator parameters', { parameters: req.params })
-
if (areValidationErrors(req, res)) return
const serverActor = await getServerActor()
}
]
+const blocklistStatusValidator = [
+ query('hosts')
+ .optional()
+ .customSanitizer(arrayify)
+ .custom(isEachUniqueHostValid).withMessage('Should have a valid hosts array'),
+
+ query('accounts')
+ .optional()
+ .customSanitizer(arrayify)
+ .custom(areValidActorHandles).withMessage('Should have a valid accounts array'),
+
+ (req: express.Request, res: express.Response, next: express.NextFunction) => {
+ if (areValidationErrors(req, res)) return
+
+ return next()
+ }
+]
+
// ---------------------------------------------------------------------------
export {
unblockAccountByAccountValidator,
unblockServerByAccountValidator,
unblockAccountByServerValidator,
- unblockServerByServerValidator
+ unblockServerByServerValidator,
+ blocklistStatusValidator
}
// ---------------------------------------------------------------------------
async function doesUnblockAccountExist (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()
-
+ res.fail({
+ status: HttpStatusCode.NOT_FOUND_404,
+ message: 'Account block entry not found.'
+ })
return false
}
res.locals.accountBlock = accountBlock
-
return true
}
async function doesUnblockServerExist (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()
-
+ res.fail({
+ status: HttpStatusCode.NOT_FOUND_404,
+ message: 'Server block entry not found.'
+ })
return false
}
res.locals.serverBlock = serverBlock
-
return true
}