]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/TwitterBridge/lib/twitterimport.php
Merge remote-tracking branch 'upstream/master'
[quix0rs-gnu-social.git] / plugins / TwitterBridge / lib / twitterimport.php
index 5258bfc2c981767bba9d297f33ef56905a3a08a3..fe11695c1f41785a342965d3cea668145498bd6e 100644 (file)
@@ -102,6 +102,17 @@ class TwitterImport
             return Notice::getKV('id', $n2s->notice_id);
         }
 
+        $dupe = Notice::getKV('uri', $statusUri);
+        if($dupe instanceof Notice) {
+            // Add it to our record
+            Notice_to_status::saveNew($dupe->id, $statusId);
+            common_log(
+                LOG_INFO,
+                __METHOD__ . " - Ignoring duplicate import: {$statusId}"
+            );
+            return $dupe;
+        }
+
         // If it's a retweet, save it as a repeat!
         if (!empty($status->retweeted_status)) {
             common_log(LOG_INFO, "Status {$statusId} is a retweet of " . twitter_id($status->retweeted_status) . ".");
@@ -138,6 +149,7 @@ class TwitterImport
         $notice->profile_id = $profile->id;
         $notice->uri        = $statusUri;
         $notice->url        = $statusUri;
+        $notice->verb       = ActivityVerb::POST;
         $notice->created    = strftime(
             '%Y-%m-%d %H:%M:%S',
             strtotime($status->created_at)
@@ -172,6 +184,12 @@ class TwitterImport
 
         if (Event::handle('StartNoticeSave', array(&$notice))) {
 
+            if (empty($notice->conversation)) {
+                $conv = Conversation::create();
+                common_log(LOG_INFO, "No known conversation for status {$statusId} so a new one ({$conv->getID()}) was created.");
+                $notice->conversation = $conv->getID();
+            }
+
             $id = $notice->insert();
 
             if ($id === false) {
@@ -179,14 +197,6 @@ class TwitterImport
                 common_log(LOG_ERR, __METHOD__ . ' - Problem saving notice.');
             }
 
-            if (empty($notice->conversation)) {
-                $orig = clone($notice);
-                $conv = Conversation::create($notice);
-                common_log(LOG_INFO, "No known conversation for status {$statusId} so a new one ({$conv->id}) was created.");
-                $notice->conversation = $conv->id;
-                $notice->update($orig);
-            }
-
             Event::handle('EndNoticeSave', array($notice));
         }
 
@@ -209,7 +219,7 @@ class TwitterImport
      */
     function makeStatusURI($username, $id)
     {
-        return 'http://twitter.com/#!/'
+        return 'https://twitter.com/'
           . $username
           . '/status/'
           . $id;
@@ -233,7 +243,10 @@ class TwitterImport
         $profile->limit(1);
 
         if (!$profile->find(true)) {
-            throw new NoResultException($profile);
+            $profile->profileurl = str_replace('https://', 'http://', $profileurl);
+            if (!$profile->find(true)) {
+                throw new NoResultException($profile);
+            }
         }
         return $profile;
     }
@@ -241,7 +254,7 @@ class TwitterImport
     protected function ensureProfile($twuser)
     {
         // check to see if there's already a profile for this user
-        $profileurl = 'http://twitter.com/' . $twuser->screen_name;
+        $profileurl = 'https://twitter.com/' . $twuser->screen_name;
         try {
             $profile = $this->getProfileByUrl($twuser->screen_name, $profileurl);
             $this->updateAvatar($twuser, $profile);
@@ -353,7 +366,6 @@ class TwitterImport
         $avatar->original   = 1; // this is an original/"uploaded" avatar
         $avatar->mediatype  = $mediatype;
         $avatar->filename   = $filename;
-        $avatar->url        = Avatar::url($filename);
         $avatar->width      = $this->avatarsize;
         $avatar->height     = $this->avatarsize;
 
@@ -527,9 +539,9 @@ class TwitterImport
     static function atLink($screenName, $fullName, $orig)
     {
         if (!empty($fullName)) {
-            return "<a href='http://twitter.com/#!/{$screenName}' title='{$fullName}'>{$orig}</a>";
+            return "<a href='https://twitter.com/{$screenName}' title='{$fullName}'>{$orig}</a>";
         } else {
-            return "<a href='http://twitter.com/#!/{$screenName}'>{$orig}</a>";
+            return "<a href='https://twitter.com/{$screenName}'>{$orig}</a>";
         }
     }
 
@@ -542,17 +554,19 @@ class TwitterImport
         }
 
         foreach ($status->entities->user_mentions as $mention) {
-            $flink = Foreign_link::getByForeignID($mention->id, TWITTER_SERVICE);
-            if (!empty($flink)) {
-                $user = User::getKV('id', $flink->user_id);
-                if (!empty($user)) {
-                    $reply = new Reply();
-                    $reply->notice_id  = $notice->id;
-                    $reply->profile_id = $user->id;
-                    $reply->modified   = $notice->created;
-                    common_log(LOG_INFO, __METHOD__ . ": saving reply: notice {$notice->id} to profile {$user->id}");
-                    $id = $reply->insert();
-                }
+            try {
+                $flink = Foreign_link::getByForeignID($mention->id, TWITTER_SERVICE);
+                $user = $flink->getUser();
+                $reply = new Reply();
+                $reply->notice_id  = $notice->id;
+                $reply->profile_id = $user->id;
+                $reply->modified   = $notice->created;
+                common_log(LOG_INFO, __METHOD__ . ": saving reply: notice {$notice->id} to profile {$user->id}");
+                $id = $reply->insert();
+            } catch (NoSuchUserException $e) {
+                common_log(LOG_WARNING, 'No local user found for Foreign_link with id: '.$mention->id);
+            } catch (NoResultException $e) {
+                common_log(LOG_WARNING, 'No foreign link or profile found for Foreign_link with id: '.$mention->id);
             }
         }
     }
@@ -564,13 +578,13 @@ class TwitterImport
      * @param Notice $notice
      * @param object $status
      */
-    function saveStatusAttachments($notice, $status)
+    function saveStatusAttachments(Notice $notice, $status)
     {
         if (common_config('attachments', 'process_links')) {
             if (!empty($status->entities) && !empty($status->entities->urls)) {
                 foreach ($status->entities->urls as $url) {
                     try {
-                        File::processNew($url->url, $notice->id);
+                        File::processNew($url->url, $notice);
                     } catch (ServerException $e) {
                         // Could not process attached URL
                     }