Twittersettings now works better as Profilesettings extension

This commit is contained in:
Mikael Nordfeldth 2015-07-17 17:50:43 +02:00
parent d6d06c8cbb
commit 5933056a5b

View File

@ -27,9 +27,7 @@
* @link http://status.net/
*/
if (!defined('STATUSNET') && !defined('LACONICA')) {
exit(1);
}
if (!defined('GNUSOCIAL')) { exit(1); }
require_once dirname(__DIR__) . '/twitter.php';
@ -46,6 +44,16 @@ require_once dirname(__DIR__) . '/twitter.php';
*/
class TwittersettingsAction extends ProfileSettingsAction
{
protected $flink = null;
protected $fuser = null;
protected function doPreparation()
{
$this->flink = Foreign_link::getByUserID($this->scoped->getID(), TWITTER_SERVICE);
if ($this->flink instanceof Foreign_link) {
$this->fuser = $this->flink->getForeignUser();
}
}
/**
* Title of the page
*
@ -81,19 +89,6 @@ class TwittersettingsAction extends ProfileSettingsAction
*/
function showContent()
{
$user = common_current_user();
$profile = $user->getProfile();
$fuser = null;
$flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
if (!empty($flink)) {
$fuser = $flink->getForeignUser();
}
$this->elementStart('form', array('method' => 'post',
'id' => 'form_settings_twitter',
'class' => 'form_settings',
@ -104,21 +99,11 @@ class TwittersettingsAction extends ProfileSettingsAction
$this->elementStart('fieldset', array('id' => 'settings_twitter_account'));
if (empty($fuser)) {
$this->elementStart('ul', 'form_data');
$this->elementStart('li', array('id' => 'settings_twitter_login_button'));
$this->element('a', array('href' => common_local_url('twitterauthorization')),
// TRANS: Link description to connect to a Twitter account.
'Connect my Twitter account');
$this->elementEnd('li');
$this->elementEnd('ul');
$this->elementEnd('fieldset');
} else {
if ($this->fuser instanceof Foreign_user) {
// TRANS: Fieldset legend.
$this->element('legend', null, _m('Twitter account'));
$this->elementStart('p', array('id' => 'form_confirmed'));
$this->element('a', array('href' => $fuser->uri), $fuser->nickname);
$this->element('a', array('href' => $this->fuser->uri), $this->fuser->nickname);
$this->elementEnd('p');
$this->element('p', 'form_note',
// TRANS: Form note when a Twitter account has been connected.
@ -130,7 +115,7 @@ class TwittersettingsAction extends ProfileSettingsAction
// TRANS: Fieldset legend.
$this->element('legend', null, _m('Disconnect my account from Twitter'));
if (!$user->password) {
if (!$this->scoped->hasPassword()) {
$this->elementStart('p', array('class' => 'form_guide'));
// TRANS: Form guide. %s is a URL to the password settings.
// TRANS: This message contains a Markdown link in the form [description](link).
@ -165,25 +150,19 @@ class TwittersettingsAction extends ProfileSettingsAction
$this->checkbox('noticesend',
// TRANS: Checkbox label.
_m('Automatically send my notices to Twitter.'),
($flink) ?
($flink->noticesync & FOREIGN_NOTICE_SEND) :
true);
$this->flink->noticesync & FOREIGN_NOTICE_SEND);
$this->elementEnd('li');
$this->elementStart('li');
$this->checkbox('replysync',
// TRANS: Checkbox label.
_m('Send local "@" replies to Twitter.'),
($flink) ?
($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) :
true);
$this->flink->noticesync & FOREIGN_NOTICE_SEND_REPLY);
$this->elementEnd('li');
$this->elementStart('li');
$this->checkbox('friendsync',
// TRANS: Checkbox label.
_m('Subscribe to my Twitter friends here.'),
($flink) ?
($flink->friendsync & FOREIGN_FRIEND_RECV) :
false);
$this->flink->friendsync & FOREIGN_FRIEND_RECV);
$this->elementEnd('li');
if (common_config('twitterimport','enabled')) {
@ -191,31 +170,37 @@ class TwittersettingsAction extends ProfileSettingsAction
$this->checkbox('noticerecv',
// TRANS: Checkbox label.
_m('Import my friends timeline.'),
($flink) ?
($flink->noticesync & FOREIGN_NOTICE_RECV) :
false);
$this->flink->noticesync & FOREIGN_NOTICE_RECV);
$this->elementEnd('li');
} else {
// preserve setting even if bidrection bridge toggled off
if ($flink && ($flink->noticesync & FOREIGN_NOTICE_RECV)) {
if ($this->flink->noticesync & FOREIGN_NOTICE_RECV) {
$this->hidden('noticerecv', true, 'noticerecv');
}
}
$this->elementEnd('ul');
if ($flink) {
if ($this->flink) {
// TRANS: Button text for saving Twitter integration settings.
$this->submit('save', _m('BUTTON','Save'));
} else {
// TRANS: Button text for adding Twitter integration.
$this->submit('add', _m('BUTTON','Add'));
}
$this->elementEnd('fieldset');
} else {
$this->elementStart('ul', 'form_data');
$this->elementStart('li', array('id' => 'settings_twitter_login_button'));
$this->element('a', array('href' => common_local_url('twitterauthorization')),
// TRANS: Link description to connect to a Twitter account.
'Connect my Twitter account');
$this->elementEnd('li');
$this->elementEnd('ul');
}
$this->elementEnd('fieldset');
$this->elementEnd('form');
}
@ -289,32 +274,25 @@ class TwittersettingsAction extends ProfileSettingsAction
$friendsync = $this->boolean('friendsync');
$replysync = $this->boolean('replysync');
$user = common_current_user();
$flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
if (empty($flink)) {
common_log_db_error($flink, 'SELECT', __FILE__);
// @todo FIXME: Shouldn't this be a serverError()?
if (!$this->flink instanceof Foreign_link) {
common_log_db_error($this->flink, 'SELECT', __FILE__);
// TRANS: Server error displayed when saving Twitter integration preferences fails.
$this->showForm(_m('Could not save Twitter preferences.'));
return;
throw new ServerException(_m('Your account is not linked to Twitter.'));
}
$original = clone($flink);
$original = clone($this->flink);
$wasReceiving = (bool)($original->noticesync & FOREIGN_NOTICE_RECV);
$flink->set_flags($noticesend, $noticerecv, $replysync, $friendsync);
$result = $flink->update($original);
$this->flink->set_flags($noticesend, $noticerecv, $replysync, $friendsync);
$result = $this->flink->update($original);
if ($result === false) {
common_log_db_error($flink, 'UPDATE', __FILE__);
// @todo FIXME: Shouldn't this be a serverError()?
common_log_db_error($this->flink, 'UPDATE', __FILE__);
// TRANS: Server error displayed when saving Twitter integration preferences fails.
$this->showForm(_m('Could not save Twitter preferences.'));
return;
throw new ServerException(_m('Could not save Twitter preferences.'));
}
if ($wasReceiving xor $noticerecv) {
$this->notifyDaemon($flink->foreign_id, $noticerecv);
$this->notifyDaemon($this->flink->foreign_id, $noticerecv);
}
// TRANS: Success message after saving Twitter integration preferences.