From 5318b521e721b6ec34fd64fc9ffe7250e4c5c60f Mon Sep 17 00:00:00 2001 From: HF Date: Fri, 20 Nov 2020 11:49:40 +0100 Subject: [PATCH] add route for blocking user --- src/actions/index.js | 2 +- src/core/passport.js | 5 --- src/data/models/UserBlock.js | 4 +- src/data/models/index.js | 3 ++ src/routes/api/block.js | 87 ++++++++++++++++++++++++++++++++++++ src/routes/api/index.js | 3 ++ 6 files changed, 96 insertions(+), 8 deletions(-) create mode 100644 src/routes/api/block.js diff --git a/src/actions/index.js b/src/actions/index.js index 2202844..67d7928 100644 --- a/src/actions/index.js +++ b/src/actions/index.js @@ -785,7 +785,7 @@ export function startDm(query): PromiseAction { } } } catch { - + dispatch(notify('Couldn\'t start DM')); } }; } diff --git a/src/core/passport.js b/src/core/passport.js index 889c8a8..55444f7 100644 --- a/src/core/passport.js +++ b/src/core/passport.js @@ -41,11 +41,6 @@ const include = [{ 'id', 'name', ], - }, { - model: RegUser, - as: 'blocked', - through: UserBlock, - foreignKey: 'uid', }], }, { model: RegUser, diff --git a/src/data/models/UserBlock.js b/src/data/models/UserBlock.js index 14c9571..fe02bf7 100644 --- a/src/data/models/UserBlock.js +++ b/src/data/models/UserBlock.js @@ -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'], diff --git a/src/data/models/index.js b/src/data/models/index.js index f8d1785..4e7b71d 100644 --- a/src/data/models/index.js +++ b/src/data/models/index.js @@ -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', diff --git a/src/routes/api/block.js b/src/routes/api/block.js new file mode 100644 index 0000000..d84fc2e --- /dev/null +++ b/src/routes/api/block.js @@ -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; diff --git a/src/routes/api/index.js b/src/routes/api/index.js index 6b674ce..6030b6c 100644 --- a/src/routes/api/index.js +++ b/src/routes/api/index.js @@ -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;