1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
import { body, param } from 'express-validator/check'
import * as express from 'express'
import { logger } from '../../helpers/logger'
import { areValidationErrors } from './utils'
import { isAccountNameWithHostExist } from '../../helpers/custom-validators/accounts'
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'
import { ServerModel } from '../../models/server/server'
import { CONFIG } from '../../initializers'
const blockAccountByAccountValidator = [
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
if (!await isAccountNameWithHostExist(req.body.accountName, res)) return
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
}
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
if (!await isAccountNameWithHostExist(req.params.accountName, res)) return
const user = res.locals.oauth.token.User as UserModel
const targetAccount = res.locals.account
if (!await isUnblockAccountExists(user.Account.id, targetAccount.id, res)) return
return next()
}
]
const blockServerByAccountValidator = [
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()
}
]
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
if (!await isUnblockServerExists(user.Account.id, req.params.host, res)) return
return next()
}
]
// ---------------------------------------------------------------------------
export {
blockServerByAccountValidator,
blockAccountByAccountValidator,
unblockAccountByAccountValidator,
unblockServerByAccountValidator
}
// ---------------------------------------------------------------------------
async function isUnblockAccountExists (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()
return false
}
res.locals.accountBlock = accountBlock
return true
}
async function isUnblockServerExists (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()
return false
}
res.locals.serverBlock = serverBlock
return true
}
|