[Pkg-mozext-commits] [wot] 111/226: Implemented commenting API (get, submit, remove)

David Prévot taffit at moszumanska.debian.org
Fri May 1 00:35:41 UTC 2015


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository wot.

commit b92aa04cac55242c48d6e2adda756c973db35a58
Author: Sergey Andryukhin <sorgoz at yandex.com>
Date:   Wed Aug 21 17:36:34 2013 +0300

    Implemented commenting API (get, submit, remove)
---
 content/api.js             | 442 ++++++++++++++++++++++++++++++++++++++++++---
 content/cache.js           |  33 ++++
 content/config.js          |  42 ++++-
 content/overlay.xul        |   8 +-
 content/ratingwindow.js    |  80 ++++++--
 content/rw/proxies.js      |  12 +-
 content/rw/ratingwindow.js |  33 ++--
 content/util.js            |  21 ++-
 skin/ratingwindow.css      |   2 +-
 9 files changed, 591 insertions(+), 82 deletions(-)

diff --git a/content/api.js b/content/api.js
index e43c49b..097faa3 100644
--- a/content/api.js
+++ b/content/api.js
@@ -237,23 +237,18 @@ var wot_api_query =
 
 					if (request.status == 200) {
 						wot_cache.add_query(
-							request.responseXML.getElementsByTagName(
-								WOT_SERVICE_XML_QUERY),
-							request.responseXML.getElementsByTagName(
-								WOT_SERVICE_XML_QUERY_TARGET),
+							request.responseXML.getElementsByTagName(WOT_SERVICE_XML_QUERY),
+							request.responseXML.getElementsByTagName(WOT_SERVICE_XML_QUERY_TARGET),
 							false);
 
 						wot_api_query.parse_messages(
-							request.responseXML.getElementsByTagName(
-									WOT_SERVICE_XML_QUERY_MSG));
+							request.responseXML.getElementsByTagName(WOT_SERVICE_XML_QUERY_MSG));
 
 						wot_api_query.parse_users(
-							request.responseXML.getElementsByTagName(
-									WOT_SERVICE_XML_QUERY_USER));
+							request.responseXML.getElementsByTagName(WOT_SERVICE_XML_QUERY_USER));
 
 						wot_api_query.parse_status(
-							request.responseXML.getElementsByTagName(
-									WOT_SERVICE_XML_QUERY_STATUS));
+							request.responseXML.getElementsByTagName(WOT_SERVICE_XML_QUERY_STATUS));
 					}
 
 					wot_cache.set(hostname, "inprogress", false);
@@ -375,6 +370,7 @@ var wot_api_query =
                 user_item = {},
                 a = null,
                 user_props = {
+                    // so far we don't need most of fields any more that is why they are commented out
 //                icon:       WOT_SERVICE_XML_QUERY_USER_ICON,
 //                bar:        WOT_SERVICE_XML_QUERY_USER_BAR,
 //                "length":   WOT_SERVICE_XML_QUERY_USER_LENGTH,
@@ -471,20 +467,15 @@ var wot_api_register =
 			}
 
 			if (wot_browser.isoffline()) {
-				wot_status.set("offline",
-					wot_util.getstring("messages_offline"));
-				this.timeout = window.setTimeout(wot_api_register.send,
-					WOT_INTERVAL_REGISTER_OFFLINE);
+				wot_status.set("offline", wot_util.getstring("messages_offline"));
+				this.timeout = window.setTimeout(wot_api_register.send, WOT_INTERVAL_REGISTER_OFFLINE);
 				return;
 			}
 
-			wot_status.set("notready",
-				wot_util.getstring("messages_notready"));
+			wot_status.set("notready", wot_util.getstring("messages_notready"));
 
-			if (!this.geteid() ||
-					wot_hashtable.get(WOT_REGISTER_RUNNING)) {
-				this.timeout = window.setTimeout(wot_api_register.send,
-					WOT_INTERVAL_REGISTER_ERROR);
+			if (!this.geteid() || wot_hashtable.get(WOT_REGISTER_RUNNING)) {
+				this.timeout = window.setTimeout(wot_api_register.send, WOT_INTERVAL_REGISTER_ERROR);
 				return;
 			}
 
@@ -627,8 +618,7 @@ var wot_api_reload =
 			}
 
 			var reload = null;
-			var tags = event.target.responseXML.getElementsByTagName(
-							WOT_SERVICE_XML_RELOAD);
+			var tags = event.target.responseXML.getElementsByTagName(WOT_SERVICE_XML_RELOAD);
 
 			if (tags) {
 				reload = tags.item(0);
@@ -639,10 +629,8 @@ var wot_api_reload =
 				return;
 			}
 
-			var id  = reload.attributes.getNamedItem(
-							WOT_SERVICE_XML_RELOAD_ID);
-			var key = reload.attributes.getNamedItem(
-							WOT_SERVICE_XML_RELOAD_KEY);
+			var id  = reload.attributes.getNamedItem(WOT_SERVICE_XML_RELOAD_ID);
+			var key = reload.attributes.getNamedItem(WOT_SERVICE_XML_RELOAD_KEY);
 
 			if (!id || !id.value || !key || !key.value ||
 				id.value.length  != WOT_LENGTH_WITNESS_ID ||
@@ -751,8 +739,7 @@ var wot_api_submit =
 			{
 				try {
 					if (request.status == 200) {
-						var submit = request.responseXML.getElementsByTagName(
-										WOT_SERVICE_XML_SUBMIT);
+						var submit = request.responseXML.getElementsByTagName(WOT_SERVICE_XML_SUBMIT);
 
 						if (submit && submit.length > 0) {
 							wot_pending.clear(pref);
@@ -1133,3 +1120,402 @@ var wot_pending =
 		}
 	}
 };
+
+var wot_api_comments = {
+//    server: "beta.mywot.com",
+    server: "dev.mywot.com",
+    version: "1",   // Comments API version
+    PENDING_COMMENT_SID: "pending_comment:",
+    PENDING_REMOVAL_SID: "pending_removal:",
+    MAX_TRIES: 10,  // maximum amount of tries to send a comment or remove a comment
+    retrytimeout: {
+        submit: 30 * 1000,
+        remove: 20 * 1000
+    },
+    nonces: {},     // to know connection between nonce and target
+
+    serialize: function (obj) {
+        var str = [];
+        for(var p in obj){
+            if (obj.hasOwnProperty(p)) {
+                str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
+            }
+        }
+        return str.join("&");
+    },
+
+    call: function (apiname, options, params, on_error, on_success) {
+        try {
+            var _this = wot_api_comments,
+                nonce = wot_crypto.nonce(),
+                original_target = params.target;
+
+            params = params || {};
+            var post_params = {};
+
+            params.id = wot_prefs.witness_id;
+            params.nonce = nonce;
+            params.version = WOT_PLATFORM + "-" +WOT_VERSION;
+
+            options = options || { type: "GET" };
+
+            if (options.encryption) {
+                params.target = wot_crypto.encrypt(params.target, nonce);
+            }
+
+            var components = [];
+
+            for (var i in params) {
+                if (params[i] != null) {
+                    var param_name = i,
+                        param_value = params[i];
+
+                    // Use a hash instead of the real value in the authenticated query
+                    if (options.hash && options.hash == i) {
+                        param_name = "SHA1";
+                        param_value = wot_hash.bintohex(wot_hash.sha1str(unescape( encodeURIComponent( params[i] )))); //wot_crypto.bintohex(wot_crypto.sha1.sha1str(unescape( encodeURIComponent( params[i] ))));
+                    }
+
+                    components.push(param_name + "=" + encodeURIComponent(param_value));
+                }
+            }
+
+            var query_string = components.join("&"),
+                path = "/api/" + _this.version + "/addon/comment/" + apiname,
+                full_path = path + "?" + query_string;
+
+            if (options.authentication) {
+                var auth = wot_crypto.authenticate(full_path);
+
+                if (!auth || !components.length) {
+                    return false;
+                }
+                full_path += "&auth=" + auth;
+            }
+
+            if (options.type == "POST") {
+                post_params.query = full_path;
+
+                if (options.hash) {
+                    post_params[options.hash] = params[options.hash];   // submit the real value of the parameter that is authenticated as the hash
+                }
+            }
+
+            // the add-on does NOT have permissions for httpS://www.mywot.com so we use http and own encryption
+            var type = options.type ? options.type : "GET";
+            var url = "http://" + this.server + (type == "POST" ? path : full_path);
+
+            _this.nonces[nonce] = original_target;    // remember the link between nonce and target
+
+            var request = new XMLHttpRequest();
+            request.open(type, url);
+
+            new wot_cookie_remover(request);
+
+            request.onload = function (event) {
+                if (!event || !event.target || event.target.status != 200 ||
+                    !event.target.responseText) {
+                    wdump("api.comments.call.error: url = " + url + ", status = " + event.target.status);
+
+                    if (typeof(on_error) == "function") {
+                        on_error(request, event.target.status, {});
+                    }
+                    return;
+                }
+
+                wdump("api.comments.call.success: url = " + url + ", status = " + event.target.status);
+
+                var data = JSON.parse(event.target.responseText);
+
+                if (typeof(on_success) == "function") {
+                    on_success(data, event.target.status, nonce);
+                }
+
+            };
+
+            var prepared_post_params = null;
+
+            if (type == "POST") {
+                prepared_post_params = _this.serialize(post_params);
+                request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
+                request.setRequestHeader("Content-length", prepared_post_params.length);
+                request.setRequestHeader("Connection", "close");
+            }
+
+            request.send(prepared_post_params);
+            return true;
+
+        } catch (e) {
+            wdump("wot_api_comments.call(): failed with " + e);
+        }
+
+        return false;
+
+    },
+
+    get: function(target) {
+        var _this = wot_api_comments;
+        wdump("wot.api.comments.get(target) " + target);
+
+        _this.call("get",
+            {
+                encryption: true,
+                authentication: true
+            },
+            {
+                target: target
+            },
+            null,   // TODO: handle network errors
+            function (data) {
+                _this.on_get_comment_response(data);
+            }
+        );
+    },
+
+    submit: function (target, comment, comment_id, votes) {
+
+        var _this = wot_api_comments,
+            pref_pending_name = _this.PENDING_COMMENT_SID + target;
+
+        // try to restore pending submission first
+        var state_json = wot_prefs.getChar(pref_pending_name);
+        var state = state_json ? JSON.parse(state_json) : {
+            target: target,
+            comment_data: {},
+            tries: 0
+        };
+
+        // if params are given, it means we are on normal way of sending data (not on retrying)
+        if (comment && votes) {
+            state.comment_data.comment = comment;
+            state.comment_data.cid = comment_id || 0;
+            state.comment_data.categories = votes;
+            state.tries = 0;
+        }
+
+        if (++state.tries > _this.MAX_TRIES) {
+            wdump("FAIL: api.comments.submit: failed " + target + " (max tries)");
+            wot_prefs.clear(pref_pending_name);
+            return;
+        }
+
+        wot_prefs.setChar(pref_pending_name, JSON.stringify(state));    // remember the submission
+
+        state.comment_data['target'] = target;
+
+        _this.call("submit",
+            {
+                encryption: true,
+                authentication: true,
+                type: "POST",
+                hash: "comment" // this field must be hashed and the hash must be authenticated
+            },
+            state.comment_data,
+            function (request) { // handle network errors
+                if (request.status != 403) {
+                    wot_api_comments.retry("submit", [ target ]);
+                } else {
+                    wdump("api.comment.submit: failed " + target + " (403)");
+                    wot_prefs.clear(wot_api_comments.PENDING_COMMENT_SID + target);
+                }
+            },
+            wot_api_comments.on_submit_comment_response
+        );
+
+        // set the local cache to the comment value
+        wot_cache.set_comment(target, {
+            comment: comment,
+            wcid: comment_id,
+            status: WOT_QUERY_RETRY,    // the sign of unverified submission // TODO: replace to BUSY constant
+            timestamp: Date.now()
+        });
+    },
+
+    remove: function (target) {
+//            console.log("wot.api.comments.remove(target)", target);
+
+        var _this =  wot_api_comments,
+            pref_pending_name = _this.PENDING_REMOVAL_SID + target;
+
+        // try to restore pending submission first
+        var state_json = wot_prefs.getChar(pref_pending_name);
+        var state = state_json ? JSON.parse(state_json) : {
+            target: target,
+            tries: 0
+        };
+
+        if (++state.tries > _this.MAX_TRIES) {
+            wdump("api.comments.submit: failed " + target + " (max tries)");
+            wot_prefs.clear(pref_pending_name);
+            return;
+        }
+
+        wot_prefs.setChar(pref_pending_name, JSON.stringify(state));    // remember the submission
+
+        _this.call("remove",
+            {
+                encryption: true,
+                authentication: true,
+                type: "POST"
+            },
+            {
+                target: target
+            },
+            function (request) {   // handle network errors
+                if (request.status != 403) {
+                    wot_api_comments.retry("remove", [ target ]);
+                } else {
+                    wdump("api.comment.remove: failed " + target + " (403)");
+                    wot_prefs.clear(wot_api_comments.PENDING_REMOVAL_SID + target);
+                }
+            },
+            wot_api_comments.on_remove_comment_response
+        );
+    },
+
+    retry: function(apiname, params, customtimeout)
+    {
+        var timeout = customtimeout || wot_api_comments.retrytimeout[apiname];
+
+        if (timeout) {
+            window.setTimeout(function() {
+                wot_api_comments[apiname].apply(wot_api_comments, params || []);
+            }, timeout);
+        }
+    },
+
+    processpending: function()
+    {
+//        wot.prefs.each(function(name, value) {
+//            if (/^pending_comment\:/.test(name)) {
+//                wot.api.comments.submit(name.replace(/^pending_comment\:/, ""));
+//            } else if (/^pending_removal\:/.test(name)) {
+//                wot.api.comments.remove(name.replace(/^pending_removal\:/, ""));
+//            }
+//            return false;
+//        });
+    },
+
+    pull_nonce: function (nonce) {
+        wdump("wot.api.comments._pull_once(nonce) " + nonce);
+
+        var _this = wot_api_comments,
+            target = null;
+
+        if (_this.nonces[nonce]) {
+            target = _this.nonces[nonce];
+            delete _this.nonces[nonce];
+        }
+
+        return target;
+    },
+
+    is_error: function (error) {
+        wdump("wot_api_comments.is_error(error)" + error);
+
+        var error_code = 0,
+            error_debug = "it is raining outside :(";
+
+        if (error instanceof Array && error.length > 1) {
+            error_code = error[0];
+            error_debug = error[1];
+        } else {
+            error_code = (error !== undefined ? error : 0);
+        }
+
+        if (error_code && error_code != WOT_COMMENTS.error_codes.COMMENT_NOT_FOUND) {
+            wdump("Error is returned:" + error_code + " / " + error_debug + " / " + error);
+        }
+
+        return error_code;  // if not zero, than it is error
+    },
+
+    on_get_comment_response: function (data) {
+        wdump("wot_api_comments.on_get_comment_response(data)" + JSON.stringify(data));
+        // check whether error occured or data arrived
+        var _this = wot_api_comments,
+            nonce = data.nonce, // to recover target from response
+            target = _this.pull_nonce(nonce),
+            error_code = _this.is_error(data.error);
+
+        switch (error_code) {
+            case WOT_COMMENTS.error_codes.SUCCESS:
+                wot_cache.set_comment(target, data); // TODO: implement
+                break;
+            case WOT_COMMENTS.error_codes.COMMENT_NOT_FOUND:
+                wot_cache.remove_comment(target);   // remove the comment if it is cached
+                break;
+            default:
+                wot_cache.set_comment(target, { status: WOT_QUERY_ERROR, error_code: error_code });
+        }
+
+        wot_cache.set_captcha(!!data.captcha);
+
+        wot_rw.update_ratingwindow_comment();
+    },
+
+    on_submit_comment_response: function (data) {
+        /* Handler for "Submit" responses. On success it updates the local cache  */
+
+        wdump("wot_api_comments.on_submit_comment_response(data) " + data);
+        var _this = wot_api_comments,
+            nonce = data.nonce, // to recover target from response
+            target = _this.pull_nonce(nonce),
+            error_code = _this.is_error(data.error);
+
+        switch (error_code) {
+            case WOT_COMMENTS.error_codes.SUCCESS:
+//                wot.keeper.remove_by_name(target);  // delete the locally saved comment only on successful submit
+                wot_cache.update_comment(target, { status: WOT_QUERY_OK, error_code: error_code });
+                wot_prefs.clear(_this.PENDING_COMMENT_SID + target); // don't try to send again
+                break;
+
+            // for these errors we should try again, because there is non-zero possibility of quantum glitches around
+            case WOT_COMMENTS.error_codes.AUTHENTICATION_FAILED:
+            case WOT_COMMENTS.error_codes.AUTHENTICATION_REP_SERVER_ERROR:
+            case WOT_COMMENTS.error_codes.COMMENT_SAVE_FAILED:
+                wot_cache.update_comment(target, { status: WOT_QUERY_ERROR, error_code: error_code });
+                _this.retry("submit", [ target ]);   // yeah, try it again, ddos own server ;)
+                break;
+
+            default:
+                wot_cache.update_comment(target, { status: WOT_QUERY_ERROR, error_code: error_code });
+                wot_prefs.clear(_this.PENDING_COMMENT_SID + target);
+        }
+
+        wot_cache.set_captcha(!!data.captcha);
+
+        wot_rw.update_ratingwindow_comment(); // to update status "the website is commented by the user"
+    },
+
+    on_remove_comment_response: function (data) {
+        wdump("wot.api.comments.on_remove_comment_response(data) " + data);
+
+        var _this = wot_api_comments,
+            nonce = data.nonce, // to recover target from response
+            target = _this.pull_nonce(nonce),
+            error_code = _this.is_error(data.error);
+
+        switch (error_code) {
+            case WOT_COMMENTS.error_codes.SUCCESS:
+                wot_cache.remove_comment(target);
+//                wot.keeper.remove_comment(target);
+                wot_prefs.clear(_this.PENDING_REMOVAL_SID + target);
+                break;
+
+            // some errors require retry due to singularity of the Universe
+            case WOT_COMMENTS.error_codes.AUTHENTICATION_FAILED:
+            case WOT_COMMENTS.error_codes.AUTHENTICATION_REP_SERVER_ERROR:
+            case WOT_COMMENTS.error_codes.COMMENT_REMOVAL_FAILED:
+                wot_cache.update_comment(target, { status: WOT_QUERY_ERROR, error_code: error_code });
+                _this.retry("remove", [ target ]);
+                break;
+
+            default:
+                wot_cache.update_comment(target, { status: WOT_QUERY_ERROR, error_code: error_code });
+                wot_prefs.clear(_this.PENDING_REMOVAL_SID + target);
+        }
+
+        wot_rw.update_ratingwindow_comment(); // to update status "the website is commented by the user"
+    }
+};
\ No newline at end of file
diff --git a/content/cache.js b/content/cache.js
index 3b10574..26eef8e 100644
--- a/content/cache.js
+++ b/content/cache.js
@@ -308,6 +308,39 @@ var wot_cache =
 		}
 	},
 
+    set_captcha: function (captcha_required) {
+        wot_hashtable.set("captcha_required", !!captcha_required);
+    },
+
+    get_captcha: function () {
+        return !!wot_hashtable.get("captcha_required");
+    },
+
+    get_comment: function (name) {
+        var json_data = this.get(name, "comment"),
+            data = {};
+        if (json_data) {
+            data = JSON.parse(json_data);
+        }
+        return data;
+    },
+
+    set_comment: function (name, comment_data) {
+        this.set(name, "comment", JSON.stringify(comment_data));
+    },
+
+    update_comment: function (name, data) {
+        var comment_data = this.get_comment(name);
+        for (var key in data) {
+            comment_data[key] = data[key];
+        }
+        this.set_comment(name, comment_data);
+    },
+
+    remove_comment: function (name) {
+        this.remove(name, "comment");
+    },
+
 	add_target: function(nonce, target, islink)
 	{
 		try {
diff --git a/content/config.js b/content/config.js
index e397b8a..ab534d7 100644
--- a/content/config.js
+++ b/content/config.js
@@ -62,15 +62,6 @@ const WOT_CONFIDENCELEVELS = [
         { level: "5", name: "c5", min: WOT_MIN_CONFIDENCE_5 }
     ];
 
-/* Testimony values and rounding */
-const WOT_TESTIMONY_QUICK_5 = WOT_MIN_REPUTATION_5;
-const WOT_TESTIMONY_QUICK_4 = WOT_MIN_REPUTATION_4;
-const WOT_TESTIMONY_QUICK_3 = WOT_MIN_REPUTATION_3;
-const WOT_TESTIMONY_QUICK_2 = WOT_MIN_REPUTATION_2;
-const WOT_TESTIMONY_QUICK_1 = 0;
-const WOT_TESTIMONY_ROUND = 1; /* Testimony steps */
-const WOT_MIN_COMMENT_DIFF = 35;
-
 // reference: http://www.mywot.com/wiki/Activity_scores
 const WOT_ACTIVITYSCORE_LEVELS = [
         { name: "rookie", min: 0 },
@@ -376,6 +367,39 @@ const WOT_URL_MENUMY =       "menu-my";
 const WOT_URL_BTN =          "button";
 const WOT_URL_CTX =          "contextmenu";
 
+const WOT_COMMENTS = {
+    error_codes: {
+        "0": "SUCCESS",
+        "1": "NO_ACTION_DEFINED",
+        "2": "IS_BANNED",
+        "3": "AUTHENTICATION_FAILED",
+        "4": "NO_TARGET",
+        "5": "COMMENT_NOT_FOUND",
+        "6": "COMMENT_REMOVAL_FAILED",
+        "7": "COMMENT_NOT_ALLOWED",
+        "8": "NO_COMMENTID",
+        "9": "NO_CATEGORIES_SPECIFIED",
+        "10": "NO_COMMENT_SPECIFIED",
+        "11": "AUTHENTICATION_INVALID_QUERY_PARAMETERS",
+        "12": "AUTHENTICATION_REP_SERVER_ERROR",
+        "13": "NO_QUERY_SPECIFIED",
+        "14": "QUERY_STRING_MISSING",
+        "15": "COMMENT_HAS_BEEN_ALTERED",
+        "16": "COMMENT_TOO_SHORT",
+        "17": "COMMENT_TOO_LONG",
+        "18": "COMMENT_SAVE_FAILED",
+        SUCCESS: 0,
+        NO_ACTION_DEFINED: 1,
+        IS_BANNED: 2,
+        AUTHENTICATION_FAILED: 3,
+        COMMENT_NOT_FOUND: 5,
+        COMMENT_REMOVAL_FAILED: 6,
+        COMMENT_NOT_ALLOWED: 7,
+        AUTHENTICATION_REP_SERVER_ERROR: 12,
+        COMMENT_SAVE_FAILED: 18
+    }
+};
+
 var wot_modules = [];
 
 // Dirty hack. This function should be moved to other place
diff --git a/content/overlay.xul b/content/overlay.xul
index 517c05a..d6c515c 100644
--- a/content/overlay.xul
+++ b/content/overlay.xul
@@ -62,14 +62,14 @@
 
     <popupset id="mainPopupSet">
         <!-- Testimony window -->
-        <menupopup id="wot-popup" noautohide="true" position="after_start"
+        <panel id="wot-popup" position="topcenter"
                    onpopupshowing="wot_rw.on_rw_open(event);"
-                   onpopuphiding="wot_rw.on_hide_popup();" showcaret="true">
+                   onpopuphiding="wot_rw.on_hide_popup();" noautofocus="false" level="parent">
 
             <iframe id="wot-rwframe" src="chrome://wot/content/rw/ratingwindow.html" type="content-primary"
-                    transparent="false" showcaret="true" width="100%" height="414px"/>
+                    transparent="false" width="100%" height="414px"/>
 
-        </menupopup>
+        </panel>
 
         <!-- Indicator tooltip -->
         <tooltip id="wot-tooltip"
diff --git a/content/ratingwindow.js b/content/ratingwindow.js
index 8bbc209..1562f12 100644
--- a/content/ratingwindow.js
+++ b/content/ratingwindow.js
@@ -26,6 +26,7 @@ var wot_rw = {
     CHAN_ELEM_ID: "wot-comm-channel",
     CHAN_EVENT_ID: "wotrw",
     IGNORED_PREFS: ["ratingwindow_shown"],
+    LEVELS: ["registered"], // list of possible levels to test
 
     get_rw_window: function () {
         var rw_frame = document.getElementById(this.FRAME_ID);
@@ -123,19 +124,11 @@ var wot_rw = {
         }
     },
 
-    update: function () {
-        // Updates content of Rating Window. RW must be already initialized (locales, categories info, etc).
-        wdump("RW.update()");
+    get_cached: function () {
 
-        var rw = this.get_rw_window(),
-            rw_doc = this.get_rw_document(),
-            rw_wot = this.get_rw_wot(),
-            data = {},
-            target = wot_core.hostname;
-
-        wdump("Target: " + target);
+        var target = wot_core.hostname,
+            data = {};
 
-        if (!rw || !rw_doc || !rw_wot) return;
 
         if (target && wot_cache.isok(target)) {
             var normalized_target = wot_cache.get(target, "normalized") || target;
@@ -147,7 +140,8 @@ var wot_rw = {
                 updated: wot_cache.get(target, "time"),
                 cached: {
                     status: wot_cache.get(target, "status"),
-                    value: {}
+                    value: {},
+                    comment: wot_cache.get_comment(target)
                 }
             };
 
@@ -183,6 +177,24 @@ var wot_rw = {
             };
         }
 
+        return data;
+    },
+
+    update: function () {
+        // Updates content of Rating Window. RW must be already initialized (locales, categories info, etc).
+        wdump("RW.update()");
+
+        var rw = this.get_rw_window(),
+            rw_doc = this.get_rw_document(),
+            rw_wot = this.get_rw_wot(),
+            target = wot_core.hostname;
+
+        wdump("\tTarget: " + target);
+
+        if (!rw || !rw_doc || !rw_wot) return;
+
+        var data = wot_rw.get_cached();
+
         wot_rw.push_preferences(rw, wot_rw.get_preferences());  // update preferences every time before showing RW
 
         wot_rw.update_messages();
@@ -195,11 +207,31 @@ var wot_rw = {
             ];
         }
 
-        wdump("data: " + JSON.stringify(data));
+        // iterate through known user levels to find current one and provide it to rating window
+        for (var l, i=0; i < wot_rw.LEVELS.length; i++) {
+            l = wot_rw.LEVELS[i];
+            if (wot_crypto.islevel(l) && rw.wot_bg) {
+                rw.wot_bg.wot.core._level = l;  // set the level into rating window
+                break;
+            }
+        }
+
+        wdump("\tdata: " + JSON.stringify(data));
 
         rw_wot.ratingwindow.update(target, JSON.stringify(data));
     },
 
+    update_ratingwindow_comment: function () {
+
+        var target = wot_core.hostname,
+            cached = wot_rw.get_cached(),
+            rw = wot_rw.get_rw_window(),
+            rw_wot = wot_rw.get_rw_wot(),
+            local_comment = {}; //TODO: wot.keeper.get_comment(target); // get locally stored comment if exists
+
+        rw_wot.ratingwindow.update_comment(cached.cached, local_comment, wot_cache.get_captcha());
+    },
+
     get_preferences: function () {
         var prefs = {};
         try {
@@ -289,7 +321,7 @@ var wot_rw = {
                     break;
 
                 case "update_ratingwindow_comment":
-                    // TODO: implement
+                    wot_rw.update_ratingwindow_comment();
                     break;
 
                 case "unseenmessage":
@@ -314,6 +346,18 @@ var wot_rw = {
                 case "submit":
                     wot_rw.on_submit(data);
                     break;
+
+                case "get_comment":
+                    wot_api_comments.get(data.target);
+                    break;
+
+                case "submit_comment":
+                    wot_api_comments.submit(data.target, data.user_comment, data.user_comment_id, data.votes);
+                    break;
+
+                case "remove_comment":
+                    wot_api_comments.remove(data.target);
+                    break;
             }
 
             return true;
@@ -348,25 +392,21 @@ var wot_rw = {
             var locale_strings = wot_util.get_all_strings();
             rw.chrome.i18n.loadMessages(JSON.stringify(locale_strings));    // using JSON to push data to sandboxed content
 
-            // TODO: provide preferences to RW
-            wdump(JSON.stringify(this.get_preferences()));
-
             var prefs = this.get_preferences();
             this.push_preferences(rw, prefs);
+            wdump(JSON.stringify(prefs));
 
             // setup categories data in the RW
             rw_wot.categories = wot_categories.categories;
             rw_wot.grouping = wot_categories.grouping;
             rw_wot.cgroups = wot_categories.cgroups;
 
-            // TODO: provide "level" (decrypted) value
-
             rw_wot.ratingwindow.onload();   // this runs only once in FF
 
             this.is_inited = true;
 
         } catch (e) {
-
+            wdump("Failed with wot_rw.initialize()" + e);
         }
     }
 
diff --git a/content/rw/proxies.js b/content/rw/proxies.js
index bb6c769..2ffef37 100644
--- a/content/rw/proxies.js
+++ b/content/rw/proxies.js
@@ -116,15 +116,20 @@ var wot_bg = {    // background page object
             comments: {
 
                 get: function (target) {
-
+                    wot_bg.wot.core.moz_send("get_comment", { target: target });
                 },
 
                 submit: function (target, user_comment, user_comment_id, votes) {
-
+                    wot_bg.wot.core.moz_send("submit_comment", {
+                        target: target,
+                        user_comment: user_comment,
+                        user_comment_id: user_comment_id,
+                        votes: votes
+                    });
                 },
 
                 remove: function (target) {
-
+                    wot_bg.wot.core.moz_send("remove_comment", { target: target });
                 }
 
             }
@@ -135,6 +140,7 @@ var wot_bg = {    // background page object
             cacheratingstate: function (target, state, votes) {
                 // Detects whether testimonies where changed.
                 // This function doesn't store anything in the Cache as against Chrome implementation.
+                // TODO: it should store new state/votes so when finish state is called again it doesn't send ratings second time
 
                 var changed = false,
                     obj = wot.ratingwindow.getcached();
diff --git a/content/rw/ratingwindow.js b/content/rw/ratingwindow.js
index d8cab3a..48636d5 100644
--- a/content/rw/ratingwindow.js
+++ b/content/rw/ratingwindow.js
@@ -539,10 +539,15 @@ $.extend(wot, { ratingwindow: {
 
     update: function(target, data)
     {
-        var _rw = wot.ratingwindow;
+
         try {
+            var _rw = wot.ratingwindow;
+            var bg = chrome.extension.getBackgroundPage();
+
             data = JSON.parse(data);    // for safety
             _rw.current = data || {};
+            _rw.is_registered = bg.wot.core.is_level("registered"); // update the state on every window update
+
             _rw.updatecontents();
             _rw.update_categories();
 
@@ -550,7 +555,6 @@ $.extend(wot, { ratingwindow: {
                 // ask server if there is my comment for the website
                 _rw.comments.get_comment(data.target);
             } else {
-                var bg = chrome.extension.getBackgroundPage();
                 bg.wot.core.update_ratingwindow_comment(); // don't allow unregistered addons to comment
             }
 
@@ -563,7 +567,6 @@ $.extend(wot, { ratingwindow: {
     },
 
     update_comment: function (cached, local_comment, captcha_required) {
-        wot.log("update_comment()", cached);
 
         var _rw = wot.ratingwindow,
             _comments = wot.ratingwindow.comments,
@@ -581,12 +584,11 @@ $.extend(wot, { ratingwindow: {
 
         var error_code = data.error_code || 0;
 
-//        _comments.allow_commenting = ([
-//            wot.comments.error_codes.AUTHENTICATION_FAILED,
-//            wot.comments.error_codes.COMMENT_NOT_ALLOWED,
-//            wot.comments.error_codes.IS_BANNED
-//        ].indexOf(error_code) < 0); // if none of these codes are found
-        _comments.allow_commenting = false; // FIXME: remove this temporary line when commenting feature is ready
+        _comments.allow_commenting = ([
+            wot.comments.error_codes.AUTHENTICATION_FAILED,
+            wot.comments.error_codes.COMMENT_NOT_ALLOWED,
+            wot.comments.error_codes.IS_BANNED
+        ].indexOf(error_code) < 0); // if none of these codes are found
 
 
         _comments.is_banned = (error_code == wot.comments.error_codes.IS_BANNED);
@@ -626,6 +628,8 @@ $.extend(wot, { ratingwindow: {
             _comments.posted_comment = {};
         }
 
+        bg.console.log("Registered? " + _rw.is_registered);
+
         // change appearance of commenting area regarding to permissions
         if (!_rw.is_registered) {
             // show the invitation to create an account
@@ -908,8 +912,6 @@ $.extend(wot, { ratingwindow: {
 
         wot.init_categories(_rw.prefs);
 
-        _rw.is_registered = bg.wot.core.is_level("registered");
-
         /* accessibility */
         // TODO: use only 1 "global" style on the most top element to specify accessible mode for all children elements
         $("#wot-header-logo, " +
@@ -1957,7 +1959,7 @@ $.extend(wot, { ratingwindow: {
 
     /* Start of Comments API and Comments UI code */
     comments: {
-        allow_commenting: false,    // FIXME: set true again when commenting feature will be ready
+        allow_commenting: true,
         is_banned: false,
         captcha_required: false,
         MIN_LIMIT: 30,
@@ -2030,7 +2032,6 @@ $.extend(wot, { ratingwindow: {
             }
 
             $_button.toggle(!_this.is_banned);  // don't show this button to banned users
-            $_button.hide(); // FIXME: remove this when Commenting feature is ready
 
         },
 
@@ -2064,6 +2065,6 @@ $.extend(wot, { ratingwindow: {
 
 }});
 
-$(document).ready(function() {
-//    wot.ratingwindow.onload();
-});
+//$(document).ready(function() {
+////    wot.ratingwindow.onload();
+//});
diff --git a/content/util.js b/content/util.js
index a41f7d1..d087900 100644
--- a/content/util.js
+++ b/content/util.js
@@ -953,11 +953,30 @@ var wot_crypto =
 
 			return (l == h);
 		} catch (e) {
-			dump("wot_crypto.islevel: failed with " + e + "\n");
+			wdump("wot_crypto.islevel: failed with " + e);
 		}
 		return false;
 	},
 
+    encrypt: function(data, nonce)
+    {
+        try {
+            if (data && nonce) {
+                var key = wot_prefs.witness_key;
+
+                if (key) {
+                    return btoa(wot_hash.bintostr(wot_arc4.crypt(
+                        wot_arc4.create(wot_hash.hmac_sha1hex(key, nonce)),
+                        wot_hash.strtobin(data))));
+                }
+            }
+        } catch (e) {
+            wdump("crypto.encrypt: failed with " + e);
+        }
+
+        return null;
+    },
+
     decrypt: function(data, nonce, index)
     {
         try {
diff --git a/skin/ratingwindow.css b/skin/ratingwindow.css
index 327b6c2..d4a4985 100644
--- a/skin/ratingwindow.css
+++ b/skin/ratingwindow.css
@@ -1653,7 +1653,7 @@ input[type=checkbox].css-checkbox:checked + label.css-label {
 }
 
 #user-comment {
-    height: 115px;
+    height: 125px; /* different in Firefox */
     width: 350px;
     /*margin-right: 10px;*/
     padding: 8px;

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mozext/wot.git



More information about the Pkg-mozext-commits mailing list