TwitterBridge is closer to working again

This commit is contained in:
Mikael Nordfeldth 2015-07-17 21:03:37 +02:00
parent 6cd7a4a400
commit e10d081a56
5 changed files with 61 additions and 67 deletions

View 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);
$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)

View 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;

View 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;

View 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)

View 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!
}
}
/**