]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/inboxnoticestream.php
inboxnoticestream.php gotta go fast
[quix0rs-gnu-social.git] / lib / inboxnoticestream.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2011, StatusNet, Inc.
5  *
6  * Stream of notices for a profile's "all" feed
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Affero General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU Affero General Public License for more details.
17  *
18  * You should have received a copy of the GNU Affero General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  *
21  * @category  NoticeStream
22  * @package   StatusNet
23  * @author    Evan Prodromou <evan@status.net>
24  * @author    Mikael Nordfeldth <mmn@hethane.se>
25  * @author    Alexei Sorokin <sor.alexei@meowr.ru>
26  * @copyright 2011 StatusNet, Inc.
27  * @copyright 2014 Free Software Foundation, Inc.
28  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
29  * @link      http://status.net/
30  */
31
32 if (!defined('GNUSOCIAL')) {
33     exit(1);
34 }
35
36 /**
37  * Stream of notices for a profile's "all" feed
38  *
39  * @category  General
40  * @package   StatusNet
41  * @author    Evan Prodromou <evan@status.net>
42  * @author    Mikael Nordfeldth <mmn@hethane.se>
43  * @author    Alexei Sorokin <sor.alexei@meowr.ru>
44  * @copyright 2011 StatusNet, Inc.
45  * @copyright 2014 Free Software Foundation, Inc.
46  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
47  * @link      http://status.net/
48  */
49 class InboxNoticeStream extends ScopingNoticeStream
50 {
51     /**
52      * Constructor
53      *
54      * @param Profile $target Profile to get a stream for
55      * @param Profile $scoped Currently scoped profile (if null, it is fetched)
56      */
57     public function __construct(Profile $target, Profile $scoped = null)
58     {
59         // FIXME: we don't use CachingNoticeStream - but maybe we should?
60         parent::__construct(new CachingNoticeStream(new RawInboxNoticeStream($target), 'profileall'), $scoped);
61     }
62 }
63
64 /**
65  * Raw stream of notices for the target's inbox
66  *
67  * @category  General
68  * @package   StatusNet
69  * @author    Evan Prodromou <evan@status.net>
70  * @author    Mikael Nordfeldth <mmn@hethane.se>
71  * @author    Alexei Sorokin <sor.alexei@meowr.ru>
72  * @copyright 2011 StatusNet, Inc.
73  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
74  * @link      http://status.net/
75  */
76 class RawInboxNoticeStream extends FullNoticeStream
77 {
78     protected $target = null;
79     protected $inbox = null;
80
81     /**
82      * Constructor
83      *
84      * @param Profile $target Profile to get a stream for
85      */
86     public function __construct(Profile $target)
87     {
88         parent::__construct();
89         $this->target = $target;
90     }
91
92     /**
93      * Get IDs in a range
94      *
95      * @param int $offset Offset from start
96      * @param int $limit Limit of number to get
97      * @param int $since_id Since this notice
98      * @param int $max_id Before this notice
99      *
100      * @return array IDs found
101      */
102     public function getNoticeIds($offset, $limit, $since_id = null, $max_id = null)
103     {
104         $notice_ids = [];
105
106         // Grab all the profiles target is subscribed to (every user is subscribed to themselves)
107         $subscription = new Subscription();
108         $subscription->selectAdd();
109         $subscription->selectAdd('subscribed');
110         $subscription->whereAdd(sprintf('subscriber = %1$d', $this->target->id));
111         $subscription_profile_ids = $subscription->fetchAll('subscribed');
112
113         // Grab all the notices were target was mentioned
114         $reply = new Reply();
115         $reply->selectAdd();
116         $reply->selectAdd('notice_id');
117         $reply->whereAdd(sprintf('profile_id = %1$d', $this->target->id));
118         $notice_ids += $reply->fetchAll('notice_id');
119
120         // Grab all the notices that require target's attention
121         $attention = new Attention();
122         $attention->selectAdd();
123         $attention->selectAdd('notice_id');
124         $attention->whereAdd(sprintf('profile_id = %1$d', $this->target->id));
125         $notice_ids += $attention->fetchAll('notice_id');
126
127         // Grab all the notices posted on groups target is a member of
128         $group_inbox = new Group_inbox();
129         $group_inbox->selectAdd();
130         $group_inbox->selectAdd('notice_id');
131         $group_inbox->whereAdd(
132             sprintf(
133                 'group_id IN (SELECT group_id FROM group_member WHERE profile_id = %1$d)',
134                 $this->target->id
135             )
136         );
137         $notice_ids += $group_inbox->fetchAll('notice_id');
138
139         // This is just to make the query lighter when processed by the Database server
140         $notice_ids = array_filter($notice_ids, function ($id) {
141             // Keep id (a.k.a.: return true) if:
142             // - id higher than since id (just constrain if specified)
143             // - id lower than max id (just constrain if specified)
144             return (empty($since_id) || $id > $since_id) && (empty($max_id) || $id <= $max_id);
145         });
146
147         $query_ids = '';
148
149         if (!empty($notice_ids)) { // Replies, Attentions and Groups
150             $query_ids .= 'notice.id IN (' . implode(', ', $notice_ids) . ') OR ';
151         }
152         // every user is at least subscribed to themselves
153         $query_ids .= 'notice.profile_id IN (' . implode(', ', $subscription_profile_ids) . ')';
154
155         $notice = new Notice();
156         $notice->selectAdd();
157         $notice->selectAdd('id');
158         $notice->whereAdd(sprintf('notice.created > "%s"', $notice->escape($this->target->created)));
159         $notice->whereAdd($query_ids);
160
161         if (!empty($since_id)) {
162             $notice->whereAdd(sprintf('notice.id > %d', $since_id));
163         }
164         if (!empty($max_id)) {
165             $notice->whereAdd(sprintf('notice.id <= %d', $max_id));
166         }
167
168         self::filterVerbs($notice, $this->selectVerbs);
169
170         $notice->limit($offset, $limit);
171         // notice.id will give us even really old posts, which were
172         // recently imported. For example if a remote instance had
173         // problems and just managed to post here. Another solution
174         // would be to have a 'notice.imported' field and order by it.
175         $notice->orderBy('notice.id DESC');
176
177         if (!$notice->find()) {
178             return [];
179         }
180
181
182         return $notice->fetchAll('id');
183     }
184 }