mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-08-31 04:57:59 +02:00
feat: POST /api/v3/chats/:roomId
This commit is contained in:
@@ -1,17 +1,17 @@
|
||||
'use strict';
|
||||
|
||||
const user = require('../user');
|
||||
const meta = require('../meta');
|
||||
const privileges = require('../privileges');
|
||||
const messaging = require('../messaging');
|
||||
const plugins = require('../plugins');
|
||||
|
||||
|
||||
const websockets = require('../socket.io');
|
||||
const socketHelpers = require('../socket.io/helpers');
|
||||
// const websockets = require('../socket.io');
|
||||
// const socketHelpers = require('../socket.io/helpers');
|
||||
|
||||
const chatsAPI = module.exports;
|
||||
|
||||
function rateLimitExceeded(caller) {
|
||||
const session = caller.request ? caller.request.session : caller.session; // socket vs req
|
||||
const session = caller.request ? caller.request.session : caller.session; // socket vs req
|
||||
const now = Date.now();
|
||||
session.lastChatMessageTime = session.lastChatMessageTime || 0;
|
||||
if (now - session.lastChatMessageTime < meta.config.chatMessageDelay) {
|
||||
@@ -35,3 +35,27 @@ chatsAPI.create = async function (caller, data) {
|
||||
|
||||
return await messaging.getRoomData(roomId);
|
||||
};
|
||||
|
||||
chatsAPI.post = async (caller, data) => {
|
||||
if (rateLimitExceeded(caller)) {
|
||||
throw new Error('[[error:too-many-messages]]');
|
||||
}
|
||||
|
||||
({ data } = await plugins.hooks.fire('filter:messaging.send', {
|
||||
data,
|
||||
uid: caller.uid,
|
||||
}));
|
||||
|
||||
await messaging.canMessageRoom(caller.uid, data.roomId);
|
||||
const message = await messaging.sendMessage({
|
||||
uid: caller.uid,
|
||||
roomId: data.roomId,
|
||||
content: data.message,
|
||||
timestamp: Date.now(),
|
||||
ip: caller.ip,
|
||||
});
|
||||
messaging.notifyUsersInRoom(caller.uid, data.roomId, message);
|
||||
user.updateOnlineUsers(caller.uid);
|
||||
|
||||
return message;
|
||||
};
|
||||
|
||||
@@ -36,7 +36,12 @@ Chats.get = async (req, res) => {
|
||||
};
|
||||
|
||||
Chats.post = async (req, res) => {
|
||||
// ...
|
||||
const messageObj = await api.chats.post(req, {
|
||||
...req.body,
|
||||
roomId: req.params.roomId,
|
||||
});
|
||||
|
||||
helpers.formatApiResponse(200, res, messageObj);
|
||||
};
|
||||
|
||||
Chats.users = async (req, res) => {
|
||||
|
||||
@@ -15,7 +15,7 @@ module.exports = function () {
|
||||
|
||||
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, 'post', '/:roomId', [...middlewares, middleware.assert.room], controllers.write.chats.post);
|
||||
setupApiRoute(router, 'post', '/:roomId', [...middlewares, middleware.assert.room, middleware.checkRequired.bind(null, ['message'])], controllers.write.chats.post);
|
||||
// // no route for room deletion, reserved just in case...
|
||||
|
||||
// setupApiRoute(router, 'get', '/:roomId/users', [...middlewares, middleware.assert.room], controllers.write.chats.users);
|
||||
|
||||
@@ -59,46 +59,20 @@ SocketModules.chats.newRoom = async function (socket, data) {
|
||||
};
|
||||
|
||||
SocketModules.chats.send = async function (socket, data) {
|
||||
sockets.warnDeprecated(socket, 'POST /api/v3/chats/:roomId');
|
||||
|
||||
if (!data || !data.roomId || !socket.uid) {
|
||||
throw new Error('[[error:invalid-data]]');
|
||||
}
|
||||
|
||||
if (rateLimitExceeded(socket)) {
|
||||
throw new Error('[[error:too-many-messages]]');
|
||||
}
|
||||
const canChat = await privileges.global.can('chat', socket.uid);
|
||||
if (!canChat) {
|
||||
throw new Error('[[error:no-privileges]]');
|
||||
}
|
||||
const results = await plugins.hooks.fire('filter:messaging.send', {
|
||||
data: data,
|
||||
uid: socket.uid,
|
||||
});
|
||||
data = results.data;
|
||||
|
||||
await Messaging.canMessageRoom(socket.uid, data.roomId);
|
||||
const message = await Messaging.sendMessage({
|
||||
uid: socket.uid,
|
||||
roomId: data.roomId,
|
||||
content: data.message,
|
||||
timestamp: Date.now(),
|
||||
ip: socket.ip,
|
||||
});
|
||||
Messaging.notifyUsersInRoom(socket.uid, data.roomId, message);
|
||||
user.updateOnlineUsers(socket.uid);
|
||||
return message;
|
||||
return api.chats.post(socket, data);
|
||||
};
|
||||
|
||||
function rateLimitExceeded(socket) {
|
||||
const now = Date.now();
|
||||
socket.lastChatMessageTime = socket.lastChatMessageTime || 0;
|
||||
if (now - socket.lastChatMessageTime < meta.config.chatMessageDelay) {
|
||||
return true;
|
||||
}
|
||||
socket.lastChatMessageTime = now;
|
||||
return false;
|
||||
}
|
||||
|
||||
SocketModules.chats.loadRoom = async function (socket, data) {
|
||||
sockets.warnDeprecated(socket, 'GET /api/v3/chats/:roomId');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user