]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/showstream.php
3eae5eb0ffb3d9e0a6da940be990c2135b1769fa
[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         }
140
141         function no_such_user() {
142                 $this->client_error(_('No such user.'), 404);
143         }
144
145         function show_profile($profile) {
146
147                 common_element_start('div', array('id' => 'profile'));
148
149                 $this->show_personal($profile);
150
151                 $this->show_last_notice($profile);
152
153                 $cur = common_current_user();
154
155                 $this->show_subscriptions($profile);
156
157                 common_element_end('div');
158         }
159
160         function show_personal($profile) {
161
162                 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
163                 common_element_start('div', array('id' => 'profile_avatar'));
164                 common_element('img', array('src' => ($avatar) ? common_avatar_display_url($avatar) : common_default_avatar(AVATAR_PROFILE_SIZE),
165                                                                         'class' => 'avatar profile',
166                                                                         'width' => AVATAR_PROFILE_SIZE,
167                                                                         'height' => AVATAR_PROFILE_SIZE,
168                                                                         'alt' => $profile->nickname));
169
170         common_element_start('ul', array('id' => 'profile_actions'));
171
172         common_element_start('li', array('id' => 'profile_subscribe'));
173                 $cur = common_current_user();
174                 if ($cur) {
175                         if ($cur->id != $profile->id) {
176                                 if ($cur->isSubscribed($profile)) {
177                                         $this->show_unsubscribe_form($profile);
178                                 } else {
179                                         $this->show_subscribe_form($profile);
180                                 }
181                         }
182                 } else {
183                         $this->show_remote_subscribe_link($profile);
184                 }
185         common_element_end('li');
186                 
187                 $user = User::staticGet('id', $profile->id);
188                 
189                 if ($cur && $cur->id != $user->id && $cur->mutuallySubscribed($user)) {
190             common_element_start('li', array('id' => 'profile_send_a_new_message'));
191                         common_element('a', array('href' => common_local_url('newmessage', array('to' => $user->id))),
192                                                    _('Send a message'));
193             common_element_end('li');
194             if ($user->email && $user->emailnotifynudge) {
195                 common_element_start('li', array('id' => 'profile_nudge'));
196                 common_nudge_form($user);
197                 common_element_end('li');
198             }
199                 }
200         common_element_end('ul');
201                 
202                 common_element_end('div');
203
204                 common_element_start('div', array('id' => 'profile_information'));
205
206                 if ($profile->fullname) {
207                         common_element('h1', NULL, $profile->fullname . ' (' . $profile->nickname . ')');
208                 } else {
209                         common_element('h1', NULL, $profile->nickname);
210                 }
211
212
213                 if ($profile->location) {
214                         common_element('p', 'location', $profile->location);
215                 }
216                 if ($profile->bio) {
217                         common_element('p', 'description', $profile->bio);
218                 }
219                 if ($profile->homepage) {
220                         common_element_start('p', 'website');
221                         common_element('a', array('href' => $profile->homepage,
222                                                                           'rel' => 'me'),
223                                                    $profile->homepage);
224                         common_element_end('p');
225                 }
226
227                 $this->show_statistics($profile);
228
229                 common_element_end('div');
230         }
231
232         function show_subscribe_form($profile) {
233                 common_element_start('form', array('id' => 'subscribe', 'method' => 'post',
234                                                                                    'action' => common_local_url('subscribe')));
235                 common_hidden('token', common_session_token());
236                 common_element('input', array('id' => 'subscribeto',
237                                                                           'name' => 'subscribeto',
238                                                                           'type' => 'hidden',
239                                                                           'value' => $profile->nickname));
240                 common_element('input', array('type' => 'submit',
241                                                                           'class' => 'submit',
242                                                                           'value' => _('Subscribe')));
243                 common_element_end('form');
244         }
245
246         function show_remote_subscribe_link($profile) {
247                 $url = common_local_url('remotesubscribe',
248                                         array('nickname' => $profile->nickname));
249                 common_element('a', array('href' => $url,
250                                                                   'id' => 'remotesubscribe'),
251                                            _('Subscribe'));
252         }
253
254         function show_unsubscribe_form($profile) {
255                 common_element_start('form', array('id' => 'unsubscribe', 'method' => 'post',
256                                                                                    'action' => common_local_url('unsubscribe')));
257                 common_hidden('token', common_session_token());
258                 common_element('input', array('id' => 'unsubscribeto',
259                                                                           'name' => 'unsubscribeto',
260                                                                           'type' => 'hidden',
261                                                                           'value' => $profile->nickname));
262                 common_element('input', array('type' => 'submit',
263                                                                           'class' => 'submit',
264                                                                           'value' => _('Unsubscribe')));
265                 common_element_end('form');
266         }
267
268         function show_subscriptions($profile) {
269                 global $config;
270
271                 $subs = DB_DataObject::factory('subscription');
272                 $subs->subscriber = $profile->id;
273                 $subs->whereAdd('subscribed != ' . $profile->id);
274                 
275                 $subs->orderBy('created DESC');
276
277                 # We ask for an extra one to know if we need to do another page
278
279                 $subs->limit(0, SUBSCRIPTIONS + 1);
280
281                 $subs_count = $subs->find();
282
283                 common_element_start('div', array('id' => 'subscriptions'));
284
285                 common_element('h2', NULL, _('Subscriptions'));
286
287                 if ($subs_count > 0) {
288
289                         common_element_start('ul', array('id' => 'subscriptions_avatars'));
290
291                         for ($i = 0; $i < min($subs_count, SUBSCRIPTIONS); $i++) {
292
293                                 if (!$subs->fetch()) {
294                                         common_debug('Weirdly, broke out of subscriptions loop early', __FILE__);
295                                         break;
296                                 }
297
298                                 $other = Profile::staticGet($subs->subscribed);
299
300                                 if (!$other) {
301                                         common_log_db_error($subs, 'SELECT', __FILE__);
302                                         continue;
303                                 }
304                                 
305                                 common_element_start('li');
306                                 common_element_start('a', array('title' => ($other->fullname) ?
307                                                                                                 $other->fullname :
308                                                                                                 $other->nickname,
309                                                                                                 'href' => $other->profileurl,
310                                                                                                 'rel' => 'contact',
311                                                                                                 'class' => 'subscription'));
312                                 $avatar = $other->getAvatar(AVATAR_MINI_SIZE);
313                                 common_element('img', array('src' => (($avatar) ? common_avatar_display_url($avatar) :  common_default_avatar(AVATAR_MINI_SIZE)),
314                                                                                         'width' => AVATAR_MINI_SIZE,
315                                                                                         'height' => AVATAR_MINI_SIZE,
316                                                                                         'class' => 'avatar mini',
317                                                                                         'alt' =>  ($other->fullname) ?
318                                                                                         $other->fullname :
319                                                                                         $other->nickname));
320                                 common_element_end('a');
321                                 common_element_end('li');
322                         }
323
324                         common_element_end('ul');
325                 }
326
327                 if ($subs_count > SUBSCRIPTIONS) {
328                         common_element_start('p', array('id' => 'subscriptions_viewall'));
329
330                         common_element('a', array('href' => common_local_url('subscriptions',
331                                                                                                                                  array('nickname' => $profile->nickname)),
332                                                                           'class' => 'moresubscriptions'),
333                                                    _('All subscriptions'));
334                         common_element_end('p');
335                 }
336
337                 common_element_end('div');
338         }
339
340         function show_statistics($profile) {
341
342                 // XXX: WORM cache this
343                 $subs = DB_DataObject::factory('subscription');
344                 $subs->subscriber = $profile->id;
345                 $subs_count = (int) $subs->count() - 1;
346
347                 $subbed = DB_DataObject::factory('subscription');
348                 $subbed->subscribed = $profile->id;
349                 $subbed_count = (int) $subbed->count() - 1;
350
351                 $notices = DB_DataObject::factory('notice');
352                 $notices->profile_id = $profile->id;
353                 $notice_count = (int) $notices->count();
354
355                 common_element_start('div', 'statistics');
356                 common_element('h2', 'statistics', _('Statistics'));
357
358                 # Other stats...?
359                 common_element_start('dl', 'statistics');
360                 common_element('dt', 'membersince', _('Member since'));
361                 common_element('dd', 'membersince', date('j M Y',
362                                                                                                  strtotime($profile->created)));
363
364                 common_element_start('dt', 'subscriptions');
365                 common_element('a', array('href' => common_local_url('subscriptions',
366                                                                                                                          array('nickname' => $profile->nickname))),
367                                            _('Subscriptions'));
368                 common_element_end('dt');
369                 common_element('dd', 'subscriptions', (is_int($subs_count)) ? $subs_count : '0');
370                 common_element_start('dt', 'subscribers');
371                 common_element('a', array('href' => common_local_url('subscribers',
372                                                                                                                          array('nickname' => $profile->nickname))),
373                                            _('Subscribers'));
374                 common_element_end('dt');
375                 common_element('dd', 'subscribers', (is_int($subbed_count)) ? $subbed_count : '0');
376                 common_element('dt', 'notices', _('Notices'));
377                 common_element('dd', 'notices', (is_int($notice_count)) ? $notice_count : '0');
378                 # XXX: link these to something
379                 common_element('dt', 'tags', _('Tags'));
380                 common_element_start('dd', 'tags');
381                 $tags = Profile_tag::getTags($profile->id, $profile->id);
382                 foreach ($tags as $tag) {
383                         common_element('a', array('rel' => 'tag',
384                                                                           'href' => common_local_url('peopletag',
385                                                                                                                                  array('tag' => $tag))),
386                                                    $tag);
387                 }
388             common_element_end('dd');
389         
390                 common_element_end('dl');
391
392                 common_element_end('div');
393         }
394
395         function show_notices($user) {
396
397                 $page = ($this->arg('page')) ? ($this->arg('page')+0) : 1;
398
399                 $notice = $user->getNotices(($page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
400                 
401                 $cnt = 0;
402
403                 if ($notice) {
404                 
405                         common_element_start('ul', array('id' => 'notices'));
406                         
407                         while ($notice->fetch()) {
408                                 $cnt++;
409                                 if ($cnt > NOTICES_PER_PAGE) {
410                                         break;
411                                 }
412                                 $this->show_notice($notice);
413                         }
414
415                         common_element_end('ul');
416                 }
417                 
418                 common_pagination($page>1, $cnt>NOTICES_PER_PAGE, $page,
419                                                   'showstream', array('nickname' => $user->nickname));
420         }
421
422         function show_last_notice($profile) {
423
424                 common_element('h2', NULL, _('Currently'));
425
426                 $notice = $profile->getCurrentNotice();
427
428                 if ($notice) {
429                         # FIXME: URL, image, video, audio
430                         common_element_start('p', array('class' => 'notice_current'));
431                         if ($notice->rendered) {
432                                 common_raw($notice->rendered);
433                         } else {
434                                 # XXX: may be some uncooked notices in the DB,
435                                 # we cook them right now. This can probably disappear in future
436                                 # versions (>> 0.4.x)
437                                 common_raw(common_render_content($notice->content, $notice));
438                         }
439                         common_element_end('p');
440                 }
441         }
442
443         function show_notice($notice) {
444                 $profile = $notice->getProfile();
445                 $user = common_current_user();
446
447                 # XXX: RDFa
448                 common_element_start('li', array('class' => 'notice_single',
449                                                                                  'id' => 'notice-' . $notice->id));
450                 if ($user) {
451                         if ($user->hasFave($notice)) {
452                                 common_disfavor_form($notice);
453                         } else {
454                                 common_favor_form($notice);
455                         }
456                 }
457                 $noticeurl = common_local_url('shownotice', array('notice' => $notice->id));
458                 # FIXME: URL, image, video, audio
459                 common_element_start('p');
460                 if ($notice->rendered) {
461                         common_raw($notice->rendered);
462                 } else {
463                         # XXX: may be some uncooked notices in the DB,
464                         # we cook them right now. This can probably disappear in future
465                         # versions (>> 0.4.x)
466                         common_raw(common_render_content($notice->content, $notice));
467                 }
468                 common_element_end('p');
469                 common_element_start('p', array('class' => 'time'));
470                 common_element('a', array('class' => 'permalink',
471                                                                   'href' => $noticeurl,
472                                                                   'title' => common_exact_date($notice->created)),
473                                            common_date_string($notice->created));
474                 if ($notice->source) {
475                         common_text(_(' from '));
476                         $this->source_link($notice->source);
477                 }
478                 if ($notice->reply_to) {
479                         $replyurl = common_local_url('shownotice', array('notice' => $notice->reply_to));
480                         common_text(' (');
481                         common_element('a', array('class' => 'inreplyto',
482                                                                           'href' => $replyurl),
483                                                    _('in reply to...'));
484                         common_text(')');
485                 }
486                 common_element_start('a',
487                                                          array('href' => common_local_url('newnotice',
488                                                                                                                           array('replyto' => $profile->nickname)),
489                                                                    'onclick' => 'doreply("'.$profile->nickname.'"); return false',
490                                                                    'title' => _('reply'),
491                                                                    'class' => 'replybutton'));
492                 common_raw('&rarr;');
493                 common_element_end('a');
494                 if ($user && $notice->profile_id == $user->id) {
495                         $deleteurl = common_local_url('deletenotice', array('notice' => $notice->id));
496                         common_element_start('a', array('class' => 'deletenotice',
497                                                                                         'href' => $deleteurl,
498                                                                                         'title' => _('delete')));
499                         common_raw('&times;');
500                         common_element_end('a');
501                 }
502                 
503                 common_element_end('p');
504                 common_element_end('li');
505         }
506 }