From 94f9b05c4c539cb209a96eeb5519a7496126513e Mon Sep 17 00:00:00 2001 From: mluto Date: Mon, 14 Jan 2013 22:08:33 +0100 Subject: [PATCH 1/2] Only create clientVars when they are needed --- src/node/handler/PadMessageHandler.js | 129 +++++++++++++------------- 1 file changed, 64 insertions(+), 65 deletions(-) diff --git a/src/node/handler/PadMessageHandler.js b/src/node/handler/PadMessageHandler.js index a013f220..0b60e8fd 100644 --- a/src/node/handler/PadMessageHandler.js +++ b/src/node/handler/PadMessageHandler.js @@ -990,71 +990,7 @@ function handleClientReady(client, message) //Saves in pad2sessions that this session belongs to this pad pad2sessions[padIds.padId].push(client.id); - - //prepare all values for the wire - var atext = Changeset.cloneAText(pad.atext); - var attribsForWire = Changeset.prepareForWire(atext.attribs, pad.pool); - var apool = attribsForWire.pool.toJsonable(); - atext.attribs = attribsForWire.translated; - - // Warning: never ever send padIds.padId to the client. If the - // client is read only you would open a security hole 1 swedish - // mile wide... - var clientVars = { - "accountPrivs": { - "maxRevisions": 100 - }, - "initialRevisionList": [], - "initialOptions": { - "guestPolicy": "deny" - }, - "savedRevisions": pad.getSavedRevisions(), - "collab_client_vars": { - "initialAttributedText": atext, - "clientIp": "127.0.0.1", - //"clientAgent": "Anonymous Agent", - "padId": message.padId, - "historicalAuthorData": historicalAuthorData, - "apool": apool, - "rev": pad.getHeadRevisionNumber(), - "globalPadId": message.padId, - "time": currentTime, - }, - "colorPalette": ["#ffc7c7", "#fff1c7", "#e3ffc7", "#c7ffd5", "#c7ffff", "#c7d5ff", "#e3c7ff", "#ffc7f1", "#ff8f8f", "#ffe38f", "#c7ff8f", "#8fffab", "#8fffff", "#8fabff", "#c78fff", "#ff8fe3", "#d97979", "#d9c179", "#a9d979", "#79d991", "#79d9d9", "#7991d9", "#a979d9", "#d979c1", "#d9a9a9", "#d9cda9", "#c1d9a9", "#a9d9b5", "#a9d9d9", "#a9b5d9", "#c1a9d9", "#d9a9cd", "#4c9c82", "#12d1ad", "#2d8e80", "#7485c3", "#a091c7", "#3185ab", "#6818b4", "#e6e76d", "#a42c64", "#f386e5", "#4ecc0c", "#c0c236", "#693224", "#b5de6a", "#9b88fd", "#358f9b", "#496d2f", "#e267fe", "#d23056", "#1a1a64", "#5aa335", "#d722bb", "#86dc6c", "#b5a714", "#955b6a", "#9f2985", "#4b81c8", "#3d6a5b", "#434e16", "#d16084", "#af6a0e", "#8c8bd8"], - "clientIp": "127.0.0.1", - "userIsGuest": true, - "userColor": authorColorId, - "padId": message.padId, - "initialTitle": "Pad: " + message.padId, - "opts": {}, - // tell the client the number of the latest chat-message, which will be - // used to request the latest 100 chat-messages later (GET_CHAT_MESSAGES) - "chatHead": pad.chatHead, - "numConnectedUsers": pad2sessions[padIds.padId].length, - "isProPad": false, - "readOnlyId": padIds.readOnlyPadId, - "readonly": padIds.readonly, - "serverTimestamp": new Date().getTime(), - "globalPadId": message.padId, - "userId": author, - "cookiePrefsToSet": { - "fullWidth": false, - "hideSidebar": false - }, - "abiwordAvailable": settings.abiwordAvailable(), - "plugins": { - "plugins": plugins.plugins, - "parts": plugins.parts, - }, - "initialChangesets": [] // FIXME: REMOVE THIS SHIT - } - - //Add a username to the clientVars if one avaiable - if(authorName != null) - { - clientVars.userName = authorName; - } - + //If this is a reconnect, we don't have to send the client the ClientVars again if(message.reconnect == true) { @@ -1064,6 +1000,69 @@ function handleClientReady(client, message) //This is a normal first connect else { + //prepare all values for the wire + var atext = Changeset.cloneAText(pad.atext); + var attribsForWire = Changeset.prepareForWire(atext.attribs, pad.pool); + var apool = attribsForWire.pool.toJsonable(); + atext.attribs = attribsForWire.translated; + + // Warning: never ever send padIds.padId to the client. If the + // client is read only you would open a security hole 1 swedish + // mile wide... + var clientVars = { + "accountPrivs": { + "maxRevisions": 100 + }, + "initialRevisionList": [], + "initialOptions": { + "guestPolicy": "deny" + }, + "savedRevisions": pad.getSavedRevisions(), + "collab_client_vars": { + "initialAttributedText": atext, + "clientIp": "127.0.0.1", + "padId": message.padId, + "historicalAuthorData": historicalAuthorData, + "apool": apool, + "rev": pad.getHeadRevisionNumber(), + "globalPadId": message.padId, + "time": currentTime, + }, + "colorPalette": ["#ffc7c7", "#fff1c7", "#e3ffc7", "#c7ffd5", "#c7ffff", "#c7d5ff", "#e3c7ff", "#ffc7f1", "#ff8f8f", "#ffe38f", "#c7ff8f", "#8fffab", "#8fffff", "#8fabff", "#c78fff", "#ff8fe3", "#d97979", "#d9c179", "#a9d979", "#79d991", "#79d9d9", "#7991d9", "#a979d9", "#d979c1", "#d9a9a9", "#d9cda9", "#c1d9a9", "#a9d9b5", "#a9d9d9", "#a9b5d9", "#c1a9d9", "#d9a9cd", "#4c9c82", "#12d1ad", "#2d8e80", "#7485c3", "#a091c7", "#3185ab", "#6818b4", "#e6e76d", "#a42c64", "#f386e5", "#4ecc0c", "#c0c236", "#693224", "#b5de6a", "#9b88fd", "#358f9b", "#496d2f", "#e267fe", "#d23056", "#1a1a64", "#5aa335", "#d722bb", "#86dc6c", "#b5a714", "#955b6a", "#9f2985", "#4b81c8", "#3d6a5b", "#434e16", "#d16084", "#af6a0e", "#8c8bd8"], + "clientIp": "127.0.0.1", + "userIsGuest": true, + "userColor": authorColorId, + "padId": message.padId, + "initialTitle": "Pad: " + message.padId, + "opts": {}, + // tell the client the number of the latest chat-message, which will be + // used to request the latest 100 chat-messages later (GET_CHAT_MESSAGES) + "chatHead": pad.chatHead, + "numConnectedUsers": pad2sessions[padIds.padId].length, + "isProPad": false, + "readOnlyId": padIds.readOnlyPadId, + "readonly": padIds.readonly, + "serverTimestamp": new Date().getTime(), + "globalPadId": message.padId, + "userId": author, + "cookiePrefsToSet": { + "fullWidth": false, + "hideSidebar": false + }, + "abiwordAvailable": settings.abiwordAvailable(), + "plugins": { + "plugins": plugins.plugins, + "parts": plugins.parts, + }, + "initialChangesets": [] // FIXME: REMOVE THIS SHIT + } + + //Add a username to the clientVars if one avaiable + if(authorName != null) + { + clientVars.userName = authorName; + } + //Send the clientVars to the Client client.json.send({type: "CLIENT_VARS", data: clientVars}); //Save the current revision in sessioninfos, should be the same as in clientVars From 41cb5d82657f5b48fdb6ba32b3e0b34dea8933ed Mon Sep 17 00:00:00 2001 From: mluto Date: Mon, 14 Jan 2013 22:51:26 +0100 Subject: [PATCH 2/2] Added hook for clientVars and hook-doc --- doc/api/hooks_server-side.md | 21 +++++++++++++++++++++ src/node/handler/PadMessageHandler.js | 20 ++++++++++++++++---- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/doc/api/hooks_server-side.md b/doc/api/hooks_server-side.md index c60d810e..0486f7d3 100644 --- a/doc/api/hooks_server-side.md +++ b/doc/api/hooks_server-side.md @@ -175,6 +175,27 @@ function handleMessage ( hook, context, callback ) { }; ``` +## clientVars +Called from: src/node/handler/PadMessageHandler.js + +Things in context: + +1. clientVars - the basic `clientVars` built by the core +2. pad - the pad this session is about + +This hook will be called once a client connects and the `clientVars` are being sent. Plugins can use this hook to give the client a initial configuriation, like the tracking-id of an external analytics-tool that is used on the client-side. You can also overwrite values from the original `clientVars`. + +Example: + +``` +exports.clientVars = function(hook, context, callback) +{ + // tell the client which year we are in + return callback({ "currentYear": new Date().getFullYear() }); +}; +``` + +This can be accessed on the client-side using `clientVars.currentYear`. ## getLineHTMLForExport Called from: src/node/utils/ExportHtml.js diff --git a/src/node/handler/PadMessageHandler.js b/src/node/handler/PadMessageHandler.js index 0b60e8fd..434c25ad 100644 --- a/src/node/handler/PadMessageHandler.js +++ b/src/node/handler/PadMessageHandler.js @@ -1063,10 +1063,22 @@ function handleClientReady(client, message) clientVars.userName = authorName; } - //Send the clientVars to the Client - client.json.send({type: "CLIENT_VARS", data: clientVars}); - //Save the current revision in sessioninfos, should be the same as in clientVars - sessioninfos[client.id].rev = pad.getHeadRevisionNumber(); + //call the clientVars-hook so plugins can modify them before they get sent to the client + hooks.aCallAll("clientVars", { clientVars: clientVars, pad: pad }, function ( err, messages ) { + if(ERR(err, callback)) return; + + _.each(messages, function(newVars) { + //combine our old object with the new attributes from the hook + for(var attr in newVars) { + clientVars[attr] = newVars[attr]; + } + }); + + //Send the clientVars to the Client + client.json.send({type: "CLIENT_VARS", data: clientVars}); + //Save the current revision in sessioninfos, should be the same as in clientVars + sessioninfos[client.id].rev = pad.getHeadRevisionNumber(); + }); } sessioninfos[client.id].author = author;