Skip to content

Node.js based forum software built for the modern web

fix: chat list search ignores the uid of the chats page being viewed (#15001)

palmoni51fc9d42a3e07
diff --git a/public/src/client/chats/search.js b/public/src/client/chats/search.js
index 40053c0428..bc8c31658f 100644
--- a/public/src/client/chats/search.js
+++ b/public/src/client/chats/search.js
@@ -9,7 +9,7 @@ export function init(searchEl, listEl) {
}
searchEl.addEventListener('keyup', utils.debounce(async function (e) {
- const data = await get(`/chats/search?query=${e.target.value}`);
+ const data = await get('/chats/search', { query: e.target.value, uid: ajaxify.data.uid });
const html = await app.parseAndTranslate('chats', 'rooms', data);
$(listEl).html(html);
}, 300));
diff --git a/src/controllers/write/chats.js b/src/controllers/write/chats.js
index a52183575c..0891412224 100644
--- a/src/controllers/write/chats.js
+++ b/src/controllers/write/chats.js
@@ -33,7 +33,8 @@ Chats.search = async (req, res) => {
return Chats.list(req, res);
}
- const rooms = await messaging.searchRecentChats(req.uid, req.uid, query);
+ const uid = isFinite(req.query.uid) && parseInt(req.query.uid, 10);
+ const rooms = await messaging.searchRecentChats(req.uid, uid || req.uid, query);
helpers.formatApiResponse(200, res, rooms);
};
diff --git a/test/messaging.js b/test/messaging.js
index daf12b3850..e7e1d2aeb1 100644
--- a/test/messaging.js
+++ b/test/messaging.js
@@ -12,6 +12,7 @@ const meta = require('../src/meta');
const User = require('../src/user');
const Groups = require('../src/groups');
const Messaging = require('../src/messaging');
+const plugins = require('../src/plugins');
const api = require('../src/api');
const helpers = require('./helpers');
const request = require('../src/request');
@@ -1025,6 +1026,34 @@ describe('Messaging Library', () => {
assert.equal(body, '/user/herp/chats');
});
+ it('should search the chats of the uid passed in', async () => {
+ const method = async (data) => {
+ data.canGet = true;
+ return data;
+ };
+ plugins.hooks.register('test', { hook: 'filter:messaging.canGetRecentChats', method });
+ try {
+ const query = 'ba';
+ const [ownRooms, fooRooms] = await Promise.all([
+ Messaging.searchRecentChats(mocks.users.herp.uid, mocks.users.herp.uid, query),
+ Messaging.searchRecentChats(mocks.users.foo.uid, mocks.users.foo.uid, query),
+ ]);
+ const { body } = await request.get(`${nconf.get('url')}/api/v3/chats/search?query=${query}&uid=${mocks.users.foo.uid}`, { jar });
+
+ const roomIds = body.response.rooms.map(room => room.roomId);
+ assert.deepStrictEqual(roomIds, fooRooms.rooms.map(room => room.roomId));
+ assert.notDeepStrictEqual(roomIds, ownRooms.rooms.map(room => room.roomId));
+ } finally {
+ plugins.hooks.unregister('test', 'filter:messaging.canGetRecentChats', method);
+ }
+ });
+
+ it('should not search the chats of another user without privileges', async () => {
+ const { response } = await request.get(`${nconf.get('url')}/api/v3/chats/search?query=ba&uid=${mocks.users.foo.uid}`, { jar });
+
+ assert.notStrictEqual(response.statusCode, 200);
+ });
+
it('should return 404 if user is not in room', async () => {
const data = await helpers.loginUser('baz', 'quuxquux');
const { response } = await request.get(`${nconf.get('url')}/api/user/baz/chats/${roomId}`, { jar: data.jar });