From cb2b93b1339d27a21e3937ab58ea1614d7118b04 Mon Sep 17 00:00:00 2001 From: mluto Date: Sat, 26 Jan 2013 14:35:26 +0100 Subject: [PATCH] Added getChatHistory and getChatHead (api+code+doc) --- doc/api/http_api.md | 28 +++++++++++ src/node/db/API.js | 88 ++++++++++++++++++++++++++++++++++ src/node/handler/APIHandler.js | 39 +++++++++++++++ 3 files changed, 155 insertions(+) diff --git a/doc/api/http_api.md b/doc/api/http_api.md index 0d2cc375..a7fc0bed 100644 --- a/doc/api/http_api.md +++ b/doc/api/http_api.md @@ -292,6 +292,34 @@ returns the text of a pad formatted as HTML * `{code: 0, message:"ok", data: {html:"Welcome Text
More Text"}}` * `{code: 1, message:"padID does not exist", data: null}` +### Chat +#### getChatHistory(padID, [start, end]) + * API >= 1.2.7 + +returns + +* a part of the chat history, when `start` and `end` are given +* the whole chat histroy, when no extra parameters are given + + +*Example returns:* + +* `{"code":0,"message":"ok","data":{"messages":[{"text":"foo","userId":"a.foo","time":1359199533759,"userName":"test"},{"text":"bar","userId":"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}` + +#### getChatHead(padID) + * API >= 1.2.7 + +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}` + + ### Pad Group pads are normal pads, but with the name schema GROUPID$PADNAME. A security manager controls access of them and its forbidden for normal pads to include a $ in the name. diff --git a/src/node/db/API.js b/src/node/db/API.js index 9cad415d..cee63a9e 100644 --- a/src/node/db/API.js +++ b/src/node/db/API.js @@ -277,6 +277,77 @@ exports.setHTML = function(padID, html, callback) }); } +/******************/ +/**CHAT FUNCTIONS */ +/******************/ + +/** +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","userId":"a.foo","time":1359199533759,"userName":"test"}, + {"text":"bar","userId":"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 = function(padID, start, end, callback) +{ + if(start && end) + { + if(start < 0) + { + callback(new customError("start is below zero","apierror")); + return; + } + if(end < 0) + { + callback(new customError("end is below zero","apierror")); + return; + } + if(start > end) + { + callback(new customError("start is higher than end","apierror")); + return; + } + } + + //get the pad + getPadSafe(padID, true, function(err, pad) + { + if(ERR(err, callback)) return; + var chatHead = pad.chatHead; + + // fall back to getting the whole chat-history if a parameter is missing + if(!start || !end) + { + start = 0; + end = pad.chatHead - 1; + } + + if(start >= chatHead) + { + callback(new customError("start is higher or equal to the current chatHead","apierror")); + return; + } + if(end >= chatHead) + { + callback(new customError("end is higher or equal to the current chatHead","apierror")); + return; + } + + // the the whole message-log and return it to the client + pad.getChatMessages(start, end, + function(err, msgs) + { + if(ERR(err, callback)) return; + callback(null, {messages: msgs}); + }); + }); +} + /*****************/ /**PAD FUNCTIONS */ /*****************/ @@ -567,6 +638,23 @@ exports.checkToken = function(callback) callback(); } +/** +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 = function(padID, callback) +{ + //get the pad + getPadSafe(padID, true, function(err, pad) + { + if(ERR(err, callback)) return; + callback(null, {chatHead: pad.chatHead}); + }); +} /******************************/ /** INTERNAL HELPER FUNCTIONS */ diff --git a/src/node/handler/APIHandler.js b/src/node/handler/APIHandler.js index ae93e933..6085edbf 100644 --- a/src/node/handler/APIHandler.js +++ b/src/node/handler/APIHandler.js @@ -174,6 +174,45 @@ var version = , "listAllGroups" : [] , "checkToken" : [] } +, "1.2.7": + { "createGroup" : [] + , "createGroupIfNotExistsFor" : ["groupMapper"] + , "deleteGroup" : ["groupID"] + , "listPads" : ["groupID"] + , "listAllPads" : [] + , "createPad" : ["padID", "text"] + , "createGroupPad" : ["groupID", "padName", "text"] + , "createAuthor" : ["name"] + , "createAuthorIfNotExistsFor": ["authorMapper" , "name"] + , "listPadsOfAuthor" : ["authorID"] + , "createSession" : ["groupID", "authorID", "validUntil"] + , "deleteSession" : ["sessionID"] + , "getSessionInfo" : ["sessionID"] + , "listSessionsOfGroup" : ["groupID"] + , "listSessionsOfAuthor" : ["authorID"] + , "getText" : ["padID", "rev"] + , "setText" : ["padID", "text"] + , "getHTML" : ["padID", "rev"] + , "setHTML" : ["padID", "html"] + , "getRevisionsCount" : ["padID"] + , "getLastEdited" : ["padID"] + , "deletePad" : ["padID"] + , "getReadOnlyID" : ["padID"] + , "setPublicStatus" : ["padID", "publicStatus"] + , "getPublicStatus" : ["padID"] + , "setPassword" : ["padID", "password"] + , "isPasswordProtected" : ["padID"] + , "listAuthorsOfPad" : ["padID"] + , "padUsersCount" : ["padID"] + , "getAuthorName" : ["authorID"] + , "padUsers" : ["padID"] + , "sendClientsMessage" : ["padID", "msg"] + , "listAllGroups" : [] + , "checkToken" : [] + , "getChatHistory" : ["padID"] + , "getChatHistory" : ["padID", "start", "end"] + , "getChatHead" : ["padID"] + } }; /**