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
This commit is contained in:
Craig Andrews 2009-08-07 18:00:04 -04:00
parent 63cedb7c31
commit 11086c7823
6 changed files with 306 additions and 1 deletions

View File

@ -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";

View File

@ -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);

View File

@ -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();
}
}

View File

@ -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'));

View File

@ -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');

View File

@ -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;
}
});
}
);
});