]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/showstream.php
typo in LocalID generation in xrds
[quix0rs-gnu-social.git] / actions / showstream.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/lib/stream.php');
23
24 define('SUBSCRIPTIONS_PER_ROW', 4);
25 define('SUBSCRIPTIONS', 80);
26
27 class ShowstreamAction extends StreamAction {
28
29         function handle($args) {
30
31                 parent::handle($args);
32
33                 $nickname = common_canonical_nickname($this->arg('nickname'));
34                 $user = User::staticGet('nickname', $nickname);
35
36                 if (!$user) {
37                         $this->no_such_user();
38                         return;
39                 }
40
41                 $profile = $user->getProfile();
42
43                 if (!$profile) {
44                         common_server_error(_t('User record exists without profile.'));
45                         return;
46                 }
47
48                 # Looks like we're good; start output
49                 
50                 # For YADIS discovery, we also have a <meta> tag
51
52                 header('X-XRDS-Location: '. common_local_url('xrds', array('nickname' =>
53                                                                                                                                    $user->nickname)));
54
55                 common_show_header($profile->nickname, array($this, 'show_header'), $user);
56
57                 $cur = common_current_user();
58
59                 if ($cur && $profile->id == $cur->id) {
60                         common_notice_form();
61                 }
62
63                 $this->show_sidebar($profile);
64                 
65                 $this->show_notices($profile);
66                 
67                 common_show_footer();
68         }
69
70         function show_header($user) {
71                 common_element('link', array('rel' => 'alternate',
72                                                                          'href' => common_local_url('userrss', array('nickname' =>
73                                                                                                                                                            $user->nickname)),
74                                                                          'type' => 'application/rss+xml',
75                                                                          'title' => _t('Notice feed for ') . $user->nickname));
76                 # for remote subscriptions etc.
77                 common_element('meta', array('http-equiv' => 'X-XRDS-Location',
78                                                                          'content' => common_local_url('xrds', array('nickname' =>
79                                                                                                                                                            $user->nickname))));
80         }
81         
82         function no_such_user() {
83                 common_user_error('No such user');
84         }
85
86         function show_sidebar($profile) {
87
88                 common_element_start('div', 'sidebar width33 floatRight greenBg');
89
90                 $this->show_profile($profile);
91
92                 $this->show_last_notice($profile);
93
94                 $cur = common_current_user();
95
96                 if ($cur) {
97                         if ($cur->id != $profile->id) {
98                                 if ($cur->isSubscribed($profile)) {
99                                         $this->show_unsubscribe_form($profile);
100                                 } else {
101                                         $this->show_subscribe_form($profile);
102                                 }
103                         }
104                 } else {
105                         $this->show_remote_subscribe_form($profile);
106                 }
107
108                 $this->show_statistics($profile);
109
110                 $this->show_subscriptions($profile);
111
112                 common_element_end('div');
113         }
114         
115         function show_profile($profile) {
116                 common_element_start('div', 'profile');
117
118                 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
119                 if ($avatar) {
120                         common_element('img', array('src' => $avatar->url,
121                                                                                 'class' => 'avatar profile',
122                                                                                 'width' => AVATAR_PROFILE_SIZE,
123                                                                                 'height' => AVATAR_PROFILE_SIZE,
124                                                                                 'alt' => $profile->nickname));
125                 }
126                 if ($profile->fullname) {
127                         common_element_start('div', 'fullname');
128                         if ($profile->homepage) {
129                                 common_element('a', array('href' => $profile->homepage),
130                                                            $profile->fullname);
131                         } else {
132                                 common_text($profile->fullname);
133                         }
134                         common_element_end('div');
135                 }
136                 if ($profile->location) {
137                         common_element('div', 'location', $profile->location);
138                 }
139                 if ($profile->bio) {
140                         common_element('div', 'bio', $profile->bio);
141                 }
142                 common_element_end('div');
143         }
144
145         function show_subscribe_form($profile) {
146                 common_element_start('form', array('id' => 'subscribe', 'method' => 'POST',
147                                                                                    'action' => common_local_url('subscribe')));
148                 common_element('input', array('id' => 'subscribeto',
149                                                                           'name' => 'subscribeto',
150                                                                           'type' => 'hidden',
151                                                                           'value' => $profile->nickname));
152                 common_element('input', array('type' => 'submit',
153                                                                           'class' => 'button',
154                                                                           'value' => _t('Subscribe')));
155                 common_element_end('form');
156         }
157
158         function show_remote_subscribe_form($profile) {
159                 common_element_start('form', array('id' => 'remotesubscribe', 'method' => 'POST',
160                                                                                    'action' => common_local_url('remotesubscribe')));
161                 common_hidden('nickname', $profile->nickname);
162                 common_input('profile', _t('Profile'));
163                 common_submit('submit',_t('Subscribe'));
164                 common_element_end('form');
165         }
166         
167         function show_unsubscribe_form($profile) {
168                 common_element_start('form', array('id' => 'unsubscribe', 'method' => 'POST',
169                                                                                    'action' => common_local_url('unsubscribe')));
170                 common_element('input', array('id' => 'unsubscribeto',
171                                                                           'name' => 'unsubscribeto',
172                                                                           'type' => 'hidden',
173                                                                           'value' => $profile->nickname));
174                 common_element('input', array('type' => 'submit',
175                                                                           'class' => 'button',
176                                                                           'value' => _t('Unsubscribe')));
177                 common_element_end('form');
178         }
179
180         function show_subscriptions($profile) {
181                 global $config;
182                 
183                 # XXX: add a limit
184                 $subs = DB_DataObject::factory('subscription');
185                 $subs->subscriber = $profile->id;
186
187                 # We ask for an extra one to know if we need to do another page
188
189                 $subs->limit(0, SUBSCRIPTIONS);
190
191                 $subs_count = $subs->find();
192
193                 common_element_start('div', 'subscriptions');
194
195                 common_element('h2', 'subscriptions', _t('Subscriptions'));
196
197                 $idx = 0;
198
199                 while ($subs->fetch()) {
200                         $idx++;
201                         if ($idx % SUBSCRIPTIONS_PER_ROW == 1) {
202                                 common_element_start('div', 'row');
203                         }
204                         
205                         $other = Profile::staticGet($subs->subscribed);
206                         
207                         common_element_start('a', array('title' => ($other->fullname) ?
208                                                                                         $other->fullname :
209                                                                                         $other->nickname,
210                                                                                         'href' => $other->profileurl,
211                                                                                         'class' => 'subscription'));
212                         $avatar = $other->getAvatar(AVATAR_MINI_SIZE);
213                         common_element('img', array('src' => (($avatar) ? $avatar->url :  common_default_avatar(AVATAR_MINI_SIZE)),
214                                                                                 'width' => AVATAR_MINI_SIZE,
215                                                                                 'height' => AVATAR_MINI_SIZE,
216                                                                                 'class' => 'avatar mini',
217                                                                                 'alt' =>  ($other->fullname) ?
218                                                                                 $other->fullname :
219                                                                                 $other->nickname));
220                         common_element_end('a');
221                         
222                         if ($idx % SUBSCRIPTIONS_PER_ROW == 0) {
223                                 common_element_end('div');
224                         }
225                         
226                         if ($idx == SUBSCRIPTIONS) {
227                                 break;
228                         }
229                 }
230
231                 # close any unclosed row
232                 if ($idx % SUBSCRIPTIONS_PER_ROW != 0) {
233                         common_element_end('div');
234                 }
235
236                 common_element('a', array('href' => common_local_url('subscriptions',
237                                                                                                                          array('nickname' => $profile->nickname)),
238                                                                   'class' => 'moresubscriptions'),
239                                            _t('All subscriptions'));
240
241                 common_element_end('div');
242         }
243
244         function show_statistics($profile) {
245
246                 // XXX: WORM cache this
247                 $subs = DB_DataObject::factory('subscription');
248                 $subs->subscriber = $profile->id;
249                 $subs_count = (int) $subs->count();
250
251                 $subbed = DB_DataObject::factory('subscription');
252                 $subbed->subscribed = $profile->id;
253                 $subbed_count = (int) $subbed->count();
254
255                 $notices = DB_DataObject::factory('notice');
256                 $notices->profile_id = $profile->id;
257                 $notice_count = (int) $notices->count();
258
259                 common_element_start('div', 'statistics');
260                 common_element('h2', 'statistics', _t('Statistics'));
261
262                 # Other stats...?
263                 common_element_start('dl', 'statistics');
264                 common_element('dt', 'subscriptions', _t('Subscriptions'));
265                 common_element('dd', 'subscriptions', $subs_count);
266                 common_element('dt', 'subscribers', _t('Subscribers'));
267                 common_element('dd', 'subscribers', $subbed_count);
268                 common_element('dt', 'notices', _t('Notices'));
269                 common_element('dd', 'notices', $notice_count);
270                 common_element_end('dl');
271
272                 common_element_end('div');
273         }
274
275         function show_notices($profile) {
276
277                 $notice = DB_DataObject::factory('notice');
278                 $notice->profile_id = $profile->id;
279
280                 $notice->orderBy('created DESC');
281
282                 $page = ($this->arg('page')) ? ($this->arg('page')+0) : 1;
283
284                 $notice->limit((($page-1)*NOTICES_PER_PAGE), NOTICES_PER_PAGE + 1);
285
286                 $cnt = $notice->find();
287
288                 common_element_start('div', 'notices width66 floatLeft');
289
290                 for ($i = 0; $i < min($cnt, NOTICES_PER_PAGE); $i++) {
291                         if ($notice->fetch()) {
292                                 $this->show_notice($notice);
293                         } else {
294                                 // shouldn't happen!
295                                 break;
296                         }
297                 }
298
299                 if ($page > 1) {
300                         common_element_start('span', 'floatLeft width25');
301                         common_element('a', array('href' => common_local_url('showstream', 
302                                                                                                                                  array('nickname' => $profile->nickname,
303                                                                                                                                            'page' => $page-1)),
304                                                                           'class' => 'newer'),
305                                                    _t('Newer'));
306                         common_element_end('span');
307                 }
308                 
309                 if ($cnt > NOTICES_PER_PAGE) {
310                         common_element_start('span', 'floatRight width25');
311                         common_element('a', array('href' => common_local_url('showstream', 
312                                                                                                                                  array('nickname' => $profile->nickname,
313                                                                                                                                            'page' => $page+1)),
314                                                                           'class' => 'older'),
315                                                    _t('Older'));
316                         common_element_end('span');
317                 }
318                 
319                 # XXX: show a link for the next page
320                 common_element_end('div');
321         }
322
323         function show_last_notice($profile) {
324
325                 common_element_start('div', 'lastnotice');
326                 common_element('h2', 'lastnotice', _t('Currently'));
327
328                 $notice = DB_DataObject::factory('notice');
329                 $notice->profile_id = $profile->id;
330                 $notice->orderBy('created DESC');
331                 $notice->limit(0, 1);
332
333                 if ($notice->find(true)) {
334                         # FIXME: URL, image, video, audio
335                         common_element_start('span', array('class' => 'content'));
336                         common_raw(common_render_content($notice->content, $notice));
337                         common_element_end('span');
338                 }
339
340                 common_element_end('div');
341         }
342         
343         function show_notice($notice) {
344                 $profile = $notice->getProfile();
345                 # XXX: RDFa
346                 common_element_start('div', array('class' => 'notice',
347                                                                                   'id' => 'notice-' . $notice->id));
348                 $noticeurl = common_local_url('shownotice', array('notice' => $notice->id));
349                 # FIXME: URL, image, video, audio
350                 common_element_start('span', array('class' => 'content'));
351                 common_raw(common_render_content($notice->content, $notice));
352                 common_element_end('span');
353                 common_element('a', array('class' => 'notice',
354                                                                   'href' => $noticeurl),
355                                            common_date_string($notice->created));
356                 common_element_end('div');
357         }
358 }