]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/useractivitystream.php
Merge commit 'refs/merge-requests/41' of https://gitorious.org/social/mainline into...
[quix0rs-gnu-social.git] / lib / useractivitystream.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010 StatusNet, 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 /**
21  * Class for activity streams
22  *
23  * Includes objects like notices, subscriptions and from plugins.
24  *
25  * We extend atomusernoticefeed since it does some nice setup for us.
26  *
27  */
28 class UserActivityStream extends AtomUserNoticeFeed
29 {
30     public $activities = array();
31     public $after = null;
32
33     const OUTPUT_STRING = 1;
34     const OUTPUT_RAW = 2;
35     public $outputMode = self::OUTPUT_STRING;
36
37     /**
38      *
39      * @param User $user
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.
45      */
46     function __construct($user, $indent = true, $outputMode = UserActivityStream::OUTPUT_STRING, $after = null)
47     {
48         parent::__construct($user, null, $indent);
49
50         $this->outputMode = $outputMode;
51
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');
61             }
62             $this->xw->setIndent($indent);
63
64             // We'll fetch notices later.
65             $notices = array();
66         } else {
67             throw new Exception('Invalid outputMode provided to ' . __METHOD__);
68         }
69
70         $this->after = $after;
71
72         // Assume that everything but notices is feasible
73         // to pull at once and work with in memory...
74
75         $subscriptions = $this->getSubscriptions();
76         $subscribers   = $this->getSubscribers();
77         $groups        = $this->getGroups();
78
79         $objs = array_merge($subscriptions, $subscribers, $groups, $notices);
80
81         Event::handle('AppendUserActivityStreamObjects', array($this, &$objs));
82
83         $subscriptions = null;
84         $subscribers   = null;
85         $groups        = null;
86
87         unset($subscriptions);
88         unset($subscribers);
89         unset($groups);
90
91         // Sort by create date
92
93         usort($objs, 'UserActivityStream::compareObject');
94
95         // We'll keep these around for later, and interleave them into
96         // the output stream with the user's notices.
97
98         $this->objs = $objs;
99     }
100
101     /**
102      * Interleave the pre-sorted objects with the user's
103      * notices, all in reverse chron order.
104      */
105     function renderEntries($format=Feed::ATOM, $handle=null)
106     {
107         $haveOne = false;
108
109         $end = time() + 1;
110         foreach ($this->objs as $obj) {
111             try {
112                 $act = $obj->asActivity();
113             } catch (Exception $e) {
114                 common_log(LOG_ERR, $e->getMessage());
115                 continue;
116             }
117
118             $start = $act->time;
119
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.
123                 try {
124                     $notices = $this->getNoticesBetween($start, $end);
125                     foreach ($notices as $noticeAct) {
126                         try {
127                             $nact = $noticeAct->asActivity($this->user);
128                             if ($format == Feed::ATOM) {
129                                 $nact->outputTo($this, false, false);
130                             } else {
131                                 if ($haveOne) {
132                                     fwrite($handle, ",");
133                                 }
134                                 fwrite($handle, json_encode($nact->asArray()));
135                                 $haveOne = true;
136                             }
137                         } catch (Exception $e) {
138                             common_log(LOG_ERR, $e->getMessage());
139                             continue;
140                         }
141                         $nact = null;
142                         unset($nact);
143                     }
144                 } catch (Exception $e) {
145                     common_log(LOG_ERR, $e->getMessage());
146                 }
147             }
148
149             $notices = null;
150             unset($notices);
151
152             try {
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()));
156                 } else {
157                     if ($haveOne) {
158                         fwrite($handle, ",");
159                     }
160                     fwrite($handle, json_encode($act->asArray()));
161                     $haveOne = true;
162                 }
163             } catch (Exception $e) {
164                 common_log(LOG_ERR, $e->getMessage());
165             }
166
167             $act = null;
168             unset($act);
169
170             $end = $start;
171         }
172
173         if ($this->outputMode == self::OUTPUT_RAW) {
174             // Grab anything after the last pre-sorted activity.
175             try {
176                 if (!empty($this->after)) {
177                     $notices = $this->getNoticesBetween($this->after, $end);
178                 } else {
179                     $notices = $this->getNoticesBetween(0, $end);
180                 }
181                 foreach ($notices as $noticeAct) {
182                     try {
183                         $nact = $noticeAct->asActivity($this->user);
184                         if ($format == Feed::ATOM) {
185                             $nact->outputTo($this, false, false);
186                         } else {
187                             if ($haveOne) {
188                                 fwrite($handle, ",");
189                             }
190                             fwrite($handle, json_encode($nact->asArray()));
191                             $haveOne = true;
192                         }
193                     } catch (Exception $e) {
194                         common_log(LOG_ERR, $e->getMessage());
195                         continue;
196                     }
197                 }
198             } catch (Exception $e) {
199                 common_log(LOG_ERR, $e->getMessage());
200             }
201         }
202
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.
206
207             try {
208                 $ract = $this->user->registrationActivity();
209                 if ($format == Feed::ATOM) {
210                     $ract->outputTo($this, false, false);
211                 } else {
212                     if ($haveOne) {
213                         fwrite($handle, ",");
214                     }
215                     fwrite($handle, json_encode($ract->asArray()));
216                     $haveOne = true;
217                 }
218             } catch (Exception $e) {
219                 common_log(LOG_ERR, $e->getMessage());
220                 continue;
221             }
222         }
223     }
224
225     function compareObject($a, $b)
226     {
227         $ac = strtotime((empty($a->created)) ? $a->modified : $a->created);
228         $bc = strtotime((empty($b->created)) ? $b->modified : $b->created);
229
230         return (($ac == $bc) ? 0 : (($ac < $bc) ? 1 : -1));
231     }
232
233     function getSubscriptions()
234     {
235         $subs = array();
236
237         $sub = new Subscription();
238
239         $sub->subscriber = $this->user->id;
240
241         if (!empty($this->after)) {
242             $sub->whereAdd("created > '" . common_sql_date($this->after) . "'");
243         }
244
245         if ($sub->find()) {
246             while ($sub->fetch()) {
247                 if ($sub->subscribed != $this->user->id) {
248                     $subs[] = clone($sub);
249                 }
250             }
251         }
252
253         return $subs;
254     }
255
256     function getSubscribers()
257     {
258         $subs = array();
259
260         $sub = new Subscription();
261
262         $sub->subscribed = $this->user->id;
263
264         if (!empty($this->after)) {
265             $sub->whereAdd("created > '" . common_sql_date($this->after) . "'");
266         }
267
268         if ($sub->find()) {
269             while ($sub->fetch()) {
270                 if ($sub->subscriber != $this->user->id) {
271                     $subs[] = clone($sub);
272                 }
273             }
274         }
275
276         return $subs;
277     }
278
279     /**
280      *
281      * @param int $start unix timestamp for earliest
282      * @param int $end unix timestamp for latest
283      * @return array of Notice objects
284      */
285     function getNoticesBetween($start=0, $end=0)
286     {
287         $notices = array();
288
289         $notice = new Notice();
290
291         $notice->profile_id = $this->user->id;
292
293         // Only stuff after $this->after
294
295         if (!empty($this->after)) {
296             if ($start) {
297                 $start = max($start, $this->after);
298             }
299             if ($end) {
300                 $end = max($end, $this->after);
301             }
302         }
303
304         if ($start) {
305             $tsstart = common_sql_date($start);
306             $notice->whereAdd("created >= '$tsstart'");
307         }
308         if ($end) {
309             $tsend = common_sql_date($end);
310             $notice->whereAdd("created < '$tsend'");
311         }
312
313         $notice->orderBy('created DESC');
314
315         if ($notice->find()) {
316             while ($notice->fetch()) {
317                 $notices[] = clone($notice);
318             }
319         }
320
321         return $notices;
322     }
323
324     function getNotices()
325     {
326         if (!empty($this->after)) {
327             return $this->getNoticesBetween($this->after);
328         } else {
329             return $this->getNoticesBetween();
330         }
331     }
332
333     function getGroups()
334     {
335         $groups = array();
336
337         $gm = new Group_member();
338
339         $gm->profile_id = $this->user->id;
340
341         if (!empty($this->after)) {
342             $gm->whereAdd("created > '" . common_sql_date($this->after) . "'");
343         }
344
345         if ($gm->find()) {
346             while ($gm->fetch()) {
347                 $groups[] = clone($gm);
348             }
349         }
350
351         return $groups;
352     }
353
354     function createdAfter($item) {
355         $created = strtotime((empty($item->created)) ? $item->modified : $item->created);
356         return ($created >= $this->after);
357     }
358
359     function writeJSON($handle)
360     {
361         require_once INSTALLDIR.'/lib/activitystreamjsondocument.php';
362         fwrite($handle, '{"items": [');
363         $this->renderEntries(Feed::JSON, $handle);
364         fwrite($handle, ']}');
365     }
366 }