db/PadMessageHandler.js: partial conversion to Promises

Converted those functions that API.js still depends on, and others that at this
point are never called via the nodeback mechanism.
This commit is contained in:
Ray Bellis 2019-01-25 18:07:01 +00:00
parent 8f53e4407e
commit 1b6430ae9f

View file

@ -330,7 +330,7 @@ function handleSaveRevisionMessage(client, message){
* @param msg {Object} the message we're sending * @param msg {Object} the message we're sending
* @param sessionID {string} the socketIO session to which we're sending this message * @param sessionID {string} the socketIO session to which we're sending this message
*/ */
exports.handleCustomObjectMessage = thenify(function(msg, sessionID, cb) { exports.handleCustomObjectMessage = async function(msg, sessionID) {
if (msg.data.type === "CUSTOM") { if (msg.data.type === "CUSTOM") {
if (sessionID){ if (sessionID){
// a sessionID is targeted: directly to this sessionID // a sessionID is targeted: directly to this sessionID
@ -340,9 +340,7 @@ exports.handleCustomObjectMessage = thenify(function(msg, sessionID, cb) {
socketio.sockets.in(msg.data.payload.padId).json.send(msg); socketio.sockets.in(msg.data.payload.padId).json.send(msg);
} }
} }
cb(null, {}); }
});
/** /**
* Handles a custom message (sent via HTTP API request) * Handles a custom message (sent via HTTP API request)
@ -809,7 +807,7 @@ function handleUserChanges(data, cb)
}); });
} }
exports.updatePadClients = function(pad, callback) exports.updatePadClients = thenify(function(pad, callback)
{ {
// skip this if no-one is on this pad // skip this if no-one is on this pad
var roomClients = _getRoomClients(pad.id); var roomClients = _getRoomClients(pad.id);
@ -886,7 +884,7 @@ exports.updatePadClients = function(pad, callback)
callback callback
); );
},callback); },callback);
} });
/** /**
* Copied from the Etherpad Source Code. Don't know what this method does excatly... * Copied from the Etherpad Source Code. Don't know what this method does excatly...
@ -1503,7 +1501,7 @@ function handleChangesetRequest(client, message)
* Tries to rebuild the getChangestInfo function of the original Etherpad * Tries to rebuild the getChangestInfo function of the original Etherpad
* https://github.com/ether/pad/blob/master/etherpad/src/etherpad/control/pad/pad_changeset_control.js#L144 * https://github.com/ether/pad/blob/master/etherpad/src/etherpad/control/pad/pad_changeset_control.js#L144
*/ */
function getChangesetInfo(padId, startNum, endNum, granularity, callback) let getChangesetInfo = thenify(function getChangesetInfo(padId, startNum, endNum, granularity, callback)
{ {
var forwardsChangesets = []; var forwardsChangesets = [];
var backwardsChangesets = []; var backwardsChangesets = [];
@ -1643,13 +1641,13 @@ function getChangesetInfo(padId, startNum, endNum, granularity, callback)
start: startNum, start: startNum,
granularity: granularity }); granularity: granularity });
}); });
} });
/** /**
* Tries to rebuild the getPadLines function of the original Etherpad * Tries to rebuild the getPadLines function of the original Etherpad
* https://github.com/ether/pad/blob/master/etherpad/src/etherpad/control/pad/pad_changeset_control.js#L263 * https://github.com/ether/pad/blob/master/etherpad/src/etherpad/control/pad/pad_changeset_control.js#L263
*/ */
function getPadLines(padId, revNum, callback) let getPadLines = thenify(function getPadLines(padId, revNum, callback)
{ {
var atext; var atext;
var result = {}; var result = {};
@ -1693,13 +1691,13 @@ function getPadLines(padId, revNum, callback)
callback(null, result); callback(null, result);
}); });
} });
/** /**
* Tries to rebuild the composePadChangeset function of the original Etherpad * Tries to rebuild the composePadChangeset function of the original Etherpad
* https://github.com/ether/pad/blob/master/etherpad/src/etherpad/control/pad/pad_changeset_control.js#L241 * https://github.com/ether/pad/blob/master/etherpad/src/etherpad/control/pad/pad_changeset_control.js#L241
*/ */
function composePadChangesets(padId, startNum, endNum, callback) let composePadChangesets = thenify(function(padId, startNum, endNum, callback)
{ {
var pad; var pad;
var changesets = {}; var changesets = {};
@ -1772,7 +1770,7 @@ function composePadChangesets(padId, startNum, endNum, callback)
callback(null, changeset); callback(null, changeset);
}); });
} });
function _getRoomClients(padID) { function _getRoomClients(padID) {
var roomClients = []; var roomClients = [];
@ -1790,39 +1788,32 @@ function _getRoomClients(padID) {
/** /**
* Get the number of users in a pad * Get the number of users in a pad
*/ */
exports.padUsersCount = thenify(function(padID, callback) { exports.padUsersCount = function(padID) {
callback(null, { return {
padUsersCount: _getRoomClients(padID).length padUsersCount: _getRoomClients(padID).length
}); }
}); }
/** /**
* Get the list of users in a pad * Get the list of users in a pad
*/ */
exports.padUsers = thenify(function(padID, callback) { exports.padUsers = async function(padID) {
var result = [];
var roomClients = _getRoomClients(padID); let padUsers = [];
let roomClients = _getRoomClients(padID);
async.forEach(roomClients, function(roomClient, callback) { for (let i = 0, n = roomClients.length; i < n; ++i) {
var s = sessioninfos[roomClient.id]; let roomClient = roomClients[i];
let s = sessioninfos[roomClient.id];
if (s) { if (s) {
authorManager.getAuthor(s.author, function(err, author) { let author = await authorManager.getAuthor(s.author);
if (ERR(err, callback)) return; author.id = s.author;
padUsers.push(author);
author.id = s.author;
result.push(author);
callback();
});
} else {
callback();
} }
}, }
function(err) {
if (ERR(err, callback)) return;
callback(null, {padUsers: result}); return { padUsers };
}); }
});
exports.sessioninfos = sessioninfos; exports.sessioninfos = sessioninfos;