mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-01 19:46:01 +01:00
refactor(socket.io): deprecate SocketModules.sortPublicRooms in favour of api.chats.sortPublicRooms
This commit is contained in:
@@ -194,6 +194,8 @@ paths:
|
|||||||
$ref: 'write/chats.yaml'
|
$ref: 'write/chats.yaml'
|
||||||
/chats/unread:
|
/chats/unread:
|
||||||
$ref: 'write/chats/unread.yaml'
|
$ref: 'write/chats/unread.yaml'
|
||||||
|
/chats/sort:
|
||||||
|
$ref: 'write/chats/sort.yaml'
|
||||||
/chats/{roomId}:
|
/chats/{roomId}:
|
||||||
$ref: 'write/chats/roomId.yaml'
|
$ref: 'write/chats/roomId.yaml'
|
||||||
/chats/{roomId}/state:
|
/chats/{roomId}/state:
|
||||||
|
|||||||
33
public/openapi/write/chats/sort.yaml
Normal file
33
public/openapi/write/chats/sort.yaml
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
put:
|
||||||
|
tags:
|
||||||
|
- chats
|
||||||
|
summary: sort public chat rooms
|
||||||
|
description: This operation sorts the publicly available chat rooms. This is a privileged function; only superadmins can call it.
|
||||||
|
requestBody:
|
||||||
|
required: true
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
roomIds:
|
||||||
|
type: array
|
||||||
|
description: A list of room ids.
|
||||||
|
example: [1]
|
||||||
|
scores:
|
||||||
|
type: array
|
||||||
|
description: A list of sort orders associated with the passed-in `roomIds`
|
||||||
|
example: [0]
|
||||||
|
responses:
|
||||||
|
'200':
|
||||||
|
description: Public chat rooms successfully re-ordered.
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
status:
|
||||||
|
$ref: ../../components/schemas/Status.yaml#/Status
|
||||||
|
response:
|
||||||
|
type: object
|
||||||
|
properties: {}
|
||||||
@@ -141,7 +141,7 @@ define('forum/chats', [
|
|||||||
data.roomIds.push($(el).attr('data-roomid'));
|
data.roomIds.push($(el).attr('data-roomid'));
|
||||||
data.scores.push(idx);
|
data.scores.push(idx);
|
||||||
});
|
});
|
||||||
await socket.emit('modules.chats.sortPublicRooms', data);
|
await api.put('/chats/sort', data);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -84,6 +84,22 @@ chatsAPI.getUnread = async (caller) => {
|
|||||||
return { count };
|
return { count };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
chatsAPI.sortPublicRooms = async (caller, { roomIds, scores }) => {
|
||||||
|
[roomIds, scores].forEach((arr) => {
|
||||||
|
if (!Array.isArray(arr) || !arr.every(value => isFinite(value))) {
|
||||||
|
throw new Error('[[error:invalid-data]]');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const isAdmin = await user.isAdministrator(caller.uid);
|
||||||
|
if (!isAdmin) {
|
||||||
|
throw new Error('[[error:no-privileges]]');
|
||||||
|
}
|
||||||
|
|
||||||
|
await db.sortedSetAdd(`chat:rooms:public:order`, scores, roomIds);
|
||||||
|
require('../cache').del(`chat:rooms:public:order:all`);
|
||||||
|
};
|
||||||
|
|
||||||
chatsAPI.get = async (caller, { uid, roomId }) => await messaging.loadRoom(caller.uid, { uid, roomId });
|
chatsAPI.get = async (caller, { uid, roomId }) => await messaging.loadRoom(caller.uid, { uid, roomId });
|
||||||
|
|
||||||
chatsAPI.post = async (caller, data) => {
|
chatsAPI.post = async (caller, data) => {
|
||||||
|
|||||||
@@ -68,7 +68,6 @@ Categories.setWatchState = async (req, res) => {
|
|||||||
} else if (Object.keys(categories.watchStates).includes(state)) {
|
} else if (Object.keys(categories.watchStates).includes(state)) {
|
||||||
state = categories.watchStates[state]; // convert to integer for backend processing
|
state = categories.watchStates[state]; // convert to integer for backend processing
|
||||||
} else {
|
} else {
|
||||||
console.log('throwing', cid, uid, state);
|
|
||||||
throw new Error('[[error:invalid-data]]');
|
throw new Error('[[error:invalid-data]]');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -32,6 +32,13 @@ Chats.create = async (req, res) => {
|
|||||||
// currently only returns unread count, but open-ended for future additions if warranted.
|
// currently only returns unread count, but open-ended for future additions if warranted.
|
||||||
Chats.getUnread = async (req, res) => helpers.formatApiResponse(200, res, await api.chats.getUnread(req));
|
Chats.getUnread = async (req, res) => helpers.formatApiResponse(200, res, await api.chats.getUnread(req));
|
||||||
|
|
||||||
|
Chats.sortPublicRooms = async (req, res) => {
|
||||||
|
const { roomIds, scores } = req.body;
|
||||||
|
await api.chats.sortPublicRooms(req, { roomIds, scores });
|
||||||
|
|
||||||
|
helpers.formatApiResponse(200, res);
|
||||||
|
};
|
||||||
|
|
||||||
Chats.exists = async (req, res) => {
|
Chats.exists = async (req, res) => {
|
||||||
// yes, this is fine. Room existence is checked via middleware :)
|
// yes, this is fine. Room existence is checked via middleware :)
|
||||||
helpers.formatApiResponse(200, res);
|
helpers.formatApiResponse(200, res);
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ module.exports = function () {
|
|||||||
setupApiRoute(router, 'post', '/', [...middlewares, middleware.checkRequired.bind(null, ['uids'])], controllers.write.chats.create);
|
setupApiRoute(router, 'post', '/', [...middlewares, middleware.checkRequired.bind(null, ['uids'])], controllers.write.chats.create);
|
||||||
|
|
||||||
setupApiRoute(router, 'get', '/unread', [...middlewares], controllers.write.chats.getUnread);
|
setupApiRoute(router, 'get', '/unread', [...middlewares], controllers.write.chats.getUnread);
|
||||||
|
setupApiRoute(router, 'put', '/sort', [...middlewares, middleware.checkRequired.bind(null, ['roomIds', 'scores'])], controllers.write.chats.sortPublicRooms);
|
||||||
|
|
||||||
setupApiRoute(router, 'head', '/:roomId', [...middlewares, middleware.assert.room], controllers.write.chats.exists);
|
setupApiRoute(router, 'head', '/:roomId', [...middlewares, middleware.assert.room], controllers.write.chats.exists);
|
||||||
setupApiRoute(router, 'get', '/:roomId', [...middlewares, middleware.assert.room], controllers.write.chats.get);
|
setupApiRoute(router, 'get', '/:roomId', [...middlewares, middleware.assert.room], controllers.write.chats.get);
|
||||||
|
|||||||
@@ -147,15 +147,13 @@ async function joinLeave(socket, roomIds, method, prefix = 'chat_room') {
|
|||||||
}
|
}
|
||||||
|
|
||||||
SocketModules.chats.sortPublicRooms = async function (socket, data) {
|
SocketModules.chats.sortPublicRooms = async function (socket, data) {
|
||||||
if (!data || !Array.isArray(data.scores) || !Array.isArray(data.roomIds)) {
|
sockets.warnDeprecated(socket, 'PUT /api/v3/chats/sort');
|
||||||
|
|
||||||
|
if (!data) {
|
||||||
throw new Error('[[error:invalid-data]]');
|
throw new Error('[[error:invalid-data]]');
|
||||||
}
|
}
|
||||||
const isAdmin = await user.isAdministrator(socket.uid);
|
|
||||||
if (!isAdmin) {
|
await api.chats.sortPublicRooms(socket, data);
|
||||||
throw new Error('[[error:no-privileges]]');
|
|
||||||
}
|
|
||||||
await db.sortedSetAdd(`chat:rooms:public:order`, data.scores, data.roomIds);
|
|
||||||
require('../cache').del(`chat:rooms:public:order:all`);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketModules.chats.searchMembers = async function (socket, data) {
|
SocketModules.chats.searchMembers = async function (socket, data) {
|
||||||
|
|||||||
Reference in New Issue
Block a user