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