]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/twitterapi.php
Merge commit 'br3nda/0.8.x-pginstaller' into 0.8.x
[quix0rs-gnu-social.git] / lib / twitterapi.php
index 40e5b5067796e45e1d914a3d2bca335ddd16524a..79da82a19408ea44a195087faf31dbc6d9ae389e 100644 (file)
@@ -89,7 +89,7 @@ class TwitterapiAction extends Action
 
         $twitter_user['url'] = ($profile->homepage) ? $profile->homepage : null;
         $twitter_user['protected'] = false; # not supported by Laconica yet
-        $twitter_user['followers_count'] = $this->count_subscriptions($profile);
+        $twitter_user['followers_count'] = $profile->subscriberCount();
 
         // To be supported soon...
         $twitter_user['profile_background_color'] = '';
@@ -98,17 +98,11 @@ class TwitterapiAction extends Action
         $twitter_user['profile_sidebar_fill_color'] = '';
         $twitter_user['profile_sidebar_border_color'] = '';
 
-        $subbed = DB_DataObject::factory('subscription');
-        $subbed->subscriber = $profile->id;
-        $subbed_count = (int) $subbed->count() - 1;
-        $twitter_user['friends_count'] = (is_int($subbed_count)) ? $subbed_count : 0;
+        $twitter_user['friends_count'] = $profile->subscriptionCount();
 
         $twitter_user['created_at'] = $this->date_twitter($profile->created);
 
-        $faves = DB_DataObject::factory('fave');
-        $faves->user_id = $user->id;
-        $faves_count = (int) $faves->count();
-        $twitter_user['favourites_count'] = $faves_count; // British spelling!
+        $twitter_user['favourites_count'] = $profile->faveCount(); // British spelling!
 
         // Need to pull up the user for some of this
         $user = User::staticGet($profile->id);
@@ -129,11 +123,7 @@ class TwitterapiAction extends Action
         $twitter_user['profile_background_image_url'] = '';
         $twitter_user['profile_background_tile'] = false;
 
-        $notices = DB_DataObject::factory('notice');
-        $notices->profile_id = $profile->id;
-        $notice_count = (int) $notices->count();
-
-        $twitter_user['statuses_count'] = (is_int($notice_count)) ? $notice_count : 0;
+        $twitter_user['statuses_count'] = $profile->noticeCount();
 
         // Is the requesting user following this user?
         $twitter_user['following'] = false;
@@ -196,6 +186,24 @@ class TwitterapiAction extends Action
             $twitter_status['favorited'] = false;
         }
 
+        // Enclosures
+        $attachments = $notice->attachments();
+        $enclosures = array();
+
+        foreach ($attachments as $attachment) {
+            if ($attachment->isEnclosure()) {
+                 $enclosure = array();
+                 $enclosure['url'] = $attachment->url;
+                 $enclosure['mimetype'] = $attachment->mimetype;
+                 $enclosure['size'] = $attachment->size;
+                 $enclosures[] = $enclosure;
+            }
+        }
+
+        if (!empty($enclosures)) {
+            $twitter_status['attachments'] = $enclosures;
+        }
+
         if ($include_user) {
             # Don't get notice (recursive!)
             $twitter_user = $this->twitter_user_array($profile, false);
@@ -207,11 +215,10 @@ class TwitterapiAction extends Action
 
     function twitter_rss_entry_array($notice)
     {
-
         $profile = $notice->getProfile();
         $entry = array();
 
-        # We trim() to avoid extraneous whitespace in the output
+        // We trim() to avoid extraneous whitespace in the output
 
         $entry['content'] = common_xml_safe_str(trim($notice->rendered));
         $entry['title'] = $profile->nickname . ': ' . common_xml_safe_str(trim($notice->content));
@@ -224,7 +231,41 @@ class TwitterapiAction extends Action
         $entry['updated'] = $entry['published'];
         $entry['author'] = $profile->getBestName();
 
-        # RSS Item specific
+        // Enclosures
+        $attachments = $notice->attachments();
+        $enclosures = array();
+
+        foreach ($attachments as $attachment) {
+            if ($attachment->isEnclosure()) {
+                 $enclosure = array();
+                 $enclosure['url'] = $attachment->url;
+                 $enclosure['mimetype'] = $attachment->mimetype;
+                 $enclosure['size'] = $attachment->size;
+                 $enclosures[] = $enclosure;
+            }
+        }
+
+        if (!empty($enclosures)) {
+            $entry['enclosures'] = $enclosures;
+        }
+
+/*
+        // Enclosure
+        $attachments = $notice->attachments();
+        if($attachments){
+            $entry['enclosures']=array();
+            foreach($attachments as $attachment){
+                if ($attachment->isEnclosure()) {
+                    $enclosure=array();
+                    $enclosure['url']=$attachment->url;
+                    $enclosure['mimetype']=$attachment->mimetype;
+                    $enclosure['size']=$attachment->size;
+                    $entry['enclosures'][]=$enclosure;
+                }
+            }
+        }
+*/
+        // RSS Item specific
         $entry['description'] = $entry['content'];
         $entry['pubDate'] = common_date_rfc2822($notice->created);
         $entry['guid'] = $entry['link'];
@@ -350,6 +391,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);
             }
@@ -370,6 +414,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');
@@ -378,6 +436,13 @@ class TwitterapiAction extends Action
         $this->element('pubDate', null, $entry['pubDate']);
         $this->element('guid', null, $entry['guid']);
         $this->element('link', null, $entry['link']);
+
+        # RSS only supports 1 enclosure per item
+        if($entry['enclosures']){
+            $enclosure = $entry['enclosures'][0];
+            $this->element('enclosure', array('url'=>$enclosure['url'],'type'=>$enclosure['mimetype'],'length'=>$enclosure['size']), null);
+        }
+
         $this->elementEnd('item');
     }
 
@@ -765,6 +830,34 @@ class TwitterapiAction extends Action
         }
     }
 
+    function get_group($id, $apidata=null)
+    {
+        if (empty($id)) {
+
+            if (is_numeric($this->arg('id'))) {
+                return User_group::staticGet($this->arg('id'));
+            } else if ($this->arg('id')) {
+                $nickname = common_canonical_nickname($this->arg('id'));
+                return User_group::staticGet('nickname', $nickname);
+            } else if ($this->arg('group_id')) {
+                // This is to ensure that a non-numeric user_id still
+                // overrides screen_name even if it doesn't get used
+                if (is_numeric($this->arg('group_id'))) {
+                    return User_group::staticGet('id', $this->arg('group_id'));
+                }
+            } else if ($this->arg('group_name')) {
+                $nickname = common_canonical_nickname($this->arg('group_name'));
+                return User_group::staticGet('nickname', $nickname);
+            }
+
+        } else if (is_numeric($id)) {
+            return User_group::staticGet($id);
+        } else {
+            $nickname = common_canonical_nickname($id);
+            return User_group::staticGet('nickname', $nickname);
+        }
+    }
+
     function get_profile($id)
     {
         if (is_numeric($id)) {