Use NoticeStream::filterVerbs for filtering in noticestreams

This commit is contained in:
Mikael Nordfeldth 2016-02-14 20:46:13 +01:00
parent c23c3a4f53
commit e2a090c9cc
9 changed files with 69 additions and 72 deletions

View File

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

View 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

View 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();

View File

@ -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);
}
}

View File

@ -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');

View File

@ -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();

View File

@ -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');

View File

@ -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'));
$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');
}
if (!empty($this->unselectVerbs)) {
$notice->whereAddIn('!verb', $this->unselectVerbs, $notice->columnType('verb'));
}
$nt->orderBy('created DESC, notice_id DESC');

View File

@ -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();
}
/**