[Pkg-mozext-commits] [wot] 97/226: Added ratings+votes submit feature

David Prévot taffit at moszumanska.debian.org
Fri May 1 00:35:38 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 ea878593c27ae53090c9165b3177253e8d761816
Author: Sergey Andryukhin <sorgoz at yandex.com>
Date:   Tue Aug 6 17:30:08 2013 +0300

    Added ratings+votes submit feature
---
 content/api.js               | 171 ++++++++++++++++++++++++-------------------
 content/cache.js             |  56 ++++++++------
 content/categories.js        |  79 ++++++++------------
 content/commands.js          |  82 ++++++++++-----------
 content/ratingwindow.js      |  46 ++++++++++++
 content/rw/proxies.js        |  37 +++++++++-
 content/rw/ratingwindow.html |  12 +--
 content/rw/ratingwindow.js   |  70 ++++++++++--------
 8 files changed, 326 insertions(+), 227 deletions(-)

diff --git a/content/api.js b/content/api.js
index b4bf50e..bb4abd5 100644
--- a/content/api.js
+++ b/content/api.js
@@ -693,7 +693,7 @@ var wot_api_reload =
 
 var wot_api_submit =
 {
-	send: function(pref, target, testimonies)
+	send: function(pref, target, testimonies, votes)
 	{
 		try {
 			if (!wot_util.isenabled() || !pref || !target ||
@@ -703,49 +703,47 @@ var wot_api_submit =
 
 			var nonce = wot_crypto.nonce();
 
-			var context = wot_arc4.create(wot_hash.hmac_sha1hex(
-								wot_prefs.witness_key, nonce));
+			var context = wot_arc4.create(wot_hash.hmac_sha1hex(wot_prefs.witness_key, nonce));
 
 			if (!context) {
 				return;
 			}
 
-			var crypted = wot_arc4.crypt(context,
-								wot_hash.strtobin(target));
+			var crypted = wot_arc4.crypt(context,wot_hash.strtobin(target));
 
-			if (!crypted) {
-				return;
-			}
+			if (!crypted) return;
 
 			var qs = WOT_SERVICE_API_SUBMIT +
 				"?id="		+ wot_prefs.witness_id +
 				"&nonce="	+ nonce +
-				"&target="	+ encodeURIComponent(btoa(
-									wot_hash.bintostr(crypted)));
+				"&target="	+ encodeURIComponent(btoa(wot_hash.bintostr(crypted)));
 
 			var found = 0;
 
-			for (var i = 0; i < WOT_APPLICATIONS; ++i) {
-				if (testimonies[i] >= 0) {
-					qs += "&testimony_" + i + "=" + testimonies[i];
+			for (var i = 0; i < WOT_COMPONENTS.length; ++i) {
+                var app = WOT_COMPONENTS[i];
+				if (testimonies[app] >= -1) {
+					qs += "&testimony_" + app + "=" + testimonies[app];
 					++found;
 				}
 			}
 
-			if (!found) {
-				return;
-			}
+            if (votes && votes.length > 0) {
+                qs += "&votes=" + votes;
+            }
+
+			if (!found) return;
 
 			qs += wot_url.getapiparams();
 			   
 			var request = new XMLHttpRequest();
 
-			if (!request) {
-				return;
-			}
+			if (!request) return;
 
-			request.open("GET", wot_core.wot_service_url() +
-					wot_crypto.authenticate_query(qs));
+            var url = wot_core.wot_service_url() + wot_crypto.authenticate_query(qs);
+            wdump("API Submit: " + url);
+
+			request.open("GET", url);
 
 			new wot_cookie_remover(request);
 
@@ -943,33 +941,38 @@ var wot_api_update =
 
 var wot_pending =
 {
-	store: function(name) /* host */
-	{
+	store: function(hostname) {
+        // Stores user's testimonies from memory cache to preferences (which is more persistent storage)
 		try {
-			if (!wot_cache.iscached(name) ||
-					!wot_cache.get(name, "pending")) {
+			if (!wot_cache.iscached(hostname) ||
+					!wot_cache.get(hostname, "pending")) {
 				return false;
 			}
 
-			var data = wot_idn.utftoidn(name);
+			var target = wot_idn.utftoidn(hostname),
+                obj = {};
 
-			if (!data) {
-				return false;
-			}
+			if (!target) return false;
+
+            obj.target = target;
 
-			for (var i = 0; i < WOT_APPLICATIONS; ++i) {
-				data += " " + wot_cache.get(name, "testimony_" + i);
+			for (var i = 0; i < WOT_COMPONENTS.length; ++i) {
+                var app = WOT_COMPONENTS[i];
+				obj["testimony_" + app] = wot_cache.get(hostname, "testimony_" + app);
 			}
 
-			var pref = Date.now();
+            obj.votes = wot_cache.get(hostname, "votes") || "";
 
-			if (wot_prefs.setChar("pending." + pref, data)) {
+			var pref_name = Date.now();
+
+			if (wot_prefs.setChar("pending." + pref_name, JSON.stringify(obj))) {
+                wdump("Stored in prefs: " + JSON.stringify(obj));
 				return true;
 			}
 
 			wot_prefs.flush();
 		} catch (e) {
-			dump("wot_pending.store: failed with " + e + "\n");
+			wdump("wot_pending.store: failed with " + e);
 		}
 
 		return false;
@@ -998,44 +1001,61 @@ var wot_pending =
 		}
 	},
 
-	parse: function(pref, data)
+	parse: function(pref, json_data)
 	{
 		try {
-			var m = /^([^\s]+)(.*)/.exec(data);
+//			var m = /^([^\s]+)(.*)/.exec(data);
+
+            var data = JSON.parse(json_data);
 
-			if (!m || !m[1] || !m[2]) {
-				dump("wot_pending.parse: invalid entry: " + pref + ": " +
-					data + "\n");
+			if (!data || !data.target) {
+				wdump("wot_pending.parse: invalid entry: " + pref + ": " + json_data);
 				this.clear(pref);
 				return null;
 			}
 
 			var rv = {
-				target: m[1],
-				testimonies: []
+				target: data.target,
+				testimonies: [],
+                votes: data.votes || "",    // categories' votes as a string
+                votes_list: {}              // parsed votes as an object
 			};
-			var values = m[2];
 
-			for (var i = 0; i < WOT_APPLICATIONS; ++i) {
-				m = /^\s*(-?\d+)(.*)/.exec(values);
+			for (var i = 0; i < WOT_COMPONENTS.length; ++i) {
+                var app = WOT_COMPONENTS[i],
+                    t = data["testimony_" + app];
 
-				if (!m || m[1] == null || Number(m[1]) < 0) {
-					rv.testimonies[i] = -1;
+				if (t === null || t < 0 || isNaN(t)) {
+					rv.testimonies[app] = -1;
 				} else {
-					rv.testimonies[i] = Number(m[1]);
+					rv.testimonies[app] = Number(t);
 
-					if (rv.testimonies[i] > WOT_MAX_REPUTATION) {
-						rv.testimonies[i] = WOT_MAX_REPUTATION;
+					if (rv.testimonies[app] > WOT_MAX_REPUTATION) {
+						rv.testimonies[app] = WOT_MAX_REPUTATION;
 					}
 				}
-
-				values = m[2];
 			}
 
-			dump("wot_pending.parse: " + pref + ": " + rv.target + "\n");
+//            // parsing string votes
+            // FIXME: use the code below for restoring user's votes from pending submission when cache doesn't have them
+//            if (data.votes && data.votes.length > 2) {
+//                var votes_array = data.votes.split("/");
+//                for (i = 0; i < votes_array.length; i++) {
+//                    var vv = votes_array[i];
+//                    if (vv && vv.length > 0) {
+//                        var v = vv.split(":", 2);
+//                        if (v && v.length == 2) {
+//                            rv.votes_list[v[0]] = { v: v[1] };
+//                        }
+//                    }
+//                }
+//            }
+
+			wdump("wot_pending.parse: " + pref + ": " + rv.target);
 			return rv;
+
 		} catch (e) {
-			dump("wot_pending.parse: failed with " + e + "\n");
+			wdump("wot_pending.parse: failed with " + e);
 		}
 
 		return null;
@@ -1055,17 +1075,15 @@ var wot_pending =
 				}
 
 				var base = "pending." + pref;
-				var data = wot_prefs.getChar(base, null);
+				var json_data = wot_prefs.getChar(base, null);
 
-				if (!data) {
-					continue;
-				}
+				if (!json_data) continue;
 
-				var submit = wot_prefs.getChar(base + ".submit", null);
+				var submit_time = wot_prefs.getChar(base + ".submit", null);
 
-				if (submit) {
-					submit = Date.now() - Number(submit);
-					if (submit < WOT_INTERVAL_SUBMIT_ERROR) {
+				if (submit_time) {
+					submit_time = Date.now() - Number(submit_time);
+					if (submit_time < WOT_INTERVAL_SUBMIT_ERROR) {
 						continue;
 					}
 				}
@@ -1087,30 +1105,31 @@ var wot_pending =
 					continue;
 				}
 
-				var parsed = this.parse(pref, data);
+				var parsed = this.parse(pref, json_data);
 
-				if (!parsed) {
-					continue;
-				}
+				if (!parsed) continue;
 
-				wot_api_submit.send(pref, parsed.target, parsed.testimonies);
+                wdump("API Submits Parsed: " + JSON.stringify(parsed));
 
-				if (!wot_cache.iscached(parsed.target) ||
-						wot_cache.get(parsed.target, "pending")) {
+                wot_api_submit.send(pref, parsed.target, parsed.testimonies, parsed.votes);
+
+				if (!wot_cache.iscached(parsed.target) || wot_cache.get(parsed.target, "pending")) {
 					continue;
 				}
 
-				for (var i = 0; i < WOT_APPLICATIONS; ++i) {
-					if (parsed.testimonies[i] < 0) {
-						continue;
-					}
-
-					wot_cache.set(parsed.target, "testimony_" + i,
-						parsed.testimonies[i]);
+                // Now update cache with user's input: testimonies first
+				for (i = 0; i < WOT_COMPONENTS.length; ++i) {
+                    var app = WOT_COMPONENTS[i];
+					wot_cache.set(parsed.target, "testimony_" + app, parsed.testimonies[app]);
 				}
+
+                // Categories
+                // TODO: implement restoring votes from parsed voted in case if votes were restored from "pending" data (i.e. not in cache yet)
+
+                wot_cache.set(parsed.target, "votes", parsed.votes); // FIXME: do we still need this here?
 			}
 		} catch (e) {
-			dump("wot_pending.submit: failed with " + e + "\n");
+			dump("wot_pending.submit: failed with " + e);
 		}
 	}
 };
diff --git a/content/cache.js b/content/cache.js
index 130fc6c..3b10574 100644
--- a/content/cache.js
+++ b/content/cache.js
@@ -256,8 +256,9 @@ var wot_cache =
 			this.set(name, "status", WOT_QUERY_RETRY);
 			this.set(name, "time", Date.now());
 			this.set(name, "normalized", "");
-			this.set(name, "categories", 0);
-			this.set(name, "blacklists", 0);
+			this.set(name, "cats", "");
+			this.set(name, "blacklists", "");
+			this.set(name, "votes", "");
 
 			// FIXME: don't create redundant apps. Use only 0 and 4
             for (var i = 0; i < WOT_APPLICATIONS; ++i) {
@@ -290,10 +291,12 @@ var wot_cache =
 			this.remove(name, "status");
 			this.remove(name, "time");
 			this.remove(name, "normalized");
-			this.remove(name, "categories");
+			this.remove(name, "cats");
 			this.remove(name, "blacklists");
+			this.remove(name, "votes");
 
-			for (var i = 0; i < WOT_APPLICATIONS; ++i) {
+			// FIXME: use WOT_COMPONTENTS here
+            for (var i = 0; i < WOT_APPLICATIONS; ++i) {
 				this.remove(name, "reputation_" + i);
 				this.remove(name, "confidence_" + i);
 				this.remove(name, "testimony_" + i);
@@ -351,8 +354,8 @@ var wot_cache =
 				this.set(name, "status", WOT_QUERY_OK);
 			}
 
-            var categories_number = 0,
-                blacklists_number = 0;
+            var blacklists = [], bl_object = {},
+                cats = {}, cat_object = {};
 
 			while (child) {
                 switch (child.localName) {
@@ -365,11 +368,13 @@ var wot_cache =
                         break;
 
                     case WOT_SERVICE_XML_QUERY_CATEGORY:
-                        categories_number += this.add_category(name, child, categories_number);
+                        cat_object = this.add_category(name, child);
+                        cats[cat_object.name] = cat_object;
                         break;
 
                     case WOT_SERVICE_XML_QUERY_BLACKLIST:
-                        blacklists_number += this.add_blacklist(name, child, blacklists_number);
+                        bl_object = this.add_blacklist(name, child);
+                        blacklists.push(bl_object);
                         break;
 
                     default:
@@ -379,8 +384,8 @@ var wot_cache =
                 child = child.nextSibling;
 			}
 
-            this.set(name, "categories", categories_number);
-            this.set(name, "blacklists", blacklists_number);
+            this.set(name, "cats", JSON.stringify(cats));
+            this.set(name, "blacklists", JSON.stringify(blacklists));
 
             // process GFeedbackLoop Question
             this.add_question(name, target.firstChild);
@@ -425,7 +430,10 @@ var wot_cache =
 
     },
 
-    process_attributes: function (attrs_list, hostname, node, slot_name, slot_index) {
+    process_attributes: function (attrs_list, hostname, node) {
+
+        var obj = {};
+
         for (var i = 0; i < attrs_list.length; i++) {
             var attr = attrs_list[i],
                 attr_node = node.attributes.getNamedItem(attr),
@@ -436,12 +444,15 @@ var wot_cache =
                 } else {
                     val = Number(attr_node.value);
                 }
-                this.set(hostname, slot_name + "_" + slot_index + "_" + attr, val);
+
+                obj[attr] = val;
             }
         }
+
+        return obj;
     },
 
-    add_category: function (hostname, node, index) {
+    add_category: function (hostname, node) {
         try {
             var attrs_list = [
                 WOT_SERVICE_XML_QUERY_CATEGORY_NAME,
@@ -451,31 +462,32 @@ var wot_cache =
                 WOT_SERVICE_XML_QUERY_CATEGORY_VOTE
             ];
 
-            this.process_attributes(attrs_list, hostname, node, "category", index);
+            var cat = this.process_attributes(attrs_list, hostname, node);
+            // small hack to comply with Chrome's add-on codebase
+            cat.v = cat.vote;
+            delete cat.vote;
+            cat.id = cat.name;
+            return cat;
 
         } catch (e) {
             wdump("ERROR: wot_cache.add_category: failed with " + e);
-            return 0;
+            return {};
         }
-
-        return 1;
     },
 
-    add_blacklist: function (hostname, node, index) {
+    add_blacklist: function (hostname, node) {
         try {
             var attrs_list = [
                 WOT_SERVICE_XML_QUERY_BLACKLIST_TYPE,
                 WOT_SERVICE_XML_QUERY_BLACKLIST_TIME
             ];
 
-            this.process_attributes(attrs_list, hostname, node, "blacklist", index);
+            return this.process_attributes(attrs_list, hostname, node);
 
         } catch (e) {
             wdump("ERROR: wot_cache.add_category: failed with " + e);
-            return 0;
+            return {};
         }
-
-        return 1;
     },
 
 	add_question: function (hostname, target_node)
diff --git a/content/categories.js b/content/categories.js
index 217b529..edc9344 100644
--- a/content/categories.js
+++ b/content/categories.js
@@ -178,34 +178,10 @@ var wot_categories = {
         // return categories reported by API server (both identified and votes) taking them from cache.
         // Result is an Object.
 
-        var count = wot_cache.get(target, "categories", 0),
-            cats = {},
-            attrs_list = [
-                WOT_SERVICE_XML_QUERY_CATEGORY_NAME,
-                WOT_SERVICE_XML_QUERY_CATEGORY_GROUP,
-                WOT_SERVICE_XML_QUERY_CATEGORY_C,
-                WOT_SERVICE_XML_QUERY_CATEGORY_I,
-                WOT_SERVICE_XML_QUERY_CATEGORY_VOTE
-            ];
-
-        for (var i = 0, slot=""; i < count; i++) {
-            var cat_obj = {}, val = null;
-            for (var a = 0; a < attrs_list.length; a++) {
-                slot = "category_" + i + "_" + attrs_list[a];
-                val = wot_cache.get(target, slot, null);
-                if (val !== null) {
-                    cat_obj[attrs_list[a]] = val;
-                }
-            }
-            if (!wot_util.isEmpty(cat_obj)) {
-                cat_obj['id'] = cat_obj.name;
-                cat_obj.v = cat_obj.vote;   // comply with Chrome's codebase
-                delete cat_obj.vote;
-                cats[cat_obj.name] = cat_obj;
-            }
-        }
+        var cats_json = wot_cache.get(target, "cats"),
+            cats = (cats_json && cats_json.length > 0) ? JSON.parse(cats_json) : {};
 
-//        wdump("target_categories:: " + JSON.stringify(cats));
+        wdump("target_categories:: " + JSON.stringify(cats));
         return cats;
     },
 
@@ -213,32 +189,39 @@ var wot_categories = {
         // return categories reported by API server (both identified and votes) taking them from cache.
         // Result is an Object.
 
-        var count = wot_cache.get(target, "blacklists", 0),
-            bls = [],
-            attrs_list = [
-                WOT_SERVICE_XML_QUERY_BLACKLIST_TYPE,
-                WOT_SERVICE_XML_QUERY_BLACKLIST_TIME
-            ];
-
-        for (var i = 0, slot=""; i < count; i++) {
-            var obj = {}, val = null;
-            for (var a = 0; a < attrs_list.length; a++) {
-                slot = "blacklist_" + i + "_" + attrs_list[a];
-                val = wot_cache.get(target, slot, null);
-                if (val !== null) {
-                    obj[attrs_list[a]] = val;
-                }
-            }
-            if (!wot_util.isEmpty(obj)) {
-                bls.push(obj);
-            }
-        }
+        var bl_json = wot_cache.get(target, "blacklists"),
+            bls = (bl_json && bl_json.length > 0) ? JSON.parse(bl_json) : [];
 
-//        wdump("target_categories:: " + JSON.stringify(cats));
+        wdump("target_blacklists:: " + JSON.stringify(bls));
         return bls;
 
     },
 
+    cache_categories: function (target, categories) {
+
+        // remove unvoted and non-identified categories from cached cats
+        var new_cats = {};
+
+        for (var cid in categories) {
+            var cat = categories[cid];
+            if ((cat.v == 0 || cat.v === null) && !cat.c) {
+                continue;   // skip unvoted and not-identified
+            }
+
+            // clean categories object from unnesessary data
+            // FIXME: this requires cloning of the object before doing removals
+//            cat.description = undefined;
+//            cat.viewdescription = undefined;
+//            cat.text = undefined;
+//            cat.shorttext = undefined;
+
+            // copy category to new list
+            new_cats[cid] = cat;
+        }
+
+        wot_cache.set(target, "cats", JSON.stringify(new_cats));
+    },
+
     select_identified: function (target_cats) {
         // Returns categories identified by community (unsorted!)
         var res = {};
diff --git a/content/commands.js b/content/commands.js
index 1ebad9d..c5ae8fd 100644
--- a/content/commands.js
+++ b/content/commands.js
@@ -208,12 +208,12 @@ var wot_commands =
 	{
 		try {
 			if (wot_cache.isok(wot_core.hostname)) {
-				wot_cache.set(wot_core.hostname, "testimony_0",
-					Number(value));
-				wot_cache.set(wot_core.hostname, "pending", true);
-				wot_core.pending[wot_core.hostname] = true;
-				wot_core.update();
-			}
+                wot_cache.set(wot_core.hostname, "testimony_0",
+                    Number(value));
+                wot_cache.set(wot_core.hostname, "pending", true);
+                wot_core.pending[wot_core.hostname] = true;
+                wot_core.update();
+            }
 		} catch (e) {
 			dump("wot_commands.quicktestify: failed with " + e + "\n");
 		}
@@ -233,42 +233,42 @@ wot_modules.push({ name: "wot_commands", obj: wot_commands });
 
 var wot_events =
 {
-	testimonydown: -1,
-
-	get_slider_pos: function(event, testimony)
-	{
-		var pos = -1;
-		try {
-			var slider = document.getElementById("wot-rating-" +
-				testimony + "-slider");
-			var sld_rule = wot_css.getstyle(WOT_STYLESHEET,
-				".wot-rating-slider");
-
-			if (!slider || !sld_rule) {
-				return pos;
-			}
+//	testimonydown: -1,
 
-			/* Calculate testimony value */
-			var sld_w = wot_css.getstyle_numeric(sld_rule, "width");
-
-			pos = WOT_MAX_REPUTATION * (event.screenX -
-						slider.boxObject.screenX) / sld_w;
-
-			/* Limit to a valid range */
-			if (pos > WOT_MAX_REPUTATION) {
-				pos = WOT_MAX_REPUTATION;
-			} else if (pos < 0) {
-				pos = 0;
-			}
-
-			/* Round */
-			pos = (pos / WOT_TESTIMONY_ROUND).toFixed() * WOT_TESTIMONY_ROUND;
-		} catch (e) {
-			dump("wot_events.get_slider_pos: failed with " + e + "\n");
-			pos = -1;
-		}
-		return pos;
-	},
+//	get_slider_pos: function(event, testimony)
+//	{
+//		var pos = -1;
+//		try {
+//			var slider = document.getElementById("wot-rating-" +
+//				testimony + "-slider");
+//			var sld_rule = wot_css.getstyle(WOT_STYLESHEET,
+//				".wot-rating-slider");
+//
+//			if (!slider || !sld_rule) {
+//				return pos;
+//			}
+//
+//			/* Calculate testimony value */
+//			var sld_w = wot_css.getstyle_numeric(sld_rule, "width");
+//
+//			pos = WOT_MAX_REPUTATION * (event.screenX -
+//						slider.boxObject.screenX) / sld_w;
+//
+//			/* Limit to a valid range */
+//			if (pos > WOT_MAX_REPUTATION) {
+//				pos = WOT_MAX_REPUTATION;
+//			} else if (pos < 0) {
+//				pos = 0;
+//			}
+//
+//			/* Round */
+//			pos = (pos / WOT_TESTIMONY_ROUND).toFixed() * WOT_TESTIMONY_ROUND;
+//		} catch (e) {
+//			dump("wot_events.get_slider_pos: failed with " + e + "\n");
+//			pos = -1;
+//		}
+//		return pos;
+//	},
 
 //	/* Handles testimony slider events and updates the new pending testimony to
 //	   query cache */
diff --git a/content/ratingwindow.js b/content/ratingwindow.js
index d235b25..8bbc209 100644
--- a/content/ratingwindow.js
+++ b/content/ratingwindow.js
@@ -67,6 +67,9 @@ var wot_rw = {
         try {
             wot_rw.unseenmessage();
 
+            var rw_wot = wot_rw.get_rw_wot();
+            rw_wot.ratingwindow.finishstate(true);  // finish state with unload = true to indicate the unintentional closing of RW
+
             /* Stores any pending testimonies */
             wot_core.update();
         } catch (e) {
@@ -225,6 +228,45 @@ var wot_rw = {
         }
     },
 
+    on_submit: function (data) {
+        wdump("RW: on_submit() " + JSON.stringify(data));
+
+        if (!data || !data.params) {
+            wdump("on_submit() received empty data to submit.");
+            return;
+        }
+
+        var target = data.target,
+            testimony_0 = data.params.testimony_0,
+            testimony_4 = data.params.testimony_4,
+            votes = data.params.votes,
+            changed_votes = data.params.changed_votes;   // as object
+
+        if (target && wot_cache.isok(target)) {
+            wot_cache.set(target, "testimony_0", Number(testimony_0));
+            wot_cache.set(target, "testimony_4", Number(testimony_4));
+            wot_cache.set(target, "votes", votes);  // votes as a string ready to submit. Real cache update should be done later.
+
+            // Update cached categories with user's votes
+            var cached_categories = wot_categories.target_categories(target);
+            for (var cid in changed_votes) {
+                if (cached_categories[cid]) {
+                    // if category is assigned to the site already just update the vote
+                    cached_categories[cid].v = changed_votes[cid];
+                } else {
+                    // if cat is not assigned yet do it now
+                    cached_categories[cid] = wot_categories.get_category(cid);
+                    cached_categories[cid].v = changed_votes[cid];
+                }
+            }
+            wot_categories.cache_categories(target, cached_categories); // put it back to the cache
+
+            wot_cache.set(target, "pending", true);
+            wot_core.pending[target] = true;
+            wot_core.update();
+        }
+    },
+
     on_ratingwindow_event: function (event) {
         try {
             var details = event.detail;
@@ -268,6 +310,10 @@ var wot_rw = {
                 case "close":
                     wot_rw.hide_ratingwindow();
                     break;
+
+                case "submit":
+                    wot_rw.on_submit(data);
+                    break;
             }
 
             return true;
diff --git a/content/rw/proxies.js b/content/rw/proxies.js
index 53c2019..bb6c769 100644
--- a/content/rw/proxies.js
+++ b/content/rw/proxies.js
@@ -109,8 +109,8 @@ var wot_bg = {    // background page object
 
         api: {
 
-            submit: function () {
-
+            submit: function (target, params) {
+                wot_bg.wot.core.moz_send("submit", { target: target, params: params });
             },
 
             comments: {
@@ -131,6 +131,36 @@ var wot_bg = {    // background page object
 
         },
 
+        cache: {
+            cacheratingstate: function (target, state, votes) {
+                // Detects whether testimonies where changed.
+                // This function doesn't store anything in the Cache as against Chrome implementation.
+
+                var changed = false,
+                    obj = wot.ratingwindow.getcached();
+
+                wot.components.forEach(function(item) {
+                    if (state[item.name]) {
+                        obj.value[item.name] = obj.value[item.name] || {};
+
+                        if (obj.value[item.name].t != state[item.name].t) {
+//                            obj.value[item.name].t  = state[item.name].t;
+                            changed = true;
+                            return false;   // exit the cycle
+                        }
+                    }
+                });
+
+                return changed;
+
+            },
+
+            setflags: function (target, flags) {
+                // {warned: true, warned_expire: warned_expire }
+                // TODO: implement sending flags to BG cache
+            }
+        },
+
         ga: {}  // this object is replaced on every chrome.extension.getBackgroundPage() call
     },
 
@@ -140,16 +170,19 @@ var wot_bg = {    // background page object
             if (window.console && window.console.log) {
                 window.console.log("LOG: " + arguments[1] + " , " + arguments[2] + " , " + arguments[3]);
             }
+            wot_bg.wot.core.moz_send("log", { args: arguments });
         },
         warn: function (args) {
             if (window.console && window.console.log) {
                 window.console.log("WARN: " + arguments[1] + " , " + arguments[2] + " , " + arguments[3]);
             }
+            wot_bg.wot.core.moz_send("log", { args: arguments });
         },
         error: function (args) {
             if (window.console && window.console.log) {
                 window.console.log("ERROR: " + arguments[1] + " , " + arguments[2] + " , " + arguments[3]);
             }
+            wot_bg.wot.core.moz_send("log", { args: arguments });
         }
     }
 
diff --git a/content/rw/ratingwindow.html b/content/rw/ratingwindow.html
index 34b2df7..5787705 100644
--- a/content/rw/ratingwindow.html
+++ b/content/rw/ratingwindow.html
@@ -113,15 +113,15 @@
                 </div>
             </div>
 
-                    <div class="user-comm-activity">
-                        <div id="wot-user-0-header" class="wot-user-header"></div>
-                        <div id="user-activityscore"></div>
-                        <div id="wot-user-0-notice" class="wot-user-notice"></div>
-                    </div>
+            <div class="user-comm-activity">
+                <div id="wot-user-0-header" class="wot-user-header"></div>
+                <div id="user-activityscore"></div>
+                <div id="wot-user-0-notice" class="wot-user-notice"></div>
+            </div>
 
             <div id="rated-votes">
                 <div id="voted-categories">
-                        <div id="voted-categories-content"></div>
+                    <div id="voted-categories-content"></div>
                 </div>
                 <div id="change-ratings"></div>
             </div>
diff --git a/content/rw/ratingwindow.js b/content/rw/ratingwindow.js
index b5ae92c..251b567 100644
--- a/content/rw/ratingwindow.js
+++ b/content/rw/ratingwindow.js
@@ -59,13 +59,16 @@ $.extend(wot, { ratingwindow: {
 
     updatestate: function(target, data)
     {
+        var _this = wot.ratingwindow;
         /* initialize on target change */
-        if (this.state.target != target) {
-            this.finishstate(false);
-            this.state = { target: target, down: -1 };
+        if (_this.state.target != target) {
+            _this.finishstate(false);
+            _this.state = { target: target, down: -1 };
         }
 
-        var state = {};
+        var state = {
+            target: target
+        };
 
         /* add existing ratings to state */
         if (data && data.status == wot.cachestatus.ok) {
@@ -75,23 +78,20 @@ $.extend(wot, { ratingwindow: {
 
                 if (datav && datav.t >= 0) {
                     state[item.name] = { t: datav.t };
+                } else {
+                    state[item.name] = { t: -1 };
                 }
             });
         }
 
         /* remember previous state */
-        this.state = $.extend(state, this.state);
+        _this.state = $.extend(_this.state, state);
     },
 
     setstate: function (component, t) {
         // This only changes the user's testimonies' state
-        var new_value = {};
-        if (t >= 0) {
-            new_value = { t: parseInt(t) };
-        } else {
-            new_value = { t: -1 };
-        }
-
+        var new_value = { name: component };
+        new_value.t = t >= 0 ? parseInt(t) : -1;
         this.state[component] = new_value;
         this.update_catsel_state();
     },
@@ -247,7 +247,9 @@ $.extend(wot, { ratingwindow: {
                 });
 
                 if (votes_changed) {
-                    params.votes = rw._make_votes(changed_votes);
+                    params.votes = rw._make_votes(changed_votes);   // diff of votes
+                    params._user_votes = votes;                     // all user's votes
+                    params.changed_votes = changed_votes;
                 }
 
                 bgwot.api.submit(target, params);
@@ -373,19 +375,20 @@ $.extend(wot, { ratingwindow: {
     updatecontents: function()
     {
         var bg = chrome.extension.getBackgroundPage();
-        var cached = this.getcached(),
+        var _this = wot.ratingwindow,
+            cached = _this.getcached(),
             visible_hostname = "",
             rw_title = "";
 
         /* update current rating state */
-        this.updatestate(this.current.target, cached);
-        var normalized_target = cached.value.normalized ? cached.value.normalized : this.current.target;
+        _this.updatestate(_this.current.target, cached);
+        var normalized_target = cached.value.normalized ? cached.value.normalized : _this.current.target;
 
         var $_hostname = $("#hostname-text"),
             $_wot_title_text = $("#wot-title-text");
 
         /* target */
-        if (this.current.target && cached.status == wot.cachestatus.ok) {
+        if (_this.current.target && cached.status == wot.cachestatus.ok) {
             visible_hostname = bg.wot.url.decodehostname(normalized_target);
             rw_title = wot.i18n("messages", "ready");
         } else if (cached.status == wot.cachestatus.busy) {
@@ -393,7 +396,7 @@ $.extend(wot, { ratingwindow: {
         } else if (cached.status == wot.cachestatus.error) {
             rw_title = wot.i18n("messages", "failed");
         } else {
-            rw_title = wot.i18n("messages", this.current.status || "notavailable");
+            rw_title = wot.i18n("messages", _this.current.status || "notavailable");
         }
 
         $_hostname.text(visible_hostname);
@@ -420,8 +423,8 @@ $.extend(wot, { ratingwindow: {
             $_rep_legend.attr("r", rep_level);
             $_rep_legend.text(wot.get_level_label(item.name, rep_level, false));
 
+            _this.rate_control.updateratings({ name: item.name, t: cachedv.t }); // update visual ratingbars
         });
-        this.rate_control.updateratings();
 
         /* message */
 
@@ -520,9 +523,9 @@ $.extend(wot, { ratingwindow: {
             // delete categories from the visible area
             _rw.insert_categories({}, $_tr_list);
 
-            if (this.current.target && cached.status == wot.cachestatus.ok && cached.value) {
+            if (_rw.current.target && cached.status == wot.cachestatus.ok && cached.value) {
                 var cats = cached.value.cats;
-                if (cats != null) {
+                if (!wot.utils.isEmptyObject(cats)) {
                     var sorted = wot.rearrange_categories(wot.select_identified(cats));    // sort categories and split into two parts (TR, CS)
                     _rw.insert_categories(sorted.all, $_tr_list);
                 }
@@ -536,7 +539,6 @@ $.extend(wot, { ratingwindow: {
     {
         var _rw = wot.ratingwindow;
         try {
-            console.log(target, data);
             data = JSON.parse(data);    // for safety
             _rw.current = data || {};
             _rw.updatecontents();
@@ -551,6 +553,7 @@ $.extend(wot, { ratingwindow: {
             }
 
             _rw.modes.reset();
+            _rw.cat_selector.init_voted();
             _rw.modes.auto();
         } catch (e) {
             console.log("ratingwindow.update: failed with ", e);
@@ -776,7 +779,6 @@ $.extend(wot, { ratingwindow: {
         } else {
             // try to get user's votes from cache (server response)
             voted = wot.select_voted(_rw.getcached().value.cats);
-            console.log(JSON.stringify(_rw.getcached().value.cats));
             for(cat in voted) {
                 if (voted[cat].v == 1) {
                     up_voted.push(_rw.build_voted_category_html(wot.get_category(cat), voted[cat].v));
@@ -1128,6 +1130,7 @@ $.extend(wot, { ratingwindow: {
         _rw.comments.update_hint();
 
         wot.ratingwindow.finishstate(false);
+        bg.wot.core.moz_send("update", { update_rw: true });    // force to update RW with newest state data
         _rw.modes.auto();   // switch RW mode according to current state
     },
 
@@ -1142,14 +1145,17 @@ $.extend(wot, { ratingwindow: {
             var a = item.name;
             var t = (cached.value[a] && cached.value[a].t !== undefined) ? cached.value[a].t : -1;
             if (_rw.state[a]) {
+                _rw.state[a].name = a;  // stupid hack
                 _rw.state[a].t = t;
             } else {
-                _rw.state[a] = { t: t };
+                _rw.state[a] = { name: a, t: t };
             }
+            _rw.rate_control.updateratings(_rw.state[a]);  // restore user's testimonies visually
         });
 
+        _rw.get_bg().console.log(_rw.state);
+
         _rw.cat_selector.init_voted(); // restore previous votes
-        _rw.rate_control.updateratings(_rw.state);  // restore user's testimonies visually
 
         bg.wot.keeper.remove_comment(_rw.state.target); // remove locally saved comment
         _rw.update_comment(cached, null); // restore comment to server-side version
@@ -1165,6 +1171,7 @@ $.extend(wot, { ratingwindow: {
         var _rw = wot.ratingwindow;
         wot.ratingwindow.finishstate(false);
         if (_rw.delete_action) {
+            _rw.get_bg().wot.core.update(true);
             _rw.modes.auto();   // switch RW mode according to current state
         } else {
             _rw.modes.thanks.activate();
@@ -1269,13 +1276,12 @@ $.extend(wot, { ratingwindow: {
         updateratings: function(state)
         {
             /* indicator state */
-            state = state || {};
-
             var _rw = wot.ratingwindow;
+            state = state || {};
 
             /* update each component */
             wot.components.forEach(function(item) {
-                if (state.name != null && state.name != item.name) {
+                if (state.name !== null && state.name != item.name) {
                     return;
                 }
 
@@ -1289,7 +1295,7 @@ $.extend(wot, { ratingwindow: {
                     elems[elem] = $("#wot-rating-" + item.name + "-" + elem);
                 });
 
-                t = (wrs && wrs.t != null) ? wrs.t : t;
+                t = (wrs && wrs.t !== null) ? wrs.t : t;
 
                 if (t >= 0) {
                     /* rating */
@@ -1320,7 +1326,7 @@ $.extend(wot, { ratingwindow: {
                     elems.indicator.attr("r", rep);
                     elems.data.attr("r", rep);
                 }
-                }
+            }
 
                 var helptext = wot.get_level_label(item.name, rep, true);
 
@@ -1363,9 +1369,9 @@ $.extend(wot, { ratingwindow: {
 
             activate: function () {
                 if (!wot.ratingwindow.modes._activate("rated")) return false;
-                console.log("Rated");
+                wot_bg.console.log("Rated");
                 wot.ratingwindow.update_uservoted();
-                console.log("update_uservoted");
+                wot_bg.console.log("update_uservoted");
                 return true;
             }
         },

-- 
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