]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/TwitterBridge/twitterimport.php
Show photos on all of a user's pages
[quix0rs-gnu-social.git] / plugins / TwitterBridge / twitterimport.php
index 1b2d3953049594c7f242d44b24a18ac407b212a5..3a4c0c43c0f1170e0b42345c81f9a36d5c0f5d69 100644 (file)
@@ -56,14 +56,14 @@ class TwitterImport
 
         if (preg_match("/$source/", mb_strtolower($status->source))) {
             common_debug($this->name() . ' - Skipping import of status ' .
-                         $status->id . ' with source ' . $source);
-            continue;
+                         twitter_id($status) . ' with source ' . $source);
+            return null;
         }
 
         // Don't save it if the user is protected
         // FIXME: save it but treat it as private
         if ($status->user->protected) {
-            continue;
+            return null;
         }
 
         $notice = $this->saveStatus($status);
@@ -86,23 +86,24 @@ class TwitterImport
             return null;
         }
 
-        $statusUri = $this->makeStatusURI($status->user->screen_name, $status->id);
+        $statusId = twitter_id($status);
+        $statusUri = $this->makeStatusURI($status->user->screen_name, $statusId);
 
         // check to see if we've already imported the status
-        $n2s = Notice_to_status::staticGet('status_id', $status->id);
+        $n2s = Notice_to_status::staticGet('status_id', $statusId);
 
         if (!empty($n2s)) {
             common_log(
                 LOG_INFO,
                 $this->name() .
-                " - Ignoring duplicate import: {$status->id}"
+                " - Ignoring duplicate import: {$statusId}"
             );
             return Notice::staticGet('id', $n2s->notice_id);
         }
 
         // If it's a retweet, save it as a repeat!
         if (!empty($status->retweeted_status)) {
-            common_log(LOG_INFO, "Status {$status->id} is a retweet of {$status->retweeted_status->id}.");
+            common_log(LOG_INFO, "Status {$statusId} is a retweet of " . twitter_id($status->retweeted_status) . ".");
             $original = $this->saveStatus($status->retweeted_status);
             if (empty($original)) {
                 return null;
@@ -126,7 +127,7 @@ class TwitterImport
                                                 'uri' => $statusUri,
                                                 'is_local' => Notice::GATEWAY));
                 common_log(LOG_INFO, "Saved {$repeat->id} as a repeat of {$original->id}");
-                Notice_to_status::saveNew($repeat->id, $status->id);
+                Notice_to_status::saveNew($repeat->id, $statusId);
                 return $repeat;
             }
         }
@@ -145,17 +146,18 @@ class TwitterImport
 
         $notice->reply_to   = null;
 
-        if (!empty($status->in_reply_to_status_id)) {
-            common_log(LOG_INFO, "Status {$status->id} is a reply to status {$status->in_reply_to_status_id}");
-            $n2s = Notice_to_status::staticGet('status_id', $status->in_reply_to_status_id);
+        $replyTo = twitter_id($status, 'in_reply_to_status_id');
+        if (!empty($replyTo)) {
+            common_log(LOG_INFO, "Status {$statusId} is a reply to status {$replyTo}");
+            $n2s = Notice_to_status::staticGet('status_id', $replyTo);
             if (empty($n2s)) {
-                common_log(LOG_INFO, "Couldn't find local notice for status {$status->in_reply_to_status_id}");
+                common_log(LOG_INFO, "Couldn't find local notice for status {$replyTo}");
             } else {
                 $reply = Notice::staticGet('id', $n2s->notice_id);
                 if (empty($reply)) {
-                    common_log(LOG_INFO, "Couldn't find local notice for status {$status->in_reply_to_status_id}");
+                    common_log(LOG_INFO, "Couldn't find local notice for status {$replyTo}");
                 } else {
-                    common_log(LOG_INFO, "Found local notice {$reply->id} for status {$status->in_reply_to_status_id}");
+                    common_log(LOG_INFO, "Found local notice {$reply->id} for status {$replyTo}");
                     $notice->reply_to     = $reply->id;
                     $notice->conversation = $reply->conversation;
                 }
@@ -165,7 +167,7 @@ class TwitterImport
         if (empty($notice->conversation)) {
             $conv = Conversation::create();
             $notice->conversation = $conv->id;
-            common_log(LOG_INFO, "No known conversation for status {$status->id} so making a new one {$conv->id}.");
+            common_log(LOG_INFO, "No known conversation for status {$statusId} so making a new one {$conv->id}.");
         }
 
         $notice->is_local   = Notice::GATEWAY;
@@ -186,9 +188,10 @@ class TwitterImport
             Event::handle('EndNoticeSave', array($notice));
         }
 
-        Notice_to_status::saveNew($notice->id, $status->id);
+        Notice_to_status::saveNew($notice->id, $statusId);
 
         $this->saveStatusMentions($notice, $status);
+        $this->saveStatusAttachments($notice, $status);
 
         $notice->blowOnInsert();
 
@@ -204,7 +207,7 @@ class TwitterImport
      */
     function makeStatusURI($username, $id)
     {
-        return 'http://twitter.com/'
+        return 'http://twitter.com/#!/'
           . $username
           . '/status/'
           . $id;
@@ -261,7 +264,7 @@ class TwitterImport
     function ensureProfile($user)
     {
         // check to see if there's already a profile for this user
-        $profileurl = 'http://twitter.com/' . $user->screen_name;
+        $profileurl = 'http://twitter.com/#!/' . $user->screen_name;
         $profile = $this->getProfileByUrl($user->screen_name, $profileurl);
 
         if (!empty($profile)) {
@@ -542,7 +545,8 @@ class TwitterImport
         $text = $status->text;
 
         if (empty($status->entities)) {
-            common_log(LOG_WARNING, "No entities data for {$status->id}; trying to fake up links ourselves.");
+            $statusId = twitter_id($status);
+            common_log(LOG_WARNING, "No entities data for {$statusId}; trying to fake up links ourselves.");
             $text = common_replace_urls_callback($text, 'common_linkify');
             $text = preg_replace('/(^|\&quot\;|\'|\(|\[|\{|\s+)#([\pL\pN_\-\.]{1,64})/e', "'\\1#'.TwitterStatusFetcher::tagLink('\\2')", $text);
             $text = preg_replace('/(^|\s+)@([a-z0-9A-Z_]{1,64})/e', "'\\1@'.TwitterStatusFetcher::atLink('\\2')", $text);
@@ -614,15 +618,15 @@ class TwitterImport
 
     static function tagLink($tag)
     {
-        return "<a href='https://twitter.com/search?q=%23{$tag}' class='hashtag'>{$tag}</a>";
+        return "<a href='https://search.twitter.com/search?q=%23{$tag}' class='hashtag'>{$tag}</a>";
     }
 
     static function atLink($screenName, $fullName=null)
     {
         if (!empty($fullName)) {
-            return "<a href='http://twitter.com/{$screenName}' title='{$fullName}'>{$screenName}</a>";
+            return "<a href='http://twitter.com/#!/{$screenName}' title='{$fullName}'>{$screenName}</a>";
         } else {
-            return "<a href='http://twitter.com/{$screenName}'>{$screenName}</a>";
+            return "<a href='http://twitter.com/#!/{$screenName}'>{$screenName}</a>";
         }
     }
 
@@ -648,4 +652,22 @@ class TwitterImport
             }
         }
     }
+
+    /**
+     * Record URL links from the notice. Needed to get thumbnail records
+     * for referenced photo and video posts, etc.
+     *
+     * @param Notice $notice
+     * @param object $status
+     */
+    function saveStatusAttachments($notice, $status)
+    {
+        if (common_config('attachments', 'process_links')) {
+            if (!empty($status->entities) && !empty($status->entities->urls)) {
+                foreach ($status->entities->urls as $url) {
+                    File::processNew($url->url, $notice->id);
+                }
+            }
+        }
+    }
 }
\ No newline at end of file