]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/inboxnoticestream.php
New mechanism for "all" feed (InboxNoticeStream)
[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  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category  NoticeStream
24  * @package   StatusNet
25  * @author    Evan Prodromou <evan@status.net>
26  * @author    Mikael Nordfeldth <mmn@hethane.se>
27  * @copyright 2011 StatusNet, Inc.
28  * @copyright 2014 Free Software Foundation, Inc.
29  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
30  * @link      http://status.net/
31  */
32
33 if (!defined('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); }
34
35 /**
36  * Stream of notices for a profile's "all" feed
37  *
38  * @category  General
39  * @package   StatusNet
40  * @author    Evan Prodromou <evan@status.net>
41  * @author    Mikael Nordfeldth <mmn@hethane.se>
42  * @copyright 2011 StatusNet, Inc.
43  * @copyright 2014 Free Software Foundation, Inc.
44  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
45  * @link      http://status.net/
46  */
47 class InboxNoticeStream extends ScopingNoticeStream
48 {
49     /**
50      * Constructor
51      *
52      * @param Profile $target Profile to get a stream for
53      * @param Profile $scoped Currently scoped profile (if null, it is fetched)
54      */
55     function __construct(Profile $target, Profile $scoped=null)
56     {
57         if ($scoped === null) {
58             $scoped = Profile::current();
59         }
60         // Note: we don't use CachingNoticeStream since RawInboxNoticeStream
61         // uses Inbox::getKV(), which is cached.
62         parent::__construct(new RawInboxNoticeStream($target), $scoped);
63     }
64 }
65
66 /**
67  * Raw stream of notices for the target's inbox
68  *
69  * @category  General
70  * @package   StatusNet
71  * @author    Evan Prodromou <evan@status.net>
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 NoticeStream
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     function __construct(Profile $target)
87     {
88         $this->target  = $target;
89     }
90
91     /**
92      * Get IDs in a range
93      *
94      * @param int $offset   Offset from start
95      * @param int $limit    Limit of number to get
96      * @param int $since_id Since this notice
97      * @param int $max_id   Before this notice
98      *
99      * @return Array IDs found
100      */
101     function getNoticeIds($offset, $limit, $since_id, $max_id)
102     {
103         $notice = new Notice();
104         $notice->selectAdd();
105         $notice->selectAdd('id');
106         $notice->whereAdd(sprintf('notice.created > "%s"', $notice->escape($this->target->created)));
107         // Reply:: is a table of mentions
108         // Subscription:: is a table of subscriptions (every user is subscribed to themselves)
109         $notice->whereAdd(
110                 sprintf('notice.id IN (SELECT notice_id FROM reply WHERE profile_id=%1$d) ' .
111                     'OR notice.profile_id IN (SELECT subscribed FROM subscription WHERE subscriber=%d)', $this->target->id)
112             );
113         $notice->limit($offset, $limit);
114         $notice->orderBy('notice.created DESC');
115
116         if (!$notice->find()) {
117             return array();
118         }
119
120         $ids = $notice->fetchAll('id');
121
122         return $ids;
123     }
124
125     function getNotices($offset, $limit, $sinceId, $maxId)
126     {
127         $all = array();
128
129         do {
130
131             $ids = $this->getNoticeIds($offset, $limit, $sinceId, $maxId);
132
133             $notices = Notice::pivotGet('id', $ids);
134
135             // By default, takes out false values
136
137             $notices = array_filter($notices);
138
139             $all = array_merge($all, $notices);
140
141             if (count($notices < count($ids))) {
142                 $offset += $limit;
143                 $limit  -= count($notices);
144             }
145
146         } while (count($notices) < count($ids) && count($ids) > 0);
147
148         return new ArrayWrapper($all);
149     }
150 }