3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2011, StatusNet, Inc.
6 * Stream of notices for a profile's "all" feed
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.
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.
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/>.
21 * @category NoticeStream
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/
32 if (!defined('GNUSOCIAL')) {
37 * Stream of notices for a profile's "all" feed
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/
49 class InboxNoticeStream extends ScopingNoticeStream
54 * @param Profile $target Profile to get a stream for
55 * @param Profile $scoped Currently scoped profile (if null, it is fetched)
57 public function __construct(Profile $target, Profile $scoped = null)
59 // FIXME: we don't use CachingNoticeStream - but maybe we should?
60 parent::__construct(new CachingNoticeStream(new RawInboxNoticeStream($target), 'profileall'), $scoped);
65 * Raw stream of notices for the target's inbox
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/
76 class RawInboxNoticeStream extends FullNoticeStream
78 protected $target = null;
79 protected $inbox = null;
84 * @param Profile $target Profile to get a stream for
86 public function __construct(Profile $target)
88 parent::__construct();
89 $this->target = $target;
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
100 * @return array IDs found
102 public function getNoticeIds($offset, $limit, $since_id = null, $max_id = null)
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');
113 // Grab all the notices were target was mentioned
114 $reply = new Reply();
116 $reply->selectAdd('notice_id');
117 $reply->whereAdd(sprintf('profile_id = %1$d', $this->target->id));
118 $notice_ids += $reply->fetchAll('notice_id');
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');
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(
133 'group_id IN (SELECT group_id FROM group_member WHERE profile_id = %1$d)',
137 $notice_ids += $group_inbox->fetchAll('notice_id');
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);
149 if (!empty($notice_ids)) { // Replies, Attentions and Groups
150 $query_ids .= 'notice.id IN (' . implode(', ', $notice_ids) . ') OR ';
152 // every user is at least subscribed to themselves
153 $query_ids .= 'notice.profile_id IN (' . implode(', ', $subscription_profile_ids) . ')';
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);
161 if (!empty($since_id)) {
162 $notice->whereAdd(sprintf('notice.id > %d', $since_id));
164 if (!empty($max_id)) {
165 $notice->whereAdd(sprintf('notice.id <= %d', $max_id));
168 self::filterVerbs($notice, $this->selectVerbs);
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');
177 if (!$notice->find()) {
182 return $notice->fetchAll('id');