]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/profilenoticestream.php
Sanity check on profile streams
[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         // Sanity check
66         if (common_config('notice', 'hidespam')) {
67             if ($this->streamProfile->hasRole(Profile_role::SILENCED) &&
68                 (empty($this->profile) || !$this->profile->hasRole(Profile_role::MODERATOR))) {
69                 throw new ClientException(_("This account silenced by moderators."), 403);
70             }
71         }
72
73         return parent::getNoticeIds($offset, $limit, $since_id, $max_id);
74     }
75
76     function getNotices($offset, $limit, $sinceId = null, $maxId = null)
77     {
78         // Sanity check
79         if (common_config('notice', 'hidespam')) {
80             if ($this->streamProfile->hasRole(Profile_role::SILENCED) &&
81                 (empty($this->profile) || !$this->profile->hasRole(Profile_role::MODERATOR))) {
82                 throw new ClientException(_("This account silenced by moderators."), 403);
83             }
84         }
85
86         return parent::getNotices($offset, $limit, $sinceId, $maxId);
87     }
88 }
89
90 /**
91  * Raw stream of notices by a profile
92  *
93  * @category  General
94  * @package   StatusNet
95  * @author    Evan Prodromou <evan@status.net>
96  * @copyright 2011 StatusNet, Inc.
97  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
98  * @link      http://status.net/
99  */
100
101 class RawProfileNoticeStream extends NoticeStream
102 {
103     protected $profile;
104
105     function __construct($profile)
106     {
107         $this->profile = $profile;
108     }
109
110     function getNoticeIds($offset, $limit, $since_id, $max_id)
111     {
112         $notice = new Notice();
113
114         $notice->profile_id = $this->profile->id;
115
116         $notice->selectAdd();
117         $notice->selectAdd('id');
118
119         Notice::addWhereSinceId($notice, $since_id);
120         Notice::addWhereMaxId($notice, $max_id);
121
122         $notice->orderBy('created DESC, id DESC');
123
124         if (!is_null($offset)) {
125             $notice->limit($offset, $limit);
126         }
127
128         $notice->find();
129
130         $ids = array();
131
132         while ($notice->fetch()) {
133             $ids[] = $notice->id;
134         }
135
136         return $ids;
137     }
138 }