]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/profilenoticestream.php
Use NoticeStream::filterVerbs for filtering in noticestreams
[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('GNUSOCIAL')) { exit(1); }
32
33 /**
34  * Stream of notices by a profile
35  *
36  * @category  General
37  * @package   StatusNet
38  * @author    Evan Prodromou <evan@status.net>
39  * @copyright 2011 StatusNet, Inc.
40  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
41  * @link      http://status.net/
42  */
43
44 class ProfileNoticeStream extends ScopingNoticeStream
45 {
46     var $streamProfile;
47     var $userProfile;
48
49     function __construct($profile, $userProfile = -1)
50     {
51         if (is_int($userProfile) && $userProfile == -1) {
52             $userProfile = Profile::current();
53         }
54         $this->streamProfile = $profile;
55         $this->userProfile   = $userProfile;
56         parent::__construct(new CachingNoticeStream(new RawProfileNoticeStream($profile),
57                                                     'profile:notice_ids:' . $profile->id),
58                             $userProfile);
59     }
60
61     function getNoticeIds($offset, $limit, $since_id=null, $max_id=null)
62     {
63         if ($this->impossibleStream()) {
64             return array();
65         } else {
66             return parent::getNoticeIds($offset, $limit, $since_id, $max_id);
67         }
68     }
69
70     function getNotices($offset, $limit, $since_id=null, $max_id=null)
71     {
72         if ($this->impossibleStream()) {
73             throw new PrivateStreamException($this->streamProfile, $this->userProfile);
74         } else {
75             return parent::getNotices($offset, $limit, $since_id, $max_id);
76         }
77     }
78
79     function impossibleStream() 
80     {
81         if (!$this->streamProfile->readableBy($this->userProfile)) {
82             // cannot read because it's a private stream and either noone's logged in or they are not subscribers
83             return true;
84         }
85
86         // If it's a spammy stream, and no user or not a moderator
87
88         if (common_config('notice', 'hidespam')) {
89             if ($this->streamProfile->hasRole(Profile_role::SILENCED) &&
90                 (empty($this->userProfile) || (($this->userProfile->id !== $this->streamProfile->id) && !$this->userProfile->hasRight(Right::REVIEWSPAM)))) {
91                 return true;
92             }
93         }
94
95         return false;
96     }
97 }
98
99 /**
100  * Raw stream of notices by a profile
101  *
102  * @category  General
103  * @package   StatusNet
104  * @author    Evan Prodromou <evan@status.net>
105  * @copyright 2011 StatusNet, Inc.
106  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
107  * @link      http://status.net/
108  */
109
110 class RawProfileNoticeStream extends NoticeStream
111 {
112     protected $profile;
113     protected $selectVerbs = array();   // select all verbs
114
115     function __construct($profile)
116     {
117         parent::__construct();
118         $this->profile = $profile;
119     }
120
121     function getNoticeIds($offset, $limit, $since_id, $max_id)
122     {
123         $notice = new Notice();
124
125         $notice->profile_id = $this->profile->id;
126
127         $notice->selectAdd();
128         $notice->selectAdd('id');
129
130         Notice::addWhereSinceId($notice, $since_id);
131         Notice::addWhereMaxId($notice, $max_id);
132
133         self::filterVerbs($notice, $this->selectVerbs);
134
135         $notice->orderBy('created DESC, id DESC');
136
137         if (!is_null($offset)) {
138             $notice->limit($offset, $limit);
139         }
140
141         $notice->find();
142
143         $ids = array();
144
145         while ($notice->fetch()) {
146             $ids[] = $notice->id;
147         }
148
149         return $ids;
150     }
151 }