We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

Commit ed9ba9d9 authored by Zach Copley's avatar Zach Copley

Reorganize, make bare auth work, output If-Modified-Since header

parent 49b701f9
...@@ -36,6 +36,9 @@ require_once INSTALLDIR.'/lib/twitterapi.php'; ...@@ -36,6 +36,9 @@ require_once INSTALLDIR.'/lib/twitterapi.php';
class ApifriendstimelineAction extends TwitterapiAction class ApifriendstimelineAction extends TwitterapiAction
{ {
var $user = null;
var $notices = null;
/** /**
* Take arguments for running * Take arguments for running
* *
...@@ -48,93 +51,120 @@ class ApifriendstimelineAction extends TwitterapiAction ...@@ -48,93 +51,120 @@ class ApifriendstimelineAction extends TwitterapiAction
function prepare($args) function prepare($args)
{ {
parent::prepare($args); parent::prepare($args);
return true;
} $this->page = (int)$this->arg('page', 1);
$this->count = (int)$this->arg('count', 20);
$this->max_id = (int)$this->arg('max_id', 0);
$this->since_id = (int)$this->arg('since_id', 0);
$this->since = $this->arg('since');
function handle($args) { if ($this->requiresAuth()) {
if ($this->checkBasicAuthUser() == false) {
return;
}
}
parent::handle($args); $this->user = $this->getTargetUser($this->arg('id'));
common_debug(var_export($args, true));
if ($this->requiresAuth()) { if (empty($this->user)) {
if ($this->showBasicAuthHeader()) { $this->clientError(_('No such user!'), 404, $this->arg('format'));
$this->showTimeline(); return;
}
} else {
$this->showTimeline();
} }
}
function showTimeline() $this->notices = $this->getNotices();
{
common_debug('Auth user = ' . var_export($this->auth_user, true));
$user = $this->getTargetUser($this->arg('id')); return true;
}
if (empty($user)) { function handle($args) {
$this->clientError(_('No such user!'), 404, $this->arg('format')); parent::handle($args);
return; $this->showTimeline();
} }
$profile = $user->getProfile(); function showTimeline()
{
$profile = $this->user->getProfile();
$sitename = common_config('site', 'name'); $sitename = common_config('site', 'name');
$title = sprintf(_("%s and friends"), $user->nickname); $title = sprintf(_("%s and friends"), $user->nickname);
$taguribase = common_config('integration', 'taguri'); $taguribase = common_config('integration', 'taguri');
$id = "tag:$taguribase:FriendsTimeline:" . $user->id; $id = "tag:$taguribase:FriendsTimeline:" . $user->id;
$link = common_local_url('all', $link = common_local_url('all',
array('nickname' => $user->nickname)); array('nickname' => $user->nickname));
$subtitle = sprintf(_('Updates from %1$s and friends on %2$s!'), $subtitle = sprintf(_('Updates from %1$s and friends on %2$s!'),
$user->nickname, $sitename); $user->nickname, $sitename);
$page = (int)$this->arg('page', 1);
$count = (int)$this->arg('count', 20);
$max_id = (int)$this->arg('max_id', 0);
$since_id = (int)$this->arg('since_id', 0);
$since = $this->arg('since');
if (!empty($this->auth_user) && $this->auth_user->id == $user->id) {
$notice = $user->noticeInbox(($page-1)*$count,
$count, $since_id, $max_id, $since);
} else {
$notice = $user->noticesWithFriends(($page-1)*$count,
$count, $since_id, $max_id, $since);
}
switch($this->arg('format')) { switch($this->arg('format')) {
case 'xml': case 'xml':
$this->show_xml_timeline($notice); $this->show_xml_timeline($this->notices);
break; break;
case 'rss': case 'rss':
$this->show_rss_timeline($notice, $title, $link, $subtitle); $this->show_rss_timeline($this->notices, $title, $link, $subtitle);
break; break;
case 'atom': case 'atom':
$target_id = $this->arg('id'); $target_id = $this->arg('id');
if (isset($target_id)) { if (isset($target_id)) {
$selfuri = common_root_url() . $selfuri = common_root_url() .
'api/statuses/friends_timeline/' . 'api/statuses/friends_timeline/' .
$target_id . '.atom'; $target_id . '.atom';
} else { } else {
$selfuri = common_root_url() . $selfuri = common_root_url() .
'api/statuses/friends_timeline.atom'; 'api/statuses/friends_timeline.atom';
} }
$this->show_atom_timeline($notice, $title, $id, $link, $this->show_atom_timeline($this->notices, $title, $id, $link,
$subtitle, null, $selfuri); $subtitle, null, $selfuri);
break; break;
case 'json': case 'json':
$this->show_json_timeline($notice); $this->show_json_timeline($this->notices);
break; break;
default: default:
$this->clientError(_('API method not found!'), $code = 404); $this->clientError(_('API method not found!'), $code = 404);
break;
}
}
function getNotices()
{
$notices = array();
if (!empty($this->auth_user) && $this->auth_user->id == $this->user->id) {
$notice = $this->user->noticeInbox(($this->page-1) * $this->count,
$this->count, $this->since_id,
$this->max_id, $this->since);
} else {
$notice = $this->user->noticesWithFriends(($this->page-1) * $this->count,
$this->count, $this->since_id,
$this->max_id, $this->since);
}
while ($notice->fetch()) {
$notices[] = clone($notice);
} }
return $notices;
} }
function requiresAuth() function requiresAuth()
{ {
return true; // If the site is "private", all API methods except statusnet/config
// need authentication
if (common_config('site', 'private')) {
return true;
}
// bare auth: only needs auth if without an argument or query param specifying user id
$id = $this->arg('id');
$user_id = $this->arg('user_id');
$screen_name = $this->arg('screen_name');
if (empty($id) && empty($user_id) && empty($screen_name)) {
return true;
}
return false;
} }
/** /**
...@@ -149,13 +179,21 @@ class ApifriendstimelineAction extends TwitterapiAction ...@@ -149,13 +179,21 @@ class ApifriendstimelineAction extends TwitterapiAction
} }
/** /**
* When was this page last modified? * When was this feed last modified?
* *
*/ */
function lastModified() function lastModified()
{ {
if (empty($this->notices)) {
return null;
}
if (count($this->notices) == 0) {
return null;
}
return strtotime($this->notices[0]->created);
} }
} }
\ No newline at end of file
...@@ -1098,7 +1098,6 @@ class TwitterapiAction extends Action ...@@ -1098,7 +1098,6 @@ class TwitterapiAction extends Action
} }
} }
function get_group($id, $apidata=null) function get_group($id, $apidata=null)
{ {
if (empty($id)) { if (empty($id)) {
...@@ -1206,7 +1205,7 @@ class TwitterapiAction extends Action ...@@ -1206,7 +1205,7 @@ class TwitterapiAction extends Action
} }
} }
function showBasicAuthHeader() function checkBasicAuthUser()
{ {
$this->basicAuthProcessHeader(); $this->basicAuthProcessHeader();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment