]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Use NoticeStream::filterVerbs for filtering in noticestreams
authorMikael Nordfeldth <mmn@hethane.se>
Sun, 14 Feb 2016 19:46:13 +0000 (20:46 +0100)
committerMikael Nordfeldth <mmn@hethane.se>
Sun, 14 Feb 2016 19:46:13 +0000 (20:46 +0100)
lib/conversationnoticestream.php
lib/inboxnoticestream.php
lib/networkpublicnoticestream.php
lib/noticestream.php
lib/profilenoticestream.php
lib/publicnoticestream.php
lib/replynoticestream.php
lib/tagnoticestream.php
plugins/Favorite/lib/favenoticestream.php

index 9c32159d42aabe90de6fa2284e3c0ac281671fca..21b2d7f0be59a0561e6ecf1994cfcc7e89a451dd 100644 (file)
  * @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
index 496fe0d05d5425f6c784a0cf448b4fe34e4bb52d..3609f81ed388a836303cf32edf894fda3ed64d0e 100644 (file)
@@ -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
index 3320b7cd5a65e89d60e5f3a1d81ed7faab7d3367..9a10c2898833d2aea93cb6087a3beb9b94bf678f 100644 (file)
@@ -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();
 
index 01c5ee4a72e56e970aec2db641f26daf63b3300d..02b2a2da8642d2e97d3de27e4534474b6ed564f9 100644 (file)
  * @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);
+    }
 }
index a31fb585d18af16c7fc397964997557a8acd4b8a..7ff4163fcbc96dbdde5b583219872ef46a886591 100644 (file)
  * @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');
 
index 757c2164c08fc69af8e00bb9a24418d4b13899ec..0137814ba4680df10a729208e48c91f0a1df515d 100644 (file)
  * @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();
 
index 9fea5cac1e5accdc62a77b65526d468c2ae7d8ea..9eb188d54db2d43d0105a4ea48d2b395b911f64d 100644 (file)
  * @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');
index d24907fa7ee5176acd45a9433a6439a456a4ef8e..3d81f7415ad1bed4d18acb5f23c21dfc47fc79dd 100644 (file)
  * @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');
index 6294c8cdda44f9d7378ae8cbe5f34bdeebd14af4..d10272ac91578478d58aa589bbcfa16791d53912 100644 (file)
  * @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();
     }
 
     /**