]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/noticelist.php
Optionally hide spam from timelines
[quix0rs-gnu-social.git] / lib / noticelist.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * widget for displaying a list of notices
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  UI
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @author    Sarven Capadisli <csarven@status.net>
26  * @copyright 2008 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
32     exit(1);
33 }
34
35 require_once INSTALLDIR.'/lib/favorform.php';
36 require_once INSTALLDIR.'/lib/disfavorform.php';
37 require_once INSTALLDIR.'/lib/attachmentlist.php';
38
39 /**
40  * widget for displaying a list of notices
41  *
42  * There are a number of actions that display a list of notices, in
43  * reverse chronological order. This widget abstracts out most of the
44  * code for UI for notice lists. It's overridden to hide some
45  * data for e.g. the profile page.
46  *
47  * @category UI
48  * @package  StatusNet
49  * @author   Evan Prodromou <evan@status.net>
50  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
51  * @link     http://status.net/
52  * @see      Notice
53  * @see      NoticeListItem
54  * @see      ProfileNoticeList
55  */
56 class NoticeList extends Widget
57 {
58     /** the current stream of notices being displayed. */
59
60     var $notice = null;
61
62     /**
63      * constructor
64      *
65      * @param Notice $notice stream of notices from DB_DataObject
66      */
67     function __construct($notice, $out=null)
68     {
69         parent::__construct($out);
70         $this->notice = $notice;
71     }
72
73     /**
74      * show the list of notices
75      *
76      * "Uses up" the stream by looping through it. So, probably can't
77      * be called twice on the same list.
78      *
79      * @return int count of notices listed.
80      */
81     function show()
82     {
83         $this->out->elementStart('div', array('id' =>'notices_primary'));
84         $this->out->elementStart('ol', array('class' => 'notices xoxo'));
85
86                 $notices = $this->notice->fetchAll();
87                 $total   = count($notices);
88                 $notices = array_slice($notices, 0, NOTICES_PER_PAGE);
89                 
90         self::prefill($notices);
91         
92         foreach ($notices as $notice) {
93
94             try {
95                 $item = $this->newListItem($notice);
96                 $item->show();
97             } catch (Exception $e) {
98                 // we log exceptions and continue
99                 common_log(LOG_ERR, $e->getMessage());
100                 continue;
101             }
102         }
103
104         $this->out->elementEnd('ol');
105         $this->out->elementEnd('div');
106
107         return $total;
108     }
109
110     /**
111      * returns a new list item for the current notice
112      *
113      * Recipe (factory?) method; overridden by sub-classes to give
114      * a different list item class.
115      *
116      * @param Notice $notice the current notice
117      *
118      * @return NoticeListItem a list item for displaying the notice
119      */
120     function newListItem($notice)
121     {
122         return new NoticeListItem($notice, $this->out);
123     }
124     
125     static function prefill(&$notices, $avatarSize=AVATAR_STREAM_SIZE)
126     {
127         if (Event::handle('StartNoticeListPrefill', array(&$notices, $avatarSize))) {
128
129             // Prefill attachments
130             Notice::fillAttachments($notices);
131             // Prefill attachments
132             Notice::fillFaves($notices);
133             // Prefill repeat data
134             Notice::fillRepeats($notices);
135             // Prefill the profiles
136             $profiles = Notice::fillProfiles($notices);
137             // Prefill the avatars
138             Profile::fillAvatars($profiles, $avatarSize);
139         
140             $p = Profile::current();
141         
142             if (!empty($p)) {
143
144                 $ids = array();
145         
146                 foreach ($notices as $notice) {
147                     $ids[] = $notice->id;
148                 }
149         
150                 Memcached_DataObject::pivotGet('Fave', 'notice_id', $ids, array('user_id' => $p->id));
151                 Memcached_DataObject::pivotGet('Notice', 'repeat_of', $ids, array('profile_id' => $p->id));
152             }
153
154             if (common_config('notice', 'hidespam')) {
155
156                 $pids = array();
157
158                 foreach ($profiles as $profile) {
159                     $pids[] = $profile->id;
160                 }
161                 
162                 Memcached_DataObject::pivotGet('Profile_role',
163                                                'profile_id',
164                                                $pids,
165                                                array('role' => Profile_role::SILENCED));
166             }
167
168             Event::handle('EndNoticeListPrefill', array(&$notices, &$profiles, $avatarSize));
169         }
170     }
171 }