From 288e81c486467f97e77829c0a4a1224d25f4c608 Mon Sep 17 00:00:00 2001 From: zeners Date: Sun, 14 Feb 2016 19:44:00 +0100 Subject: [PATCH] webui: full chat display --- libresapi/src/webui-src/app/chat.js | 34 +++++++++-- libresapi/src/webui-src/app/retroshare.js | 71 +++++++++++++++++------ 2 files changed, 81 insertions(+), 24 deletions(-) diff --git a/libresapi/src/webui-src/app/chat.js b/libresapi/src/webui-src/app/chat.js index d8bfdfdb8..59f39afbe 100644 --- a/libresapi/src/webui-src/app/chat.js +++ b/libresapi/src/webui-src/app/chat.js @@ -46,15 +46,37 @@ function getLobbyDetails(lobbyid){ } function lobby(lobbyid){ + var msgs; var lobby = getLobbyDetails(lobbyid); - if (lobby == null) { - return m("div","waiting ..."); - } - var msgs = rs("chat/messages/" + lobbyid); var info = rs("chat/info/" + lobbyid); - if (msgs === undefined || info === undefined) { + if (lobby == null || info === undefined) { return m("div","waiting ..."); } + + var mem = rs.memory("chat/info/" + lobbyid); + if (mem.msg === undefined) { + mem.msg = []; + }; + + var reqData = {}; + if (mem.lastKnownMsg != undefined) { + reqData.begin_after = mem.lastKnownMsg; + } + + rs.request("chat/messages/" + lobbyid, reqData, function (data) { + mem.msg = mem.msg.concat(data); + if (mem.msg.length > 0) { + mem.lastKnownMsg = mem.msg[mem.msg.length -1].id; + } + if (data.length > 0 ) { + rs.request("chat/mark_chat_as_read/" + lobbyid,{}, null, + {allow: "ok|not_set"}); + } + }, { + onmismatch: function (){}, + log:function(){} //no logging (pulling) + }); + var intro = [ m("h2",lobby.name), m("p",lobby.topic), @@ -81,7 +103,7 @@ function lobby(lobbyid){ } return [ intro, - msgs.map(function(item){ + mem.msg.map(function(item){ var d = new Date(new Number(item.send_time)*1000); return msg( item.author_name, diff --git a/libresapi/src/webui-src/app/retroshare.js b/libresapi/src/webui-src/app/retroshare.js index 1f4b3674d..95c4a61a8 100644 --- a/libresapi/src/webui-src/app/retroshare.js +++ b/libresapi/src/webui-src/app/retroshare.js @@ -179,25 +179,21 @@ function requestFail(path, response, value) { function rs(path, args, callback, options){ if(cache[path] === undefined){ - if (options === undefined){ - options = {}; - } + options=optionsPrep(options,path); var req = { data: args, statetoken: undefined, requested: false, - allow: options.allow === undefined ? "ok" : options.allow, + allow: options.allow, then: function(response){ - console.log(path + ": response: " + response.returncode); + options.log(path + ": response: " + response.returncode); if (!this.allow.match(response.returncode)) { - requestFail(path, response, null); + options.onmismatch(response); } else if (callback != undefined && callback != null) { callback(response.data, response.statetoken); } }, - errorCallback: function(value){ - requestFail(path, null, value); - } + errorCallback: options.onfail }; cache[path] = req; schedule_request_missing(); @@ -209,9 +205,7 @@ module.exports = rs; // single request for action rs.request=function(path, args, callback, options){ - if (options === undefined) { - options = {}; - } + options = optionsPrep(options, path); var req = m.request({ method: options.method === undefined ? "POST" : options.method, url: api_url + path, @@ -219,18 +213,45 @@ rs.request=function(path, args, callback, options){ background: true }); req.then(function checkResponseAndCallback(response){ - console.log(path + ": response: " + response.returncode); - if (response.returncode != "ok") { - requestFail(path, response, null); + options.log(path + ": response: " + response.returncode); + if (!options.allow.match(response.returncode)) { + options.onmismatch(response); } else if (callback != undefined && callback != null) { callback(response.data, response.statetoken); } - }, function errhandling(value){ - requestFail(path, null, value); - }); + }, options.onfail); return req; }; +//set default-values for shared options in rs() and rs.request() +function optionsPrep(options, path) { + if (options === undefined) { + options = {}; + } + + if (options.onfail === undefined) { + options.onfail = function errhandling(value){ + requestFail(path, null, value); + } + }; + if (options.onmismatch === undefined) { + options.onmismatch = function errhandling(response){ + requestFail(path, response,null); + } + }; + + if (options.log === undefined) { + options.log = function(message) { + console.log(message); + } + } + + if (options.allow === undefined) { + options.allow = "ok"; + }; + return options; +} + // force reload for path rs.forceUpdate = function(path){ cache[path].requested=false; @@ -269,3 +290,17 @@ rs.list = function(path, buildfktn){ }; return list.map(buildfktn); } + +//remember additional data (feature of last resort) +rs.memory = function(path, args){ + var item = cache[path]; + if (item === undefined) { + rs(path, args); + item = cache[path]; + } + if (item.memory === undefined) { + item.memory = {}; + } + return item.memory; +}; +