refactor(socket.io): deprecate SocketModules.chats.canMessage and .markAllRead with no alternative. deprecate .getRecentChats in favour of api.chats.list

This commit is contained in:
Julian Lam
2023-11-13 15:35:46 -05:00
parent eebea4df2e
commit a4133500fe
8 changed files with 65 additions and 37 deletions

View File

@@ -1,6 +1,7 @@
'use strict';
const validator = require('validator');
const winston = require('winston');
const db = require('../database');
const user = require('../user');
@@ -32,12 +33,14 @@ async function rateLimitExceeded(caller, field) {
return false;
}
chatsAPI.list = async (caller, { page, perPage }) => {
const start = Math.max(0, page - 1) * perPage;
const stop = start + perPage;
const { rooms } = await messaging.getRecentChats(caller.uid, caller.uid, start, stop);
chatsAPI.list = async (caller, { uid, start, stop, page, perPage }) => {
if (!start && !stop && page) {
winston.warn('[api/chats] Sending `page` and `perPage` to .list() is deprecated in favour of `start` and `stop`. The deprecated parameters will be removed in v4.');
start = Math.max(0, page - 1) * perPage;
stop = start + perPage - 1;
}
return { rooms };
return await messaging.getRecentChats(caller.uid, uid || caller.uid, start, stop);
};
chatsAPI.create = async function (caller, data) {

View File

@@ -6,11 +6,22 @@ const helpers = require('../helpers');
const Chats = module.exports;
Chats.list = async (req, res) => {
const page = (isFinite(req.query.page) && parseInt(req.query.page, 10)) || 1;
const perPage = (isFinite(req.query.perPage) && parseInt(req.query.perPage, 10)) || 20;
const { rooms } = await api.chats.list(req, { page, perPage });
let stop;
let { page, perPage, start, uid } = req.query;
([page, perPage, start, uid] = [page, perPage, start, uid].map(value => isFinite(value) && parseInt(value, 10)));
page = page || 1;
perPage = perPage || 20;
helpers.formatApiResponse(200, res, { rooms });
// start supercedes page
if (start) {
stop = start + perPage - 1;
} else {
start = Math.max(0, page - 1) * perPage;
stop = start + perPage - 1;
}
const { rooms, nextStart } = await api.chats.list(req, { start, stop, uid });
helpers.formatApiResponse(200, res, { rooms, nextStart });
};
Chats.create = async (req, res) => {

View File

@@ -173,7 +173,7 @@ Messaging.getPublicRooms = async (callerUid, uid) => {
Messaging.getRecentChats = async (callerUid, uid, start, stop) => {
const ok = await canGet('filter:messaging.canGetRecentChats', callerUid, uid);
if (!ok) {
return null;
throw new Error('[[error:no-privileges]]');
}
const roomIds = await db.getSortedSetRevRange(`uid:${uid}:chat:rooms`, start, stop);

View File

@@ -45,22 +45,29 @@ SocketModules.chats.isDnD = async function (socket, uid) {
};
SocketModules.chats.canMessage = async function (socket, roomId) {
sockets.warnDeprecated(socket);
await Messaging.canMessageRoom(socket.uid, roomId);
};
SocketModules.chats.markAllRead = async function (socket) {
// no v3 method ?
sockets.warnDeprecated(socket);
await Messaging.markAllRead(socket.uid);
Messaging.pushUnreadCount(socket.uid);
};
SocketModules.chats.getRecentChats = async function (socket, data) {
sockets.warnDeprecated(socket, 'GET /api/v3/chats');
if (!data || !utils.isNumber(data.after) || !utils.isNumber(data.uid)) {
throw new Error('[[error:invalid-data]]');
}
const start = parseInt(data.after, 10);
const stop = start + 9;
return await Messaging.getRecentChats(socket.uid, data.uid, start, stop);
const { uid } = data;
return api.chats.list(socket, { uid, start, stop });
};
SocketModules.chats.hasPrivateChat = async function (socket, uid) {