From 26b608d914bb5a04c2285111588cbdad12a5a936 Mon Sep 17 00:00:00 2001 From: Craig Andrews Date: Thu, 6 Aug 2009 15:14:27 -0400 Subject: Support the 'lite' parameter to statuses/friends and statuses/followers twitter api methods. http://laconi.ca/trac/ticket/1786 --- lib/twitterapi.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/twitterapi.php') diff --git a/lib/twitterapi.php b/lib/twitterapi.php index 4115d9dcb..4737c5874 100644 --- a/lib/twitterapi.php +++ b/lib/twitterapi.php @@ -844,9 +844,9 @@ class TwitterapiAction extends Action $this->endXML(); } - function show_profile($profile, $content_type='xml', $notice=null) + function show_profile($profile, $content_type='xml', $notice=null, $includeStatuses=true) { - $profile_array = $this->twitter_user_array($profile, true); + $profile_array = $this->twitter_user_array($profile, $includeStatuses); switch ($content_type) { case 'xml': $this->show_twitter_xml_user($profile_array); -- cgit v1.2.3-54-g00ecf From 11086c78239a30dc47622837a2800d899ebf9b0f Mon Sep 17 00:00:00 2001 From: Craig Andrews Date: Fri, 7 Aug 2009 18:00:04 -0400 Subject: Implemented the list_all and list groups API methods as defined at http://laconi.ca/trac/wiki/ProposedGroupsAPI Made the Autocomplete plugin also autocomplete groups --- actions/api.php | 5 +- actions/twitapigroups.php | 97 +++++++++++++++++++++++++++ classes/User_group.php | 41 ++++++++++++ lib/router.php | 22 +++++++ lib/twitterapi.php | 123 +++++++++++++++++++++++++++++++++++ plugins/Autocomplete/Autocomplete.js | 19 ++++++ 6 files changed, 306 insertions(+), 1 deletion(-) (limited to 'lib/twitterapi.php') diff --git a/actions/api.php b/actions/api.php index 99ab262ad..6d226af7e 100644 --- a/actions/api.php +++ b/actions/api.php @@ -131,6 +131,8 @@ class ApiAction extends Action 'tags/timeline', 'oembed/oembed', 'groups/show', + 'groups/timeline', + 'groups/list_all', 'groups/timeline'); static $bareauth = array('statuses/user_timeline', @@ -140,7 +142,8 @@ class ApiAction extends Action 'statuses/mentions', 'statuses/followers', 'favorites/favorites', - 'friendships/show'); + 'friendships/show', + 'groups/list_groups'); $fullname = "$this->api_action/$this->api_method"; diff --git a/actions/twitapigroups.php b/actions/twitapigroups.php index 82604ebff..bebc07fa1 100644 --- a/actions/twitapigroups.php +++ b/actions/twitapigroups.php @@ -51,6 +51,103 @@ require_once INSTALLDIR.'/lib/twitterapi.php'; class TwitapigroupsAction extends TwitterapiAction { + function list_groups($args, $apidata) + { + parent::handle($args); + + common_debug("in groups api action"); + + $this->auth_user = $apidata['user']; + $user = $this->get_user($apidata['api_arg'], $apidata); + + if (empty($user)) { + $this->clientError('Not Found', 404, $apidata['content-type']); + return; + } + + $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'); + $group = $user->getGroups(($page-1)*$count, + $count, $since_id, $max_id, $since); + + $sitename = common_config('site', 'name'); + $title = sprintf(_("%s's groups"), $user->nickname); + $taguribase = common_config('integration', 'taguri'); + $id = "tag:$taguribase:Groups"; + $link = common_root_url(); + $subtitle = sprintf(_("groups %s is a member of on %s"), $user->nickname, $sitename); + + switch($apidata['content-type']) { + case 'xml': + $this->show_xml_groups($group); + break; + case 'rss': + $this->show_rss_groups($group, $title, $link, $subtitle); + break; + case 'atom': + $selfuri = common_root_url() . 'api/laconica/groups/list/' . $user->id . '.atom'; + $this->show_atom_groups($group, $title, $id, $link, + $subtitle, $selfuri); + break; + case 'json': + $this->show_json_groups($group); + break; + default: + $this->clientError(_('API method not found!'), $code = 404); + break; + } + } + + function list_all($args, $apidata) + { + parent::handle($args); + + common_debug("in groups api action"); + + $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'); + + /* TODO: + Use the $page, $count, $max_id, $since_id, and $since parameters + */ + $group = new User_group(); + $group->orderBy('created DESC'); + $group->find(); + + $sitename = common_config('site', 'name'); + $title = sprintf(_("%s groups"), $sitename); + $taguribase = common_config('integration', 'taguri'); + $id = "tag:$taguribase:Groups"; + $link = common_root_url(); + $subtitle = sprintf(_("groups on %s"), $sitename); + + switch($apidata['content-type']) { + case 'xml': + $this->show_xml_groups($group); + break; + case 'rss': + $this->show_rss_groups($group, $title, $link, $subtitle); + break; + case 'atom': + $selfuri = common_root_url() . 'api/laconica/groups/list_all.atom'; + $this->show_atom_groups($group, $title, $id, $link, + $subtitle, $selfuri); + break; + case 'json': + $this->show_json_groups($group); + break; + default: + $this->clientError(_('API method not found!'), $code = 404); + break; + } + } + function show($args, $apidata) { parent::handle($args); diff --git a/classes/User_group.php b/classes/User_group.php index b1ab1c2d3..ea19cbb97 100644 --- a/classes/User_group.php +++ b/classes/User_group.php @@ -297,4 +297,45 @@ class User_group extends Memcached_DataObject return $ids; } + + function asAtomEntry($namespace=false, $source=false) + { + $xs = new XMLStringer(true); + + if ($namespace) { + $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom', + 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0'); + } else { + $attrs = array(); + } + + $xs->elementStart('entry', $attrs); + + if ($source) { + $xs->elementStart('source'); + $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name')); + $xs->element('link', array('href' => $this->permalink())); + } + + if ($source) { + $xs->elementEnd('source'); + } + + $xs->element('title', null, $this->nickname); + $xs->element('summary', null, $this->description); + + $xs->element('link', array('rel' => 'alternate', + 'href' => $this->permalink())); + + $xs->element('id', null, $this->permalink()); + + $xs->element('published', null, common_date_w3dtf($this->created)); + $xs->element('updated', null, common_date_w3dtf($this->modified)); + + $xs->element('content', array('type' => 'html'), $this->description); + + $xs->elementEnd('entry'); + + return $xs->getString(); + } } diff --git a/lib/router.php b/lib/router.php index 19839b997..9ab46856d 100644 --- a/lib/router.php +++ b/lib/router.php @@ -409,6 +409,28 @@ class Router 'apiaction' => 'laconica')); // Groups + //'list' has to be handled differently, as php will not allow a method to be named 'list' + $m->connect('api/laconica/groups/list/:argument', + array('action' => 'api', + 'method' => 'list_groups', + 'apiaction' => 'groups')); + foreach (array('xml', 'json', 'rss', 'atom') as $e) { + $m->connect('api/laconica/groups/list.' . $e, + array('action' => 'api', + 'method' => 'list_groups.' . $e, + 'apiaction' => 'groups')); + } + + $m->connect('api/laconica/groups/:method', + array('action' => 'api', + 'apiaction' => 'statuses'), + array('method' => '(list_all|)(\.(atom|rss|xml|json))?')); + + $m->connect('api/statuses/:method/:argument', + array('action' => 'api', + 'apiaction' => 'statuses'), + array('method' => '(|user_timeline|friends_timeline|replies|mentions|show|destroy|friends|followers)')); + $m->connect('api/laconica/groups/:method/:argument', array('action' => 'api', 'apiaction' => 'groups')); diff --git a/lib/twitterapi.php b/lib/twitterapi.php index 4737c5874..a5dc2067f 100644 --- a/lib/twitterapi.php +++ b/lib/twitterapi.php @@ -233,6 +233,24 @@ class TwitterapiAction extends Action return $twitter_group; } + function twitter_rss_group_array($group) + { + $entry = array(); + $entry['content']=$group->description; + $entry['title']=$group->nickname; + $entry['link']=$group->permalink(); + $entry['published']=common_date_iso8601($group->created); + $entry['updated']==common_date_iso8601($group->modified); + $taguribase = common_config('integration', 'groupuri'); + $entry['id'] = "group:$groupuribase:$entry[link]"; + + $entry['description'] = $entry['content']; + $entry['pubDate'] = common_date_rfc2822($group->created); + $entry['guid'] = $entry['link']; + + return $entry; + } + function twitter_rss_entry_array($notice) { $profile = $notice->getProfile(); @@ -644,6 +662,65 @@ class TwitterapiAction extends Action } + function show_rss_groups($group, $title, $link, $subtitle) + { + + $this->init_document('rss'); + + $this->elementStart('channel'); + $this->element('title', null, $title); + $this->element('link', null, $link); + $this->element('description', null, $subtitle); + $this->element('language', null, 'en-us'); + $this->element('ttl', null, '40'); + + if (is_array($group)) { + foreach ($group as $g) { + $twitter_group = $this->twitter_rss_group_array($g); + $this->show_twitter_rss_item($twitter_group); + } + } else { + while ($group->fetch()) { + $twitter_group = $this->twitter_rss_group_array($group); + $this->show_twitter_rss_item($twitter_group); + } + } + + $this->elementEnd('channel'); + $this->end_twitter_rss(); + } + + function show_atom_groups($group, $title, $id, $link, $subtitle=null, $selfuri=null) + { + + $this->init_document('atom'); + + $this->element('title', null, $title); + $this->element('id', null, $id); + $this->element('link', array('href' => $link, 'rel' => 'alternate', 'type' => 'text/html'), null); + + if (!is_null($selfuri)) { + $this->element('link', array('href' => $selfuri, + 'rel' => 'self', 'type' => 'application/atom+xml'), null); + } + + $this->element('updated', null, common_date_iso8601('now')); + $this->element('subtitle', null, $subtitle); + + if (is_array($group)) { + foreach ($group as $g) { + $this->raw($g->asAtomEntry()); + } + } else { + while ($group->fetch()) { + $this->raw($group->asAtomEntry()); + } + } + + $this->end_document('atom'); + + } + function show_json_timeline($notice) { @@ -668,6 +745,52 @@ class TwitterapiAction extends Action $this->end_document('json'); } + function show_json_groups($group) + { + + $this->init_document('json'); + + $groups = array(); + + if (is_array($group)) { + foreach ($group as $g) { + $twitter_group = $this->twitter_group_array($g); + array_push($groups, $twitter_group); + } + } else { + while ($group->fetch()) { + $twitter_group = $this->twitter_group_array($group); + array_push($groups, $twitter_group); + } + } + + $this->show_json_objects($groups); + + $this->end_document('json'); + } + + function show_xml_groups($group) + { + + $this->init_document('xml'); + $this->elementStart('groups', array('type' => 'array')); + + if (is_array($group)) { + foreach ($group as $g) { + $twitter_group = $this->twitter_group_array($g); + $this->show_twitter_xml_group($twitter_group); + } + } else { + while ($group->fetch()) { + $twitter_group = $this->twitter_group_array($group); + $this->show_twitter_xml_group($twitter_group); + } + } + + $this->elementEnd('groups'); + $this->end_document('xml'); + } + function show_single_json_group($group) { $this->init_document('json'); diff --git a/plugins/Autocomplete/Autocomplete.js b/plugins/Autocomplete/Autocomplete.js index 759ed60ae..e799c11e5 100644 --- a/plugins/Autocomplete/Autocomplete.js +++ b/plugins/Autocomplete/Autocomplete.js @@ -4,6 +4,7 @@ $(document).ready(function(){ $('#notice_data-text').autocomplete(friends, { multiple: true, multipleSeparator: " ", + minChars: 1, formatItem: function(row, i, max){ return '@' + row.screen_name + ' (' + row.name + ')'; }, @@ -16,4 +17,22 @@ $(document).ready(function(){ }); } ); + $.getJSON($('address .url')[0].href+'/api/laconica/groups/list.json?user_id=' + current_user['id'] + '&callback=?', + function(groups){ + $('#notice_data-text').autocomplete(groups, { + multiple: true, + multipleSeparator: " ", + minChars: 1, + formatItem: function(row, i, max){ + return '!' + row.nickname + ' (' + row.fullname + ')'; + }, + formatMatch: function(row, i, max){ + return '!' + row.nickname; + }, + formatResult: function(row){ + return '!' + row.nickname; + } + }); + } + ); }); -- cgit v1.2.3-54-g00ecf From 348fa35c6b465b404f0bd056d0917c501a8145eb Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Fri, 17 Jul 2009 12:33:51 -0700 Subject: Also show XML representation of attachments in the API --- lib/twitterapi.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'lib/twitterapi.php') diff --git a/lib/twitterapi.php b/lib/twitterapi.php index 749083c7c..66664334c 100644 --- a/lib/twitterapi.php +++ b/lib/twitterapi.php @@ -369,6 +369,9 @@ class TwitterapiAction extends Action case 'text': $this->element($element, null, common_xml_safe_str($value)); break; + case 'attachments': + $this->show_xml_attachments($twitter_status['attachments']); + break; default: $this->element($element, null, $value); } @@ -389,6 +392,20 @@ class TwitterapiAction extends Action $this->elementEnd($role); } + function show_xml_attachments($attachments) { + if (!empty($attachments)) { + $this->elementStart('attachments', array('type' => 'array')); + foreach ($attachments as $attachment) { + $attrs = array(); + $attrs['url'] = $attachment['url']; + $attrs['mimetype'] = $attachment['mimetype']; + $attrs['size'] = $attachment['size']; + $this->element('enclosure', $attrs, ''); + } + $this->elementEnd('attachments'); + } + } + function show_twitter_rss_item($entry) { $this->elementStart('item'); -- cgit v1.2.3-54-g00ecf From 9ec022df93a41c33c15ed0b2b3592f3faff40414 Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Fri, 17 Jul 2009 12:39:54 -0700 Subject: Only populate attachments array element if there are attachments --- lib/twitterapi.php | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'lib/twitterapi.php') diff --git a/lib/twitterapi.php b/lib/twitterapi.php index 66664334c..ab6c0d62c 100644 --- a/lib/twitterapi.php +++ b/lib/twitterapi.php @@ -188,15 +188,18 @@ class TwitterapiAction extends Action # Enclosures $attachments = $notice->attachments(); - $twitter_status['attachments']=array(); - if($attachments){ - foreach($attachments as $attachment){ + + if (!empty($attachments)) { + + $twitter_status['attachments'] = array(); + + foreach ($attachments as $attachment) { if ($attachment->isEnclosure()) { - $enclosure=array(); - $enclosure['url']=$attachment->url; - $enclosure['mimetype']=$attachment->mimetype; - $enclosure['size']=$attachment->size; - $twitter_status['attachments'][]=$enclosure; + $enclosure = array(); + $enclosure['url'] = $attachment->url; + $enclosure['mimetype'] = $attachment->mimetype; + $enclosure['size'] = $attachment->size; + $twitter_status['attachments'][] = $enclosure; } } } -- cgit v1.2.3-54-g00ecf