]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
TwitterBridge is closer to working again
authorMikael Nordfeldth <mmn@hethane.se>
Fri, 17 Jul 2015 19:03:37 +0000 (21:03 +0200)
committerMikael Nordfeldth <mmn@hethane.se>
Fri, 17 Jul 2015 19:03:37 +0000 (21:03 +0200)
classes/Foreign_link.php
classes/Foreign_user.php
plugins/TwitterBridge/TwitterBridgePlugin.php
plugins/TwitterBridge/actions/twitterauthorization.php
plugins/TwitterBridge/actions/twittersettings.php

index 0d942443f358aa877359147e44129d8d5f8896af..6e050d0c94d6d0d2d4a554ae3cebf563cc194095 100644 (file)
@@ -56,34 +56,37 @@ class Foreign_link extends Managed_DataObject
     static function getByUserID($user_id, $service)
     {
         if (empty($user_id) || empty($service)) {
-            return null;
+            throw new ServerException('Empty user_id or service for Foreign_link::getByUserID');
         }
 
         $flink = new Foreign_link();
-
         $flink->service = $service;
         $flink->user_id = $user_id;
         $flink->limit(1);
 
-        $result = $flink->find(true);
+        if (!$flink->find(true)) {
+            throw new NoResultException($flink);
+        }
 
-        return empty($result) ? null : $flink;
+        return $flink;
     }
 
     static function getByForeignID($foreign_id, $service)
     {
         if (empty($foreign_id) || empty($service)) {
-            return null;
-        } else {
-            $flink = new Foreign_link();
-            $flink->service = $service;
-            $flink->foreign_id = $foreign_id;
-            $flink->limit(1);
+            throw new ServerException('Empty foreign_id or service for Foreign_link::getByForeignID');
+        }
 
-            $result = $flink->find(true);
+        $flink = new Foreign_link();
+        $flink->service = $service;
+        $flink->foreign_id = $foreign_id;
+        $flink->limit(1);
 
-            return empty($result) ? null : $flink;
+        if (!$flink->find(true)) {
+            throw new NoResultException($flink);
         }
+
+        return $flink;
     }
 
     function set_flags($noticesend, $noticerecv, $replysync, $friendsync)
index 3d23eabef96b34c6d208f6e9b71e515170c05431..1f6c77851d8768ef2275ec6fb52dea004dc3b414 100644 (file)
@@ -41,7 +41,12 @@ class Foreign_user extends Managed_DataObject
         );
     }
 
-    static function getForeignUser($id, $service) {
+    static function getForeignUser($id, $service)
+    {
+        if (empty($id) || empty($service)) {
+            throw new ServerException('Empty foreign user id or service for Foreign_user::getForeignUser');
+        }
+
         $fuser = new Foreign_user();
         $fuser->id      = $id;
         $fuser->service = $service;
index 566038f2ec9f16204cca32a9ab635cea45773fcf..84c6285029e0112dc1d90ab6f7a45525a4103739 100644 (file)
@@ -512,16 +512,15 @@ class TwitterBridgePlugin extends Plugin
     {
         $fuser = null;
 
-        $flink = Foreign_link::getByUserID($profile->id, TWITTER_SERVICE);
-
-        if (!empty($flink)) {
+        try {
+            $flink = Foreign_link::getByUserID($profile->id, TWITTER_SERVICE);
             $fuser = $flink->getForeignUser();
 
-            if (!empty($fuser)) {
-                $links[] = array("href" => $fuser->uri,
-                                 "text" => sprintf(_("@%s on Twitter"), $fuser->nickname),
-                                 "image" => $this->path("icons/twitter-bird-white-on-blue.png"));
-            }
+            $links[] = array("href" => $fuser->uri,
+                             "text" => sprintf(_("@%s on Twitter"), $fuser->nickname),
+                             "image" => $this->path("icons/twitter-bird-white-on-blue.png"));
+        } catch (NoResultException $e) {
+            // no foreign link for Twitter on this profile ID
         }
 
         return true;
index 90e4c35410e546e1642412915d36d517c9bd3ffa..43a46835c3a416c700a9bbb6a7a27942769becda 100644 (file)
@@ -31,6 +31,7 @@
 if (!defined('GNUSOCIAL')) { exit(1); }
 
 require_once dirname(__DIR__) . '/twitter.php';
+require_once INSTALLDIR . '/lib/oauthclient.php';
 
 /**
  * Class for doing OAuth authentication against Twitter
@@ -81,16 +82,6 @@ class TwitterauthorizationAction extends FormAction
                 }
             }
         }
-
-        // $this->oauth_token is only populated once Twitter authorizes our
-        // request token. If it's empty we're at the beginning of the auth
-        // process
-        if (empty($this->oauth_token)) {
-            // authorizeRequestToken either throws an exception or redirects
-            $this->authorizeRequestToken();
-        } else {
-            $this->saveAccessToken();
-        }
     }
 
     protected function doPost()
@@ -127,12 +118,10 @@ class TwitterauthorizationAction extends FormAction
     {
         try {
             // Get a new request token and authorize it
-
             $client  = new TwitterOAuthClient();
             $req_tok = $client->getTwitterRequestToken();
 
             // Sock the request token away in the session temporarily
-
             $_SESSION['twitter_request_token']        = $req_tok->key;
             $_SESSION['twitter_request_token_secret'] = $req_tok->secret;
 
@@ -170,16 +159,12 @@ class TwitterauthorizationAction extends FormAction
         $twitter_user = null;
 
         try {
-
-            $client = new TwitterOAuthClient($_SESSION['twitter_request_token'],
-                $_SESSION['twitter_request_token_secret']);
+            $client = new TwitterOAuthClient($_SESSION['twitter_request_token'], $_SESSION['twitter_request_token_secret']);
 
             // Exchange the request token for an access token
-
             $atok = $client->getTwitterAccessToken($this->verifier);
 
             // Test the access token and get the user's Twitter info
-
             $client       = new TwitterOAuthClient($atok->key, $atok->secret);
             $twitter_user = $client->verifyCredentials();
 
@@ -190,13 +175,11 @@ class TwitterauthorizationAction extends FormAction
                 $e->getMessage()
             );
             common_log(LOG_INFO, 'Twitter bridge - ' . $msg);
-            $this->serverError(
-                // TRANS: Server error displayed when linking to a Twitter account fails.
-                _m('Could not link your Twitter account.')
-            );
+            // TRANS: Server error displayed when linking to a Twitter account fails.
+            throw new ServerException(_m('Could not link your Twitter account.'));
         }
 
-        if (common_logged_in()) {
+        if ($this->scoped instanceof Profile) {
             // Save the access token and Twitter user info
 
             $this->saveForeignLink($this->scoped->getID(), $twitter_user->id, $atok);
@@ -208,7 +191,7 @@ class TwitterauthorizationAction extends FormAction
             $this->tw_fields = array("screen_name" => $twitter_user->screen_name,
                                      "fullname" => $twitter_user->name);
             $this->access_token = $atok;
-            $this->tryLogin();
+            return $this->tryLogin();
         }
 
         // Clean up the the mess we made in the session
@@ -282,6 +265,21 @@ class TwitterauthorizationAction extends FormAction
         return _m('Twitter Account Setup');
     }
 
+    public function showPage()
+    {
+        // $this->oauth_token is only populated once Twitter authorizes our
+        // request token. If it's empty we're at the beginning of the auth
+        // process
+        if (empty($this->oauth_token)) {
+            // authorizeRequestToken either throws an exception or redirects
+            $this->authorizeRequestToken();
+        } else {
+            $this->saveAccessToken();
+        }
+
+        parent::showPage();
+    }
+
     /**
      * @fixme much of this duplicates core code, which is very fragile.
      * Should probably be replaced with an extensible mini version of
@@ -289,11 +287,6 @@ class TwitterauthorizationAction extends FormAction
      */
     function showContent()
     {
-        if (!empty($this->message_text)) {
-            $this->element('p', null, $this->message);
-            return;
-        }
-
         $this->elementStart('form', array('method' => 'post',
                                           'id' => 'form_settings_twitter_connect',
                                           'class' => 'form_settings',
@@ -310,7 +303,7 @@ class TwitterauthorizationAction extends FormAction
         $this->hidden('token', common_session_token());
 
         // Don't allow new account creation if site is flagged as invite only
-       if (common_config('site', 'inviteonly') == false) {
+        if (common_config('site', 'inviteonly') == false) {
             $this->elementStart('fieldset');
             $this->element('legend', null,
                            // TRANS: Fieldset legend.
@@ -425,12 +418,6 @@ class TwitterauthorizationAction extends FormAction
         return '';
     }
 
-    function message($msg)
-    {
-        $this->message_text = $msg;
-        $this->showPage();
-    }
-
     function createNewUser()
     {
         if (!Event::handle('StartRegistrationTry', array($this))) {
@@ -567,14 +554,12 @@ class TwitterauthorizationAction extends FormAction
                 common_real_login(true);
                 $this->goHome($user->nickname);
             }
-
-        } else {
-
-            common_debug('TwitterBridge Plugin - ' .
-                         "No flink found for twuid: $this->twuid - new user");
-
-            $this->showForm(null, $this->bestNewNickname());
         }
+        common_debug('TwitterBridge Plugin - ' .
+                     "No flink found for twuid: $this->twuid - new user");
+
+        return;
+        throw new ServerException(_m('No foreign link found for Twitter user'));
     }
 
     function goHome($nickname)
index 85d1a4bbe266cae7f9f75744948f0ab22178a201..efde36797c0f36d5e5377599b8e9bd58a55910fb 100644 (file)
@@ -49,9 +49,11 @@ class TwittersettingsAction extends ProfileSettingsAction
 
     protected function doPreparation()
     {
-        $this->flink = Foreign_link::getByUserID($this->scoped->getID(), TWITTER_SERVICE);
-        if ($this->flink instanceof Foreign_link) {
+        try {
+            $this->flink = Foreign_link::getByUserID($this->scoped->getID(), TWITTER_SERVICE);
             $this->fuser = $this->flink->getForeignUser();
+        } catch (NoResultException $e) {
+            // No foreign link found for this user!
         }
     }
     /**