]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/inboxnoticestream.php
58b007505084baa76ab557c23d48249ff69406c4
[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 the user's inbox
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  * @copyright 2011 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 /**
38  * Stream of notices for the user's inbox
39  *
40  * @category  General
41  * @package   StatusNet
42  * @author    Evan Prodromou <evan@status.net>
43  * @copyright 2011 StatusNet, 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 User $user User to get a stream for
53      */
54     function __construct($user, $profile = -1)
55     {
56         if (is_int($profile) && $profile == -1) {
57             $profile = Profile::current();
58         }
59         // Note: we don't use CachingNoticeStream since RawInboxNoticeStream
60         // uses Inbox::staticGet(), which is cached.
61         parent::__construct(new RawInboxNoticeStream($user), $profile);
62     }
63 }
64
65 /**
66  * Raw stream of notices for the user's inbox
67  *
68  * @category  General
69  * @package   StatusNet
70  * @author    Evan Prodromou <evan@status.net>
71  * @copyright 2011 StatusNet, Inc.
72  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
73  * @link      http://status.net/
74  */
75 class RawInboxNoticeStream extends NoticeStream
76 {
77     protected $user  = null;
78     protected $inbox = null;
79
80     /**
81      * Constructor
82      *
83      * @param User $user User to get a stream for
84      */
85     function __construct($user)
86     {
87         $this->user  = $user;
88         $this->inbox = Inbox::staticGet('user_id', $user->id);
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         if (empty($this->inbox)) {
104             $this->inbox = Inbox::fromNoticeInbox($user_id);
105             if (empty($this->inbox)) {
106                 return array();
107             } else {
108                 $this->inbox->encache();
109             }
110         }
111
112         $ids = $this->inbox->unpack();
113
114         if (!empty($since_id)) {
115             $newids = array();
116             foreach ($ids as $id) {
117                 if ($id > $since_id) {
118                     $newids[] = $id;
119                 }
120             }
121             $ids = $newids;
122         }
123
124         if (!empty($max_id)) {
125             $newids = array();
126             foreach ($ids as $id) {
127                 if ($id <= $max_id) {
128                     $newids[] = $id;
129                 }
130             }
131             $ids = $newids;
132         }
133
134         $ids = array_slice($ids, $offset, $limit);
135
136         return $ids;
137     }
138
139     function getNotices($offset, $limit, $sinceId, $maxId)
140     {
141         $all = array();
142
143         do {
144
145             $ids = $this->getNoticeIds($offset, $limit, $sinceId, $maxId);
146
147             $notices = Memcached_DataObject::pivotGet('Notice', 'id', $ids);
148
149             // By default, takes out false values
150
151             $notices = array_filter($notices);
152
153             $all = array_merge($all, $notices);
154
155             if (count($notices < count($ids))) {
156                 $offset += $limit;
157                 $limit  -= count($notices);
158             }
159
160         } while (count($notices) < count($ids) && count($ids) > 0);
161
162         return new ArrayWrapper($all);
163     }
164 }