1 import { body, param } from 'express-validator'
2 import * as express from 'express'
3 import { logger } from '../../helpers/logger'
4 import { areValidationErrors } from './utils'
5 import { AccountBlocklistModel } from '../../models/account/account-blocklist'
6 import { isHostValid } from '../../helpers/custom-validators/servers'
7 import { ServerBlocklistModel } from '../../models/server/server-blocklist'
8 import { ServerModel } from '../../models/server/server'
9 import { getServerActor } from '../../helpers/utils'
10 import { WEBSERVER } from '../../initializers/constants'
11 import { doesAccountNameWithHostExist } from '../../helpers/middlewares'
13 const blockAccountValidator = [
14 body('accountName').exists().withMessage('Should have an account name with host'),
16 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
17 logger.debug('Checking blockAccountByAccountValidator parameters', { parameters: req.body })
19 if (areValidationErrors(req, res)) return
20 if (!await doesAccountNameWithHostExist(req.body.accountName, res)) return
22 const user = res.locals.oauth.token.User
23 const accountToBlock = res.locals.account
25 if (user.Account.id === accountToBlock.id) {
27 .send({ error: 'You cannot block yourself.' })
37 const unblockAccountByAccountValidator = [
38 param('accountName').exists().withMessage('Should have an account name with host'),
40 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
41 logger.debug('Checking unblockAccountByAccountValidator parameters', { parameters: req.params })
43 if (areValidationErrors(req, res)) return
44 if (!await doesAccountNameWithHostExist(req.params.accountName, res)) return
46 const user = res.locals.oauth.token.User
47 const targetAccount = res.locals.account
48 if (!await doesUnblockAccountExist(user.Account.id, targetAccount.id, res)) return
54 const unblockAccountByServerValidator = [
55 param('accountName').exists().withMessage('Should have an account name with host'),
57 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
58 logger.debug('Checking unblockAccountByServerValidator parameters', { parameters: req.params })
60 if (areValidationErrors(req, res)) return
61 if (!await doesAccountNameWithHostExist(req.params.accountName, res)) return
63 const serverActor = await getServerActor()
64 const targetAccount = res.locals.account
65 if (!await doesUnblockAccountExist(serverActor.Account.id, targetAccount.id, res)) return
71 const blockServerValidator = [
72 body('host').custom(isHostValid).withMessage('Should have a valid host'),
74 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
75 logger.debug('Checking serverGetValidator parameters', { parameters: req.body })
77 if (areValidationErrors(req, res)) return
79 const host: string = req.body.host
81 if (host === WEBSERVER.HOST) {
82 return res.status(409)
83 .send({ error: 'You cannot block your own server.' })
87 const server = await ServerModel.loadByHost(host)
89 return res.status(404)
90 .send({ error: 'Server host not found.' })
94 res.locals.server = server
100 const unblockServerByAccountValidator = [
101 param('host').custom(isHostValid).withMessage('Should have an account name with host'),
103 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
104 logger.debug('Checking unblockServerByAccountValidator parameters', { parameters: req.params })
106 if (areValidationErrors(req, res)) return
108 const user = res.locals.oauth.token.User
109 if (!await doesUnblockServerExist(user.Account.id, req.params.host, res)) return
115 const unblockServerByServerValidator = [
116 param('host').custom(isHostValid).withMessage('Should have an account name with host'),
118 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
119 logger.debug('Checking unblockServerByServerValidator parameters', { parameters: req.params })
121 if (areValidationErrors(req, res)) return
123 const serverActor = await getServerActor()
124 if (!await doesUnblockServerExist(serverActor.Account.id, req.params.host, res)) return
130 // ---------------------------------------------------------------------------
133 blockServerValidator,
134 blockAccountValidator,
135 unblockAccountByAccountValidator,
136 unblockServerByAccountValidator,
137 unblockAccountByServerValidator,
138 unblockServerByServerValidator
141 // ---------------------------------------------------------------------------
143 async function doesUnblockAccountExist (accountId: number, targetAccountId: number, res: express.Response) {
144 const accountBlock = await AccountBlocklistModel.loadByAccountAndTarget(accountId, targetAccountId)
147 .send({ error: 'Account block entry not found.' })
153 res.locals.accountBlock = accountBlock
158 async function doesUnblockServerExist (accountId: number, host: string, res: express.Response) {
159 const serverBlock = await ServerBlocklistModel.loadByAccountAndHost(accountId, host)
162 .send({ error: 'Server block entry not found.' })
168 res.locals.serverBlock = serverBlock