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