]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/showstream.php
generate an etag for shownotice
[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(_('User has no 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,
56                                                    array($this, 'show_header'), $user,
57                                                    array($this, 'show_top'));
58
59                 $this->show_profile($profile);
60
61                 $this->show_notices($user);
62
63                 common_show_footer();
64         }
65
66         function show_top($user) {
67                 $cur = common_current_user();
68
69                 if ($cur && $cur->id == $user->id) {
70                         common_notice_form('showstream');
71                 }
72
73                 $this->views_menu();
74
75                 $this->show_feeds_list(array(0=>array('href'=>common_local_url('userrss', array('nickname' => $user->nickname)), 
76                                                                                           'type' => 'rss',
77                                                                                           'version' => 'RSS 1.0',
78                                                                                           'item' => 'notices'),
79                                                                          1=>array('href'=>common_local_url('usertimeline', array('nickname' => $user->nickname)), 
80                                                                                           'type' => 'atom',
81                                                                                           'version' => 'Atom 1.0',
82                                                                                           'item' => 'usertimeline'),
83
84                                                                          2=>array('href'=>common_local_url('foaf',array('nickname' => $user->nickname)),
85                                                                                           'type' => 'rdf',
86                                                                                           'version' => 'FOAF',
87                                                                                           'item' => 'foaf')));
88         }
89
90
91
92
93         function show_header($user) {
94                 # Feeds
95                 common_element('link', array('rel' => 'alternate',
96                                                                          'href' => common_local_url('api', 
97                                                                                                                                 array('apiaction' => 'statuses',
98                                                                                                                                           'method' => 'user_timeline.rss',
99                                                                                                                                           'argument' => $user->nickname)),
100                                                                          'type' => 'application/rss+xml',
101                                                                          'title' => sprintf(_('Notice feed for %s'), $user->nickname)));
102                 common_element('link', array('rel' => 'alternate feed',
103                                                                          'href' => common_local_url('api', 
104                                                                                                                                 array('apiaction' => 'statuses',
105                                                                                                                                           'method' => 'user_timeline.atom',
106                                                                                                                                           'argument' => $user->nickname)),
107                                                                          'type' => 'application/atom+xml',
108                                                                          'title' => sprintf(_('Notice feed for %s'), $user->nickname)));
109                 common_element('link', array('rel' => 'alternate',
110                                                                          'href' => common_local_url('userrss', array('nickname' =>
111                                                                                                                                                            $user->nickname)),
112                                                                          'type' => 'application/rdf+xml',
113                                                                          'title' => sprintf(_('Notice feed for %s'), $user->nickname)));
114                 # FOAF
115                 common_element('link', array('rel' => 'meta',
116                                                                          'href' => common_local_url('foaf', array('nickname' =>
117                                                                                                                                                           $user->nickname)),
118                                                                          'type' => 'application/rdf+xml',
119                                                                          'title' => 'FOAF'));
120                 # for remote subscriptions etc.
121                 common_element('meta', array('http-equiv' => 'X-XRDS-Location',
122                                                                          'content' => common_local_url('xrds', array('nickname' =>
123                                                                                                                                                            $user->nickname))));
124                 $profile = $user->getProfile();
125                 if ($profile->bio) {
126                         common_element('meta', array('name' => 'description',
127                                                                                  'content' => $profile->bio));
128                 }
129
130                 if ($user->emailmicroid && $user->email && $profile->profileurl) {
131                         common_element('meta', array('name' => 'microid',
132                                                                                  'content' => "mailto+http:sha1:" . sha1(sha1('mailto:' . $user->email) . sha1($profile->profileurl))));
133                 }
134                 if ($user->jabbermicroid && $user->jabber && $profile->profileurl) {
135                         common_element('meta', array('name' => 'microid',
136                                                                                  'content' => "xmpp+http:sha1:" . sha1(sha1('xmpp:' . $user->jabber) . sha1($profile->profileurl))));
137                 }
138
139                 # See https://wiki.mozilla.org/Microsummaries
140                 
141                 common_element('link', array('rel' => 'microsummary',
142                                                                          'href' => common_local_url('microsummary',
143                                                                                                                                 array('nickname' => $profile->nickname))));
144         }
145
146         function no_such_user() {
147                 $this->client_error(_('No such user.'), 404);
148         }
149
150         function show_profile($profile) {
151
152                 common_element_start('div', array('id' => 'profile'));
153
154                 $this->show_personal($profile);
155
156                 $this->show_last_notice($profile);
157
158                 $cur = common_current_user();
159
160                 $this->show_subscriptions($profile);
161
162                 common_element_end('div');
163         }
164
165         function show_personal($profile) {
166
167                 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
168                 common_element_start('div', array('id' => 'profile_avatar'));
169                 common_element('img', array('src' => ($avatar) ? common_avatar_display_url($avatar) : common_default_avatar(AVATAR_PROFILE_SIZE),
170                                                                         'class' => 'avatar profile',
171                                                                         'width' => AVATAR_PROFILE_SIZE,
172                                                                         'height' => AVATAR_PROFILE_SIZE,
173                                                                         'alt' => $profile->nickname));
174
175         common_element_start('ul', array('id' => 'profile_actions'));
176
177         common_element_start('li', array('id' => 'profile_subscribe'));
178                 $cur = common_current_user();
179                 if ($cur) {
180                         if ($cur->id != $profile->id) {
181                                 if ($cur->isSubscribed($profile)) {
182                                         common_unsubscribe_form($profile);
183                                 } else {
184                                         common_subscribe_form($profile);
185                                 }
186                         }
187                 } else {
188                         $this->show_remote_subscribe_link($profile);
189                 }
190         common_element_end('li');
191                 
192                 $user = User::staticGet('id', $profile->id);
193                 common_profile_new_message_nudge($cur, $user, $profile);
194         
195                 common_element_end('ul');
196                 
197                 common_element_end('div');
198
199                 common_element_start('div', array('id' => 'profile_information'));
200
201                 if ($profile->fullname) {
202                         common_element('h1', NULL, $profile->fullname . ' (' . $profile->nickname . ')');
203                 } else {
204                         common_element('h1', NULL, $profile->nickname);
205                 }
206
207
208                 if ($profile->location) {
209                         common_element('p', 'location', $profile->location);
210                 }
211                 if ($profile->bio) {
212                         common_element('p', 'description', $profile->bio);
213                 }
214                 if ($profile->homepage) {
215                         common_element_start('p', 'website');
216                         common_element('a', array('href' => $profile->homepage,
217                                                                           'rel' => 'me'),
218                                                    $profile->homepage);
219                         common_element_end('p');
220                 }
221
222                 $this->show_statistics($profile);
223
224                 common_element_end('div');
225         }
226
227         function show_remote_subscribe_link($profile) {
228                 $url = common_local_url('remotesubscribe',
229                                         array('nickname' => $profile->nickname));
230                 common_element('a', array('href' => $url,
231                                                                   'id' => 'remotesubscribe'),
232                                            _('Subscribe'));
233         }
234
235         function show_unsubscribe_form($profile) {
236                 common_element_start('form', array('id' => 'unsubscribe', 'method' => 'post',
237                                                                                    'action' => common_local_url('unsubscribe')));
238                 common_hidden('token', common_session_token());
239                 common_element('input', array('id' => 'unsubscribeto',
240                                                                           'name' => 'unsubscribeto',
241                                                                           'type' => 'hidden',
242                                                                           'value' => $profile->nickname));
243                 common_element('input', array('type' => 'submit',
244                                                                           'class' => 'submit',
245                                                                           'value' => _('Unsubscribe')));
246                 common_element_end('form');
247         }
248
249         function show_subscriptions($profile) {
250                 global $config;
251
252                 $subs = DB_DataObject::factory('subscription');
253                 $subs->subscriber = $profile->id;
254                 $subs->whereAdd('subscribed != ' . $profile->id);
255                 
256                 $subs->orderBy('created DESC');
257
258                 # We ask for an extra one to know if we need to do another page
259
260                 $subs->limit(0, SUBSCRIPTIONS + 1);
261
262                 $subs_count = $subs->find();
263
264                 common_element_start('div', array('id' => 'subscriptions'));
265
266                 common_element('h2', NULL, _('Subscriptions'));
267
268                 if ($subs_count > 0) {
269
270                         common_element_start('ul', array('id' => 'subscriptions_avatars'));
271
272                         for ($i = 0; $i < min($subs_count, SUBSCRIPTIONS); $i++) {
273
274                                 if (!$subs->fetch()) {
275                                         common_debug('Weirdly, broke out of subscriptions loop early', __FILE__);
276                                         break;
277                                 }
278
279                                 $other = Profile::staticGet($subs->subscribed);
280
281                                 if (!$other) {
282                                         common_log_db_error($subs, 'SELECT', __FILE__);
283                                         continue;
284                                 }
285                                 
286                                 common_element_start('li');
287                                 common_element_start('a', array('title' => ($other->fullname) ?
288                                                                                                 $other->fullname :
289                                                                                                 $other->nickname,
290                                                                                                 'href' => $other->profileurl,
291                                                                                                 'rel' => 'contact',
292                                                                                                 'class' => 'subscription'));
293                                 $avatar = $other->getAvatar(AVATAR_MINI_SIZE);
294                                 common_element('img', array('src' => (($avatar) ? common_avatar_display_url($avatar) :  common_default_avatar(AVATAR_MINI_SIZE)),
295                                                                                         'width' => AVATAR_MINI_SIZE,
296                                                                                         'height' => AVATAR_MINI_SIZE,
297                                                                                         'class' => 'avatar mini',
298                                                                                         'alt' =>  ($other->fullname) ?
299                                                                                         $other->fullname :
300                                                                                         $other->nickname));
301                                 common_element_end('a');
302                                 common_element_end('li');
303                         }
304
305                         common_element_end('ul');
306                 }
307
308                 if ($subs_count > SUBSCRIPTIONS) {
309                         common_element_start('p', array('id' => 'subscriptions_viewall'));
310
311                         common_element('a', array('href' => common_local_url('subscriptions',
312                                                                                                                                  array('nickname' => $profile->nickname)),
313                                                                           'class' => 'moresubscriptions'),
314                                                    _('All subscriptions'));
315                         common_element_end('p');
316                 }
317
318                 common_element_end('div');
319         }
320
321         function show_statistics($profile) {
322
323                 // XXX: WORM cache this
324                 $subs = DB_DataObject::factory('subscription');
325                 $subs->subscriber = $profile->id;
326                 $subs_count = (int) $subs->count() - 1;
327
328                 $subbed = DB_DataObject::factory('subscription');
329                 $subbed->subscribed = $profile->id;
330                 $subbed_count = (int) $subbed->count() - 1;
331
332                 $notices = DB_DataObject::factory('notice');
333                 $notices->profile_id = $profile->id;
334                 $notice_count = (int) $notices->count();
335
336                 common_element_start('div', 'statistics');
337                 common_element('h2', 'statistics', _('Statistics'));
338
339                 # Other stats...?
340                 common_element_start('dl', 'statistics');
341                 common_element('dt', 'membersince', _('Member since'));
342                 common_element('dd', 'membersince', date('j M Y',
343                                                                                                  strtotime($profile->created)));
344
345                 common_element_start('dt', 'subscriptions');
346                 common_element('a', array('href' => common_local_url('subscriptions',
347                                                                                                                          array('nickname' => $profile->nickname))),
348                                            _('Subscriptions'));
349                 common_element_end('dt');
350                 common_element('dd', 'subscriptions', (is_int($subs_count)) ? $subs_count : '0');
351                 common_element_start('dt', 'subscribers');
352                 common_element('a', array('href' => common_local_url('subscribers',
353                                                                                                                          array('nickname' => $profile->nickname))),
354                                            _('Subscribers'));
355                 common_element_end('dt');
356                 common_element('dd', 'subscribers', (is_int($subbed_count)) ? $subbed_count : '0');
357                 common_element('dt', 'notices', _('Notices'));
358                 common_element('dd', 'notices', (is_int($notice_count)) ? $notice_count : '0');
359                 # XXX: link these to something
360                 common_element('dt', 'tags', _('Tags'));
361                 common_element_start('dd', 'tags');
362                 $tags = Profile_tag::getTags($profile->id, $profile->id);
363
364                 common_element_start('ul', 'tags xoxo');
365                 foreach ($tags as $tag) {
366                         common_element_start('li');
367                         common_element('a', array('rel' => 'bookmark tag',
368                                                                           'href' => common_local_url('peopletag',
369                                                                                                                                  array('tag' => $tag))),
370                                                    $tag);
371                         common_element_end('li');
372                 }
373                 common_element_end('ul');
374             common_element_end('dd');
375         
376                 common_element_end('dl');
377
378                 common_element_end('div');
379         }
380
381         function show_notices($user) {
382
383                 $page = ($this->arg('page')) ? ($this->arg('page')+0) : 1;
384
385                 $notice = $user->getNotices(($page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
386                 
387                 $cnt = 0;
388
389                 if ($notice) {
390                 
391                         common_element_start('ul', array('id' => 'notices'));
392                         
393                         while ($notice->fetch()) {
394                                 $cnt++;
395                                 if ($cnt > NOTICES_PER_PAGE) {
396                                         break;
397                                 }
398                                 $this->show_notice($notice);
399                         }
400
401                         common_element_end('ul');
402                 }
403                 
404                 common_pagination($page>1, $cnt>NOTICES_PER_PAGE, $page,
405                                                   'showstream', array('nickname' => $user->nickname));
406         }
407
408         function show_last_notice($profile) {
409
410                 common_element('h2', NULL, _('Currently'));
411
412                 $notice = $profile->getCurrentNotice();
413
414                 if ($notice) {
415                         # FIXME: URL, image, video, audio
416                         common_element_start('p', array('class' => 'notice_current'));
417                         if ($notice->rendered) {
418                                 common_raw($notice->rendered);
419                         } else {
420                                 # XXX: may be some uncooked notices in the DB,
421                                 # we cook them right now. This can probably disappear in future
422                                 # versions (>> 0.4.x)
423                                 common_raw(common_render_content($notice->content, $notice));
424                         }
425                         common_element_end('p');
426                 }
427         }
428
429         function show_notice($notice) {
430                 $profile = $notice->getProfile();
431                 $user = common_current_user();
432
433                 # XXX: RDFa
434                 common_element_start('li', array('class' => 'notice_single',
435                                                                                  'id' => 'notice-' . $notice->id));
436                 if ($user) {
437                         if ($user->hasFave($notice)) {
438                                 common_disfavor_form($notice);
439                         } else {
440                                 common_favor_form($notice);
441                         }
442                 }
443                 $noticeurl = common_local_url('shownotice', array('notice' => $notice->id));
444                 # FIXME: URL, image, video, audio
445                 common_element_start('p');
446                 if ($notice->rendered) {
447                         common_raw($notice->rendered);
448                 } else {
449                         # XXX: may be some uncooked notices in the DB,
450                         # we cook them right now. This can probably disappear in future
451                         # versions (>> 0.4.x)
452                         common_raw(common_render_content($notice->content, $notice));
453                 }
454                 common_element_end('p');
455                 common_element_start('p', array('class' => 'time'));
456                 common_element('a', array('class' => 'permalink',
457                                                                   'href' => $noticeurl,
458                                                                   'title' => common_exact_date($notice->created)),
459                                            common_date_string($notice->created));
460                 if ($notice->source) {
461                         common_text(_(' from '));
462                         $this->source_link($notice->source);
463                 }
464                 if ($notice->reply_to) {
465                         $replyurl = common_local_url('shownotice', array('notice' => $notice->reply_to));
466                         common_text(' (');
467                         common_element('a', array('class' => 'inreplyto',
468                                                                           'href' => $replyurl),
469                                                    _('in reply to...'));
470                         common_text(')');
471                 }
472                 common_element_start('a',
473                                                          array('href' => common_local_url('newnotice',
474                                                                                                                           array('replyto' => $profile->nickname)),
475                                                                    'onclick' => 'doreply("'.$profile->nickname.'"); return false',
476                                                                    'title' => _('reply'),
477                                                                    'class' => 'replybutton'));
478                 common_raw('&rarr;');
479                 common_element_end('a');
480                 if ($user && $notice->profile_id == $user->id) {
481                         $deleteurl = common_local_url('deletenotice', array('notice' => $notice->id));
482                         common_element_start('a', array('class' => 'deletenotice',
483                                                                                         'href' => $deleteurl,
484                                                                                         'title' => _('delete')));
485                         common_raw('&times;');
486                         common_element_end('a');
487                 }
488                 
489                 common_element_end('p');
490                 common_element_end('li');
491         }
492 }