]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/inboxnoticestream.php
Merge remote-tracking branch 'gnuio/master' into nightly
[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')) { 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         // FIXME: we don't use CachingNoticeStream - but maybe we should?
58         parent::__construct(new CachingNoticeStream(new RawInboxNoticeStream($target), 'profileall'), $scoped);
59     }
60 }
61
62 /**
63  * Raw stream of notices for the target's inbox
64  *
65  * @category  General
66  * @package   StatusNet
67  * @author    Evan Prodromou <evan@status.net>
68  * @copyright 2011 StatusNet, Inc.
69  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
70  * @link      http://status.net/
71  */
72 class RawInboxNoticeStream extends FullNoticeStream
73 {
74     protected $target  = null;
75     protected $inbox = null;
76
77     /**
78      * Constructor
79      *
80      * @param Profile $target Profile to get a stream for
81      */
82     function __construct(Profile $target)
83     {
84         parent::__construct();
85         $this->target  = $target;
86     }
87
88     /**
89      * Get IDs in a range
90      *
91      * @param int $offset   Offset from start
92      * @param int $limit    Limit of number to get
93      * @param int $since_id Since this notice
94      * @param int $max_id   Before this notice
95      *
96      * @return Array IDs found
97      */
98     function getNoticeIds($offset, $limit, $since_id, $max_id)
99     {
100         $notice = new Notice();
101         $notice->selectAdd();
102         $notice->selectAdd('id');
103         $notice->whereAdd(sprintf('notice.created > "%s"', $notice->escape($this->target->created)));
104         // Reply:: is a table of mentions
105         // Subscription:: is a table of subscriptions (every user is subscribed to themselves)
106         $notice->whereAdd(
107                 sprintf('notice.id IN (SELECT notice_id FROM reply WHERE profile_id=%1$d) ' .
108                         'OR notice.profile_id IN (SELECT subscribed FROM subscription WHERE subscriber=%1$d) ' .
109                         '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))' .
110                         'OR notice.id IN (SELECT notice_id FROM attention WHERE profile_id=%1$d)',
111                     $this->target->id)
112             );
113         if (!empty($since_id)) {
114             $notice->whereAdd(sprintf('notice.id > %d', $since_id));
115         }
116         if (!empty($max_id)) {
117             $notice->whereAdd(sprintf('notice.id <= %d', $max_id));
118         }
119
120         self::filterVerbs($notice, $this->selectVerbs);
121
122         $notice->limit($offset, $limit);
123         // notice.id will give us even really old posts, which were
124         // recently imported. For example if a remote instance had
125         // problems and just managed to post here. Another solution
126         // would be to have a 'notice.imported' field and order by it.
127         $notice->orderBy('notice.id DESC');
128
129         if (!$notice->find()) {
130             return array();
131         }
132
133         $ids = $notice->fetchAll('id');
134
135         return $ids;
136     }
137 }