]> git.mxchange.org Git - friendica-addons.git/commitdiff
jappixmini: autosubscribe only when contacts list changed
authorLeberwurscht <leberwurscht@hoegners.de>
Tue, 17 Apr 2012 19:18:11 +0000 (21:18 +0200)
committerLeberwurscht <leberwurscht@hoegners.de>
Tue, 17 Apr 2012 19:18:11 +0000 (21:18 +0200)
jappixmini/jappixmini.php
jappixmini/lib.js

index 6840c1d991d40352c6b44341600a06e2eef71358..4c3ebcc47f1b364064dee3ad210f3d6234b341f0 100644 (file)
@@ -458,6 +458,7 @@ function jappixmini_script(&$a,&$s) {
        $contacts[$address] = $name;
     }
     $contacts_json = json_encode($contacts);
+    $contacts_hash = sha1($contacts_json);
 
     // get nickname
     $r = q("SELECT `username` FROM `user` WHERE `uid`=$uid");
@@ -466,7 +467,7 @@ function jappixmini_script(&$a,&$s) {
     // add javascript to start Jappix Mini
     $a->page['htmlhead'] .= "<script type=\"text/javascript\">
         jQuery(document).ready(function() {
-           jappixmini_addon_start('$server', '$username', '$proxy', '$bosh', $encrypt, '$password', $nickname, $contacts_json, $autoapprove, $autosubscribe);
+           jappixmini_addon_start('$server', '$username', '$proxy', '$bosh', $encrypt, '$password', $nickname, $contacts_json, '$contacts_hash', $autoapprove, $autosubscribe);
         });
     </script>";
 
index 1f2586b357ef61a9e145f3bc467c7780415472df..6b7031e6bc6bab8e723760f175181f5ae00318a3 100644 (file)
@@ -91,7 +91,7 @@ function jappixmini_addon_decrypt_password(encrypted_password, callback) {
        });
 }
 
-function jappixmini_manage_roster(contacts, autoapprove, autosubscribe) {
+function jappixmini_manage_roster(contacts, contacts_hash, autoapprove, autosubscribe) {
        // listen for subscriptions
        con.registerHandler('presence',function(presence){
                var type = presence.getType();
@@ -137,6 +137,10 @@ function jappixmini_manage_roster(contacts, autoapprove, autosubscribe) {
        // autosubscribe
        if (!autosubscribe) return;
 
+       stored_hash = getPersistent("jappix-mini", "contacts-hash");
+       contacts_changed = (stored_hash != contacts_hash); // stored_hash gets updated later if everything was successful
+       if (!contacts_changed) return;
+
        console.log("Start autosubscribe.");
 
        var get_roster = new JSJaCIQ();
@@ -202,6 +206,8 @@ function jappixmini_manage_roster(contacts, autoapprove, autosubscribe) {
                        con.send(iq);
                        console.log("Added "+xid+" to roster.");
                }
+
+               setPersistent("jappix-mini", "contacts-hash", contacts_hash);
                console.log("Autosubscribe done.");
        });
 
@@ -217,7 +223,7 @@ function jappixmini_addon_subscribe() {
        sendSubscribe(xid, "subscribe");
 }
 
-function jappixmini_addon_start(server, username, proxy, bosh, encrypted, password, nickname, contacts, autoapprove, autosubscribe) {
+function jappixmini_addon_start(server, username, proxy, bosh, encrypted, password, nickname, contacts, contacts_hash, autoapprove, autosubscribe) {
     handler = function(password){
         // check if settings have changed, reinitialize jappix mini if this is the case
         settings_identifier = str_sha1(server);
@@ -227,15 +233,19 @@ function jappixmini_addon_start(server, username, proxy, bosh, encrypted, passwo
         settings_identifier += str_sha1(password);
         settings_identifier += str_sha1(nickname);
 
-        saved_identifier = getDB("jappix-mini", "settings_identifier");
-        if (saved_identifier != settings_identifier) removeDB('jappix-mini', 'dom');
-        setDB("jappix-mini", "settings_identifier", settings_identifier);
+        saved_identifier = getDB("jappix-mini", "settings-identifier");
+        if (saved_identifier != settings_identifier) {
+            disconnectMini();
+            removeDB('jappix-mini', 'dom');
+            removePersistent("jappix-mini", "contacts-hash");
+        }
+        setDB("jappix-mini", "settings-identifier", settings_identifier);
 
-        // set HOST_BOSH
-       if (proxy)
-               HOST_BOSH = proxy+"?host_bosh="+encodeURI(bosh);
-       else
-               HOST_BOSH = bosh;
+       // set HOST_BOSH
+       if (proxy)
+           HOST_BOSH = proxy+"?host_bosh="+encodeURI(bosh);
+       else
+           HOST_BOSH = bosh;
 
         // start jappix mini
         MINI_NICKNAME = nickname;
@@ -246,7 +256,7 @@ function jappixmini_addon_start(server, username, proxy, bosh, encrypted, passwo
         priority = 101;
         presenceMini(null,null,priority);
 
-        jappixmini_manage_roster(contacts, autoapprove, autosubscribe)
+        jappixmini_manage_roster(contacts, contacts_hash, autoapprove, autosubscribe)
     }
 
     // decrypt password if necessary