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