2018-10-12 15:26:04 +02:00
|
|
|
import * as express from 'express'
|
|
|
|
import 'multer'
|
|
|
|
import { getFormattedObjects } from '../../../helpers/utils'
|
|
|
|
import {
|
|
|
|
asyncMiddleware,
|
|
|
|
asyncRetryTransactionMiddleware,
|
|
|
|
authenticate,
|
|
|
|
paginationValidator,
|
|
|
|
setDefaultPagination,
|
|
|
|
setDefaultSort,
|
|
|
|
unblockAccountByAccountValidator
|
|
|
|
} from '../../../middlewares'
|
|
|
|
import {
|
|
|
|
accountsBlocklistSortValidator,
|
2018-10-15 13:03:04 +02:00
|
|
|
blockAccountValidator,
|
|
|
|
blockServerValidator,
|
2018-10-12 15:26:04 +02:00
|
|
|
serversBlocklistSortValidator,
|
|
|
|
unblockServerByAccountValidator
|
|
|
|
} from '../../../middlewares/validators'
|
|
|
|
import { AccountBlocklistModel } from '../../../models/account/account-blocklist'
|
|
|
|
import { addAccountInBlocklist, addServerInBlocklist, removeAccountFromBlocklist, removeServerFromBlocklist } from '../../../lib/blocklist'
|
|
|
|
import { ServerBlocklistModel } from '../../../models/server/server-blocklist'
|
|
|
|
|
|
|
|
const myBlocklistRouter = express.Router()
|
|
|
|
|
|
|
|
myBlocklistRouter.get('/me/blocklist/accounts',
|
|
|
|
authenticate,
|
|
|
|
paginationValidator,
|
|
|
|
accountsBlocklistSortValidator,
|
|
|
|
setDefaultSort,
|
|
|
|
setDefaultPagination,
|
|
|
|
asyncMiddleware(listBlockedAccounts)
|
|
|
|
)
|
|
|
|
|
|
|
|
myBlocklistRouter.post('/me/blocklist/accounts',
|
|
|
|
authenticate,
|
2018-10-15 13:03:04 +02:00
|
|
|
asyncMiddleware(blockAccountValidator),
|
2018-10-12 15:26:04 +02:00
|
|
|
asyncRetryTransactionMiddleware(blockAccount)
|
|
|
|
)
|
|
|
|
|
|
|
|
myBlocklistRouter.delete('/me/blocklist/accounts/:accountName',
|
|
|
|
authenticate,
|
|
|
|
asyncMiddleware(unblockAccountByAccountValidator),
|
|
|
|
asyncRetryTransactionMiddleware(unblockAccount)
|
|
|
|
)
|
|
|
|
|
|
|
|
myBlocklistRouter.get('/me/blocklist/servers',
|
|
|
|
authenticate,
|
|
|
|
paginationValidator,
|
|
|
|
serversBlocklistSortValidator,
|
|
|
|
setDefaultSort,
|
|
|
|
setDefaultPagination,
|
|
|
|
asyncMiddleware(listBlockedServers)
|
|
|
|
)
|
|
|
|
|
|
|
|
myBlocklistRouter.post('/me/blocklist/servers',
|
|
|
|
authenticate,
|
2018-10-15 13:03:04 +02:00
|
|
|
asyncMiddleware(blockServerValidator),
|
2018-10-12 15:26:04 +02:00
|
|
|
asyncRetryTransactionMiddleware(blockServer)
|
|
|
|
)
|
|
|
|
|
|
|
|
myBlocklistRouter.delete('/me/blocklist/servers/:host',
|
|
|
|
authenticate,
|
|
|
|
asyncMiddleware(unblockServerByAccountValidator),
|
|
|
|
asyncRetryTransactionMiddleware(unblockServer)
|
|
|
|
)
|
|
|
|
|
|
|
|
export {
|
|
|
|
myBlocklistRouter
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
async function listBlockedAccounts (req: express.Request, res: express.Response) {
|
2019-03-19 10:35:15 +01:00
|
|
|
const user = res.locals.oauth.token.User
|
2018-10-12 15:26:04 +02:00
|
|
|
|
|
|
|
const resultList = await AccountBlocklistModel.listForApi(user.Account.id, req.query.start, req.query.count, req.query.sort)
|
|
|
|
|
|
|
|
return res.json(getFormattedObjects(resultList.data, resultList.total))
|
|
|
|
}
|
|
|
|
|
|
|
|
async function blockAccount (req: express.Request, res: express.Response) {
|
2019-03-19 10:35:15 +01:00
|
|
|
const user = res.locals.oauth.token.User
|
2019-03-19 16:23:02 +01:00
|
|
|
const accountToBlock = res.locals.account
|
2018-10-12 15:26:04 +02:00
|
|
|
|
|
|
|
await addAccountInBlocklist(user.Account.id, accountToBlock.id)
|
|
|
|
|
|
|
|
return res.status(204).end()
|
|
|
|
}
|
|
|
|
|
|
|
|
async function unblockAccount (req: express.Request, res: express.Response) {
|
2019-03-19 10:35:15 +01:00
|
|
|
const accountBlock = res.locals.accountBlock
|
2018-10-12 15:26:04 +02:00
|
|
|
|
|
|
|
await removeAccountFromBlocklist(accountBlock)
|
|
|
|
|
|
|
|
return res.status(204).end()
|
|
|
|
}
|
|
|
|
|
|
|
|
async function listBlockedServers (req: express.Request, res: express.Response) {
|
2019-03-19 10:35:15 +01:00
|
|
|
const user = res.locals.oauth.token.User
|
2018-10-12 15:26:04 +02:00
|
|
|
|
|
|
|
const resultList = await ServerBlocklistModel.listForApi(user.Account.id, req.query.start, req.query.count, req.query.sort)
|
|
|
|
|
|
|
|
return res.json(getFormattedObjects(resultList.data, resultList.total))
|
|
|
|
}
|
|
|
|
|
|
|
|
async function blockServer (req: express.Request, res: express.Response) {
|
2019-03-19 10:35:15 +01:00
|
|
|
const user = res.locals.oauth.token.User
|
|
|
|
const serverToBlock = res.locals.server
|
2018-10-12 15:26:04 +02:00
|
|
|
|
|
|
|
await addServerInBlocklist(user.Account.id, serverToBlock.id)
|
|
|
|
|
|
|
|
return res.status(204).end()
|
|
|
|
}
|
|
|
|
|
|
|
|
async function unblockServer (req: express.Request, res: express.Response) {
|
2019-03-19 10:35:15 +01:00
|
|
|
const serverBlock = res.locals.serverBlock
|
2018-10-12 15:26:04 +02:00
|
|
|
|
|
|
|
await removeServerFromBlocklist(serverBlock)
|
|
|
|
|
|
|
|
return res.status(204).end()
|
|
|
|
}
|