]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/profilenoticestream.php
Don't try to find profilenoticestream if impossible
[quix0rs-gnu-social.git] / lib / profilenoticestream.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2011, StatusNet, Inc.
5  *
6  * Stream of notices by a profile
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  Stream
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 by a profile
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
48 class ProfileNoticeStream extends ScopingNoticeStream
49 {
50     var $streamProfile;
51
52     function __construct($profile, $userProfile = -1)
53     {
54         if (is_int($userProfile) && $userProfile == -1) {
55             $userProfile = Profile::current();
56         }
57         $this->streamProfile = $profile;
58         parent::__construct(new CachingNoticeStream(new RawProfileNoticeStream($profile),
59                                                     'profile:notice_ids:' . $profile->id),
60                             $userProfile);
61     }
62
63     function getNoticeIds($offset, $limit, $since_id, $max_id)
64     {
65         if ($this->impossibleStream()) {
66             return array();
67         } else {
68             return parent::getNoticeIds($offset, $limit, $since_id, $max_id);
69         }
70     }
71
72     function getNotices($offset, $limit, $sinceId = null, $maxId = null)
73     {
74         if ($this->impossibleStream()) {
75             return array();
76         } else {
77             return parent::getNotices($offset, $limit, $sinceId, $maxId);
78         }
79     }
80
81     function impossibleStream() 
82     {
83         $user = User::staticGet('id', $this->streamProfile->id);
84
85         // If it's a private stream, and no user or not a subscriber
86
87         if (!empty($user) && $user->private_stream && 
88             empty($this->profile) || !$this->profile->isSubscribed($this->streamProfile)) {
89             return true;
90         }
91
92         // If it's a spammy stream, and no user or not a moderator
93
94         if (common_config('notice', 'hidespam')) {
95             if ($this->streamProfile->hasRole(Profile_role::SILENCED) &&
96                 (empty($this->profile) || !$this->profile->hasRole(Profile_role::MODERATOR))) {
97                 return true;
98             }
99         }
100
101         return false;
102     }
103 }
104
105 /**
106  * Raw stream of notices by a profile
107  *
108  * @category  General
109  * @package   StatusNet
110  * @author    Evan Prodromou <evan@status.net>
111  * @copyright 2011 StatusNet, Inc.
112  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
113  * @link      http://status.net/
114  */
115
116 class RawProfileNoticeStream extends NoticeStream
117 {
118     protected $profile;
119
120     function __construct($profile)
121     {
122         $this->profile = $profile;
123     }
124
125     function getNoticeIds($offset, $limit, $since_id, $max_id)
126     {
127         $notice = new Notice();
128
129         $notice->profile_id = $this->profile->id;
130
131         $notice->selectAdd();
132         $notice->selectAdd('id');
133
134         Notice::addWhereSinceId($notice, $since_id);
135         Notice::addWhereMaxId($notice, $max_id);
136
137         $notice->orderBy('created DESC, id DESC');
138
139         if (!is_null($offset)) {
140             $notice->limit($offset, $limit);
141         }
142
143         $notice->find();
144
145         $ids = array();
146
147         while ($notice->fetch()) {
148             $ids[] = $notice->id;
149         }
150
151         return $ids;
152     }
153 }