From cea748216c6edf4074f40dd0ab7fbe553be2f3d7 Mon Sep 17 00:00:00 2001 From: Lloyd Hilaiel <lloyd@hilaiel.com> Date: Wed, 2 May 2012 21:07:04 -0600 Subject: [PATCH] implement frontend interaction_data mechanisms, still require tests, event_stream population, and reduced UA information parsing --- lib/static_resources.js | 1 + lib/wsapi/interaction_data.js | 3 +- resources/static/shared/network.js | 22 ++++++++ resources/static/shared/storage.js | 89 ++++++++++++++++++++++++++++++ resources/static/shared/xhr.js | 1 - 5 files changed, 113 insertions(+), 3 deletions(-) diff --git a/lib/static_resources.js b/lib/static_resources.js index 8f88114a9..f877ca51a 100644 --- a/lib/static_resources.js +++ b/lib/static_resources.js @@ -80,6 +80,7 @@ var dialog_js = und.flatten([ '/shared/command.js', '/shared/history.js', '/shared/state_machine.js', + '/shared/interaction_data.js', '/dialog/resources/internal_api.js', '/dialog/resources/helpers.js', diff --git a/lib/wsapi/interaction_data.js b/lib/wsapi/interaction_data.js index a63462531..03a77a5a9 100644 --- a/lib/wsapi/interaction_data.js +++ b/lib/wsapi/interaction_data.js @@ -57,9 +57,8 @@ exports.process = function(req, res) { if (req.body.data) { var kpi_json = req.body.data; - logger.debug("Simulate write to KPI Backend DB - " + kpi_json); + logger.debug("Simulate write to KPI Backend DB - " + JSON.stringify(kpi_json)); try { - JSON.parse(kpi_json); store(kpi_json, function (store_success) { sendResponse(store_success); }); diff --git a/resources/static/shared/network.js b/resources/static/shared/network.js index 2f7049e6a..60651aff6 100644 --- a/resources/static/shared/network.js +++ b/resources/static/shared/network.js @@ -352,6 +352,28 @@ BrowserID.Network = (function() { }); }, + /** + * post interaction data + * @method setPassword + * @param {string} password - new password. + * @param {function} [onComplete] - Callback to call when complete. + * @param {function} [onFailure] - Called on XHR failure. + */ + sendInteractionData: function(data, onComplete, onFailure) { + post({ + url: "/wsapi/interaction_data", + data: { + // reminder, CSRF token will be inserted here by xhr.js, that's + // why this *must* be an object + data: data + }, + success: function(status) { + complete(onComplete, status.success); + }, + error: onFailure + }); + }, + /** * Update the password of the current user * @method changePassword diff --git a/resources/static/shared/storage.js b/resources/static/shared/storage.js index c1a6ec6f5..16cd8cd13 100644 --- a/resources/static/shared/storage.js +++ b/resources/static/shared/storage.js @@ -379,6 +379,58 @@ BrowserID.Storage = (function() { storage.emailToUserID = JSON.stringify(allInfo); } + function pushInteractionData(data) { + var id; + try { + id = JSON.parse(storage.interactionData); + id.unshift(data); + } catch(e) { + id = [ data ]; + } + storage.interactionData = JSON.stringify(id); + } + + function currentInteractionData() { + try { + return JSON.parse(storage.interactionData)[0]; + } catch(e) { + alert(e); + return {}; + } + } + + function setCurrentInteractionData(data) { + var id; + try { + id = JSON.parse(storage.interactionData); + id[0] = data; + } catch(e) { + alert(e); + id = [ data ]; + } + storage.interactionData = JSON.stringify(id); + } + + function getAllInteractionData() { + try { + var id = JSON.parse(storage.interactionData); + return id.slice(1); + } catch(e) { + alert(e); + return []; + } + } + + function clearInteractionData() { + try { + var id = JSON.parse(storage.interactionData); + storage.interactionData = JSON.stringify(id.slice(0,1)); + } catch(e) { + alert(e); + delete storage.interactionData; + } + } + return { /** * Add an email address and optional key pair. @@ -451,6 +503,43 @@ BrowserID.Storage = (function() { remove: generic2KeyRemove.curry("main_site", "signInEmail") }, + interactionData: { + /** + * add a data interaction blob to localstorage + * @param {object} data - an object to push onto the queue + * @method interactionData.push() + * @returns nada + */ + push: pushInteractionData, + /** + * read the current interaction data blob (the one on the top of the + * stack) + * @method interactionData.current() + * @returns a JSON object containing the latest interaction data blob + */ + current: currentInteractionData, + /** + * overwrite the current interaction data blob (the one on the top of the + * stack) + * @param {object} data - the object to overwrite current with + * @method interactionData.setCurrent() + */ + setCurrent: setCurrentInteractionData, + /** + * get all the saved interaction data (returned as a JSON array) + * @method interactionData.get() + * @returns an array, possibly of length zero if no interaction data is + * available + */ + get: getAllInteractionData, + /** + * clear all interaction data, except the current, in-progress + * collection. + * @method interactionData.clear() + */ + clear: clearInteractionData + }, + usersComputer: { /** * Query whether the user has confirmed that this is their computer diff --git a/resources/static/shared/xhr.js b/resources/static/shared/xhr.js index daf6f607b..2d86c6fde 100644 --- a/resources/static/shared/xhr.js +++ b/resources/static/shared/xhr.js @@ -132,7 +132,6 @@ BrowserID.XHR = (function() { withContext(function() { var data = options.data || {}; data.csrf = data.csrf || csrf_token; - var req = _.extend(options, { type: "POST", data: JSON.stringify(data), -- GitLab