add usersnet delete when friendship set to 'unknown'
authorMatteo Nastasi (mop) <nastasi@alternativeoutput.it>
Mon, 14 Dec 2015 17:38:28 +0000 (18:38 +0100)
committerMatteo Nastasi (mop) <nastasi@alternativeoutput.it>
Mon, 14 Dec 2015 17:38:28 +0000 (18:38 +0100)
TODO.txt
web/Obj/brisk.phh
web/Obj/dbase_base.phh
web/Obj/dbase_pgsql.phh
web/Obj/user.phh

index 3fe465b..3751b46 100644 (file)
--- a/TODO.txt
+++ b/TODO.txt
@@ -21,7 +21,7 @@
      DONE - sql
      DONE - dbase API
      DONE - add apprentices tables
-     - remove entry when set unknown
+     DONE - remove entry when set unknown
      DONE - enhance info interface
        DONE . SQL to show current evaluation
        . disable parts of interface
index 6d4096c..4592179 100644 (file)
@@ -1751,6 +1751,7 @@ class Brisk
       $mesg = "";
       $user_code = -1;
       $ret = 0;
+      $subret = 0;
 
       do {
           if (($json = json_decode($json_s)) == FALSE) {
@@ -1763,8 +1764,8 @@ class Brisk
               break;
           }
 
-          if (($user_item = $bdb->usersnet_save($user->code, $json)) != 0) {
-              $ret = 4000 + $user_item;
+          if (($subret = $bdb->usersnet_save($user->code, $json)) != 0) {
+              $ret = 4000 + $subret;
               break;
           }
       } while(0);
index 85d9315..66c998f 100644 (file)
@@ -182,9 +182,10 @@ class MailDBItem {
     }
 }
 
-define('USERSNET_FRIEND', 1);
-define('USERSNET_SKILL', 1);
-define('USERSNET_TRUST', 2);
+define('USERSNET_DEF_FRIEND', 2);
+define('USERSNET_DEF_SKILL', 2);
+define('USERSNET_DEF_TRUST', 2);
+
 class UsersNetItem {
     var $owner;
     var $target;
@@ -219,8 +220,8 @@ class UsersNetItem {
 
     static function UsersNetItemDefaults($owner, $target, $widefriend, $narrowfriend)
     {
-        $ret = new UsersNetItem($owner, $target, USERSNET_FRIEND,
-                                USERSNET_SKILL, USERSNET_TRUST,
+        $ret = new UsersNetItem($owner, $target, USERSNET_DEF_FRIEND,
+                                USERSNET_DEF_SKILL, USERSNET_DEF_TRUST,
                                 $widefriend, $narrowfriend, FALSE);
 
         return ($ret);
index c01bac9..48ca78e 100644 (file)
@@ -210,10 +210,6 @@ class BriskDB
             return (FALSE);
         }
 
-        if (($sere_pg = $this->query($sere_sql)) == FALSE) {
-            return (FALSE);
-        }
-
         $sere_sql = sprintf("SELECT * FROM %sselfreg_chk WHERE (ip & %d) = %d;",
                             $G_dbpfx, int2four($G_selfreg_mask), int2four($laddr & $G_selfreg_mask));
         if (($sere_pg  = $this->query($sere_sql)) == FALSE) {
@@ -990,44 +986,58 @@ INSERT INTO %smails (code, ucode, type, tstamp, subj, body_txt, body_htm, hash)
             $this->transaction('BEGIN');
             $trans = TRUE;
 
-            // try to update
-            $net_sql = sprintf("
+            if ($friend == USERSNET_FRIEND_UNKNOWN) {
+               // try to update
+                $net_sql = sprintf("
+                    DELETE FROM %susersnet
+                        USING %susers as us
+                        WHERE owner = %d AND us.login = '%s' AND target = us.code;",
+                                   $G_dbpfx, $G_dbpfx,
+                                   $owner_id, escsql($json->login));
+
+                if (($net_pg = $this->query($net_sql)) == FALSE) {
+                    $ret = 5;
+                    break;
+                }
+             }
+            else { // if ($friend == USERSNET_FRIEND_UNKNOWN
+                // try to update
+                $net_sql = sprintf("
                     UPDATE %susersnet SET (friend, skill, trust, mtime) =
                         (%d, %d, %d, now())
                         FROM %susers as us
                         WHERE owner = %d AND us.login = '%s' AND target = us.code RETURNING *;",
-                               $G_dbpfx,
-                               $friend, $json->skill, $json->trust,
-                               $G_dbpfx,
-                               $owner_id, escsql($json->login));
-            if (($net_pg  = $this->query($net_sql)) == FALSE || pg_numrows($net_pg) == 0) {
-                $net_sql = sprintf("
+                                   $G_dbpfx,
+                                   $friend, $json->skill, $json->trust,
+                                   $G_dbpfx,
+                                   $owner_id, escsql($json->login));
+                if (($net_pg  = $this->query($net_sql)) == FALSE || pg_numrows($net_pg) == 0) {
+                    $net_sql = sprintf("
                 INSERT INTO %susersnet SELECT %d AS owner, us.code as target,
                         %d as friend, %d as skill, %d as trust
                     FROM %susers as us WHERE us.login = '%s' RETURNING *;",
-                                   $G_dbpfx, $owner_id,
-                                   $friend, $json->skill, $json->trust,
-                                   $G_dbpfx, $json->login);
-                if (($net_pg  = $this->query($net_sql)) == FALSE) {
-                    log_wr('insert query failed');
-                    $ret = 2;
-                    break;
-                }
-                if (pg_numrows($net_pg) != 1) {
-                    log_wr(sprintf('insert numrow failed [%s] [%d]', $net_sql, pg_numrows($net_pg)));
-                    $ret = 3;
-                    break;
+                                       $G_dbpfx, $owner_id,
+                                       $friend, $json->skill, $json->trust,
+                                       $G_dbpfx, $json->login);
+                    if (($net_pg  = $this->query($net_sql)) == FALSE) {
+                        log_wr('insert query failed');
+                        $ret = 2;
+                        break;
+                    }
+                    if (pg_numrows($net_pg) != 1) {
+                        log_wr(sprintf('insert numrow failed [%s] [%d]', $net_sql, pg_numrows($net_pg)));
+                        $ret = 3;
+                        break;
+                    }
                 }
-            }
-            else {
-                if (pg_numrows($net_pg) != 1) {
-                    log_wr('update numrow failed');
-                    $ret = 4;
-                    break;
+                else {
+                    if (pg_numrows($net_pg) != 1) {
+                        log_wr('update numrow failed');
+                        $ret = 4;
+                        break;
+                    }
                 }
             }
-
-            $net_obj = pg_fetch_object($net_pg, 0);
             $this->transaction('COMMIT');
             return (0);
         } while (0);
index fde4a57..d8230b4 100644 (file)
@@ -923,6 +923,12 @@ function chunked_fini()
 
 } // end class User
 
+define('USERSNET_FRIEND_BLACK', 1);
+define('USERSNET_FRIEND_UNKNOWN', 2);
+define('USERSNET_FRIEND_TEST', 3);
+define('USERSNET_FRIEND_FRIEND', 4);
+define('USERSNET_FRIEND_BFF', 5);
+
 if (!isset($__usersnet_friend_map)) {
     $__usersnet_friend_map = array("black", "unknown", "test", "friend", "bff");
 }