]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/TwitterBridge/daemons/synctwitterfriends.php
TwitterBridge: don't array_merge() non-arrays
[quix0rs-gnu-social.git] / plugins / TwitterBridge / daemons / synctwitterfriends.php
index 76410c7cbf48bd86cc2fdac52f5c773b0e4273c4..a3862eedfde45e9fcd67da1aa4101cb71b8fd87a 100755 (executable)
@@ -31,10 +31,7 @@ Batch script for synching local friends with Twitter friends.
 END_OF_TRIM_HELP;
 
 require_once INSTALLDIR . '/scripts/commandline.inc';
-require_once INSTALLDIR . '/lib/parallelizingdaemon.php';
-require_once INSTALLDIR . '/plugins/TwitterBridge/twitter.php';
-require_once INSTALLDIR . '/plugins/TwitterBridge/twitterbasicauthclient.php';
-require_once INSTALLDIR . '/plugins/TwitterBridge/twitteroauthclient.php';
+require_once dirname(__DIR__) . '/twitter.php';
 
 /**
  * Daemon to sync local friends with Twitter friends
@@ -46,7 +43,6 @@ require_once INSTALLDIR . '/plugins/TwitterBridge/twitteroauthclient.php';
  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  * @link     http://status.net/
  */
-
 class SyncTwitterFriendsDaemon extends ParallelizingDaemon
 {
     /**
@@ -60,7 +56,6 @@ class SyncTwitterFriendsDaemon extends ParallelizingDaemon
      * @return void
      *
      **/
-
     function __construct($id = null, $interval = 60,
                          $max_children = 2, $debug = null)
     {
@@ -72,7 +67,6 @@ class SyncTwitterFriendsDaemon extends ParallelizingDaemon
      *
      * @return string Name of the daemon.
      */
-
     function name()
     {
         return ('synctwitterfriends.' . $this->_id);
@@ -111,12 +105,10 @@ class SyncTwitterFriendsDaemon extends ParallelizingDaemon
     }
 
     function childTask($flink) {
-
         // Each child ps needs its own DB connection
 
         // Note: DataObject::getDatabaseConnection() creates
-        // a new connection if there is not one already
-
+        // a new connection if there isn't one already
         $conn = &$flink->getDatabaseConnection();
 
         $this->subscribeTwitterFriends($flink);
@@ -126,9 +118,8 @@ class SyncTwitterFriendsDaemon extends ParallelizingDaemon
 
         $conn->disconnect();
 
-        // XXX: Could not find a less brutal way to blow
+        // XXX: Couldn't find a less brutal way to blow
         // away a cached connection
-
         global $_DB_DATAOBJECT;
         unset($_DB_DATAOBJECT['CONNECTIONS']);
     }
@@ -144,8 +135,8 @@ class SyncTwitterFriendsDaemon extends ParallelizingDaemon
             $client = new TwitterOAuthClient($token->key, $token->secret);
             common_debug($this->name() . '- Grabbing friends IDs with OAuth.');
         } else {
-            $client = new TwitterBasicAuthClient($flink);
-            common_debug($this->name() . '- Grabbing friends IDs with basic auth.');
+            common_debug("Skipping Twitter friends for {$flink->user_id} since not OAuth.");
+            return $friends;
         }
 
         try {
@@ -188,11 +179,13 @@ class SyncTwitterFriendsDaemon extends ParallelizingDaemon
 
             if (empty($more_friends)) {
                 common_log(LOG_WARNING, $this->name() .
-                           " - Could not retrieve page $i " .
+                           " - Couldn't retrieve page $i " .
                            "of Twitter user $flink->foreign_id friends.");
                 continue;
             } else {
-                $friends = array_merge($friends, $more_friends);
+                if (is_array($more_friends)) {
+                    $friends = array_merge($friends, $more_friends);
+                }
             }
         }
 
@@ -210,7 +203,7 @@ class SyncTwitterFriendsDaemon extends ParallelizingDaemon
             return false;
         }
 
-        $user = $flink->getUser();
+        $profile = $flink->getProfile();
 
         foreach ($friends as $friend) {
 
@@ -221,12 +214,12 @@ class SyncTwitterFriendsDaemon extends ParallelizingDaemon
             // Twitter friend
 
             if (!save_twitter_user($friend_id, $friend_name)) {
-                common_log(LOG_WARNING, $this-name() .
-                           " - Could not save $screen_name's friend, $friend_name.");
+                common_log(LOG_WARNING, $this->name() .
+                           " - Couldn't save $screen_name's friend, $friend_name.");
                 continue;
             }
 
-            // Check to see if there is a related local user
+            // Check to see if there's a related local user
 
             $friend_flink = Foreign_link::getByForeignID($friend_id,
                                                          TWITTER_SERVICE);
@@ -235,20 +228,20 @@ class SyncTwitterFriendsDaemon extends ParallelizingDaemon
 
                 // Get associated user and subscribe her
 
-                $friend_user = User::staticGet('id', $friend_flink->user_id);
+                $friend_profile = Profile::getKV('id', $friend_flink->user_id);
 
-                if (!empty($friend_user)) {
-                    $result = subs_subscribe_to($user, $friend_user);
-
-                    if ($result === true) {
+                if ($friend_profile instanceof Profile) {
+                    try {
+                        $other = Profile::getKV('id', $invites->user_id);
+                        Subscription::start($profile, $friend_profile);
                         common_log(LOG_INFO,
                                    $this->name() . ' - Subscribed ' .
-                                   "$friend_user->nickname to $user->nickname.");
-                    } else {
+                                   "{$friend_profile->nickname} to {$profile->nickname}.");
+                    } catch (Exception $e) {
                         common_debug($this->name() .
-                                     ' - Tried subscribing ' .
-                                     "$friend_user->nickname to $user->nickname - " .
-                                     $result);
+                                     ' - Tried and failed subscribing ' .
+                                     "{$friend_profile->nickname} to {$profile->nickname} - " .
+                                     $e->getMessage());
                     }
                 }
             }
@@ -278,4 +271,3 @@ if (have_option('d') || have_option('debug')) {
 
 $syncer = new SyncTwitterFriendsDaemon($id, 60, 2, $debug);
 $syncer->runOnce();
-