Made /api/account/verify_credentials.format return an extended user object. Updates to status and user API objects.

This commit is contained in:
Zach Copley 2009-03-04 16:17:40 -08:00
parent 78a715bc37
commit 36bb33fb1d
4 changed files with 73 additions and 51 deletions

View File

@ -23,16 +23,18 @@ require_once(INSTALLDIR.'/lib/twitterapi.php');
class TwitapiaccountAction extends TwitterapiAction
{
function verify_credentials($args, $apidata)
{
if ($apidata['content-type'] == 'xml') {
header('Content-Type: application/xml; charset=utf-8');
print '<authorized>true</authorized>';
} elseif ($apidata['content-type'] == 'json') {
header('Content-Type: application/json; charset=utf-8');
print '{"authorized":true}';
} else {
parent::handle($args);
switch ($apidata['content-type']) {
case 'xml':
case 'json':
$action_obj = new TwitapiusersAction();
$action_obj->prepare($args);
call_user_func(array($action_obj, 'show'), $args, $apidata);
break;
default:
header('Content-Type: text/html; charset=utf-8');
print 'Authorized';
}

View File

@ -33,14 +33,18 @@ class TwitapiusersAction extends TwitterapiAction
return;
}
$this->auth_user = $apidata['user'];
$user = null;
$email = $this->arg('email');
$user_id = $this->arg('user_id');
if ($email) {
$user = User::staticGet('email', $email);
} elseif ($user_id) {
$user = $this->get_user($user_id);
} elseif (isset($apidata['api_arg'])) {
$user = $this->get_user($apidata['api_arg']);
} elseif (isset($apidata['user'])) {
$user = $apidata['user'];
}
if (!$user) {
@ -74,9 +78,12 @@ class TwitapiusersAction extends TwitterapiAction
// Other fields Twitter sends...
$twitter_user['profile_background_color'] = '';
$twitter_user['profile_background_image_url'] = '';
$twitter_user['profile_text_color'] = '';
$twitter_user['profile_link_color'] = '';
$twitter_user['profile_sidebar_fill_color'] = '';
$twitter_user['profile_sidebar_border_color'] = '';
$twitter_user['profile_background_tile'] = 'false';
$faves = DB_DataObject::factory('fave');
$faves->user_id = $user->id;
@ -94,16 +101,25 @@ class TwitapiusersAction extends TwitterapiAction
$twitter_user['utc_offset'] = $t->format('Z');
$twitter_user['time_zone'] = $timezone;
if (isset($this->auth_user)) {
if (isset($apidata['user'])) {
if ($this->auth_user->isSubscribed($profile)) {
if ($apidata['user']->isSubscribed($profile)) {
$twitter_user['following'] = 'true';
} else {
$twitter_user['following'] = 'false';
}
// Not implemented yet
// Notifications on?
$sub = Subscription::pkeyGet(array('subscriber' =>
$apidata['user']->id, 'subscribed' => $profile->id));
if ($sub) {
if ($sub->jabber || $sub->sms) {
$twitter_user['notifications'] = 'true';
} else {
$twitter_user['notifications'] = 'false';
}
}
}
if ($apidata['content-type'] == 'xml') {
@ -114,6 +130,12 @@ class TwitapiusersAction extends TwitterapiAction
$this->init_document('json');
$this->show_json_objects($twitter_user);
$this->end_document('json');
} else {
// This is in case 'show' was called via /account/verify_credentials
// without a format (xml or json).
header('Content-Type: text/html; charset=utf-8');
print 'Authorized';
}
}

View File

@ -228,14 +228,15 @@ class Router
// users
$m->connect('api/users/show/:argument',
$m->connect('api/users/:method/:argument',
array('action' => 'api',
'apiaction' => 'users'));
'apiaction' => 'users'),
array('method' => 'show(\.(xml|json))?'));
$m->connect('api/users/:method',
array('action' => 'api',
'apiaction' => 'users'),
array('method' => 'show(\.(xml|json|atom|rss))?'));
array('method' => 'show(\.(xml|json))?'));
// direct messages

View File

@ -60,20 +60,34 @@ class TwitterapiAction extends Action
function twitter_status_array($notice, $include_user=true)
{
$profile = $notice->getProfile();
$twitter_status = array();
$twitter_status['text'] = $notice->content;
$twitter_status['truncated'] = 'false'; # Not possible on Laconica
$twitter_status['created_at'] = $this->date_twitter($notice->created);
$twitter_status['in_reply_to_status_id'] = ($notice->reply_to) ? intval($notice->reply_to) : null;
$twitter_status['in_reply_to_status_id'] = ($notice->reply_to) ?
intval($notice->reply_to) : null;
$twitter_status['source'] = $this->source_link($notice->source);
$twitter_status['id'] = intval($notice->id);
$twitter_status['in_reply_to_user_id'] = ($notice->reply_to) ? $this->replier_by_reply(intval($notice->reply_to)) : null;
$replier_profile = null;
if ($notice->reply_to) {
$reply = Notice::staticGet(intval($notice->reply_to));
if ($reply) {
$replier_profile = $reply->getProfile();
}
}
$twitter_status['in_reply_to_user_id'] =
($replier_profile) ? intval($replier_profile->id) : null;
$twitter_status['in_reply_to_screen_name'] =
($replier_profile) ? $replier_profile->nickname : null;
if (isset($this->auth_user)) {
$twitter_status['favorited'] = ($this->auth_user->hasFave($notice)) ? 'true' : 'false';
$twitter_status['favorited'] =
($this->auth_user->hasFave($notice)) ? 'true' : 'false';
} else {
$twitter_status['favorited'] = 'false';
}
@ -137,7 +151,6 @@ class TwitterapiAction extends Action
function twitter_dmsg_array($message)
{
$twitter_dm = array();
$from_profile = $message->getFrom();
@ -387,22 +400,6 @@ class TwitterapiAction extends Action
return date("D M d G:i:s O Y", $t);
}
function replier_by_reply($reply_id)
{
$notice = Notice::staticGet($reply_id);
if ($notice) {
$profile = $notice->getProfile();
if ($profile) {
return intval($profile->id);
} else {
common_debug('Can\'t find a profile for notice: ' . $notice->id, __FILE__);
}
} else {
common_debug("Can't get notice: $reply_id", __FILE__);
}
return null;
}
// XXX: Candidate for a general utility method somewhere?
function count_subscriptions($profile)
{