Faster inboxnoticestream.php by XRevan86

This commit is contained in:
Diogo Cordeiro 2019-04-21 00:56:56 +01:00
parent 7b6c887d76
commit a59c439b46

View File

@ -24,13 +24,16 @@
* @package StatusNet * @package StatusNet
* @author Evan Prodromou <evan@status.net> * @author Evan Prodromou <evan@status.net>
* @author Mikael Nordfeldth <mmn@hethane.se> * @author Mikael Nordfeldth <mmn@hethane.se>
* @author Alexei Sorokin <sor.alexei@meowr.ru>
* @copyright 2011 StatusNet, Inc. * @copyright 2011 StatusNet, Inc.
* @copyright 2014 Free Software Foundation, Inc. * @copyright 2014 Free Software Foundation, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
* @link http://status.net/ * @link http://status.net/
*/ */
if (!defined('GNUSOCIAL')) { exit(1); } if (!defined('GNUSOCIAL')) {
exit(1);
}
/** /**
* Stream of notices for a profile's "all" feed * Stream of notices for a profile's "all" feed
@ -39,6 +42,7 @@ if (!defined('GNUSOCIAL')) { exit(1); }
* @package StatusNet * @package StatusNet
* @author Evan Prodromou <evan@status.net> * @author Evan Prodromou <evan@status.net>
* @author Mikael Nordfeldth <mmn@hethane.se> * @author Mikael Nordfeldth <mmn@hethane.se>
* @author Alexei Sorokin <sor.alexei@meowr.ru>
* @copyright 2011 StatusNet, Inc. * @copyright 2011 StatusNet, Inc.
* @copyright 2014 Free Software Foundation, Inc. * @copyright 2014 Free Software Foundation, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
@ -52,7 +56,7 @@ class InboxNoticeStream extends ScopingNoticeStream
* @param Profile $target Profile to get a stream for * @param Profile $target Profile to get a stream for
* @param Profile $scoped Currently scoped profile (if null, it is fetched) * @param Profile $scoped Currently scoped profile (if null, it is fetched)
*/ */
function __construct(Profile $target, Profile $scoped=null) public function __construct(Profile $target, Profile $scoped = null)
{ {
// FIXME: we don't use CachingNoticeStream - but maybe we should? // FIXME: we don't use CachingNoticeStream - but maybe we should?
parent::__construct(new CachingNoticeStream(new RawInboxNoticeStream($target), 'profileall'), $scoped); parent::__construct(new CachingNoticeStream(new RawInboxNoticeStream($target), 'profileall'), $scoped);
@ -65,6 +69,8 @@ class InboxNoticeStream extends ScopingNoticeStream
* @category General * @category General
* @package StatusNet * @package StatusNet
* @author Evan Prodromou <evan@status.net> * @author Evan Prodromou <evan@status.net>
* @author Mikael Nordfeldth <mmn@hethane.se>
* @author Alexei Sorokin <sor.alexei@meowr.ru>
* @copyright 2011 StatusNet, Inc. * @copyright 2011 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
* @link http://status.net/ * @link http://status.net/
@ -79,7 +85,7 @@ class RawInboxNoticeStream extends FullNoticeStream
* *
* @param Profile $target Profile to get a stream for * @param Profile $target Profile to get a stream for
*/ */
function __construct(Profile $target) public function __construct(Profile $target)
{ {
parent::__construct(); parent::__construct();
$this->target = $target; $this->target = $target;
@ -93,23 +99,59 @@ class RawInboxNoticeStream extends FullNoticeStream
* @param int $since_id Since this notice * @param int $since_id Since this notice
* @param int $max_id Before this notice * @param int $max_id Before this notice
* *
* @return Array IDs found * @return array IDs found
*/ */
function getNoticeIds($offset, $limit, $since_id, $max_id) public function getNoticeIds($offset, $limit, $since_id = null, $max_id = null)
{ {
$notice_ids = [];
// Grab all the profiles target is subscribed to (every user is subscribed to themselves)
$subscription = new Subscription();
$subscription->selectAdd();
$subscription->selectAdd('subscribed');
$subscription->whereAdd(sprintf('subscriber = %1$d', $this->target->id));
$subscription_profile_ids = $subscription->fetchAll('subscribed');
// Grab all the notices were target was mentioned
$reply = new Reply();
$reply->selectAdd();
$reply->selectAdd('notice_id');
$reply->whereAdd(sprintf('profile_id = %1$d', $this->target->id));
$notice_ids += $reply->fetchAll('notice_id');
// Grab all the notices that require target's attention
$attention = new Attention();
$attention->selectAdd();
$attention->selectAdd('notice_id');
$attention->whereAdd(sprintf('profile_id = %1$d', $this->target->id));
$notice_ids += $attention->fetchAll('notice_id');
// Grab all the notices posted on groups target is a member of
$group_inbox = new Group_inbox();
$group_inbox->selectAdd();
$group_inbox->selectAdd('notice_id');
$group_inbox->whereAdd(
sprintf(
'group_id IN (SELECT group_id FROM group_member WHERE profile_id = %1$d)',
$this->target->id
)
);
$notice_ids += $group_inbox->fetchAll('notice_id');
$query_ids = '';
if (!empty($notice_ids)) { // Replies, Attentions and Groups
$query_ids .= 'notice.id IN (' . implode(', ', $notice_ids) . ') OR ';
}
// every user is at least subscribed to themselves
$query_ids .= 'notice.profile_id IN (' . implode(', ', $subscription_profile_ids) . ')';
$notice = new Notice(); $notice = new Notice();
$notice->selectAdd(); $notice->selectAdd();
$notice->selectAdd('id'); $notice->selectAdd('id');
$notice->whereAdd(sprintf('notice.created > "%s"', $notice->escape($this->target->created))); $notice->whereAdd(sprintf('notice.created > "%s"', $notice->escape($this->target->created)));
// Reply:: is a table of mentions $notice->whereAdd($query_ids);
// Subscription:: is a table of subscriptions (every user is subscribed to themselves)
$notice->whereAdd(
sprintf('notice.id IN (SELECT notice_id FROM reply WHERE profile_id=%1$d) ' .
'OR notice.profile_id IN (SELECT subscribed FROM subscription WHERE subscriber=%1$d) ' .
'OR notice.id IN (SELECT notice_id FROM group_inbox WHERE group_id IN (SELECT group_id FROM group_member WHERE profile_id=%1$d))' .
'OR notice.id IN (SELECT notice_id FROM attention WHERE profile_id=%1$d)',
$this->target->id)
);
if (!empty($since_id)) { if (!empty($since_id)) {
$notice->whereAdd(sprintf('notice.id > %d', $since_id)); $notice->whereAdd(sprintf('notice.id > %d', $since_id));
} }
@ -127,11 +169,10 @@ class RawInboxNoticeStream extends FullNoticeStream
$notice->orderBy('notice.id DESC'); $notice->orderBy('notice.id DESC');
if (!$notice->find()) { if (!$notice->find()) {
return array(); return [];
} }
$ids = $notice->fetchAll('id');
return $ids; return $notice->fetchAll('id');
} }
} }