]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/useractivitystream.php
Code cleanup and enabling User object's etc. getUri()
[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 faves, notices, and subscriptions.
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
32     const OUTPUT_STRING = 1;
33     const OUTPUT_RAW = 2;
34     public $outputMode = self::OUTPUT_STRING;
35
36     /**
37      *
38      * @param User $user
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.
44      */
45     function __construct($user, $indent = true, $outputMode = UserActivityStream::OUTPUT_STRING, $after = null)
46     {
47         parent::__construct($user, null, $indent);
48
49         $this->outputMode = $outputMode;
50
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');
60             }
61             $this->xw->setIndent($indent);
62
63             // We'll fetch notices later.
64             $notices = array();
65         } else {
66             throw new Exception('Invalid outputMode provided to ' . __METHOD__);
67         }
68
69         $this->after = $after;
70
71         // Assume that everything but notices is feasible
72         // to pull at once and work with in memory...
73
74         $subscriptions = $this->getSubscriptions();
75         $subscribers   = $this->getSubscribers();
76         $groups        = $this->getGroups();
77         $faves         = $this->getFaves();
78         $messagesFrom  = $this->getMessagesFrom();
79         $messagesTo    = $this->getMessagesTo();
80
81         $objs = array_merge($subscriptions, $subscribers, $groups, $faves, $notices, $messagesFrom, $messagesTo);
82
83         $subscriptions = null;
84         $subscribers   = null;
85         $groups        = null;
86         $faves         = null;
87
88         unset($subscriptions);
89         unset($subscribers);
90         unset($groups);
91         unset($faves);
92
93         // Sort by create date
94
95         usort($objs, 'UserActivityStream::compareObject');
96
97         // We'll keep these around for later, and interleave them into
98         // the output stream with the user's notices.
99
100         $this->objs = $objs;
101     }
102
103     /**
104      * Interleave the pre-sorted subs/groups/faves with the user's
105      * notices, all in reverse chron order.
106      */
107     function renderEntries($format=Feed::ATOM, $handle=null)
108     {
109         $haveOne = false;
110
111         $end = time() + 1;
112         foreach ($this->objs as $obj) {
113             try {
114                 $act = $obj->asActivity();
115             } catch (Exception $e) {
116                 common_log(LOG_ERR, $e->getMessage());
117                 continue;
118             }
119
120             $start = $act->time;
121
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.
125                 try {
126                     $notices = $this->getNoticesBetween($start, $end);
127                     foreach ($notices as $noticeAct) {
128                         try {
129                             $nact = $noticeAct->asActivity($this->user);
130                             if ($format == Feed::ATOM) {
131                                 $nact->outputTo($this, false, false);
132                             } else {
133                                 if ($haveOne) {
134                                     fwrite($handle, ",");
135                                 }
136                                 fwrite($handle, json_encode($nact->asArray()));
137                                 $haveOne = true;
138                             }
139                         } catch (Exception $e) {
140                             common_log(LOG_ERR, $e->getMessage());
141                             continue;
142                         }
143                         $nact = null;
144                         unset($nact);
145                     }
146                 } catch (Exception $e) {
147                     common_log(LOG_ERR, $e->getMessage());
148                 }
149             }
150
151             $notices = null;
152             unset($notices);
153
154             try {
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()));
158                 } else {
159                     if ($haveOne) {
160                         fwrite($handle, ",");
161                     }
162                     fwrite($handle, json_encode($act->asArray()));
163                     $haveOne = true;
164                 }
165             } catch (Exception $e) {
166                 common_log(LOG_ERR, $e->getMessage());
167             }
168
169             $act = null;
170             unset($act);
171
172             $end = $start;
173         }
174
175         if ($this->outputMode == self::OUTPUT_RAW) {
176             // Grab anything after the last pre-sorted activity.
177             try {
178                 if (!empty($this->after)) {
179                     $notices = $this->getNoticesBetween($this->after, $end);
180                 } else {
181                     $notices = $this->getNoticesBetween(0, $end);
182                 }
183                 foreach ($notices as $noticeAct) {
184                     try {
185                         $nact = $noticeAct->asActivity($this->user);
186                         if ($format == Feed::ATOM) {
187                             $nact->outputTo($this, false, false);
188                         } else {
189                             if ($haveOne) {
190                                 fwrite($handle, ",");
191                             }
192                             fwrite($handle, json_encode($nact->asArray()));
193                             $haveOne = true;
194                         }
195                     } catch (Exception $e) {
196                         common_log(LOG_ERR, $e->getMessage());
197                         continue;
198                     }
199                 }
200             } catch (Exception $e) {
201                 common_log(LOG_ERR, $e->getMessage());
202             }
203         }
204
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.
208
209             try {
210                 $ract = $this->user->registrationActivity();
211                 if ($format == Feed::ATOM) {
212                     $ract->outputTo($this, false, false);
213                 } else {
214                     if ($haveOne) {
215                         fwrite($handle, ",");
216                     }
217                     fwrite($handle, json_encode($ract->asArray()));
218                     $haveOne = true;
219                 }
220             } catch (Exception $e) {
221                 common_log(LOG_ERR, $e->getMessage());
222                 continue;
223             }
224         }
225     }
226
227     function compareObject($a, $b)
228     {
229         $ac = strtotime((empty($a->created)) ? $a->modified : $a->created);
230         $bc = strtotime((empty($b->created)) ? $b->modified : $b->created);
231
232         return (($ac == $bc) ? 0 : (($ac < $bc) ? 1 : -1));
233     }
234
235     function getSubscriptions()
236     {
237         $subs = array();
238
239         $sub = new Subscription();
240
241         $sub->subscriber = $this->user->id;
242
243         if (!empty($this->after)) {
244             $sub->whereAdd("created > '" . common_sql_date($this->after) . "'");
245         }
246
247         if ($sub->find()) {
248             while ($sub->fetch()) {
249                 if ($sub->subscribed != $this->user->id) {
250                     $subs[] = clone($sub);
251                 }
252             }
253         }
254
255         return $subs;
256     }
257
258     function getSubscribers()
259     {
260         $subs = array();
261
262         $sub = new Subscription();
263
264         $sub->subscribed = $this->user->id;
265
266         if (!empty($this->after)) {
267             $sub->whereAdd("created > '" . common_sql_date($this->after) . "'");
268         }
269
270         if ($sub->find()) {
271             while ($sub->fetch()) {
272                 if ($sub->subscriber != $this->user->id) {
273                     $subs[] = clone($sub);
274                 }
275             }
276         }
277
278         return $subs;
279     }
280
281     function getFaves()
282     {
283         $faves = array();
284
285         $fave = new Fave();
286
287         $fave->user_id = $this->user->id;
288
289         if (!empty($this->after)) {
290             $fave->whereAdd("modified > '" . common_sql_date($this->after) . "'");
291         }
292
293         if ($fave->find()) {
294             while ($fave->fetch()) {
295                 $faves[] = clone($fave);
296             }
297         }
298
299         return $faves;
300     }
301
302     /**
303      *
304      * @param int $start unix timestamp for earliest
305      * @param int $end unix timestamp for latest
306      * @return array of Notice objects
307      */
308     function getNoticesBetween($start=0, $end=0)
309     {
310         $notices = array();
311
312         $notice = new Notice();
313
314         $notice->profile_id = $this->user->id;
315
316         // Only stuff after $this->after
317
318         if (!empty($this->after)) {
319             if ($start) {
320                 $start = max($start, $this->after);
321             }
322             if ($end) {
323                 $end = max($end, $this->after);
324             }
325         }
326
327         if ($start) {
328             $tsstart = common_sql_date($start);
329             $notice->whereAdd("created >= '$tsstart'");
330         }
331         if ($end) {
332             $tsend = common_sql_date($end);
333             $notice->whereAdd("created < '$tsend'");
334         }
335
336         $notice->orderBy('created DESC');
337
338         if ($notice->find()) {
339             while ($notice->fetch()) {
340                 $notices[] = clone($notice);
341             }
342         }
343
344         return $notices;
345     }
346
347     function getNotices()
348     {
349         if (!empty($this->after)) {
350             return $this->getNoticesBetween($this->after);
351         } else {
352             return $this->getNoticesBetween();
353         }
354     }
355
356     function getGroups()
357     {
358         $groups = array();
359
360         $gm = new Group_member();
361
362         $gm->profile_id = $this->user->id;
363
364         if (!empty($this->after)) {
365             $gm->whereAdd("created > '" . common_sql_date($this->after) . "'");
366         }
367
368         if ($gm->find()) {
369             while ($gm->fetch()) {
370                 $groups[] = clone($gm);
371             }
372         }
373
374         return $groups;
375     }
376
377     function getMessagesTo()
378     {
379         $msgMap = Message::listGet('to_profile', array($this->user->id));
380
381         $messages = $msgMap[$this->user->id];
382
383         if (!empty($this->after)) {
384             $messages = array_filter($messages, array($this, 'createdAfter'));
385         }
386
387         return $messages;
388     }
389
390     function getMessagesFrom()
391     {
392         $msgMap = Message::listGet('from_profile', array($this->user->id));
393
394         $messages = $msgMap[$this->user->id];
395
396         if (!empty($this->after)) {
397             $messages = array_filter($messages, array($this, 'createdAfter'));
398         }
399
400         return $messages;
401     }
402
403     function createdAfter($item) {
404         $created = strtotime((empty($item->created)) ? $item->modified : $item->created);
405         return ($created >= $this->after);
406     }
407
408     function writeJSON($handle)
409     {
410         require_once INSTALLDIR.'/lib/activitystreamjsondocument.php';
411         fwrite($handle, '{"items": [');
412         $this->renderEntries(Feed::JSON, $handle);
413         fwrite($handle, ']}');
414     }
415 }