3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010 StatusNet, Inc.
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.
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.
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/>.
21 * Class for activity streams
23 * Includes faves, notices, and subscriptions.
25 * We extend atomusernoticefeed since it does some nice setup for us.
28 class UserActivityStream extends AtomUserNoticeFeed
30 public $activities = array();
32 const OUTPUT_STRING = 1;
34 public $outputMode = self::OUTPUT_STRING;
39 * @param boolean $indent
40 * @param boolean $outputMode: UserActivityStream::OUTPUT_STRING to return a string,
41 * or UserActivityStream::OUTPUT_RAW to go to raw output.
42 * Raw output mode will attempt to stream, keeping less
43 * data in memory but will leave $this->activities incomplete.
45 function __construct($user, $indent = true, $outputMode = UserActivityStream::OUTPUT_STRING, $after = null)
47 parent::__construct($user, null, $indent);
49 $this->outputMode = $outputMode;
51 if ($this->outputMode == self::OUTPUT_STRING) {
52 // String buffering? Grab all the notices now.
53 $notices = $this->getNotices();
54 } elseif ($this->outputMode == self::OUTPUT_RAW) {
55 // Raw output... need to restructure from the stringer init.
56 $this->xw = new XMLWriter();
57 $this->xw->openURI('php://output');
58 if(is_null($indent)) {
59 $indent = common_config('site', 'indent');
61 $this->xw->setIndent($indent);
63 // We'll fetch notices later.
66 throw new Exception('Invalid outputMode provided to ' . __METHOD__);
69 $this->after = $after;
71 // Assume that everything but notices is feasible
72 // to pull at once and work with in memory...
74 $subscriptions = $this->getSubscriptions();
75 $subscribers = $this->getSubscribers();
76 $groups = $this->getGroups();
77 $faves = $this->getFaves();
78 $messagesFrom = $this->getMessagesFrom();
79 $messagesTo = $this->getMessagesTo();
81 $objs = array_merge($subscriptions, $subscribers, $groups, $faves, $notices, $messagesFrom, $messagesTo);
83 $subscriptions = null;
88 unset($subscriptions);
93 // Sort by create date
95 usort($objs, 'UserActivityStream::compareObject');
97 // We'll keep these around for later, and interleave them into
98 // the output stream with the user's notices.
104 * Interleave the pre-sorted subs/groups/faves with the user's
105 * notices, all in reverse chron order.
107 function renderEntries($format=Feed::ATOM, $handle=null)
112 foreach ($this->objs as $obj) {
114 $act = $obj->asActivity();
115 } catch (Exception $e) {
116 common_log(LOG_ERR, $e->getMessage());
122 if ($this->outputMode == self::OUTPUT_RAW && $start != $end) {
123 // In raw mode, we haven't pre-fetched notices.
124 // Grab the chunks of notices between other activities.
126 $notices = $this->getNoticesBetween($start, $end);
127 foreach ($notices as $noticeAct) {
129 $nact = $noticeAct->asActivity($this->user);
130 if ($format == Feed::ATOM) {
131 $nact->outputTo($this, false, false);
134 fwrite($handle, ",");
136 fwrite($handle, json_encode($nact->asArray()));
139 } catch (Exception $e) {
140 common_log(LOG_ERR, $e->getMessage());
146 } catch (Exception $e) {
147 common_log(LOG_ERR, $e->getMessage());
155 if ($format == Feed::ATOM) {
156 // Only show the author sub-element if it's different from default user
157 $act->outputTo($this, false, ($act->actor->id != $this->user->getUri()));
160 fwrite($handle, ",");
162 fwrite($handle, json_encode($act->asArray()));
165 } catch (Exception $e) {
166 common_log(LOG_ERR, $e->getMessage());
175 if ($this->outputMode == self::OUTPUT_RAW) {
176 // Grab anything after the last pre-sorted activity.
178 if (!empty($this->after)) {
179 $notices = $this->getNoticesBetween($this->after, $end);
181 $notices = $this->getNoticesBetween(0, $end);
183 foreach ($notices as $noticeAct) {
185 $nact = $noticeAct->asActivity($this->user);
186 if ($format == Feed::ATOM) {
187 $nact->outputTo($this, false, false);
190 fwrite($handle, ",");
192 fwrite($handle, json_encode($nact->asArray()));
195 } catch (Exception $e) {
196 common_log(LOG_ERR, $e->getMessage());
200 } catch (Exception $e) {
201 common_log(LOG_ERR, $e->getMessage());
205 if (empty($this->after) || strtotime($this->user->created) > $this->after) {
206 // We always add the registration activity at the end, even if
207 // they have older activities (from restored backups) in their stream.
210 $ract = $this->user->registrationActivity();
211 if ($format == Feed::ATOM) {
212 $ract->outputTo($this, false, false);
215 fwrite($handle, ",");
217 fwrite($handle, json_encode($ract->asArray()));
220 } catch (Exception $e) {
221 common_log(LOG_ERR, $e->getMessage());
227 function compareObject($a, $b)
229 $ac = strtotime((empty($a->created)) ? $a->modified : $a->created);
230 $bc = strtotime((empty($b->created)) ? $b->modified : $b->created);
232 return (($ac == $bc) ? 0 : (($ac < $bc) ? 1 : -1));
235 function getSubscriptions()
239 $sub = new Subscription();
241 $sub->subscriber = $this->user->id;
243 if (!empty($this->after)) {
244 $sub->whereAdd("created > '" . common_sql_date($this->after) . "'");
248 while ($sub->fetch()) {
249 if ($sub->subscribed != $this->user->id) {
250 $subs[] = clone($sub);
258 function getSubscribers()
262 $sub = new Subscription();
264 $sub->subscribed = $this->user->id;
266 if (!empty($this->after)) {
267 $sub->whereAdd("created > '" . common_sql_date($this->after) . "'");
271 while ($sub->fetch()) {
272 if ($sub->subscriber != $this->user->id) {
273 $subs[] = clone($sub);
287 $fave->user_id = $this->user->id;
289 if (!empty($this->after)) {
290 $fave->whereAdd("modified > '" . common_sql_date($this->after) . "'");
294 while ($fave->fetch()) {
295 $faves[] = clone($fave);
304 * @param int $start unix timestamp for earliest
305 * @param int $end unix timestamp for latest
306 * @return array of Notice objects
308 function getNoticesBetween($start=0, $end=0)
312 $notice = new Notice();
314 $notice->profile_id = $this->user->id;
316 // Only stuff after $this->after
318 if (!empty($this->after)) {
320 $start = max($start, $this->after);
323 $end = max($end, $this->after);
328 $tsstart = common_sql_date($start);
329 $notice->whereAdd("created >= '$tsstart'");
332 $tsend = common_sql_date($end);
333 $notice->whereAdd("created < '$tsend'");
336 $notice->orderBy('created DESC');
338 if ($notice->find()) {
339 while ($notice->fetch()) {
340 $notices[] = clone($notice);
347 function getNotices()
349 if (!empty($this->after)) {
350 return $this->getNoticesBetween($this->after);
352 return $this->getNoticesBetween();
360 $gm = new Group_member();
362 $gm->profile_id = $this->user->id;
364 if (!empty($this->after)) {
365 $gm->whereAdd("created > '" . common_sql_date($this->after) . "'");
369 while ($gm->fetch()) {
370 $groups[] = clone($gm);
377 function getMessagesTo()
379 $msgMap = Message::listGet('to_profile', array($this->user->id));
381 $messages = $msgMap[$this->user->id];
383 if (!empty($this->after)) {
384 $messages = array_filter($messages, array($this, 'createdAfter'));
390 function getMessagesFrom()
392 $msgMap = Message::listGet('from_profile', array($this->user->id));
394 $messages = $msgMap[$this->user->id];
396 if (!empty($this->after)) {
397 $messages = array_filter($messages, array($this, 'createdAfter'));
403 function createdAfter($item) {
404 $created = strtotime((empty($item->created)) ? $item->modified : $item->created);
405 return ($created >= $this->after);
408 function writeJSON($handle)
410 require_once INSTALLDIR.'/lib/activitystreamjsondocument.php';
411 fwrite($handle, '{"items": [');
412 $this->renderEntries(Feed::JSON, $handle);
413 fwrite($handle, ']}');