2021-08-27 08:32:44 -04:00
|
|
|
import express from 'express'
|
2021-12-06 10:53:00 -05:00
|
|
|
import { body, param, query } from 'express-validator'
|
|
|
|
import { areValidActorHandles } from '@server/helpers/custom-validators/activitypub/actor'
|
2021-06-03 11:33:44 -04:00
|
|
|
import { getServerActor } from '@server/models/application/application'
|
2022-08-17 09:44:32 -04:00
|
|
|
import { arrayify } from '@shared/core-utils'
|
2021-07-16 04:42:24 -04:00
|
|
|
import { HttpStatusCode } from '../../../shared/models/http/http-error-codes'
|
2021-12-06 10:53:00 -05:00
|
|
|
import { isEachUniqueHostValid, isHostValid } from '../../helpers/custom-validators/servers'
|
2021-06-03 11:33:44 -04:00
|
|
|
import { WEBSERVER } from '../../initializers/constants'
|
2018-10-12 09:26:04 -04:00
|
|
|
import { AccountBlocklistModel } from '../../models/account/account-blocklist'
|
2018-10-12 11:26:40 -04:00
|
|
|
import { ServerModel } from '../../models/server/server'
|
2021-06-03 11:33:44 -04:00
|
|
|
import { ServerBlocklistModel } from '../../models/server/server-blocklist'
|
|
|
|
import { areValidationErrors, doesAccountNameWithHostExist } from './shared'
|
2018-10-12 09:26:04 -04:00
|
|
|
|
2018-10-15 07:03:04 -04:00
|
|
|
const blockAccountValidator = [
|
2022-08-17 08:27:04 -04:00
|
|
|
body('accountName')
|
|
|
|
.exists(),
|
2018-10-12 09:26:04 -04:00
|
|
|
|
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
if (areValidationErrors(req, res)) return
|
2019-03-19 04:26:50 -04:00
|
|
|
if (!await doesAccountNameWithHostExist(req.body.accountName, res)) return
|
2018-10-12 09:26:04 -04:00
|
|
|
|
2019-03-19 05:35:15 -04:00
|
|
|
const user = res.locals.oauth.token.User
|
2018-10-12 11:26:40 -04:00
|
|
|
const accountToBlock = res.locals.account
|
|
|
|
|
|
|
|
if (user.Account.id === accountToBlock.id) {
|
2021-05-31 19:36:53 -04:00
|
|
|
res.fail({
|
|
|
|
status: HttpStatusCode.CONFLICT_409,
|
|
|
|
message: 'You cannot block yourself.'
|
|
|
|
})
|
2018-10-12 11:26:40 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-12 09:26:04 -04:00
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
const unblockAccountByAccountValidator = [
|
2022-08-17 08:27:04 -04:00
|
|
|
param('accountName')
|
|
|
|
.exists(),
|
2018-10-12 09:26:04 -04:00
|
|
|
|
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
if (areValidationErrors(req, res)) return
|
2019-03-19 04:26:50 -04:00
|
|
|
if (!await doesAccountNameWithHostExist(req.params.accountName, res)) return
|
2018-10-12 09:26:04 -04:00
|
|
|
|
2019-03-19 05:35:15 -04:00
|
|
|
const user = res.locals.oauth.token.User
|
2018-10-12 09:26:04 -04:00
|
|
|
const targetAccount = res.locals.account
|
2019-03-19 04:26:50 -04:00
|
|
|
if (!await doesUnblockAccountExist(user.Account.id, targetAccount.id, res)) return
|
2018-10-12 09:26:04 -04:00
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2018-10-15 07:03:04 -04:00
|
|
|
const unblockAccountByServerValidator = [
|
2022-08-17 08:27:04 -04:00
|
|
|
param('accountName')
|
|
|
|
.exists(),
|
2018-10-15 07:03:04 -04:00
|
|
|
|
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
if (areValidationErrors(req, res)) return
|
2019-03-19 04:26:50 -04:00
|
|
|
if (!await doesAccountNameWithHostExist(req.params.accountName, res)) return
|
2018-10-15 07:03:04 -04:00
|
|
|
|
|
|
|
const serverActor = await getServerActor()
|
|
|
|
const targetAccount = res.locals.account
|
2019-03-19 04:26:50 -04:00
|
|
|
if (!await doesUnblockAccountExist(serverActor.Account.id, targetAccount.id, res)) return
|
2018-10-15 07:03:04 -04:00
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
const blockServerValidator = [
|
2022-08-17 08:27:04 -04:00
|
|
|
body('host')
|
|
|
|
.custom(isHostValid),
|
2018-10-12 11:26:40 -04:00
|
|
|
|
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
|
|
|
|
const host: string = req.body.host
|
|
|
|
|
2019-04-11 05:33:44 -04:00
|
|
|
if (host === WEBSERVER.HOST) {
|
2021-05-31 19:36:53 -04:00
|
|
|
return res.fail({
|
|
|
|
status: HttpStatusCode.CONFLICT_409,
|
|
|
|
message: 'You cannot block your own server.'
|
|
|
|
})
|
2018-10-12 11:26:40 -04:00
|
|
|
}
|
|
|
|
|
2020-05-07 08:58:24 -04:00
|
|
|
const server = await ServerModel.loadOrCreateByHost(host)
|
2018-10-12 11:26:40 -04:00
|
|
|
|
|
|
|
res.locals.server = server
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2018-10-12 09:26:04 -04:00
|
|
|
const unblockServerByAccountValidator = [
|
2022-08-17 08:27:04 -04:00
|
|
|
param('host')
|
|
|
|
.custom(isHostValid),
|
2018-10-12 09:26:04 -04:00
|
|
|
|
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
|
2019-03-19 05:35:15 -04:00
|
|
|
const user = res.locals.oauth.token.User
|
2019-03-19 04:26:50 -04:00
|
|
|
if (!await doesUnblockServerExist(user.Account.id, req.params.host, res)) return
|
2018-10-12 09:26:04 -04:00
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2018-10-15 07:03:04 -04:00
|
|
|
const unblockServerByServerValidator = [
|
2022-08-17 08:27:04 -04:00
|
|
|
param('host')
|
|
|
|
.custom(isHostValid),
|
2018-10-15 07:03:04 -04:00
|
|
|
|
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
|
|
|
|
const serverActor = await getServerActor()
|
2019-03-19 04:26:50 -04:00
|
|
|
if (!await doesUnblockServerExist(serverActor.Account.id, req.params.host, res)) return
|
2018-10-15 07:03:04 -04:00
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2021-12-06 10:53:00 -05:00
|
|
|
const blocklistStatusValidator = [
|
|
|
|
query('hosts')
|
|
|
|
.optional()
|
2022-08-17 09:44:32 -04:00
|
|
|
.customSanitizer(arrayify)
|
2021-12-06 10:53:00 -05:00
|
|
|
.custom(isEachUniqueHostValid).withMessage('Should have a valid hosts array'),
|
|
|
|
|
|
|
|
query('accounts')
|
|
|
|
.optional()
|
2022-08-17 09:44:32 -04:00
|
|
|
.customSanitizer(arrayify)
|
2021-12-06 10:53:00 -05:00
|
|
|
.custom(areValidActorHandles).withMessage('Should have a valid accounts array'),
|
|
|
|
|
|
|
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2018-10-12 09:26:04 -04:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2018-10-15 07:03:04 -04:00
|
|
|
blockServerValidator,
|
|
|
|
blockAccountValidator,
|
2018-10-12 09:26:04 -04:00
|
|
|
unblockAccountByAccountValidator,
|
2018-10-15 07:03:04 -04:00
|
|
|
unblockServerByAccountValidator,
|
|
|
|
unblockAccountByServerValidator,
|
2021-12-06 10:53:00 -05:00
|
|
|
unblockServerByServerValidator,
|
|
|
|
blocklistStatusValidator
|
2018-10-12 09:26:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2019-03-19 04:26:50 -04:00
|
|
|
async function doesUnblockAccountExist (accountId: number, targetAccountId: number, res: express.Response) {
|
2018-10-12 09:26:04 -04:00
|
|
|
const accountBlock = await AccountBlocklistModel.loadByAccountAndTarget(accountId, targetAccountId)
|
|
|
|
if (!accountBlock) {
|
2021-05-31 19:36:53 -04:00
|
|
|
res.fail({
|
|
|
|
status: HttpStatusCode.NOT_FOUND_404,
|
|
|
|
message: 'Account block entry not found.'
|
|
|
|
})
|
2018-10-12 09:26:04 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
res.locals.accountBlock = accountBlock
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-03-19 04:26:50 -04:00
|
|
|
async function doesUnblockServerExist (accountId: number, host: string, res: express.Response) {
|
2018-10-12 09:26:04 -04:00
|
|
|
const serverBlock = await ServerBlocklistModel.loadByAccountAndHost(accountId, host)
|
|
|
|
if (!serverBlock) {
|
2021-05-31 19:36:53 -04:00
|
|
|
res.fail({
|
|
|
|
status: HttpStatusCode.NOT_FOUND_404,
|
|
|
|
message: 'Server block entry not found.'
|
|
|
|
})
|
2018-10-12 09:26:04 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
res.locals.serverBlock = serverBlock
|
|
|
|
return true
|
|
|
|
}
|