2018-10-12 10:26:40 -05:00
|
|
|
import { body, param } from 'express-validator/check'
|
2018-10-12 08:26:04 -05:00
|
|
|
import * as express from 'express'
|
|
|
|
import { logger } from '../../helpers/logger'
|
|
|
|
import { areValidationErrors } from './utils'
|
2019-03-19 03:26:50 -05:00
|
|
|
import { doesAccountNameWithHostExist } from '../../helpers/custom-validators/accounts'
|
2018-10-12 08:26:04 -05:00
|
|
|
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'
|
2018-10-12 10:26:40 -05:00
|
|
|
import { ServerModel } from '../../models/server/server'
|
|
|
|
import { CONFIG } from '../../initializers'
|
2018-10-15 06:03:04 -05:00
|
|
|
import { getServerActor } from '../../helpers/utils'
|
2018-10-12 08:26:04 -05:00
|
|
|
|
2018-10-15 06:03:04 -05:00
|
|
|
const blockAccountValidator = [
|
2018-10-12 08:26:04 -05: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 03:26:50 -05:00
|
|
|
if (!await doesAccountNameWithHostExist(req.body.accountName, res)) return
|
2018-10-12 08:26:04 -05:00
|
|
|
|
2018-10-12 10:26:40 -05:00
|
|
|
const user = res.locals.oauth.token.User as UserModel
|
|
|
|
const accountToBlock = res.locals.account
|
|
|
|
|
|
|
|
if (user.Account.id === accountToBlock.id) {
|
|
|
|
res.status(409)
|
|
|
|
.send({ error: 'You cannot block yourself.' })
|
|
|
|
.end()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-12 08:26:04 -05: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 03:26:50 -05:00
|
|
|
if (!await doesAccountNameWithHostExist(req.params.accountName, res)) return
|
2018-10-12 08:26:04 -05:00
|
|
|
|
|
|
|
const user = res.locals.oauth.token.User as UserModel
|
|
|
|
const targetAccount = res.locals.account
|
2019-03-19 03:26:50 -05:00
|
|
|
if (!await doesUnblockAccountExist(user.Account.id, targetAccount.id, res)) return
|
2018-10-12 08:26:04 -05:00
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2018-10-15 06:03:04 -05: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 03:26:50 -05:00
|
|
|
if (!await doesAccountNameWithHostExist(req.params.accountName, res)) return
|
2018-10-15 06:03:04 -05:00
|
|
|
|
|
|
|
const serverActor = await getServerActor()
|
|
|
|
const targetAccount = res.locals.account
|
2019-03-19 03:26:50 -05:00
|
|
|
if (!await doesUnblockAccountExist(serverActor.Account.id, targetAccount.id, res)) return
|
2018-10-15 06:03:04 -05:00
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
const blockServerValidator = [
|
2018-10-12 10:26:40 -05: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
|
|
|
|
|
|
|
|
if (host === CONFIG.WEBSERVER.HOST) {
|
|
|
|
return res.status(409)
|
|
|
|
.send({ error: 'You cannot block your own server.' })
|
|
|
|
.end()
|
|
|
|
}
|
|
|
|
|
|
|
|
const server = await ServerModel.loadByHost(host)
|
|
|
|
if (!server) {
|
|
|
|
return res.status(404)
|
|
|
|
.send({ error: 'Server host not found.' })
|
|
|
|
.end()
|
|
|
|
}
|
|
|
|
|
|
|
|
res.locals.server = server
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2018-10-12 08:26:04 -05: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
|
|
|
|
|
|
|
|
const user = res.locals.oauth.token.User as UserModel
|
2019-03-19 03:26:50 -05:00
|
|
|
if (!await doesUnblockServerExist(user.Account.id, req.params.host, res)) return
|
2018-10-12 08:26:04 -05:00
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2018-10-15 06:03:04 -05: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 03:26:50 -05:00
|
|
|
if (!await doesUnblockServerExist(serverActor.Account.id, req.params.host, res)) return
|
2018-10-15 06:03:04 -05:00
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2018-10-12 08:26:04 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2018-10-15 06:03:04 -05:00
|
|
|
blockServerValidator,
|
|
|
|
blockAccountValidator,
|
2018-10-12 08:26:04 -05:00
|
|
|
unblockAccountByAccountValidator,
|
2018-10-15 06:03:04 -05:00
|
|
|
unblockServerByAccountValidator,
|
|
|
|
unblockAccountByServerValidator,
|
|
|
|
unblockServerByServerValidator
|
2018-10-12 08:26:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2019-03-19 03:26:50 -05:00
|
|
|
async function doesUnblockAccountExist (accountId: number, targetAccountId: number, res: express.Response) {
|
2018-10-12 08:26:04 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-03-19 03:26:50 -05:00
|
|
|
async function doesUnblockServerExist (accountId: number, host: string, res: express.Response) {
|
2018-10-12 08:26:04 -05:00
|
|
|
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
|
|
|
|
}
|