]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/TwitterBridge/daemons/twitterstatusfetcher.php
truncate retweeted stuff if it's too long
[quix0rs-gnu-social.git] / plugins / TwitterBridge / daemons / twitterstatusfetcher.php
index cf75e48f68b0a873fa33a90feb5f58ec4d5261ad..e897cc6ea0e7f215a1edc89cadb2a233c2b5cb6c 100755 (executable)
@@ -40,7 +40,6 @@ require_once INSTALLDIR . '/scripts/commandline.inc';
 require_once INSTALLDIR . '/lib/common.php';
 require_once INSTALLDIR . '/lib/daemon.php';
 require_once INSTALLDIR . '/plugins/TwitterBridge/twitter.php';
-require_once INSTALLDIR . '/plugins/TwitterBridge/twitterbasicauthclient.php';
 require_once INSTALLDIR . '/plugins/TwitterBridge/twitteroauthclient.php';
 
 /**
@@ -179,14 +178,13 @@ class TwitterStatusFetcher extends ParallelizingDaemon
             $client = new TwitterOAuthClient($token->key, $token->secret);
             common_debug($this->name() . ' - Grabbing friends timeline with OAuth.');
         } else {
-            $client = new TwitterBasicAuthClient($flink);
-            common_debug($this->name() . ' - Grabbing friends timeline with basic auth.');
+            common_debug("Skipping friends timeline for $flink->foreign_id since not OAuth.");
         }
 
         $timeline = null;
 
         try {
-            $timeline = $client->statusesFriendsTimeline();
+            $timeline = $client->statusesHomeTimeline();
         } catch (Exception $e) {
             common_log(LOG_WARNING, $this->name() .
                        ' - Twitter client unable to get friends timeline for user ' .
@@ -222,7 +220,11 @@ class TwitterStatusFetcher extends ParallelizingDaemon
                 continue;
             }
 
-            $this->saveStatus($status, $flink);
+            $notice = $this->saveStatus($status);
+
+            if (!empty($notice)) {
+                Inbox::insertNotice($flink->user_id, $notice->id);
+            }
         }
 
         // Okay, record the time we synced with Twitter for posterity
@@ -231,29 +233,63 @@ class TwitterStatusFetcher extends ParallelizingDaemon
         $flink->update();
     }
 
-    function saveStatus($status, $flink)
+    function saveStatus($status)
     {
         $profile = $this->ensureProfile($status->user);
 
         if (empty($profile)) {
             common_log(LOG_ERR, $this->name() .
                 ' - Problem saving notice. No associated Profile.');
-            return;
+            return null;
         }
 
         $statusUri = $this->makeStatusURI($status->user->screen_name, $status->id);
 
         // check to see if we've already imported the status
 
-        $dupe = $this->checkDupe($profile, $statusUri);
+        $n2s = Notice_to_status::staticGet('status_id', $status->id);
 
-        if (!empty($dupe)) {
+        if (!empty($n2s)) {
             common_log(
                 LOG_INFO,
                 $this->name() .
-                " - Ignoring duplicate import: $statusUri"
+                " - Ignoring duplicate import: {$status->id}"
             );
-            return;
+            return Notice::staticGet('id', $n2s->notice_id);
+        }
+
+        common_debug("Saving status {$status->id} with data " . print_r($status, true));
+
+        // 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}.");
+            $original = $this->saveStatus($status->retweeted_status);
+            if (empty($original)) {
+                return null;
+            } else {
+                $author = $original->getProfile();
+                // TRANS: Message used to repeat a notice. RT is the abbreviation of 'retweet'.
+                // TRANS: %1$s is the repeated user's name, %2$s is the repeated notice.
+                $content = sprintf(_('RT @%1$s %2$s'),
+                                   $author->nickname,
+                                   $original->content);
+
+                if (Notice::contentTooLong($content)) {
+                    $contentlimit = Notice::maxContent();
+                    $content = mb_substr($content, 0, $contentlimit - 4) . ' ...';
+                }
+
+                $repeat = Notice::saveNew($profile->id,
+                                          $content,
+                                          'twitter',
+                                          array('repeat_of' => $original->id,
+                                                '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);
+                return $repeat;
+            }
         }
 
         $notice = new Notice();
@@ -271,17 +307,26 @@ class TwitterStatusFetcher extends ParallelizingDaemon
         $notice->reply_to   = null;
 
         if (!empty($status->in_reply_to_status_id)) {
-            $replyUri = $this->makeStatusURI($status->in_reply_to_screen_name, $status->in_reply_to_status_id);
-            $reply = Notice::staticGet('uri', $replyUri);
-            if (!empty($reply)) {
-                $notice->reply_to     = $reply->id;
-                $notice->conversation = $reply->conversation;
+            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);
+            if (empty($n2s)) {
+                common_log(LOG_INFO, "Couldn't find local notice for status {$status->in_reply_to_status_id}");
+            } 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}");
+                } else {
+                    common_log(LOG_INFO, "Found local notice {$reply->id} for status {$status->in_reply_to_status_id}");
+                    $notice->reply_to     = $reply->id;
+                    $notice->conversation = $reply->conversation;
+                }
             }
         }
 
         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}.");
         }
 
         $notice->is_local   = Notice::GATEWAY;
@@ -305,7 +350,7 @@ class TwitterStatusFetcher extends ParallelizingDaemon
             Event::handle('EndNoticeSave', array($notice));
         }
 
-        Inbox::insertNotice($flink->user_id, $notice->id);
+        Notice_to_status::saveNew($notice->id, $status->id);
         $notice->blowOnInsert();
 
         return $notice;