]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Implemented the list_all and list groups API methods as defined at http://laconi...
authorCraig Andrews <candrews@integralblue.com>
Fri, 7 Aug 2009 22:00:04 +0000 (18:00 -0400)
committerCraig Andrews <candrews@integralblue.com>
Fri, 7 Aug 2009 22:00:04 +0000 (18:00 -0400)
Made the Autocomplete plugin also autocomplete groups

actions/api.php
actions/twitapigroups.php
classes/User_group.php
lib/router.php
lib/twitterapi.php
plugins/Autocomplete/Autocomplete.js

index 99ab262ad77685cdad2a347264e29b079a27382f..6d226af7e604da46be55cbeae10076780a027c7d 100644 (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";
 
index 82604ebff2b90a44a6fb6fbdfb55a1187b0e947b..bebc07fa19df40589269b10d2be43739a2dfb549 100644 (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);
index b1ab1c2d313c6537f4fc0db0f01a16b51dfc6c24..ea19cbb97acff0b2a752ebc1793d02e81a94903b 100644 (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();
+    }
 }
index 19839b99722a0682b8f4c253479579d67e969744..9ab46856d193c42e01e0963f521c3709dbce0b4d 100644 (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'));
index 4737c5874f7babc28d6d65db87c396859d64826f..a5dc2067f85ffb389471fdb799254526cfe917af 100644 (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');
index 759ed60ae9c4f6592767542e9e027c3f179970b7..e799c11e50ae14ced17c0bf9ddb3e58f2bee4cd5 100644 (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;
+                }
+            });
+        }
+    );
 });