From: Mikael Nordfeldth <mmn@hethane.se>
Date: Sun, 14 Feb 2016 19:46:13 +0000 (+0100)
Subject: Use NoticeStream::filterVerbs for filtering in noticestreams
X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=e2a090c9cc796bc5116972a5e3aae2af9e391993;p=quix0rs-gnu-social.git

Use NoticeStream::filterVerbs for filtering in noticestreams
---

diff --git a/lib/conversationnoticestream.php b/lib/conversationnoticestream.php
index 9c32159d42..21b2d7f0be 100644
--- a/lib/conversationnoticestream.php
+++ b/lib/conversationnoticestream.php
@@ -28,11 +28,7 @@
  * @link      http://status.net/
  */
 
-if (!defined('STATUSNET')) {
-    // This check helps protect against security problems;
-    // your code file can't be executed directly from the web.
-    exit(1);
-}
+if (!defined('GNUSOCIAL')) { exit(1); }
 
 /**
  * Notice stream for a conversation
@@ -96,9 +92,7 @@ class RawConversationNoticeStream extends NoticeStream
             $notice->limit($offset, $limit);
         }
 
-        if (!empty($this->selectVerbs)) {
-            $notice->whereAddIn('verb', $this->selectVerbs, $notice->columnType('verb'));
-        }
+        self::filterVerbs($notice, $this->selectVerbs);
 
         // ORDER BY
         // currently imitates the previously used "_reverseChron" sorting
diff --git a/lib/inboxnoticestream.php b/lib/inboxnoticestream.php
index 496fe0d05d..3609f81ed3 100644
--- a/lib/inboxnoticestream.php
+++ b/lib/inboxnoticestream.php
@@ -30,7 +30,7 @@
  * @link      http://status.net/
  */
 
-if (!defined('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); }
+if (!defined('GNUSOCIAL')) { exit(1); }
 
 /**
  * Stream of notices for a profile's "all" feed
@@ -77,6 +77,8 @@ class RawInboxNoticeStream extends NoticeStream
     protected $target  = null;
     protected $inbox = null;
 
+    protected $selectVerbs = array();
+
     /**
      * Constructor
      *
@@ -84,8 +86,8 @@ class RawInboxNoticeStream extends NoticeStream
      */
     function __construct(Profile $target)
     {
+        parent::__construct();
         $this->target  = $target;
-        $this->unselectVerbs = array(ActivityVerb::DELETE);
     }
 
     /**
@@ -119,12 +121,9 @@ class RawInboxNoticeStream extends NoticeStream
         if (!empty($max_id)) {
             $notice->whereAdd(sprintf('notice.id <= %d', $max_id));
         }
-        if (!empty($this->selectVerbs)) {
-            $notice->whereAddIn('verb', $this->selectVerbs, $notice->columnType('verb'));
-        }
-        if (!empty($this->unselectVerbs)) {
-            $notice->whereAddIn('!verb', $this->unselectVerbs, $notice->columnType('verb'));
-        }
+
+        self::filterVerbs($notice, $this->selectVerbs);
+
         $notice->limit($offset, $limit);
         // notice.id will give us even really old posts, which were
         // recently imported. For example if a remote instance had
diff --git a/lib/networkpublicnoticestream.php b/lib/networkpublicnoticestream.php
index 3320b7cd5a..9a10c28988 100644
--- a/lib/networkpublicnoticestream.php
+++ b/lib/networkpublicnoticestream.php
@@ -46,9 +46,7 @@ class RawNetworkPublicNoticeStream extends NoticeStream
         Notice::addWhereSinceId($notice, $since_id);
         Notice::addWhereMaxId($notice, $max_id);
 
-        if (!empty($this->selectVerbs)) {
-            $notice->whereAddIn('verb', $this->selectVerbs, $notice->columnType('verb'));
-        }
+        self::filterVerbs($notice, $this->selectVerbs);
 
         $ids = array();
 
diff --git a/lib/noticestream.php b/lib/noticestream.php
index 01c5ee4a72..02b2a2da86 100644
--- a/lib/noticestream.php
+++ b/lib/noticestream.php
@@ -28,11 +28,7 @@
  * @link      http://status.net/
  */
 
-if (!defined('STATUSNET')) {
-    // This check helps protect against security problems;
-    // your code file can't be executed directly from the web.
-    exit(1);
-}
+if (!defined('GNUSOCIAL')) { exit(1); }
 
 /**
  * Class for notice streams
@@ -46,16 +42,15 @@ if (!defined('STATUSNET')) {
  */
 abstract class NoticeStream
 {
-    protected $selectVerbs   = null;    // must be set to array
-    protected $unselectVerbs = null;    // must be set to array
+    protected $selectVerbs   = array(ActivityVerb::POST => true,
+                                     ActivityVerb::DELETE => false);
 
     public function __construct()
     {
-        if ($this->selectVerbs === null) {
-            $this->selectVerbs = array(ActivityVerb::POST, ActivityUtils::resolveUri(ActivityVerb::POST, true));
-        }
-        if ($this->unselectVerbs === null) {
-            $this->unselectVerbs = array(ActivityVerb::DELETE);
+        foreach ($this->selectVerbs as $key=>$val) {
+            // to avoid database inconsistency issues we select both relative and absolute verbs
+            $this->selectVerbs[ActivityUtils::resolveUri($key)] = $val;
+            $this->selectVerbs[ActivityUtils::resolveUri($key, true)] = $val;
         }
     }
 
@@ -74,4 +69,21 @@ abstract class NoticeStream
     {
     	return Notice::multiGet('id', $ids);
     }
+
+    static function filterVerbs(Notice $notice, array $selectVerbs)
+    {
+        $filter = array_keys(array_filter($selectVerbs));
+        if (!empty($filter)) {
+            // include verbs in selectVerbs with values that equate to true
+            $notice->whereAddIn('verb', $filter, $notice->columnType('verb'));
+        }
+
+        $filter = array_keys(array_filter($selectVerbs, function ($v) { return !$v; }));
+        if (!empty($filter)) {
+            // exclude verbs in selectVerbs with values that equate to false
+            $notice->whereAddIn('!verb', $filter, $notice->columnType('verb'));
+        }
+
+        unset($filter);
+    }
 }
diff --git a/lib/profilenoticestream.php b/lib/profilenoticestream.php
index a31fb585d1..7ff4163fcb 100644
--- a/lib/profilenoticestream.php
+++ b/lib/profilenoticestream.php
@@ -28,11 +28,7 @@
  * @link      http://status.net/
  */
 
-if (!defined('STATUSNET')) {
-    // This check helps protect against security problems;
-    // your code file can't be executed directly from the web.
-    exit(1);
-}
+if (!defined('GNUSOCIAL')) { exit(1); }
 
 /**
  * Stream of notices by a profile
@@ -134,12 +130,7 @@ class RawProfileNoticeStream extends NoticeStream
         Notice::addWhereSinceId($notice, $since_id);
         Notice::addWhereMaxId($notice, $max_id);
 
-        if (!empty($this->selectVerbs)) {
-            $notice->whereAddIn('verb', $this->selectVerbs, $notice->columnType('verb'));
-        }
-        if (!empty($this->unselectVerbs)) {
-            $notice->whereAddIn('!verb', $this->unselectVerbs, $notice->columnType('verb'));
-        }
+        self::filterVerbs($notice, $this->selectVerbs);
 
         $notice->orderBy('created DESC, id DESC');
 
diff --git a/lib/publicnoticestream.php b/lib/publicnoticestream.php
index 757c2164c0..0137814ba4 100644
--- a/lib/publicnoticestream.php
+++ b/lib/publicnoticestream.php
@@ -28,11 +28,7 @@
  * @link      http://status.net/
  */
 
-if (!defined('STATUSNET')) {
-    // This check helps protect against security problems;
-    // your code file can't be executed directly from the web.
-    exit(1);
-}
+if (!defined('GNUSOCIAL')) { exit(1); }
 
 /**
  * Public stream
@@ -87,9 +83,7 @@ class RawPublicNoticeStream extends NoticeStream
         Notice::addWhereSinceId($notice, $since_id);
         Notice::addWhereMaxId($notice, $max_id);
 
-        if (!empty($this->selectVerbs)) {
-            $notice->whereAddIn('verb', $this->selectVerbs, $notice->columnType('verb'));
-        }
+        self::filterVerbs($notice, $this->selectVerbs);
 
         $ids = array();
 
diff --git a/lib/replynoticestream.php b/lib/replynoticestream.php
index 9fea5cac1e..9eb188d54d 100644
--- a/lib/replynoticestream.php
+++ b/lib/replynoticestream.php
@@ -28,11 +28,7 @@
  * @link      http://status.net/
  */
 
-if (!defined('STATUSNET')) {
-    // This check helps protect against security problems;
-    // your code file can't be executed directly from the web.
-    exit(1);
-}
+if (!defined('GNUSOCIAL')) { exit(1); }
 
 /**
  * Stream of mentions of me
@@ -92,8 +88,20 @@ class RawReplyNoticeStream extends NoticeStream
         Notice::addWhereMaxId($reply, $max_id, 'notice_id', 'reply.modified');
 
         if (!empty($this->selectVerbs)) {
+            // this is a little special since we have to join in Notice
             $reply->joinAdd(array('notice_id', 'notice:id'));
-            $reply->whereAddIn('notice.verb', $this->selectVerbs, 'string');
+
+            $filter = array_keys(array_filter($this->selectVerbs));
+            if (!empty($filter)) {
+                // include verbs in selectVerbs with values that equate to true
+                $reply->whereAddIn('notice.verb', $filter, 'string');
+            }
+
+            $filter = array_keys(array_filter($this->selectVerbs, function ($v) { return !$v; }));
+            if (!empty($filter)) {
+                // exclude verbs in selectVerbs with values that equate to false
+                $reply->whereAddIn('!notice.verb', $filter, 'string');
+            }
         }
 
         $reply->orderBy('reply.modified DESC, reply.notice_id DESC');
diff --git a/lib/tagnoticestream.php b/lib/tagnoticestream.php
index d24907fa7e..3d81f7415a 100644
--- a/lib/tagnoticestream.php
+++ b/lib/tagnoticestream.php
@@ -28,11 +28,7 @@
  * @link      http://status.net/
  */
 
-if (!defined('STATUSNET')) {
-    // This check helps protect against security problems;
-    // your code file can't be executed directly from the web.
-    exit(1);
-}
+if (!defined('GNUSOCIAL')) { exit(1); }
 
 /**
  * Stream of notices with a given tag
@@ -90,10 +86,19 @@ class RawTagNoticeStream extends NoticeStream
         Notice::addWhereMaxId($nt, $max_id, 'notice_id');
 
         if (!empty($this->selectVerbs)) {
-            $notice->whereAddIn('verb', $this->selectVerbs, $notice->columnType('verb'));
-        }
-        if (!empty($this->unselectVerbs)) {
-            $notice->whereAddIn('!verb', $this->unselectVerbs, $notice->columnType('verb'));
+            $nt->joinAdd(array('notice_id', 'notice:id'));
+
+            $filter = array_keys(array_filter($this->selectVerbs));
+            if (!empty($filter)) {
+                // include verbs in selectVerbs with values that equate to true
+                $nt->whereAddIn('notice.verb', $filter, 'string');
+            }
+
+            $filter = array_keys(array_filter($this->selectVerbs, function ($v) { return !$v; }));
+            if (!empty($filter)) {
+                // exclude verbs in selectVerbs with values that equate to false
+                $nt->whereAddIn('!notice.verb', $filter, 'string');
+            }
         }
 
         $nt->orderBy('created DESC, notice_id DESC');
diff --git a/plugins/Favorite/lib/favenoticestream.php b/plugins/Favorite/lib/favenoticestream.php
index 6294c8cdda..d10272ac91 100644
--- a/plugins/Favorite/lib/favenoticestream.php
+++ b/plugins/Favorite/lib/favenoticestream.php
@@ -28,11 +28,7 @@
  * @link      http://status.net/
  */
 
-if (!defined('STATUSNET')) {
-    // This check helps protect against security problems;
-    // your code file can't be executed directly from the web.
-    exit(1);
-}
+if (!defined('GNUSOCIAL')) { exit(1); }
 
 /**
  * Notice stream for favorites
@@ -77,14 +73,14 @@ class RawFaveNoticeStream extends NoticeStream
     protected $user_id;
     protected $own;
 
+    protected $selectVerbs = array();
+
     function __construct($user_id, $own)
     {
         parent::__construct();
 
         $this->user_id = $user_id;
         $this->own     = $own;
-
-        $this->selectVerbs = array();
     }
 
     /**