1 import { body, param } from 'express-validator/check'
2 import * as express from 'express'
3 import { logger } from '../../helpers/logger'
4 import { areValidationErrors } from './utils'
5 import { isAccountNameWithHostExist } from '../../helpers/custom-validators/accounts'
6 import { UserModel } from '../../models/account/user'
7 import { AccountBlocklistModel } from '../../models/account/account-blocklist'
8 import { isHostValid } from '../../helpers/custom-validators/servers'
9 import { ServerBlocklistModel } from '../../models/server/server-blocklist'
10 import { ServerModel } from '../../models/server/server'
11 import { CONFIG } from '../../initializers'
12 import { getServerActor } from '../../helpers/utils'
14 const blockAccountValidator = [
15 body('accountName').exists().withMessage('Should have an account name with host'),
17 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
18 logger.debug('Checking blockAccountByAccountValidator parameters', { parameters: req.body })
20 if (areValidationErrors(req, res)) return
21 if (!await isAccountNameWithHostExist(req.body.accountName, res)) return
23 const user = res.locals.oauth.token.User as UserModel
24 const accountToBlock = res.locals.account
26 if (user.Account.id === accountToBlock.id) {
28 .send({ error: 'You cannot block yourself.' })
38 const unblockAccountByAccountValidator = [
39 param('accountName').exists().withMessage('Should have an account name with host'),
41 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
42 logger.debug('Checking unblockAccountByAccountValidator parameters', { parameters: req.params })
44 if (areValidationErrors(req, res)) return
45 if (!await isAccountNameWithHostExist(req.params.accountName, res)) return
47 const user = res.locals.oauth.token.User as UserModel
48 const targetAccount = res.locals.account
49 if (!await isUnblockAccountExists(user.Account.id, targetAccount.id, res)) return
55 const unblockAccountByServerValidator = [
56 param('accountName').exists().withMessage('Should have an account name with host'),
58 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
59 logger.debug('Checking unblockAccountByServerValidator parameters', { parameters: req.params })
61 if (areValidationErrors(req, res)) return
62 if (!await isAccountNameWithHostExist(req.params.accountName, res)) return
64 const serverActor = await getServerActor()
65 const targetAccount = res.locals.account
66 if (!await isUnblockAccountExists(serverActor.Account.id, targetAccount.id, res)) return
72 const blockServerValidator = [
73 body('host').custom(isHostValid).withMessage('Should have a valid host'),
75 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
76 logger.debug('Checking serverGetValidator parameters', { parameters: req.body })
78 if (areValidationErrors(req, res)) return
80 const host: string = req.body.host
82 if (host === CONFIG.WEBSERVER.HOST) {
83 return res.status(409)
84 .send({ error: 'You cannot block your own server.' })
88 const server = await ServerModel.loadByHost(host)
90 return res.status(404)
91 .send({ error: 'Server host not found.' })
95 res.locals.server = server
101 const unblockServerByAccountValidator = [
102 param('host').custom(isHostValid).withMessage('Should have an account name with host'),
104 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
105 logger.debug('Checking unblockServerByAccountValidator parameters', { parameters: req.params })
107 if (areValidationErrors(req, res)) return
109 const user = res.locals.oauth.token.User as UserModel
110 if (!await isUnblockServerExists(user.Account.id, req.params.host, res)) return
116 const unblockServerByServerValidator = [
117 param('host').custom(isHostValid).withMessage('Should have an account name with host'),
119 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
120 logger.debug('Checking unblockServerByServerValidator parameters', { parameters: req.params })
122 if (areValidationErrors(req, res)) return
124 const serverActor = await getServerActor()
125 if (!await isUnblockServerExists(serverActor.Account.id, req.params.host, res)) return
131 // ---------------------------------------------------------------------------
134 blockServerValidator,
135 blockAccountValidator,
136 unblockAccountByAccountValidator,
137 unblockServerByAccountValidator,
138 unblockAccountByServerValidator,
139 unblockServerByServerValidator
142 // ---------------------------------------------------------------------------
144 async function isUnblockAccountExists (accountId: number, targetAccountId: number, res: express.Response) {
145 const accountBlock = await AccountBlocklistModel.loadByAccountAndTarget(accountId, targetAccountId)
148 .send({ error: 'Account block entry not found.' })
154 res.locals.accountBlock = accountBlock
159 async function isUnblockServerExists (accountId: number, host: string, res: express.Response) {
160 const serverBlock = await ServerBlocklistModel.loadByAccountAndHost(accountId, host)
163 .send({ error: 'Server block entry not found.' })
169 res.locals.serverBlock = serverBlock