]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/twitterapi.php
add nickname to list/icons links
[quix0rs-gnu-social.git] / lib / twitterapi.php
index c8d78e1a37e52e29a16a0aed3655e16a94542351..e2b0ba164d016edf947f734a8f8a3b79eefc44b3 100644 (file)
@@ -21,6 +21,8 @@ if (!defined('LACONICA')) { exit(1); }
 
 class TwitterapiAction extends Action {
 
+       var $auth_user;
+
        function handle($args) {
                parent::handle($args);
        }
@@ -53,10 +55,11 @@ class TwitterapiAction extends Action {
                return $twitter_user;
        }
 
-       function twitter_status_array($notice, $get_user=true) {
+       function twitter_status_array($notice, $include_user=true) {
 
-               $twitter_status = array();
+               $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);
@@ -64,12 +67,18 @@ class TwitterapiAction extends Action {
                $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;
-               $twitter_status['favorited'] = NULL; # XXX: Not implemented on Laconica yet.
 
-               if ($get_user) {
-                       $profile = $notice->getProfile();
+               if (isset($this->auth_user)) {
+                       common_debug("auth user set: " . $this->auth_user->nickname);
+                       $twitter_status['favorited'] = ($this->auth_user->hasFave($notice)) ? 'true' : 'false';
+               } else {
+                       common_debug("no auth user set");
+                       $twitter_status['favorited'] = 'false';
+               }
+
+               if ($include_user) {
                        # Don't get notice (recursive!)
-                       $twitter_user = $this->twitter_user_array($profile, false);
+                       $twitter_user = $this->twitter_user_array($profile, false, $user);
                        $twitter_status['user'] = $twitter_user;
                }
 
@@ -83,7 +92,7 @@ class TwitterapiAction extends Action {
                $server = common_config('site', 'server');
                $entry = array();
 
-               $entry['content'] = $profile->nickname . ': ' . $notice->content;
+               $entry['content'] = $profile->nickname . ': ' . common_xml_safe_str($notice->content);
                $entry['title'] = $entry['content'];
                $entry['link'] = common_local_url('shownotice', array('notice' => $notice->id));
                $entry['published'] = common_date_iso8601($notice->created);
@@ -106,21 +115,20 @@ class TwitterapiAction extends Action {
                $entry['title'] = sprintf('Message from %s to %s',
                        $message->getFrom()->nickname, $message->getTo()->nickname);
 
-               $entry['content'] = $message->content;
+               $entry['content'] = common_xml_safe_str($message->content);
                $entry['link'] = $message->uri;
                $entry['published'] = common_date_iso8601($message->created);
                $entry['id'] = "tag:$server,2008:$entry[link]";
                $entry['updated'] = $entry['published'];
 
                # RSS Item specific
-               $entry['description'] = $message->content;
+               $entry['description'] = $entry['content'];
                $entry['pubDate'] = common_date_rfc2822($message->created);
                $entry['guid'] = $entry['link'];
 
                return $entry;
        }
 
-
        function twitter_dmsg_array($message) {
 
                $twitter_dm = array();
@@ -144,9 +152,14 @@ class TwitterapiAction extends Action {
        function show_twitter_xml_status($twitter_status) {
                common_element_start('status');
                foreach($twitter_status as $element => $value) {
-                       if ($element == 'user') {
+                       switch ($element) {
+                       case 'user':
                                $this->show_twitter_xml_user($twitter_status['user']);
-                       } else {
+                               break;
+                       case 'text':
+                               common_element($element, NULL, common_xml_safe_str($value));
+                               break;
+                       default:
                                common_element($element, NULL, $value);
                        }
                }
@@ -186,16 +199,8 @@ class TwitterapiAction extends Action {
                common_element_end('entry');
        }
 
-       function show_twitter_json_statuses($twitter_statuses) {
-               print(json_encode($twitter_statuses));
-       }
-
-       function show_twitter_json_users($twitter_users) {
-               print(json_encode($twitter_users));
-       }
-
-       function show_twitter_json_dmsgs($twitter_dms) {
-               print(json_encode($twitter_dms));
+       function show_json_objects($objects) {
+               print(json_encode($objects));
        }
 
        function show_single_xml_status($notice) {
@@ -203,15 +208,13 @@ class TwitterapiAction extends Action {
                $twitter_status = $this->twitter_status_array($notice);
                $this->show_twitter_xml_status($twitter_status);
                $this->end_document('xml');
-               exit();
        }
 
        function show_single_json_status($notice) {
                $this->init_document('json');
                $status = $this->twitter_status_array($notice);
-               $this->show_twitter_json_statuses($status);
+               $this->show_json_objects($status);
                $this->end_document('json');
-               exit();
        }
 
        function show_single_xml_dmsg($message) {
@@ -224,22 +227,138 @@ class TwitterapiAction extends Action {
        function show_single_json_dmsg($message) {
                $this->init_document('json');
                $dmsg = $this->twitter_dmsg_array($message);
-               $this->show_twitter_json_dm($dmsg);
+               $this->show_json_objects($dmsg);
                $this->end_document('json');
        }
 
        function show_twitter_xml_dmsg($twitter_dm) {
                common_element_start('direct_message');
                foreach($twitter_dm as $element => $value) {
-                       if ($element == 'sender' || $element == 'recipient') {
+                       switch ($element) {
+                       case 'sender':
+                       case 'recipient':
                                $this->show_twitter_xml_user($value, $element);
-                       } else {
+                               break;
+                       case 'text':
+                               common_element($element, NULL, common_xml_safe_str($value));
+                               break;
+                       default:
                                common_element($element, NULL, $value);
                        }
                }
                common_element_end('direct_message');
        }
 
+       function show_xml_timeline($notice) {
+
+               $this->init_document('xml');
+               common_element_start('statuses', array('type' => 'array'));
+
+               if (is_array($notice)) {
+                       foreach ($notice as $n) {
+                               $twitter_status = $this->twitter_status_array($n);
+                               $this->show_twitter_xml_status($twitter_status);
+                       }
+               } else {
+                       while ($notice->fetch()) {
+                               $twitter_status = $this->twitter_status_array($notice);
+                               $this->show_twitter_xml_status($twitter_status);
+                       }
+               }
+
+               common_element_end('statuses');
+               $this->end_document('xml');
+       }
+
+       function show_rss_timeline($notice, $title, $link, $subtitle, $suplink=NULL) {
+
+               $this->init_document('rss');
+
+               common_element_start('channel');
+               common_element('title', NULL, $title);
+               common_element('link', NULL, $link);
+               if (!is_null($suplink)) {
+                       # For FriendFeed's SUP protocol
+                       common_element('link', array('xmlns' => 'http://www.w3.org/2005/Atom',
+                                                                                'rel' => 'http://api.friendfeed.com/2008/03#sup',
+                                                                                'href' => $suplink,
+                                                                                'type' => 'application/json'));
+               }
+               common_element('description', NULL, $subtitle);
+               common_element('language', NULL, 'en-us');
+               common_element('ttl', NULL, '40');
+
+
+               if (is_array($notice)) {
+                       foreach ($notice as $n) {
+                               $entry = $this->twitter_rss_entry_array($n);
+                               $this->show_twitter_rss_item($entry);
+                       }
+               } else {
+                       while ($notice->fetch()) {
+                               $entry = $this->twitter_rss_entry_array($notice);
+                               $this->show_twitter_rss_item($entry);
+                       }
+               }
+
+               common_element_end('channel');
+               $this->end_twitter_rss();
+       }
+
+       function show_atom_timeline($notice, $title, $id, $link, $subtitle=NULL, $suplink=NULL) {
+
+               $this->init_document('atom');
+
+               common_element('title', NULL, $title);
+               common_element('id', NULL, $id);
+               common_element('link', array('href' => $link, 'rel' => 'alternate', 'type' => 'text/html'), NULL);
+               if (!is_null($suplink)) {
+                       # For FriendFeed's SUP protocol
+                       common_element('link', array('rel' => 'http://api.friendfeed.com/2008/03#sup',
+                                                                                'href' => $suplink,
+                                                                                'type' => 'application/json'));
+               }
+               common_element('subtitle', NULL, $subtitle);
+
+               if (is_array($notice)) {
+                       foreach ($notice as $n) {
+                               $entry = $this->twitter_rss_entry_array($n);
+                               $this->show_twitter_atom_entry($entry);
+                       }
+               } else {
+                       while ($notice->fetch()) {
+                               $entry = $this->twitter_rss_entry_array($notice);
+                               $this->show_twitter_atom_entry($entry);
+                       }
+               }
+
+               $this->end_document('atom');
+
+       }
+
+       function show_json_timeline($notice) {
+
+               $this->init_document('json');
+
+               $statuses = array();
+
+               if (is_array($notice)) {
+                       foreach ($notice as $n) {
+                               $twitter_status = $this->twitter_status_array($n);
+                               array_push($statuses, $twitter_status);
+                       }
+               } else {
+                       while ($notice->fetch()) {
+                               $twitter_status = $this->twitter_status_array($notice);
+                               array_push($statuses, $twitter_status);
+                       }
+               }
+
+               $this->show_json_objects($statuses);
+
+               $this->end_document('json');
+       }
+
        // Anyone know what date format this is?
        // Twitter's dates look like this: "Mon Jul 14 23:52:38 +0000 2008" -- Zach
        function date_twitter($dt) {
@@ -381,7 +500,6 @@ class TwitterapiAction extends Action {
                        $this->end_document('json');
                }
 
-               exit();
        }
 
        function init_twitter_rss() {
@@ -411,7 +529,7 @@ class TwitterapiAction extends Action {
                        $this->show_twitter_xml_user($profile_array);
                        break;
                 case 'json':
-                       $this->show_twitter_json_users($profile_array);
+                       $this->show_json_objects($profile_array);
                        break;
                 default:
                        $this->client_error(_('Not a supported data format.'));
@@ -420,11 +538,14 @@ class TwitterapiAction extends Action {
                return;
        }
 
-       function get_user($id) {
-               if (is_numeric($id)) {
+       function get_user($id, $apidata=NULL) {
+               if (!$id) {
+                       return $apidata['user'];
+               } else if (is_numeric($id)) {
                        return User::staticGet($id);
                } else {
-                       return User::staticGet('nickname', $id);
+                       $nickname = common_canonical_nickname($id);
+                       return User::staticGet('nickname', $nickname);
                }
        }