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'
|
2020-11-18 11:13:01 +01:00
|
|
|
import { UserNotificationModel } from '@server/models/account/user-notification'
|
|
|
|
import { logger } from '@server/helpers/logger'
|
2020-12-07 14:32:36 +01:00
|
|
|
import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
|
2018-10-12 15:26:04 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2020-04-19 14:11:40 +02:00
|
|
|
const resultList = await AccountBlocklistModel.listForApi({
|
|
|
|
start: req.query.start,
|
|
|
|
count: req.query.count,
|
|
|
|
sort: req.query.sort,
|
|
|
|
search: req.query.search,
|
|
|
|
accountId: user.Account.id
|
|
|
|
})
|
2018-10-12 15:26:04 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2020-11-18 11:13:01 +01:00
|
|
|
UserNotificationModel.removeNotificationsOf({
|
|
|
|
id: accountToBlock.id,
|
|
|
|
type: 'account',
|
|
|
|
forUserId: user.id
|
|
|
|
}).catch(err => logger.error('Cannot remove notifications after an account mute.', { err }))
|
|
|
|
|
2020-12-07 14:32:36 +01:00
|
|
|
return res.status(HttpStatusCode.NO_CONTENT_204).end()
|
2018-10-12 15:26:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2020-12-07 14:32:36 +01:00
|
|
|
return res.status(HttpStatusCode.NO_CONTENT_204).end()
|
2018-10-12 15:26:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2020-04-19 14:11:40 +02:00
|
|
|
const resultList = await ServerBlocklistModel.listForApi({
|
|
|
|
start: req.query.start,
|
|
|
|
count: req.query.count,
|
|
|
|
sort: req.query.sort,
|
|
|
|
search: req.query.search,
|
|
|
|
accountId: user.Account.id
|
|
|
|
})
|
2018-10-12 15:26:04 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2020-11-18 11:13:01 +01:00
|
|
|
UserNotificationModel.removeNotificationsOf({
|
|
|
|
id: serverToBlock.id,
|
|
|
|
type: 'server',
|
|
|
|
forUserId: user.id
|
|
|
|
}).catch(err => logger.error('Cannot remove notifications after a server mute.', { err }))
|
|
|
|
|
2020-12-07 14:32:36 +01:00
|
|
|
return res.status(HttpStatusCode.NO_CONTENT_204).end()
|
2018-10-12 15:26:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2020-12-07 14:32:36 +01:00
|
|
|
return res.status(HttpStatusCode.NO_CONTENT_204).end()
|
2018-10-12 15:26:04 +02:00
|
|
|
}
|