We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

showstream.php 8.4 KB
Newer Older
Evan Prodromou's avatar
Evan Prodromou committed
1
<?php
2
/*
Evan Prodromou's avatar
Evan Prodromou committed
3 4
 * Laconica - a distributed open-source microblogging tool
 * Copyright (C) 2008, Controlez-Vous, Inc.
5
 *
Evan Prodromou's avatar
Evan Prodromou committed
6 7 8 9
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
10
 *
Evan Prodromou's avatar
Evan Prodromou committed
11 12 13 14
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
15
 *
Evan Prodromou's avatar
Evan Prodromou committed
16 17 18 19
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

Evan Prodromou's avatar
Evan Prodromou committed
20
if (!defined('LACONICA')) { exit(1); }
Evan Prodromou's avatar
Evan Prodromou committed
21

Evan Prodromou's avatar
Evan Prodromou committed
22 23
require_once(INSTALLDIR.'/lib/stream.php');

Evan Prodromou's avatar
Evan Prodromou committed
24 25 26 27 28 29
define('SUBSCRIPTIONS_PER_ROW', 5);
define('SUBSCRIPTIONS', 80);

class ShowstreamAction extends StreamAction {

	function handle($args) {
30

Evan Prodromou's avatar
Evan Prodromou committed
31 32
		parent::handle($args);

33
		$nickname = common_canonical_nickname($this->arg('nickname'));
Evan Prodromou's avatar
Evan Prodromou committed
34 35
		$user = User::staticGet('nickname', $nickname);

Evan Prodromou's avatar
Evan Prodromou committed
36 37
		if (!$user) {
			$this->no_such_user();
38
			return;
39
		}
Evan Prodromou's avatar
Evan Prodromou committed
40 41 42 43 44

		$profile = $user->getProfile();

		if (!$profile) {
			common_server_error(_t('User record exists without profile.'));
Evan Prodromou's avatar
Evan Prodromou committed
45
			return;
Evan Prodromou's avatar
Evan Prodromou committed
46
		}
47

Evan Prodromou's avatar
Evan Prodromou committed
48
		# Looks like we're good; show the header
49

50
		common_show_header($profile->nickname, array($this, 'show_header'), $user);
Evan Prodromou's avatar
Evan Prodromou committed
51 52

		$cur = common_current_user();
53

Evan Prodromou's avatar
Evan Prodromou committed
54
		if ($cur && $profile->id == $cur->id) {
Evan Prodromou's avatar
Evan Prodromou committed
55
			common_notice_form();
Evan Prodromou's avatar
Evan Prodromou committed
56
		}
57

Evan Prodromou's avatar
Evan Prodromou committed
58 59
		$this->show_sidebar($profile);
		
60 61
		$this->show_notices($profile);
		
Evan Prodromou's avatar
Evan Prodromou committed
62 63 64
		common_show_footer();
	}

65 66 67 68
	function show_header($user) {
		common_element('link', array('rel' => 'alternate',
									 'href' => common_local_url('rss10', array('nickname' =>
																			   $user->nickname)),
69
									 'type' => 'application/rss+xml',
70 71 72
									 'title' => _t('Notice feed for ') . $user->nickname));
	}
	
Evan Prodromou's avatar
Evan Prodromou committed
73 74 75 76 77 78
	function no_such_user() {
		common_user_error('No such user');
	}

	function show_sidebar($profile) {

79
		common_element_start('div', 'sidebar width33 floatRight greenBg');
Evan Prodromou's avatar
Evan Prodromou committed
80

Evan Prodromou's avatar
Evan Prodromou committed
81
		$this->show_profile($profile);
Evan Prodromou's avatar
Evan Prodromou committed
82

Evan Prodromou's avatar
Evan Prodromou committed
83
		$this->show_last_notice($profile);
Evan Prodromou's avatar
Evan Prodromou committed
84

Evan Prodromou's avatar
Evan Prodromou committed
85 86
		$cur = common_current_user();

87
		if ($cur && $cur->id != $profile->id) {
Evan Prodromou's avatar
Evan Prodromou committed
88 89 90 91 92 93
			if ($cur->isSubscribed($profile)) {
				$this->show_unsubscribe_form($profile);
			} else {
				$this->show_subscribe_form($profile);
			}
		}
94

Evan Prodromou's avatar
Evan Prodromou committed
95
		$this->show_statistics($profile);
Evan Prodromou's avatar
Evan Prodromou committed
96

Evan Prodromou's avatar
Evan Prodromou committed
97
		$this->show_subscriptions($profile);
98

Evan Prodromou's avatar
Evan Prodromou committed
99
		common_element_end('div');
Evan Prodromou's avatar
Evan Prodromou committed
100
	}
Evan Prodromou's avatar
Evan Prodromou committed
101
	
Evan Prodromou's avatar
Evan Prodromou committed
102
	function show_profile($profile) {
103
		common_element_start('div', 'profile');
Evan Prodromou's avatar
Evan Prodromou committed
104

Evan Prodromou's avatar
Evan Prodromou committed
105 106 107 108 109 110
		$avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
		if ($avatar) {
			common_element('img', array('src' => $avatar->url,
										'class' => 'avatar profile',
										'width' => AVATAR_PROFILE_SIZE,
										'height' => AVATAR_PROFILE_SIZE,
Evan Prodromou's avatar
Evan Prodromou committed
111
										'alt' => $profile->nickname));
Evan Prodromou's avatar
Evan Prodromou committed
112
		}
Evan Prodromou's avatar
Evan Prodromou committed
113
		if ($profile->fullname) {
Evan Prodromou's avatar
Evan Prodromou committed
114
			common_element_start('div', 'fullname');
Evan Prodromou's avatar
Evan Prodromou committed
115
			if ($profile->homepage) {
Evan Prodromou's avatar
Evan Prodromou committed
116
				common_element('a', array('href' => $profile->homepage),
Evan Prodromou's avatar
Evan Prodromou committed
117 118
							   $profile->fullname);
			} else {
Evan Prodromou's avatar
Evan Prodromou committed
119
				common_text($profile->fullname);
Evan Prodromou's avatar
Evan Prodromou committed
120
			}
Evan Prodromou's avatar
Evan Prodromou committed
121
			common_element_end('div');
Evan Prodromou's avatar
Evan Prodromou committed
122 123
		}
		if ($profile->location) {
Evan Prodromou's avatar
Evan Prodromou committed
124
			common_element('div', 'location', $profile->location);
Evan Prodromou's avatar
Evan Prodromou committed
125 126 127 128
		}
		if ($profile->bio) {
			common_element('div', 'bio', $profile->bio);
		}
Evan Prodromou's avatar
Evan Prodromou committed
129
		common_element_end('div');
Evan Prodromou's avatar
Evan Prodromou committed
130
	}
Evan Prodromou's avatar
Evan Prodromou committed
131 132

	function show_subscribe_form($profile) {
133
		common_element_start('form', array('id' => 'subscribe', 'method' => 'POST',
Evan Prodromou's avatar
Evan Prodromou committed
134 135 136 137 138
										   'action' => common_local_url('subscribe')));
		common_element('input', array('id' => 'subscribeto',
									  'name' => 'subscribeto',
									  'type' => 'hidden',
									  'value' => $profile->nickname));
Evan Prodromou's avatar
Evan Prodromou committed
139 140 141
		common_element('input', array('type' => 'submit',
									  'class' => 'button',
									  'value' => _t('Subscribe')));
142
		common_element_end('form');
Evan Prodromou's avatar
Evan Prodromou committed
143 144 145
	}

	function show_unsubscribe_form($profile) {
146
		common_element_start('form', array('id' => 'unsubscribe', 'method' => 'POST',
Evan Prodromou's avatar
Evan Prodromou committed
147 148 149 150 151
										   'action' => common_local_url('unsubscribe')));
		common_element('input', array('id' => 'unsubscribeto',
									  'name' => 'unsubscribeto',
									  'type' => 'hidden',
									  'value' => $profile->nickname));
Evan Prodromou's avatar
Evan Prodromou committed
152 153 154
		common_element('input', array('type' => 'submit',
									  'class' => 'button',
									  'value' => _t('Unsubscribe')));
155
		common_element_end('form');
Evan Prodromou's avatar
Evan Prodromou committed
156
	}
157

Evan Prodromou's avatar
Evan Prodromou committed
158
	function show_subscriptions($profile) {
Evan Prodromou's avatar
Evan Prodromou committed
159 160
		global $config;
		
Evan Prodromou's avatar
Evan Prodromou committed
161
		# XXX: add a limit
Evan Prodromou's avatar
Evan Prodromou committed
162
		$subs = $profile->getLink('id', 'subscription', 'subscriber');
163

164
		common_element_start('div', 'subscriptions');
165

Evan Prodromou's avatar
Evan Prodromou committed
166
		common_element('h2', 'subscriptions', _t('Subscriptions'));
Evan Prodromou's avatar
Evan Prodromou committed
167

Evan Prodromou's avatar
Evan Prodromou committed
168 169
		$cnt = 0;

170 171 172 173 174 175 176
		if ($subs) {
			while ($subs->fetch()) {
				$cnt++;
				if ($cnt % SUBSCRIPTIONS_PER_ROW == 1) {
					common_element_start('div', 'row');
				}

Evan Prodromou's avatar
Evan Prodromou committed
177 178
				common_element_start('a', array('title' => ($subs->fullname) ?
												$subs->fullname :
179 180 181 182
												$subs->nickname,
												'href' => $subs->profileurl,
												'class' => 'subscription'));
				$avatar = $subs->getAvatar(AVATAR_MINI_SIZE);
Evan Prodromou's avatar
Evan Prodromou committed
183
				common_element('img', array('src' => (($avatar) ? $avatar->url :  common_default_avatar(AVATAR_MINI_SIZE)),
184
											'width' => AVATAR_MINI_SIZE,
Evan Prodromou's avatar
Evan Prodromou committed
185
											'height' => AVATAR_MINI_SIZE,
Evan Prodromou's avatar
Evan Prodromou committed
186 187 188 189
											'class' => 'avatar mini',
											'alt' =>  ($subs->fullname) ?
												$subs->fullname :
												$subs->nickname));
190 191 192 193 194 195 196 197 198
				common_element_end('a');

				if ($cnt % SUBSCRIPTIONS_PER_ROW == 0) {
					common_element_end('div');
				}

				if ($cnt == SUBSCRIPTIONS) {
					break;
				}
Evan Prodromou's avatar
Evan Prodromou committed
199 200 201
			}
		}

202
		common_element('a', array('href' => common_local_url('subscriptions',
203
															 array('nickname' => $profile->nickname)),
Evan Prodromou's avatar
Evan Prodromou committed
204 205
								  'class' => 'moresubscriptions'),
					   _t('All subscriptions'));
Evan Prodromou's avatar
Evan Prodromou committed
206

207
		common_element_end('div');
Evan Prodromou's avatar
Evan Prodromou committed
208 209
	}

Evan Prodromou's avatar
Evan Prodromou committed
210
	function show_statistics($profile) {
Evan Prodromou's avatar
Evan Prodromou committed
211

Evan Prodromou's avatar
Evan Prodromou committed
212 213 214
		// XXX: WORM cache this
		$subs = DB_DataObject::factory('subscription');
		$subs->subscriber = $profile->id;
215
		$subs_count = $subs->count();
216

217 218 219
		if (!$subs_count) {
			$subs_count = 0;
		}
Evan Prodromou's avatar
Evan Prodromou committed
220

Evan Prodromou's avatar
Evan Prodromou committed
221 222
		$subbed = DB_DataObject::factory('subscription');
		$subbed->subscribed = $profile->id;
223
		$subbed_count = $subbed->count();
224

225 226 227
		if (!$subbed_count) {
			$subbed_count = 0;
		}
Evan Prodromou's avatar
Evan Prodromou committed
228

Evan Prodromou's avatar
Evan Prodromou committed
229
		$notices = DB_DataObject::factory('notice');
Evan Prodromou's avatar
Evan Prodromou committed
230
		$notices->profile_id = $profile->id;
231
		$notice_count = $notices->count();
Evan Prodromou's avatar
Evan Prodromou committed
232

233 234 235
		if (!$notice_count) {
			$notice_count = 0;
		}
236

Evan Prodromou's avatar
Evan Prodromou committed
237 238
		common_element_start('div', 'statistics');
		common_element('h2', 'statistics', _t('Statistics'));
Evan Prodromou's avatar
Evan Prodromou committed
239

Evan Prodromou's avatar
Evan Prodromou committed
240
		# Other stats...?
241
		common_element_start('dl', 'statistics');
Evan Prodromou's avatar
Evan Prodromou committed
242 243 244 245 246 247
		common_element('dt', 'subscriptions', _t('Subscriptions'));
		common_element('dd', 'subscriptions', $subs_count);
		common_element('dt', 'subscribers', _t('Subscribers'));
		common_element('dd', 'subscribers', $subbed_count);
		common_element('dt', 'notices', _t('Notices'));
		common_element('dd', 'notices', $notice_count);
248
		common_element_end('dl');
Evan Prodromou's avatar
Evan Prodromou committed
249

Evan Prodromou's avatar
Evan Prodromou committed
250
		common_element_end('div');
Evan Prodromou's avatar
Evan Prodromou committed
251
	}
252

Evan Prodromou's avatar
Evan Prodromou committed
253
	function show_notices($profile) {
Evan Prodromou's avatar
Evan Prodromou committed
254

Evan Prodromou's avatar
Evan Prodromou committed
255 256
		$notice = DB_DataObject::factory('notice');
		$notice->profile_id = $profile->id;
257

Evan Prodromou's avatar
Evan Prodromou committed
258
		$notice->orderBy('created DESC');
259

Evan Prodromou's avatar
Evan Prodromou committed
260
		$page = $this->arg('page') || 1;
261

262
		$notice->limit((($page-1)*NOTICES_PER_PAGE), NOTICES_PER_PAGE);
263

Evan Prodromou's avatar
Evan Prodromou committed
264 265
		$notice->find();

266
		common_element_start('div', 'notices width66 floatLeft');
Evan Prodromou's avatar
Evan Prodromou committed
267

Evan Prodromou's avatar
Evan Prodromou committed
268 269
		common_element_start('ul', 'bigLinks');
		
Evan Prodromou's avatar
Evan Prodromou committed
270 271 272
		while ($notice->fetch()) {
			$this->show_notice($notice);
		}
Evan Prodromou's avatar
Evan Prodromou committed
273 274 275
		
		common_element_end('ul');
		
276
		# XXX: show a link for the next page
277
		common_element_end('div');
Evan Prodromou's avatar
Evan Prodromou committed
278
	}
279

Evan Prodromou's avatar
Evan Prodromou committed
280
	function show_last_notice($profile) {
Evan Prodromou's avatar
Evan Prodromou committed
281

Evan Prodromou's avatar
Evan Prodromou committed
282 283 284
		common_element_start('div', 'lastnotice');
		common_element('h2', 'lastnotice', _t('Currently'));

Evan Prodromou's avatar
Evan Prodromou committed
285 286 287
		$notice = DB_DataObject::factory('notice');
		$notice->profile_id = $profile->id;
		$notice->orderBy('created DESC');
288
		$notice->limit(0, 1);
Evan Prodromou's avatar
Evan Prodromou committed
289

Evan Prodromou's avatar
Evan Prodromou committed
290
		if ($notice->find(true)) {
Evan Prodromou's avatar
Evan Prodromou committed
291
			# FIXME: URL, image, video, audio
292
			common_element('span', array('class' => 'content'),
Evan Prodromou's avatar
Evan Prodromou committed
293
						   $notice->content);
Evan Prodromou's avatar
Evan Prodromou committed
294
		}
Evan Prodromou's avatar
Evan Prodromou committed
295

Evan Prodromou's avatar
Evan Prodromou committed
296
		common_element_end('div');
Evan Prodromou's avatar
Evan Prodromou committed
297
	}
Evan Prodromou's avatar
Evan Prodromou committed
298 299 300 301 302 303 304 305 306 307 308 309
	
	function show_notice($notice) {
		$profile = $notice->getProfile();
		# XXX: RDFa
		common_element_start('li', array('class' => 'notice',
										 'id' => 'notice-' . $notice->id));
		$noticeurl = common_local_url('shownotice', array('notice' => $notice->id));
		# FIXME: URL, image, video, audio
		common_element_start('a', array('class' => 'notice',
								  'href' => $noticeurl));
		common_element('span', 'title', common_date_string($notice->created));
		common_element('span', 'desc', $notice->content);
Evan Prodromou's avatar
Evan Prodromou committed
310
		common_element_end('a');
Evan Prodromou's avatar
Evan Prodromou committed
311 312
		common_element_end('li');
	}
Evan Prodromou's avatar
Evan Prodromou committed
313
}