]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apifriendstimeline.php
Moved basic auth stuff into its own classes
[quix0rs-gnu-social.git] / actions / apifriendstimeline.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  Personal
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 class ApiFriendsTimelineAction extends ApiBareAuthAction
37 {
38
39     var $user    = null;
40     var $notices = null;
41
42     /**
43      * Take arguments for running
44      *
45      * @param array $args $_REQUEST args
46      *
47      * @return boolean success flag
48      *
49      */
50
51     function prepare($args)
52     {
53         parent::prepare($args);
54
55         $this->page     = (int)$this->arg('page', 1);
56         $this->count    = (int)$this->arg('count', 20);
57         $this->max_id   = (int)$this->arg('max_id', 0);
58         $this->since_id = (int)$this->arg('since_id', 0);
59         $this->since    = $this->arg('since');
60
61         if ($this->requiresAuth()) {
62             if ($this->checkBasicAuthUser() == false) {
63                 return;
64             }
65         }
66
67         $this->user = $this->getTargetUser($this->arg('id'));
68
69         if (empty($this->user)) {
70             $this->clientError(_('No such user!'), 404, $this->arg('format'));
71             return;
72         }
73
74         $this->notices = $this->getNotices();
75
76         return true;
77     }
78
79     function handle($args) {
80         parent::handle($args);
81         $this->showTimeline();
82     }
83
84     function showTimeline()
85     {
86         $profile    = $this->user->getProfile();
87         $sitename   = common_config('site', 'name');
88         $title      = sprintf(_("%s and friends"), $user->nickname);
89         $taguribase = common_config('integration', 'taguri');
90         $id         = "tag:$taguribase:FriendsTimeline:" . $user->id;
91         $link       = common_local_url('all',
92                                        array('nickname' => $user->nickname));
93         $subtitle   = sprintf(_('Updates from %1$s and friends on %2$s!'),
94                               $user->nickname, $sitename);
95
96         switch($this->arg('format')) {
97             case 'xml':
98                 $this->show_xml_timeline($this->notices);
99                 break;
100             case 'rss':
101                 $this->show_rss_timeline($this->notices, $title, $link, $subtitle);
102                 break;
103             case 'atom':
104
105                 $target_id = $this->arg('id');
106
107                 if (isset($target_id)) {
108                     $selfuri = common_root_url() .
109                         'api/statuses/friends_timeline/' .
110                         $target_id . '.atom';
111                 } else {
112                     $selfuri = common_root_url() .
113                         'api/statuses/friends_timeline.atom';
114                 }
115                 $this->show_atom_timeline($this->notices, $title, $id, $link,
116                                           $subtitle, null, $selfuri);
117                 break;
118             case 'json':
119                 $this->show_json_timeline($this->notices);
120                 break;
121             default:
122                 $this->clientError(_('API method not found!'), $code = 404);
123                 break;
124         }
125     }
126
127     function getNotices()
128     {
129         $notices = array();
130
131         if (!empty($this->auth_user) && $this->auth_user->id == $this->user->id) {
132             $notice = $this->user->noticeInbox(($this->page-1) * $this->count,
133                                                $this->count, $this->since_id,
134                                                $this->max_id, $this->since);
135         } else {
136             $notice = $this->user->noticesWithFriends(($this->page-1) * $this->count,
137                                                       $this->count, $this->since_id,
138                                                       $this->max_id, $this->since);
139         }
140
141         while ($notice->fetch()) {
142             $notices[] = clone($notice);
143         }
144
145         return $notices;
146     }
147
148     /**
149      * Is this page read-only?
150      *
151      * @return boolean true
152      */
153
154     function isReadOnly($args)
155     {
156         return true;
157     }
158
159     /**
160      * When was this feed last modified?
161      *
162      */
163
164     function lastModified()
165     {
166         if (empty($this->notices)) {
167             return null;
168         }
169
170         if (count($this->notices) == 0) {
171             return null;
172         }
173
174         return strtotime($this->notices[0]->created);
175     }
176
177 }