]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/TwitterBridge/daemons/twitterstatusfetcher.php
Merge branch '0.9.x' into 1.0.x
[quix0rs-gnu-social.git] / plugins / TwitterBridge / daemons / twitterstatusfetcher.php
index 81bbbc7c5f765f3022b0492115ef29653575a39b..bff657eb68c023cab98d805b5b30eb3fe87b4ba7 100755 (executable)
@@ -2,7 +2,7 @@
 <?php
 /**
  * StatusNet - the distributed open-source microblogging tool
- * Copyright (C) 2008, 2009, StatusNet, Inc.
+ * Copyright (C) 2008-2010, StatusNet, Inc.
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU Affero General Public License as published by
@@ -115,6 +115,9 @@ class TwitterStatusFetcher extends ParallelizingDaemon
             if (($flink->noticesync & FOREIGN_NOTICE_RECV) ==
                 FOREIGN_NOTICE_RECV) {
                 $flinks[] = clone($flink);
+                common_log(LOG_INFO, "sync: foreign id $flink->foreign_id");
+            } else {
+                common_log(LOG_INFO, "nothing to sync");
             }
         }
 
@@ -206,7 +209,13 @@ class TwitterStatusFetcher extends ParallelizingDaemon
                 continue;
             }
 
-            $this->saveStatus($status, $flink);
+            $notice = null;
+
+            $notice = $this->saveStatus($status, $flink);
+
+            if (!empty($notice)) {
+                common_broadcast_notice($notice);
+            }
         }
 
         // Okay, record the time we synced with Twitter for posterity
@@ -232,12 +241,14 @@ class TwitterStatusFetcher extends ParallelizingDaemon
         $uri = 'http://twitter.com/' . $status->user->screen_name .
             '/status/' . $status->id;
 
-        $notice = Notice::staticGet('uri', $uri);
-
         // check to see if we've already imported the status
 
+        $notice = Notice::staticGet('uri', $uri);
+
         if (empty($notice)) {
 
+            // XXX: transaction here?
+
             $notice = new Notice();
 
             $notice->profile_id = $id;
@@ -251,23 +262,41 @@ class TwitterStatusFetcher extends ParallelizingDaemon
             $notice->is_local   = Notice::GATEWAY;
 
             if (Event::handle('StartNoticeSave', array(&$notice))) {
-                $id = $notice->insert();
+                $notice->insert();
                 Event::handle('EndNoticeSave', array($notice));
             }
+
         }
 
-        if (!Notice_inbox::pkeyGet(array('notice_id' => $notice->id,
-                                         'user_id' => $flink->user_id))) {
-            // Add to inbox
-            $inbox = new Notice_inbox();
+        Inbox::insertNotice($flink->user_id, $notice->id);
 
-            $inbox->user_id   = $flink->user_id;
-            $inbox->notice_id = $notice->id;
-            $inbox->created   = $notice->created;
-            $inbox->source    = NOTICE_INBOX_SOURCE_GATEWAY; // From a private source
+        $notice->blowOnInsert();
 
-            $inbox->insert();
+        return $notice;
+    }
+
+    /**
+     * Look up a Profile by profileurl field.  Profile::staticGet() was
+     * not working consistently.
+     *
+     * @param string $url the profile url
+     *
+     * @return mixed the first profile with that url, or null
+     */
+
+    function getProfileByUrl($nickname, $profileurl)
+    {
+        $profile = new Profile();
+        $profile->nickname = $nickname;
+        $profile->profileurl = $profileurl;
+        $profile->limit(1);
+
+        if ($profile->find()) {
+            $profile->fetch();
+            return $profile;
         }
+
+        return null;
     }
 
     function ensureProfile($user)
@@ -275,7 +304,7 @@ class TwitterStatusFetcher extends ParallelizingDaemon
         // check to see if there's already a profile for this user
 
         $profileurl = 'http://twitter.com/' . $user->screen_name;
-        $profile = Profile::staticGet('profileurl', $profileurl);
+        $profile = $this->getProfileByUrl($user->screen_name, $profileurl);
 
         if (!empty($profile)) {
             common_debug($this->name() .
@@ -287,6 +316,7 @@ class TwitterStatusFetcher extends ParallelizingDaemon
             return $profile->id;
 
         } else {
+
             common_debug($this->name() . ' - Adding profile and remote profile ' .
                          "for Twitter user: $profileurl.");
 
@@ -301,7 +331,11 @@ class TwitterStatusFetcher extends ParallelizingDaemon
             $profile->profileurl = $profileurl;
             $profile->created = common_sql_now();
 
-            $id = $profile->insert();
+            try {
+                $id = $profile->insert();
+            } catch(Exception $e) {
+                common_log(LOG_WARNING, $this->name . ' Couldn\'t insert profile - ' . $e->getMessage());
+            }
 
             if (empty($id)) {
                 common_log_db_error($profile, 'INSERT', __FILE__);
@@ -321,7 +355,11 @@ class TwitterStatusFetcher extends ParallelizingDaemon
                 $remote_pro->uri = $profileurl;
                 $remote_pro->created = common_sql_now();
 
-                $rid = $remote_pro->insert();
+                try {
+                    $rid = $remote_pro->insert();
+                } catch (Exception $e) {
+                    common_log(LOG_WARNING, $this->name() . ' Couldn\'t save remote profile - ' . $e->getMessage());
+                }
 
                 if (empty($rid)) {
                     common_log_db_error($profile, 'INSERT', __FILE__);
@@ -441,7 +479,7 @@ class TwitterStatusFetcher extends ParallelizingDaemon
             if ($this->fetchAvatar($url, $filename)) {
                 $this->newAvatar($id, $size, $mediatype, $filename);
             } else {
-                common_log(LOG_WARNING, $this->id() .
+                common_log(LOG_WARNING, $id() .
                            " - Problem fetching Avatar: $url");
             }
         }
@@ -502,7 +540,11 @@ class TwitterStatusFetcher extends ParallelizingDaemon
 
         $avatar->created = common_sql_now();
 
-        $id = $avatar->insert();
+        try {
+            $id = $avatar->insert();
+        } catch (Exception $e) {
+            common_log(LOG_WARNING, $this->name() . ' Couldn\'t insert avatar - ' . $e->getMessage());
+        }
 
         if (empty($id)) {
             common_log_db_error($avatar, 'INSERT', __FILE__);
@@ -515,31 +557,32 @@ class TwitterStatusFetcher extends ParallelizingDaemon
         return $id;
     }
 
+    /**
+     * Fetch a remote avatar image and save to local storage.
+     *
+     * @param string $url avatar source URL
+     * @param string $filename bare local filename for download
+     * @return bool true on success, false on failure
+     */
     function fetchAvatar($url, $filename)
     {
-        $avatarfile = Avatar::path($filename);
+        common_debug($this->name() . " - Fetching Twitter avatar: $url");
 
-        $out = fopen($avatarfile, 'wb');
-        if (!$out) {
-            common_log(LOG_WARNING, $this->name() .
-                       " - Couldn't open file $filename");
+        $request = HTTPClient::start();
+        $response = $request->get($url);
+        if ($response->isOk()) {
+            $avatarfile = Avatar::path($filename);
+            $ok = file_put_contents($avatarfile, $response->getBody());
+            if (!$ok) {
+                common_log(LOG_WARNING, $this->name() .
+                           " - Couldn't open file $filename");
+                return false;
+            }
+        } else {
             return false;
         }
 
-        common_debug($this->name() . " - Fetching Twitter avatar: $url");
-
-        $ch = curl_init();
-        curl_setopt($ch, CURLOPT_URL, $url);
-        curl_setopt($ch, CURLOPT_FILE, $out);
-        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
-        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
-        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
-        $result = curl_exec($ch);
-        curl_close($ch);
-
-        fclose($out);
-
-        return $result;
+        return true;
     }
 }