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 objects like notices, subscriptions and from plugins.
25 * We extend atomusernoticefeed since it does some nice setup for us.
28 class UserActivityStream extends AtomUserNoticeFeed
30 public $activities = array();
33 const OUTPUT_STRING = 1;
35 public $outputMode = self::OUTPUT_STRING;
40 * @param boolean $indent
41 * @param boolean $outputMode: UserActivityStream::OUTPUT_STRING to return a string,
42 * or UserActivityStream::OUTPUT_RAW to go to raw output.
43 * Raw output mode will attempt to stream, keeping less
44 * data in memory but will leave $this->activities incomplete.
46 function __construct($user, $indent = true, $outputMode = UserActivityStream::OUTPUT_STRING, $after = null)
48 parent::__construct($user, null, $indent);
50 $this->outputMode = $outputMode;
52 if ($this->outputMode == self::OUTPUT_STRING) {
53 // String buffering? Grab all the notices now.
54 $notices = $this->getNotices();
55 } elseif ($this->outputMode == self::OUTPUT_RAW) {
56 // Raw output... need to restructure from the stringer init.
57 $this->xw = new XMLWriter();
58 $this->xw->openURI('php://output');
59 if(is_null($indent)) {
60 $indent = common_config('site', 'indent');
62 $this->xw->setIndent($indent);
64 // We'll fetch notices later.
67 throw new Exception('Invalid outputMode provided to ' . __METHOD__);
70 $this->after = $after;
72 // Assume that everything but notices is feasible
73 // to pull at once and work with in memory...
75 $subscriptions = $this->getSubscriptions();
76 $subscribers = $this->getSubscribers();
77 $groups = $this->getGroups();
79 $objs = array_merge($subscriptions, $subscribers, $groups, $notices);
81 Event::handle('AppendUserActivityStreamObjects', array($this, &$objs));
83 $subscriptions = null;
87 unset($subscriptions);
91 // Sort by create date
93 usort($objs, 'UserActivityStream::compareObject');
95 // We'll keep these around for later, and interleave them into
96 // the output stream with the user's notices.
102 * Interleave the pre-sorted objects with the user's
103 * notices, all in reverse chron order.
105 function renderEntries($format=Feed::ATOM, $handle=null)
110 foreach ($this->objs as $obj) {
112 $act = $obj->asActivity();
113 } catch (Exception $e) {
114 common_log(LOG_ERR, $e->getMessage());
120 if ($this->outputMode == self::OUTPUT_RAW && $start != $end) {
121 // In raw mode, we haven't pre-fetched notices.
122 // Grab the chunks of notices between other activities.
124 $notices = $this->getNoticesBetween($start, $end);
125 foreach ($notices as $noticeAct) {
127 $nact = $noticeAct->asActivity($this->user);
128 if ($format == Feed::ATOM) {
129 $nact->outputTo($this, false, false);
132 fwrite($handle, ",");
134 fwrite($handle, json_encode($nact->asArray()));
137 } catch (Exception $e) {
138 common_log(LOG_ERR, $e->getMessage());
144 } catch (Exception $e) {
145 common_log(LOG_ERR, $e->getMessage());
153 if ($format == Feed::ATOM) {
154 // Only show the author sub-element if it's different from default user
155 $act->outputTo($this, false, ($act->actor->id != $this->user->getUri()));
158 fwrite($handle, ",");
160 fwrite($handle, json_encode($act->asArray()));
163 } catch (Exception $e) {
164 common_log(LOG_ERR, $e->getMessage());
173 if ($this->outputMode == self::OUTPUT_RAW) {
174 // Grab anything after the last pre-sorted activity.
176 if (!empty($this->after)) {
177 $notices = $this->getNoticesBetween($this->after, $end);
179 $notices = $this->getNoticesBetween(0, $end);
181 foreach ($notices as $noticeAct) {
183 $nact = $noticeAct->asActivity($this->user);
184 if ($format == Feed::ATOM) {
185 $nact->outputTo($this, false, false);
188 fwrite($handle, ",");
190 fwrite($handle, json_encode($nact->asArray()));
193 } catch (Exception $e) {
194 common_log(LOG_ERR, $e->getMessage());
198 } catch (Exception $e) {
199 common_log(LOG_ERR, $e->getMessage());
203 if (empty($this->after) || strtotime($this->user->created) > $this->after) {
204 // We always add the registration activity at the end, even if
205 // they have older activities (from restored backups) in their stream.
208 $ract = $this->user->registrationActivity();
209 if ($format == Feed::ATOM) {
210 $ract->outputTo($this, false, false);
213 fwrite($handle, ",");
215 fwrite($handle, json_encode($ract->asArray()));
218 } catch (Exception $e) {
219 common_log(LOG_ERR, $e->getMessage());
225 function compareObject($a, $b)
227 $ac = strtotime((empty($a->created)) ? $a->modified : $a->created);
228 $bc = strtotime((empty($b->created)) ? $b->modified : $b->created);
230 return (($ac == $bc) ? 0 : (($ac < $bc) ? 1 : -1));
233 function getSubscriptions()
237 $sub = new Subscription();
239 $sub->subscriber = $this->user->id;
241 if (!empty($this->after)) {
242 $sub->whereAdd("created > '" . common_sql_date($this->after) . "'");
246 while ($sub->fetch()) {
247 if ($sub->subscribed != $this->user->id) {
248 $subs[] = clone($sub);
256 function getSubscribers()
260 $sub = new Subscription();
262 $sub->subscribed = $this->user->id;
264 if (!empty($this->after)) {
265 $sub->whereAdd("created > '" . common_sql_date($this->after) . "'");
269 while ($sub->fetch()) {
270 if ($sub->subscriber != $this->user->id) {
271 $subs[] = clone($sub);
281 * @param int $start unix timestamp for earliest
282 * @param int $end unix timestamp for latest
283 * @return array of Notice objects
285 function getNoticesBetween($start=0, $end=0)
289 $notice = new Notice();
291 $notice->profile_id = $this->user->id;
293 // Only stuff after $this->after
295 if (!empty($this->after)) {
297 $start = max($start, $this->after);
300 $end = max($end, $this->after);
305 $tsstart = common_sql_date($start);
306 $notice->whereAdd("created >= '$tsstart'");
309 $tsend = common_sql_date($end);
310 $notice->whereAdd("created < '$tsend'");
313 $notice->orderBy('created DESC');
315 if ($notice->find()) {
316 while ($notice->fetch()) {
317 $notices[] = clone($notice);
324 function getNotices()
326 if (!empty($this->after)) {
327 return $this->getNoticesBetween($this->after);
329 return $this->getNoticesBetween();
337 $gm = new Group_member();
339 $gm->profile_id = $this->user->id;
341 if (!empty($this->after)) {
342 $gm->whereAdd("created > '" . common_sql_date($this->after) . "'");
346 while ($gm->fetch()) {
347 $groups[] = clone($gm);
354 function createdAfter($item) {
355 $created = strtotime((empty($item->created)) ? $item->modified : $item->created);
356 return ($created >= $this->after);
359 function writeJSON($handle)
361 require_once INSTALLDIR.'/lib/activitystreamjsondocument.php';
362 fwrite($handle, '{"items": [');
363 $this->renderEntries(Feed::JSON, $handle);
364 fwrite($handle, ']}');