From e10d081a56bf600b1babc1fa1c774dc8cbe874c1 Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Fri, 17 Jul 2015 21:03:37 +0200 Subject: [PATCH] TwitterBridge is closer to working again --- classes/Foreign_link.php | 31 +++++---- classes/Foreign_user.php | 7 +- plugins/TwitterBridge/TwitterBridgePlugin.php | 15 ++-- .../actions/twitterauthorization.php | 69 ++++++++----------- .../TwitterBridge/actions/twittersettings.php | 6 +- 5 files changed, 61 insertions(+), 67 deletions(-) diff --git a/classes/Foreign_link.php b/classes/Foreign_link.php index 0d942443f3..6e050d0c94 100644 --- a/classes/Foreign_link.php +++ b/classes/Foreign_link.php @@ -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); - - $result = $flink->find(true); - - return empty($result) ? null : $flink; + throw new ServerException('Empty foreign_id or service for Foreign_link::getByForeignID'); } + + $flink = new Foreign_link(); + $flink->service = $service; + $flink->foreign_id = $foreign_id; + $flink->limit(1); + + if (!$flink->find(true)) { + throw new NoResultException($flink); + } + + return $flink; } function set_flags($noticesend, $noticerecv, $replysync, $friendsync) diff --git a/classes/Foreign_user.php b/classes/Foreign_user.php index 3d23eabef9..1f6c77851d 100644 --- a/classes/Foreign_user.php +++ b/classes/Foreign_user.php @@ -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; diff --git a/plugins/TwitterBridge/TwitterBridgePlugin.php b/plugins/TwitterBridge/TwitterBridgePlugin.php index 566038f2ec..84c6285029 100644 --- a/plugins/TwitterBridge/TwitterBridgePlugin.php +++ b/plugins/TwitterBridge/TwitterBridgePlugin.php @@ -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; diff --git a/plugins/TwitterBridge/actions/twitterauthorization.php b/plugins/TwitterBridge/actions/twitterauthorization.php index 90e4c35410..43a46835c3 100644 --- a/plugins/TwitterBridge/actions/twitterauthorization.php +++ b/plugins/TwitterBridge/actions/twitterauthorization.php @@ -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) diff --git a/plugins/TwitterBridge/actions/twittersettings.php b/plugins/TwitterBridge/actions/twittersettings.php index 85d1a4bbe2..efde36797c 100644 --- a/plugins/TwitterBridge/actions/twittersettings.php +++ b/plugins/TwitterBridge/actions/twittersettings.php @@ -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! } } /**