2019-07-25 16:23:44 +02:00
|
|
|
import { body, param } from 'express-validator'
|
2018-10-12 15:26:04 +02:00
|
|
|
import * as express from 'express'
|
|
|
|
import { logger } from '../../helpers/logger'
|
|
|
|
import { areValidationErrors } from './utils'
|
|
|
|
import { AccountBlocklistModel } from '../../models/account/account-blocklist'
|
|
|
|
import { isHostValid } from '../../helpers/custom-validators/servers'
|
|
|
|
import { ServerBlocklistModel } from '../../models/server/server-blocklist'
|
2018-10-12 17:26:40 +02:00
|
|
|
import { ServerModel } from '../../models/server/server'
|
2019-04-11 11:33:44 +02:00
|
|
|
import { WEBSERVER } from '../../initializers/constants'
|
2019-07-23 10:40:39 +02:00
|
|
|
import { doesAccountNameWithHostExist } from '../../helpers/middlewares'
|
2020-04-23 09:32:53 +02:00
|
|
|
import { getServerActor } from '@server/models/application/application'
|
2020-12-07 14:32:36 +01:00
|
|
|
import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
|
2018-10-12 15:26:04 +02:00
|
|
|
|
2018-10-15 13:03:04 +02:00
|
|
|
const blockAccountValidator = [
|
2018-10-12 15:26:04 +02:00
|
|
|
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
|
2019-03-19 09:26:50 +01:00
|
|
|
if (!await doesAccountNameWithHostExist(req.body.accountName, res)) return
|
2018-10-12 15:26:04 +02:00
|
|
|
|
2019-03-19 10:35:15 +01:00
|
|
|
const user = res.locals.oauth.token.User
|
2018-10-12 17:26:40 +02:00
|
|
|
const accountToBlock = res.locals.account
|
|
|
|
|
|
|
|
if (user.Account.id === accountToBlock.id) {
|
2020-12-07 14:32:36 +01:00
|
|
|
res.status(HttpStatusCode.CONFLICT_409)
|
2020-05-14 16:56:15 +02:00
|
|
|
.json({ error: 'You cannot block yourself.' })
|
2018-10-12 17:26:40 +02:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-12 15:26:04 +02:00
|
|
|
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
|
2019-03-19 09:26:50 +01:00
|
|
|
if (!await doesAccountNameWithHostExist(req.params.accountName, res)) return
|
2018-10-12 15:26:04 +02:00
|
|
|
|
2019-03-19 10:35:15 +01:00
|
|
|
const user = res.locals.oauth.token.User
|
2018-10-12 15:26:04 +02:00
|
|
|
const targetAccount = res.locals.account
|
2019-03-19 09:26:50 +01:00
|
|
|
if (!await doesUnblockAccountExist(user.Account.id, targetAccount.id, res)) return
|
2018-10-12 15:26:04 +02:00
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2018-10-15 13:03:04 +02:00
|
|
|
const unblockAccountByServerValidator = [
|
|
|
|
param('accountName').exists().withMessage('Should have an account name with host'),
|
|
|
|
|
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
logger.debug('Checking unblockAccountByServerValidator parameters', { parameters: req.params })
|
|
|
|
|
|
|
|
if (areValidationErrors(req, res)) return
|
2019-03-19 09:26:50 +01:00
|
|
|
if (!await doesAccountNameWithHostExist(req.params.accountName, res)) return
|
2018-10-15 13:03:04 +02:00
|
|
|
|
|
|
|
const serverActor = await getServerActor()
|
|
|
|
const targetAccount = res.locals.account
|
2019-03-19 09:26:50 +01:00
|
|
|
if (!await doesUnblockAccountExist(serverActor.Account.id, targetAccount.id, res)) return
|
2018-10-15 13:03:04 +02:00
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
const blockServerValidator = [
|
2018-10-12 17:26:40 +02:00
|
|
|
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 host: string = req.body.host
|
|
|
|
|
2019-04-11 11:33:44 +02:00
|
|
|
if (host === WEBSERVER.HOST) {
|
2020-12-07 14:32:36 +01:00
|
|
|
return res.status(HttpStatusCode.CONFLICT_409)
|
2020-05-14 16:56:15 +02:00
|
|
|
.json({ error: 'You cannot block your own server.' })
|
2018-10-12 17:26:40 +02:00
|
|
|
}
|
|
|
|
|
2020-05-07 14:58:24 +02:00
|
|
|
const server = await ServerModel.loadOrCreateByHost(host)
|
2018-10-12 17:26:40 +02:00
|
|
|
|
|
|
|
res.locals.server = server
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2018-10-12 15:26:04 +02:00
|
|
|
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
|
|
|
|
|
2019-03-19 10:35:15 +01:00
|
|
|
const user = res.locals.oauth.token.User
|
2019-03-19 09:26:50 +01:00
|
|
|
if (!await doesUnblockServerExist(user.Account.id, req.params.host, res)) return
|
2018-10-12 15:26:04 +02:00
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2018-10-15 13:03:04 +02:00
|
|
|
const unblockServerByServerValidator = [
|
|
|
|
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 unblockServerByServerValidator parameters', { parameters: req.params })
|
|
|
|
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
|
|
|
|
const serverActor = await getServerActor()
|
2019-03-19 09:26:50 +01:00
|
|
|
if (!await doesUnblockServerExist(serverActor.Account.id, req.params.host, res)) return
|
2018-10-15 13:03:04 +02:00
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2018-10-12 15:26:04 +02:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2018-10-15 13:03:04 +02:00
|
|
|
blockServerValidator,
|
|
|
|
blockAccountValidator,
|
2018-10-12 15:26:04 +02:00
|
|
|
unblockAccountByAccountValidator,
|
2018-10-15 13:03:04 +02:00
|
|
|
unblockServerByAccountValidator,
|
|
|
|
unblockAccountByServerValidator,
|
|
|
|
unblockServerByServerValidator
|
2018-10-12 15:26:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2019-03-19 09:26:50 +01:00
|
|
|
async function doesUnblockAccountExist (accountId: number, targetAccountId: number, res: express.Response) {
|
2018-10-12 15:26:04 +02:00
|
|
|
const accountBlock = await AccountBlocklistModel.loadByAccountAndTarget(accountId, targetAccountId)
|
|
|
|
if (!accountBlock) {
|
2020-12-07 14:32:36 +01:00
|
|
|
res.status(HttpStatusCode.NOT_FOUND_404)
|
2020-05-14 16:56:15 +02:00
|
|
|
.json({ error: 'Account block entry not found.' })
|
2018-10-12 15:26:04 +02:00
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
res.locals.accountBlock = accountBlock
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-03-19 09:26:50 +01:00
|
|
|
async function doesUnblockServerExist (accountId: number, host: string, res: express.Response) {
|
2018-10-12 15:26:04 +02:00
|
|
|
const serverBlock = await ServerBlocklistModel.loadByAccountAndHost(accountId, host)
|
|
|
|
if (!serverBlock) {
|
2020-12-07 14:32:36 +01:00
|
|
|
res.status(HttpStatusCode.NOT_FOUND_404)
|
2020-05-14 16:56:15 +02:00
|
|
|
.json({ error: 'Server block entry not found.' })
|
2018-10-12 15:26:04 +02:00
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
res.locals.serverBlock = serverBlock
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|