Skip to content

Commit

Permalink
chat: Move HTTP API handlers to chat.js
Browse files Browse the repository at this point in the history
  • Loading branch information
rhansen committed Apr 9, 2022
1 parent 140b047 commit 7f21015
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 96 deletions.
72 changes: 70 additions & 2 deletions src/node/chat.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const ChatMessage = require('../static/js/ChatMessage');
const CustomError = require('./utils/customError');
const api = require('./db/API');
const assert = require('assert').strict;
const authorManager = require('./db/AuthorManager');
Expand Down Expand Up @@ -182,9 +183,76 @@ exports.socketio = (hookName, {io}) => {
socketio = io;
};

const getPadSafe = async (padID) => {
if (typeof padID !== 'string') throw new CustomError('padID is not a string', 'apierror');
if (!padManager.isValidPadId(padID)) throw new CustomError('padID is not valid', 'apierror');
if (!await padManager.doesPadExist(padID)) throw new CustomError('pad not found', 'apierror');
return await padManager.getPad(padID);
};

api.registerChatHandlers({
getChatMessages,
sendChatMessageToPadClients,
/**
* appendChatMessage(padID, text, authorID, time), creates a chat message for the pad id,
* time is a timestamp
*
* Example returns:
*
* {code: 0, message:"ok", data: null}
* {code: 1, message:"padID does not exist", data: null}
*/
appendChatMessage: async (padID, text, authorID, time) => {
if (typeof text !== 'string') throw new CustomError('text is not a string', 'apierror');
if (time === undefined || !Number.isInteger(Number.parseInt(time))) time = Date.now();
await sendChatMessageToPadClients(new ChatMessage(text, authorID, time), padID);
},

/**
* getChatHead(padID) returns the chatHead (last number of the last chat-message) of the pad
*
* Example returns:
*
* {code: 0, message:"ok", data: {chatHead: 42}}
* {code: 1, message:"padID does not exist", data: null}
*/
getChatHead: async (padID) => {
const pad = await getPadSafe(padID);
return {chatHead: pad.chatHead};
},

/**
* getChatHistory(padID, start, end), returns a part of or the whole chat-history of this pad
*
* Example returns:
*
* {"code":0,"message":"ok","data":{"messages":[
* {"text":"foo","authorID":"a.foo","time":1359199533759,"userName":"test"},
* {"text":"bar","authorID":"a.foo","time":1359199534622,"userName":"test"}
* ]}}
*
* {code: 1, message:"start is higher or equal to the current chatHead", data: null}
*
* {code: 1, message:"padID does not exist", data: null}
*/
getChatHistory: async (padID, start, end) => {
if (start && end) {
if (start < 0) throw new CustomError('start is below zero', 'apierror');
if (end < 0) throw new CustomError('end is below zero', 'apierror');
if (start > end) throw new CustomError('start is higher than end', 'apierror');
}
const pad = await getPadSafe(padID);
const chatHead = pad.chatHead;
if (!start || !end) {
start = 0;
end = pad.chatHead;
}
if (start > chatHead) {
throw new CustomError('start is higher than the current chatHead', 'apierror');
}
if (end > chatHead) {
throw new CustomError('end is higher than the current chatHead', 'apierror');
}
return {messages: await getChatMessages(pad, start, end)};
},
});

pad.registerLegacyChatMethodHandlers({
Expand Down
95 changes: 1 addition & 94 deletions src/node/db/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
*/

const Changeset = require('../../static/js/Changeset');
const ChatMessage = require('../../static/js/ChatMessage');
const CustomError = require('../utils/customError');
const padManager = require('./PadManager');
const padMessageHandler = require('../handler/PadMessageHandler');
Expand Down Expand Up @@ -289,85 +288,7 @@ exports.setHTML = async (padID, html, authorId = '') => {
* CHAT FUNCTIONS *
**************** */

let chat = null;
exports.registerChatHandlers = (handlers) => chat = handlers;

/**
getChatHistory(padId, start, end), returns a part of or the whole chat-history of this pad
Example returns:
{"code":0,"message":"ok","data":{"messages":[
{"text":"foo","authorID":"a.foo","time":1359199533759,"userName":"test"},
{"text":"bar","authorID":"a.foo","time":1359199534622,"userName":"test"}
]}}
{code: 1, message:"start is higher or equal to the current chatHead", data: null}
{code: 1, message:"padID does not exist", data: null}
*/
exports.getChatHistory = async (padID, start, end) => {
if (start && end) {
if (start < 0) {
throw new CustomError('start is below zero', 'apierror');
}
if (end < 0) {
throw new CustomError('end is below zero', 'apierror');
}
if (start > end) {
throw new CustomError('start is higher than end', 'apierror');
}
}

// get the pad
const pad = await getPadSafe(padID, true);

const chatHead = pad.chatHead;

// fall back to getting the whole chat-history if a parameter is missing
if (!start || !end) {
start = 0;
end = pad.chatHead;
}

if (start > chatHead) {
throw new CustomError('start is higher than the current chatHead', 'apierror');
}
if (end > chatHead) {
throw new CustomError('end is higher than the current chatHead', 'apierror');
}

// the the whole message-log and return it to the client
const messages = await chat.getChatMessages(pad, start, end);

return {messages};
};

/**
appendChatMessage(padID, text, authorID, time), creates a chat message for the pad id,
time is a timestamp
Example returns:
{code: 0, message:"ok", data: null}
{code: 1, message:"padID does not exist", data: null}
*/
exports.appendChatMessage = async (padID, text, authorID, time) => {
// text is required
if (typeof text !== 'string') {
throw new CustomError('text is not a string', 'apierror');
}

// if time is not an integer value set time to current timestamp
if (time === undefined || !isInt(time)) {
time = Date.now();
}

// @TODO - missing getPadSafe() call ?

// save chat message to database and send message to all connected clients
await chat.sendChatMessageToPadClients(new ChatMessage(text, authorID, time), padID);
};
exports.registerChatHandlers = (handlers) => Object.assign(exports, handlers);

/* ***************
* PAD FUNCTIONS *
Expand Down Expand Up @@ -735,20 +656,6 @@ Example returns:
exports.checkToken = async () => {
};

/**
getChatHead(padID) returns the chatHead (last number of the last chat-message) of the pad
Example returns:
{code: 0, message:"ok", data: {chatHead: 42}}
{code: 1, message:"padID does not exist", data: null}
*/
exports.getChatHead = async (padID) => {
// get the pad
const pad = await getPadSafe(padID, true);
return {chatHead: pad.chatHead};
};

/**
createDiffHTML(padID, startRev, endRev) returns an object of diffs from 2 points in a pad
Expand Down

0 comments on commit 7f21015

Please sign in to comment.