feat: `POST /api/v3/chats/:roomId`

isekai-main
Julian Lam 3 years ago
parent 09cf9c7770
commit eeffb9d978

@ -94,3 +94,53 @@ get:
type: boolean type: boolean
isAdminOrGlobalMod: isAdminOrGlobalMod:
type: boolean type: boolean
post:
tags:
- chats
summary: send a chat message
description: This operation sends a chat message to a chat room
parameters:
- in: path
name: roomId
schema:
type: number
required: true
description: a valid chat room id
example: 1
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
message:
type: string
example: This is a test message
required:
- message
responses:
'200':
description: message successfully sent
content:
application/json:
schema:
type: object
properties:
status:
$ref: ../../components/schemas/Status.yaml#/Status
response:
allOf:
- $ref: ../../components/schemas/Chats.yaml#/MessageObject
- type: object
properties:
self:
type: number
description: Whether or not the message was sent by the calling user (which if you're using this route, will always be 1)
newSet:
type: boolean
description: Whether the message is considered part of a new "set" of messages. It is used in the frontend UI for explicitly denoting that a time gap existed between messages.
cleanedContent:
type: string
mid:
type: number

@ -2,8 +2,9 @@
define('forum/chats/messages', [ define('forum/chats/messages', [
'components', 'translator', 'benchpress', 'hooks', 'bootbox', 'alerts', 'messages', 'components', 'translator', 'benchpress', 'hooks',
], function (components, translator, Benchpress, hooks, bootbox, alerts, messagesModule) { 'bootbox', 'alerts', 'messages', 'api',
], function (components, translator, Benchpress, hooks, bootbox, alerts, messagesModule, api) {
const messages = {}; const messages = {};
messages.sendMessage = function (roomId, inputEl) { messages.sendMessage = function (roomId, inputEl) {
@ -24,25 +25,22 @@ define('forum/chats/messages', [
}); });
if (!mid) { if (!mid) {
socket.emit('modules.chats.send', { api.post(`/chats/${roomId}`, {
roomId: roomId,
message: msg, message: msg,
}, function (err) { }).catch((err) => {
if (err) { inputEl.val(msg);
inputEl.val(msg); messages.updateRemainingLength(inputEl.parent());
messages.updateRemainingLength(inputEl.parent()); if (err.message === '[[error:email-not-confirmed-chat]]') {
if (err.message === '[[error:email-not-confirmed-chat]]') { return messagesModule.showEmailConfirmWarning(err.message);
return messagesModule.showEmailConfirmWarning(err.message);
}
return alerts.alert({
alert_id: 'chat_spam_error',
title: '[[global:alert.error]]',
message: err.message,
type: 'danger',
timeout: 10000,
});
} }
return alerts.alert({
alert_id: 'chat_spam_error',
title: '[[global:alert.error]]',
message: err.message,
type: 'danger',
timeout: 10000,
});
}); });
} else { } else {
socket.emit('modules.chats.edit', { socket.emit('modules.chats.edit', {

@ -1,8 +1,8 @@
'use strict'; 'use strict';
define('chat', [ define('chat', [
'components', 'taskbar', 'translator', 'hooks', 'bootbox', 'alerts', 'components', 'taskbar', 'translator', 'hooks', 'bootbox', 'alerts', 'api',
], function (components, taskbar, translator, hooks, bootbox, alerts) { ], function (components, taskbar, translator, hooks, bootbox, alerts, api) {
const module = {}; const module = {};
let newMessage = false; let newMessage = false;
@ -20,27 +20,24 @@ define('chat', [
if (module.modalExists(roomId)) { if (module.modalExists(roomId)) {
loadAndCenter(module.getModal(roomId)); loadAndCenter(module.getModal(roomId));
} else { } else {
socket.emit('modules.chats.loadRoom', { roomId: roomId, uid: uid || app.user.uid }, function (err, roomData) { api.get(`/chats/${roomId}`, {
if (err) { uid: uid || app.user.uid,
return alerts.error(err); }).then((roomData) => {
}
roomData.users = roomData.users.filter(function (user) { roomData.users = roomData.users.filter(function (user) {
return user && parseInt(user.uid, 10) !== parseInt(app.user.uid, 10); return user && parseInt(user.uid, 10) !== parseInt(app.user.uid, 10);
}); });
roomData.uid = uid || app.user.uid; roomData.uid = uid || app.user.uid;
roomData.isSelf = true; roomData.isSelf = true;
module.createModal(roomData, loadAndCenter); module.createModal(roomData, loadAndCenter);
}); }).catch(alerts.error);
} }
}; };
module.newChat = function (touid, callback) { module.newChat = function (touid, callback) {
function createChat() { function createChat() {
socket.emit('modules.chats.newRoom', { touid: touid }, function (err, roomId) { api.post(`/chats`, {
if (err) { uids: [touid],
return alerts.error(err); }).then(({ roomId }) => {
}
if (!ajaxify.data.template.chats) { if (!ajaxify.data.template.chats) {
module.openChat(roomId); module.openChat(roomId);
} else { } else {
@ -48,7 +45,7 @@ define('chat', [
} }
callback(null, roomId); callback(null, roomId);
}); }).catch(alerts.error);
} }
callback = callback || function () { }; callback = callback || function () { };
@ -130,13 +127,7 @@ define('chat', [
if (module.modalExists(data.roomId)) { if (module.modalExists(data.roomId)) {
addMessageToModal(data); addMessageToModal(data);
} else if (!ajaxify.data.template.chats) { } else if (!ajaxify.data.template.chats) {
socket.emit('modules.chats.loadRoom', { api.get(`/chats/${data.roomId}`, {}).then((roomData) => {
roomId: data.roomId,
}, function (err, roomData) {
if (err) {
return alerts.error(err);
}
roomData.users = roomData.users.filter(function (user) { roomData.users = roomData.users.filter(function (user) {
return user && parseInt(user.uid, 10) !== parseInt(app.user.uid, 10); return user && parseInt(user.uid, 10) !== parseInt(app.user.uid, 10);
}); });
@ -144,7 +135,7 @@ define('chat', [
roomData.uid = app.user.uid; roomData.uid = app.user.uid;
roomData.isSelf = isSelf; roomData.isSelf = isSelf;
module.createModal(roomData); module.createModal(roomData);
}); }).catch(alerts.error);
} }
}; };

@ -1,17 +1,17 @@
'use strict'; 'use strict';
const user = require('../user');
const meta = require('../meta'); const meta = require('../meta');
const privileges = require('../privileges');
const messaging = require('../messaging'); const messaging = require('../messaging');
const plugins = require('../plugins');
// const websockets = require('../socket.io');
const websockets = require('../socket.io'); // const socketHelpers = require('../socket.io/helpers');
const socketHelpers = require('../socket.io/helpers');
const chatsAPI = module.exports; const chatsAPI = module.exports;
function rateLimitExceeded(caller) { 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(); const now = Date.now();
session.lastChatMessageTime = session.lastChatMessageTime || 0; session.lastChatMessageTime = session.lastChatMessageTime || 0;
if (now - session.lastChatMessageTime < meta.config.chatMessageDelay) { if (now - session.lastChatMessageTime < meta.config.chatMessageDelay) {
@ -35,3 +35,27 @@ chatsAPI.create = async function (caller, data) {
return await messaging.getRoomData(roomId); 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) => { 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) => { 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, '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);
// 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... // // no route for room deletion, reserved just in case...
// setupApiRoute(router, 'get', '/:roomId/users', [...middlewares, middleware.assert.room], controllers.write.chats.users); // 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) { SocketModules.chats.send = async function (socket, data) {
sockets.warnDeprecated(socket, 'POST /api/v3/chats/:roomId');
if (!data || !data.roomId || !socket.uid) { if (!data || !data.roomId || !socket.uid) {
throw new Error('[[error:invalid-data]]'); 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); const canChat = await privileges.global.can('chat', socket.uid);
if (!canChat) { if (!canChat) {
throw new Error('[[error:no-privileges]]'); 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); return api.chats.post(socket, data);
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;
}; };
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) { SocketModules.chats.loadRoom = async function (socket, data) {
sockets.warnDeprecated(socket, 'GET /api/v3/chats/:roomId'); sockets.warnDeprecated(socket, 'GET /api/v3/chats/:roomId');

@ -135,6 +135,7 @@ describe('API', async () => {
}); });
meta.config.allowTopicsThumbnail = 1; meta.config.allowTopicsThumbnail = 1;
meta.config.termsOfUse = 'I, for one, welcome our new test-driven overlords'; meta.config.termsOfUse = 'I, for one, welcome our new test-driven overlords';
meta.config.chatMessageDelay = 0;
// Create a category // Create a category
const testCategory = await categories.create({ name: 'test' }); const testCategory = await categories.create({ name: 'test' });

Loading…
Cancel
Save