add route for blocking user
This commit is contained in:
parent
389f447b5b
commit
5318b521e7
|
@ -785,7 +785,7 @@ export function startDm(query): PromiseAction {
|
|||
}
|
||||
}
|
||||
} catch {
|
||||
|
||||
dispatch(notify('Couldn\'t start DM'));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -41,11 +41,6 @@ const include = [{
|
|||
'id',
|
||||
'name',
|
||||
],
|
||||
}, {
|
||||
model: RegUser,
|
||||
as: 'blocked',
|
||||
through: UserBlock,
|
||||
foreignKey: 'uid',
|
||||
}],
|
||||
}, {
|
||||
model: RegUser,
|
||||
|
|
|
@ -17,8 +17,8 @@ const UserBlock = Model.define('UserBlock', {
|
|||
export async function isUserBlockedBy(userId, blockedById) {
|
||||
const exists = await UserBlock.findOne({
|
||||
where: {
|
||||
uid: userId,
|
||||
buid: blockedById,
|
||||
uid: blockedById,
|
||||
buid: userId,
|
||||
},
|
||||
raw: true,
|
||||
attributes: ['uid'],
|
||||
|
|
|
@ -24,6 +24,9 @@ Channel.belongsToMany(RegUser, {
|
|||
|
||||
/*
|
||||
* User blocks of other user
|
||||
*
|
||||
* uid: User that blocks
|
||||
* buid: User that is blocked
|
||||
*/
|
||||
RegUser.belongsToMany(RegUser, {
|
||||
as: 'blocked',
|
||||
|
|
87
src/routes/api/block.js
Normal file
87
src/routes/api/block.js
Normal file
|
@ -0,0 +1,87 @@
|
|||
/*
|
||||
*
|
||||
* blocks and unblocks a user
|
||||
*
|
||||
* @flow
|
||||
*/
|
||||
|
||||
import type { Request, Response } from 'express';
|
||||
|
||||
import logger from '../../core/logger';
|
||||
import { RegUser, UserBlock } from '../../data/models';
|
||||
|
||||
async function block(req: Request, res: Response) {
|
||||
let userId = parseInt(req.body.userId, 10);
|
||||
let { userName } = req.body;
|
||||
const { user } = req;
|
||||
|
||||
const errors = [];
|
||||
const query = {};
|
||||
if (userId) {
|
||||
if (userId && Number.isNaN(userId)) {
|
||||
errors.push('Invalid userId');
|
||||
}
|
||||
query.id = userId;
|
||||
}
|
||||
if (userName) {
|
||||
query.name = userName;
|
||||
}
|
||||
if (!userName && !userId) {
|
||||
errors.push('No userId or userName defined');
|
||||
}
|
||||
if (!user || !user.regUser) {
|
||||
errors.push('You are not logged in');
|
||||
}
|
||||
if (user && userId && user.id === userId) {
|
||||
errors.push('You can not DM yourself.');
|
||||
}
|
||||
if (errors.length) {
|
||||
res.status(400);
|
||||
res.json({
|
||||
errors,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const targetUser = await RegUser.findOne({
|
||||
where: query,
|
||||
attributes: [
|
||||
'id',
|
||||
'name',
|
||||
],
|
||||
raw: true,
|
||||
});
|
||||
if (!targetUser) {
|
||||
res.status(401);
|
||||
res.json({
|
||||
errors: ['Target user does not exist'],
|
||||
});
|
||||
return;
|
||||
}
|
||||
userId = targetUser.id;
|
||||
userName = targetUser.name;
|
||||
|
||||
const ret = await UserBlock.findOrCreate({
|
||||
where: {
|
||||
uid: user.id,
|
||||
buid: userId,
|
||||
},
|
||||
raw: true,
|
||||
attributes: ['uid'],
|
||||
});
|
||||
if (ret) {
|
||||
res.json({
|
||||
status: 'ok',
|
||||
});
|
||||
} else {
|
||||
res.status(502);
|
||||
res.json({
|
||||
errors: ['Could not block user'],
|
||||
});
|
||||
logger.info(
|
||||
`User ${user.getName()} blocked ${userName}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default block;
|
|
@ -19,6 +19,7 @@ import ranking from './ranking';
|
|||
import history from './history';
|
||||
import chatHistory from './chathistory';
|
||||
import startDm from './startdm';
|
||||
import block from './block';
|
||||
|
||||
|
||||
const router = express.Router();
|
||||
|
@ -83,6 +84,8 @@ router.get('/chathistory', chatHistory);
|
|||
|
||||
router.post('/startdm', startDm);
|
||||
|
||||
router.post('/block', block);
|
||||
|
||||
router.use('/auth', auth(passport));
|
||||
|
||||
export default router;
|
||||
|
|
Loading…
Reference in New Issue
Block a user