]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apitimelinefriends.php
CamelCase all function names in the API code
[quix0rs-gnu-social.git] / actions / apitimelinefriends.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Show the friends timeline
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  API
23  * @package   StatusNet
24  * @author    Zach Copley <zach@status.net>
25  * @copyright 2009 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR.'/lib/apibareauth.php';
35
36 /**
37  * Returns the most recent notices (default 20) posted by the target user.
38  * This is the equivalent of 'You and friends' page accessed via Web.
39  *
40  * @category API
41  * @package  StatusNet
42  * @author   Zach Copley <zach@status.net>
43  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44  * @link     http://status.net/
45  */
46
47 class ApiTimelineFriendsAction extends ApiBareAuthAction
48 {
49     var $notices  = null;
50
51     /**
52      * Take arguments for running
53      *
54      * @param array $args $_REQUEST args
55      *
56      * @return boolean success flag
57      *
58      */
59
60     function prepare($args)
61     {
62         parent::prepare($args);
63
64         $this->user = $this->getTargetUser($this->arg('id'));
65
66         if (empty($this->user)) {
67             $this->clientError(_('No such user!'), 404, $this->format);
68             return;
69         }
70
71         $this->notices = $this->getNotices();
72
73         return true;
74     }
75
76     /**
77      * Handle the request
78      *
79      * Just show the notices
80      *
81      * @param array $args $_REQUEST data (unused)
82      *
83      * @return void
84      */
85
86     function handle($args)
87     {
88         parent::handle($args);
89         $this->showTimeline();
90     }
91
92     /**
93      * Show the timeline of notices
94      *
95      * @return void
96      */
97
98     function showTimeline()
99     {
100         $profile    = $this->user->getProfile();
101         $sitename   = common_config('site', 'name');
102         $title      = sprintf(_("%s and friends"), $this->user->nickname);
103         $taguribase = common_config('integration', 'taguri');
104         $id         = "tag:$taguribase:FriendsTimeline:" . $this->user->id;
105         $link       = common_local_url(
106             'all', array('nickname' => $this->user->nickname)
107         );
108         $subtitle   = sprintf(
109             _('Updates from %1$s and friends on %2$s!'),
110             $this->user->nickname, $sitename
111         );
112
113         switch($this->format) {
114         case 'xml':
115             $this->showXmlTimeline($this->notices);
116             break;
117         case 'rss':
118             $this->showRssTimeline($this->notices, $title, $link, $subtitle);
119             break;
120         case 'atom':
121
122             $target_id = $this->arg('id');
123
124             if (isset($target_id)) {
125                 $selfuri = common_root_url() .
126                     'api/statuses/friends_timeline/' .
127                     $target_id . '.atom';
128             } else {
129                 $selfuri = common_root_url() .
130                     'api/statuses/friends_timeline.atom';
131             }
132
133             $this->showAtomTimeline(
134                 $this->notices, $title, $id, $link,
135                 $subtitle, null, $selfuri
136             );
137             break;
138         case 'json':
139             $this->showJsonTimeline($this->notices);
140             break;
141         default:
142             $this->clientError(_('API method not found!'), $code = 404);
143             break;
144         }
145     }
146
147     /**
148      * Get notices
149      *
150      * @return array notices
151      */
152
153     function getNotices()
154     {
155         $notices = array();
156
157         if (!empty($this->auth_user) && $this->auth_user->id == $this->user->id) {
158             $notice = $this->user->noticeInbox(
159                 ($this->page-1) * $this->count,
160                 $this->count, $this->since_id,
161                 $this->max_id, $this->since
162             );
163         } else {
164             $notice = $this->user->noticesWithFriends(
165                 ($this->page-1) * $this->count,
166                 $this->count, $this->since_id,
167                 $this->max_id, $this->since
168             );
169         }
170
171         while ($notice->fetch()) {
172             $notices[] = clone($notice);
173         }
174
175         return $notices;
176     }
177
178     /**
179      * Is this action read only?
180      *
181      * @param array $args other arguments
182      *
183      * @return boolean true
184      */
185
186     function isReadOnly($args)
187     {
188         return true;
189     }
190
191     /**
192      * When was this feed last modified?
193      *
194      * @return string datestamp of the latest notice in the stream
195      */
196
197     function lastModified()
198     {
199         if (!empty($this->notices) && (count($this->notices) > 0)) {
200             return strtotime($this->notices[0]->created);
201         }
202
203         return null;
204     }
205
206     /**
207      * An entity tag for this stream
208      *
209      * Returns an Etag based on the action name, language, user ID, and
210      * timestamps of the first and last notice in the timeline
211      *
212      * @return string etag
213      */
214
215     function etag()
216     {
217         if (!empty($this->notices) && (count($this->notices) > 0)) {
218
219             $last = count($this->notices) - 1;
220
221             return '"' . implode(
222                 ':',
223                 array($this->arg('action'),
224                       common_language(),
225                       $this->user->id,
226                       strtotime($this->notices[0]->created),
227                       strtotime($this->notices[$last]->created))
228             )
229             . '"';
230         }
231
232         return null;
233     }
234
235 }