]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/useractivitystream.php
Merge branch '0.9.x' of gitorious.org:statusnet/mainline into 0.9.x
[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)
46     {
47         parent::__construct($user, null, $indent);
48
49         $this->outputMode = $outputMode;
50         if ($this->outputMode == self::OUTPUT_STRING) {
51             // String buffering? Grab all the notices now.
52             $notices = $this->getNotices();
53         } elseif ($this->outputMode == self::OUTPUT_RAW) {
54             // Raw output... need to restructure from the stringer init.
55             $this->xw = new XMLWriter();
56             $this->xw->openURI('php://output');
57             if(is_null($indent)) {
58                 $indent = common_config('site', 'indent');
59             }
60             $this->xw->setIndent($indent);
61
62             // We'll fetch notices later.
63             $notices = array();
64         } else {
65             throw new Exception('Invalid outputMode provided to ' . __METHOD__);
66         }
67
68         // Assume that everything but notices is feasible
69         // to pull at once and work with in memory...
70         $subscriptions = $this->getSubscriptions();
71         $subscribers   = $this->getSubscribers();
72         $groups        = $this->getGroups();
73         $faves         = $this->getFaves();
74
75         $objs = array_merge($subscriptions, $subscribers, $groups, $faves, $notices);
76
77         // Sort by create date
78
79         usort($objs, 'UserActivityStream::compareObject');
80
81         // We'll keep these around for later, and interleave them into
82         // the output stream with the user's notices.
83         foreach ($objs as $obj) {
84             $this->activities[] = $obj->asActivity();
85         }
86     }
87
88     /**
89      * Interleave the pre-sorted subs/groups/faves with the user's
90      * notices, all in reverse chron order.
91      */
92     function renderEntries()
93     {
94         $end = time() + 1;
95         foreach ($this->activities as $act) {
96             $start = $act->time;
97
98             if ($this->outputMode == self::OUTPUT_RAW && $start != $end) {
99                 // In raw mode, we haven't pre-fetched notices.
100                 // Grab the chunks of notices between other activities.
101                 $notices = $this->getNoticesBetween($start, $end);
102                 foreach ($notices as $noticeAct) {
103                     $noticeAct->asActivity()->outputTo($this, false, false);
104                 }
105             }
106
107             // Only show the author sub-element if it's different from default user
108             $act->outputTo($this, false, ($act->actor->id != $this->user->uri));
109
110             $end = $start;
111         }
112
113         if ($this->outputMode == self::OUTPUT_RAW) {
114             // Grab anything after the last pre-sorted activity.
115             $notices = $this->getNoticesBetween(0, $end);
116             foreach ($notices as $noticeAct) {
117                 $noticeAct->asActivity()->outputTo($this, false, false);
118             }
119         }
120     }
121
122     function compareObject($a, $b)
123     {
124         $ac = strtotime((empty($a->created)) ? $a->modified : $a->created);
125         $bc = strtotime((empty($b->created)) ? $b->modified : $b->created);
126
127         return (($ac == $bc) ? 0 : (($ac < $bc) ? 1 : -1));
128     }
129
130     function getSubscriptions()
131     {
132         $subs = array();
133
134         $sub = new Subscription();
135
136         $sub->subscriber = $this->user->id;
137
138         if ($sub->find()) {
139             while ($sub->fetch()) {
140                 if ($sub->subscribed != $this->user->id) {
141                     $subs[] = clone($sub);
142                 }
143             }
144         }
145
146         return $subs;
147     }
148
149     function getSubscribers()
150     {
151         $subs = array();
152
153         $sub = new Subscription();
154
155         $sub->subscribed = $this->user->id;
156
157         if ($sub->find()) {
158             while ($sub->fetch()) {
159                 if ($sub->subscriber != $this->user->id) {
160                     $subs[] = clone($sub);
161                 }
162             }
163         }
164
165         return $subs;
166     }
167
168     function getFaves()
169     {
170         $faves = array();
171
172         $fave = new Fave();
173
174         $fave->user_id = $this->user->id;
175
176         if ($fave->find()) {
177             while ($fave->fetch()) {
178                 $faves[] = clone($fave);
179             }
180         }
181
182         return $faves;
183     }
184
185     /**
186      *
187      * @param int $start unix timestamp for earliest
188      * @param int $end unix timestamp for latest
189      * @return array of Notice objects
190      */
191     function getNoticesBetween($start=0, $end=0)
192     {
193         $notices = array();
194
195         $notice = new Notice();
196
197         $notice->profile_id = $this->user->id;
198
199         if ($start) {
200             $tsstart = common_sql_date($start);
201             $notice->whereAdd("created >= '$tsstart'");
202         }
203         if ($end) {
204             $tsend = common_sql_date($end);
205             $notice->whereAdd("created < '$tsend'");
206         }
207
208         $notice->orderBy('created DESC');
209
210         if ($notice->find()) {
211             while ($notice->fetch()) {
212                 $notices[] = clone($notice);
213             }
214         }
215
216         return $notices;
217     }
218
219     function getNotices()
220     {
221         return $this->getNoticesBetween();
222     }
223
224     function getGroups()
225     {
226         $groups = array();
227
228         $gm = new Group_member();
229
230         $gm->profile_id = $this->user->id;
231
232         if ($gm->find()) {
233             while ($gm->fetch()) {
234                 $groups[] = clone($gm);
235             }
236         }
237
238         return $groups;
239     }
240 }