Update twittersettings to new framework

This commit is contained in:
Evan Prodromou 2009-01-16 21:18:30 +00:00
parent c0a5ade1e7
commit d5a876865b

View File

@ -1,9 +1,12 @@
<?php <?php
/* /**
* Laconica - a distributed open-source microblogging tool * Laconica, the distributed open-source microblogging tool
* Copyright (C) 2008, Controlez-Vous, Inc.
* *
* This program is free software: you can redistribute it and/or modify * Settings for Twitter integration
*
* PHP version 5
*
* LICENCE: This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by * it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
@ -15,39 +18,88 @@
* *
* You should have received a copy of the GNU Affero General Public License * You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @category Settings
* @package Laconica
* @author Evan Prodromou <evan@controlyourself.ca>
* @copyright 2008-2009 Control Yourself, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://laconi.ca/
*/ */
if (!defined('LACONICA')) { exit(1); } if (!defined('LACONICA')) {
exit(1);
}
require_once(INSTALLDIR.'/lib/settingsaction.php'); require_once INSTALLDIR.'/lib/settingsaction.php';
define('SUBSCRIPTIONS', 80); define('SUBSCRIPTIONS', 80);
/**
* Settings for Twitter integration
*
* @category Settings
* @package Laconica
* @author Evan Prodromou <evan@controlyourself.ca>
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://laconi.ca/
*
* @see SettingsAction
*/
class TwittersettingsAction extends SettingsAction class TwittersettingsAction extends SettingsAction
{ {
/**
* Title of the page
*
* @return string Title of the page
*/
function get_instructions() function title()
{ {
return _('Add your Twitter account to automatically send your notices to Twitter, ' . _('Twitter settings');
'and subscribe to Twitter friends already here.');
} }
function show_form($msg=null, $success=false) /**
* Instructions for use
*
* @return instructions for use
*/
function getInstructions()
{
return _('Add your Twitter account to automatically send '.
' your notices to Twitter, ' .
'and subscribe to Twitter friends already here.');
}
/**
* Content area of the page
*
* Shows a form for associating a Twitter account with this
* Laconica account. Also lets the user set preferences.
*
* @return void
*/
function showContent()
{ {
$user = common_current_user(); $user = common_current_user();
$profile = $user->getProfile(); $profile = $user->getProfile();
$fuser = null; $fuser = null;
$flink = Foreign_link::getByUserID($user->id, 1); // 1 == Twitter $flink = Foreign_link::getByUserID($user->id, 1); // 1 == Twitter
if ($flink) { if ($flink) {
$fuser = $flink->getForeignUser(); $fuser = $flink->getForeignUser();
} }
$this->form_header(_('Twitter settings'), $msg, $success);
$this->elementStart('form', array('method' => 'post', $this->elementStart('form', array('method' => 'post',
'id' => 'twittersettings', 'id' => 'twittersettings',
'action' => 'action' =>
common_local_url('twittersettings'))); common_local_url('twittersettings')));
$this->hidden('token', common_session_token()); $this->hidden('token', common_session_token());
$this->element('h2', null, _('Twitter Account')); $this->element('h2', null, _('Twitter Account'));
@ -56,7 +108,7 @@ class TwittersettingsAction extends SettingsAction
$this->elementStart('p'); $this->elementStart('p');
$this->element('span', 'twitter_user', $fuser->nickname); $this->element('span', 'twitter_user', $fuser->nickname);
$this->element('a', array('href' => $fuser->uri), $fuser->uri); $this->element('a', array('href' => $fuser->uri), $fuser->uri);
$this->element('span', 'input_instructions', $this->element('span', 'input_instructions',
_('Current verified Twitter account.')); _('Current verified Twitter account.'));
$this->hidden('flink_foreign_id', $flink->foreign_id); $this->hidden('flink_foreign_id', $flink->foreign_id);
@ -64,7 +116,9 @@ class TwittersettingsAction extends SettingsAction
$this->submit('remove', _('Remove')); $this->submit('remove', _('Remove'));
} else { } else {
$this->input('twitter_username', _('Twitter user name'), $this->input('twitter_username', _('Twitter user name'),
($this->arg('twitter_username')) ? $this->arg('twitter_username') : $profile->nickname, ($this->arg('twitter_username')) ?
$this->arg('twitter_username') :
$profile->nickname,
_('No spaces, please.')); // hey, it's what Twitter says _('No spaces, please.')); // hey, it's what Twitter says
$this->password('twitter_password', _('Twitter password')); $this->password('twitter_password', _('Twitter password'));
@ -72,14 +126,23 @@ class TwittersettingsAction extends SettingsAction
$this->element('h2', null, _('Preferences')); $this->element('h2', null, _('Preferences'));
$this->checkbox('noticesync', _('Automatically send my notices to Twitter.'), $this->checkbox('noticesync',
($flink) ? ($flink->noticesync & FOREIGN_NOTICE_SEND) : true); _('Automatically send my notices to Twitter.'),
($flink) ?
($flink->noticesync & FOREIGN_NOTICE_SEND) :
true);
$this->checkbox('replysync', _('Send local "@" replies to Twitter.'), $this->checkbox('replysync',
($flink) ? ($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) : true); _('Send local "@" replies to Twitter.'),
($flink) ?
($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) :
true);
$this->checkbox('friendsync', _('Subscribe to my Twitter friends here.'), $this->checkbox('friendsync',
($flink) ? ($flink->friendsync & FOREIGN_FRIEND_RECV) : false); _('Subscribe to my Twitter friends here.'),
($flink) ?
($flink->friendsync & FOREIGN_FRIEND_RECV) :
false);
if ($flink) { if ($flink) {
$this->submit('save', _('Save')); $this->submit('save', _('Save'));
@ -87,24 +150,31 @@ class TwittersettingsAction extends SettingsAction
$this->submit('add', _('Add')); $this->submit('add', _('Add'));
} }
$this->show_twitter_subscriptions(); $this->showTwitterSubscriptions();
$this->elementEnd('form'); $this->elementEnd('form');
common_show_footer();
} }
function subscribed_twitter_users() /**
* Gets some of the user's Twitter friends
*
* Gets the number of Twitter friends that are on this
* instance of Laconica.
*
* @return array array of User objects
*/
function subscribedTwitterUsers()
{ {
$current_user = common_current_user(); $current_user = common_current_user();
$qry = 'SELECT user.* ' . $qry = 'SELECT user.* ' .
'FROM subscription ' . 'FROM subscription ' .
'JOIN user ON subscription.subscribed = user.id ' . 'JOIN user ON subscription.subscribed = user.id ' .
'JOIN foreign_link ON foreign_link.user_id = user.id ' . 'JOIN foreign_link ON foreign_link.user_id = user.id ' .
'WHERE subscriber = %d ' . 'WHERE subscriber = %d ' .
'ORDER BY user.nickname'; 'ORDER BY user.nickname';
$user = new User(); $user = new User();
@ -123,10 +193,20 @@ class TwittersettingsAction extends SettingsAction
return $users; return $users;
} }
function show_twitter_subscriptions() /**
* Show user's Twitter friends
*
* Gets the number of Twitter friends that are on this
* instance of Laconica, and shows their mini-avatars.
*
* @return void
*/
function showTwitterSubscriptions()
{ {
$friends = $this->subscribed_twitter_users(); $friends = $this->subscribedTwitterUsers();
$friends_count = count($friends); $friends_count = count($friends);
if ($friends_count > 0) { if ($friends_count > 0) {
@ -146,13 +226,19 @@ class TwittersettingsAction extends SettingsAction
$this->elementStart('li'); $this->elementStart('li');
$this->elementStart('a', array('title' => ($other->fullname) ? $this->elementStart('a', array('title' => ($other->fullname) ?
$other->fullname : $other->fullname :
$other->nickname, $other->nickname,
'href' => $other->profileurl, 'href' => $other->profileurl,
'rel' => 'contact', 'rel' => 'contact',
'class' => 'subscription')); 'class' => 'subscription'));
$avatar = $other->getAvatar(AVATAR_MINI_SIZE); $avatar = $other->getAvatar(AVATAR_MINI_SIZE);
$this->element('img', array('src' => (($avatar) ? common_avatar_display_url($avatar) : common_default_avatar(AVATAR_MINI_SIZE)),
$avatar_url = ($avatar) ?
common_avatar_display_url($avatar) :
common_default_avatar(AVATAR_MINI_SIZE);
$this->element('img', array('src' => $avatar_url,
'width' => AVATAR_MINI_SIZE, 'width' => AVATAR_MINI_SIZE,
'height' => AVATAR_MINI_SIZE, 'height' => AVATAR_MINI_SIZE,
'class' => 'avatar mini', 'class' => 'avatar mini',
@ -168,88 +254,96 @@ class TwittersettingsAction extends SettingsAction
$this->elementEnd('div'); $this->elementEnd('div');
} }
// XXX Figure out a way to show all Twitter friends... ?
/*
if ($subs_count > SUBSCRIPTIONS) {
$this->elementStart('p', array('id' => 'subscriptions_viewall'));
$this->element('a', array('href' => common_local_url('subscriptions',
array('nickname' => $profile->nickname)),
'class' => 'moresubscriptions'),
_('All subscriptions'));
$this->elementEnd('p');
}
*/
} }
function handle_post() /**
* Handle posts to this form
*
* Based on the button that was pressed, muxes out to other functions
* to do the actual task requested.
*
* All sub-functions reload the form with a message -- success or failure.
*
* @return void
*/
function handlePost()
{ {
# CSRF protection // CSRF protection
$token = $this->trimmed('token'); $token = $this->trimmed('token');
if (!$token || $token != common_session_token()) { if (!$token || $token != common_session_token()) {
$this->show_form(_('There was a problem with your session token. Try again, please.')); $this->showForm(_('There was a problem with your session token. '.
'Try again, please.'));
return; return;
} }
if ($this->arg('save')) { if ($this->arg('save')) {
$this->save_preferences(); $this->savePreferences();
} else if ($this->arg('add')) { } else if ($this->arg('add')) {
$this->add_twitter_acct(); $this->addTwitterAccount();
} else if ($this->arg('remove')) { } else if ($this->arg('remove')) {
$this->remove_twitter_acct(); $this->removeTwitterAccount();
} else { } else {
$this->show_form(_('Unexpected form submission.')); $this->showForm(_('Unexpected form submission.'));
} }
} }
function add_twitter_acct() /**
{ * Associate a Twitter account with the user's account
*
* Validates post input; verifies it against Twitter; and if
* successful stores in the database.
*
* @return void
*/
function addTwitterAccount()
{
$screen_name = $this->trimmed('twitter_username'); $screen_name = $this->trimmed('twitter_username');
$password = $this->trimmed('twitter_password'); $password = $this->trimmed('twitter_password');
$noticesync = $this->boolean('noticesync'); $noticesync = $this->boolean('noticesync');
$replysync = $this->boolean('replysync'); $replysync = $this->boolean('replysync');
$friendsync = $this->boolean('friendsync'); $friendsync = $this->boolean('friendsync');
if (!Validate::string($screen_name, if (!Validate::string($screen_name,
array( 'min_length' => 1, array('min_length' => 1,
'max_length' => 15, 'max_length' => 15,
'format' => VALIDATE_NUM . VALIDATE_ALPHA . '_'))) { 'format' => VALIDATE_NUM.VALIDATE_ALPHA.'_'))) {
$this->show_form( $this->showForm(_('Username must have only numbers, '.
_('Username must have only numbers, upper- and lowercase letters, and underscore (_). 15 chars max.')); 'upper- and lowercase letters, '.
'and underscore (_). 15 chars max.'));
return; return;
} }
if (!$this->verify_credentials($screen_name, $password)) { if (!$this->verifyCredentials($screen_name, $password)) {
$this->show_form(_('Could not verify your Twitter credentials!')); $this->showForm(_('Could not verify your Twitter credentials!'));
return; return;
} }
$twit_user = twitter_user_info($screen_name, $password); $twit_user = twitter_user_info($screen_name, $password);
if (!$twit_user) { if (!$twit_user) {
$this->show_form(sprintf(_('Unable to retrieve account information for "%s" from Twitter.'), $this->showForm(sprintf(_('Unable to retrieve account information '.
$screen_name)); 'For "%s" from Twitter.'),
$screen_name));
return; return;
} }
if (!save_twitter_user($twit_user->id, $screen_name)) { if (!save_twitter_user($twit_user->id, $screen_name)) {
$this->show_form(_('Unable to save your Twitter settings!')); $this->showForm(_('Unable to save your Twitter settings!'));
return; return;
} }
$user = common_current_user(); $user = common_current_user();
$flink = DB_DataObject::factory('foreign_link'); $flink = new Foreign_link();
$flink->user_id = $user->id;
$flink->foreign_id = $twit_user->id; $flink->user_id = $user->id;
$flink->service = 1; // Twitter $flink->foreign_id = $twit_user->id;
$flink->service = 1; // Twitter
$flink->credentials = $password; $flink->credentials = $password;
$flink->created = common_sql_now(); $flink->created = common_sql_now();
$flink->set_flags($noticesync, $replysync, $friendsync); $flink->set_flags($noticesync, $replysync, $friendsync);
@ -257,7 +351,7 @@ class TwittersettingsAction extends SettingsAction
if (!$flink_id) { if (!$flink_id) {
common_log_db_error($flink, 'INSERT', __FILE__); common_log_db_error($flink, 'INSERT', __FILE__);
$this->show_form(_('Unable to save your Twitter settings!')); $this->showForm(_('Unable to save your Twitter settings!'));
return; return;
} }
@ -265,19 +359,26 @@ class TwittersettingsAction extends SettingsAction
save_twitter_friends($user, $twit_user->id, $screen_name, $password); save_twitter_friends($user, $twit_user->id, $screen_name, $password);
} }
$this->show_form(_('Twitter settings saved.'), true); $this->showForm(_('Twitter settings saved.'), true);
} }
function remove_twitter_acct() /**
{ * Disassociate an existing Twitter account from this account
*
* @return void
*/
function removeTwitterAccount()
{
$user = common_current_user(); $user = common_current_user();
$flink = Foreign_link::getByUserID($user->id, 1); $flink = Foreign_link::getByUserID($user->id, 1);
$flink_foreign_id = $this->arg('flink_foreign_id'); $flink_foreign_id = $this->arg('flink_foreign_id');
# Maybe an old tab open...? // Maybe an old tab open...?
if ($flink->foreign_id != $flink_foreign_id) { if ($flink->foreign_id != $flink_foreign_id) {
$this->show_form(_('That is not your Twitter account.')); $this->showForm(_('That is not your Twitter account.'));
return; return;
} }
@ -289,15 +390,20 @@ class TwittersettingsAction extends SettingsAction
return; return;
} }
$this->show_form(_('Twitter account removed.'), true); $this->showForm(_('Twitter account removed.'), true);
} }
function save_preferences() /**
{ * Save user's Twitter-bridging preferences
*
* @return void
*/
function savePreferences()
{
$noticesync = $this->boolean('noticesync'); $noticesync = $this->boolean('noticesync');
$friendsync = $this->boolean('friendsync'); $friendsync = $this->boolean('friendsync');
$replysync = $this->boolean('replysync'); $replysync = $this->boolean('replysync');
$user = common_current_user(); $user = common_current_user();
@ -305,30 +411,32 @@ class TwittersettingsAction extends SettingsAction
if (!$flink) { if (!$flink) {
common_log_db_error($flink, 'SELECT', __FILE__); common_log_db_error($flink, 'SELECT', __FILE__);
$this->show_form(_('Couldn\'t save Twitter preferences.')); $this->showForm(_('Couldn\'t save Twitter preferences.'));
return; return;
} }
$twitter_id = $flink->foreign_id; $twitter_id = $flink->foreign_id;
$password = $flink->credentials; $password = $flink->credentials;
$fuser = $flink->getForeignUser(); $fuser = $flink->getForeignUser();
if (!$fuser) { if (!$fuser) {
common_log_db_error($fuser, 'SELECT', __FILE__); common_log_db_error($fuser, 'SELECT', __FILE__);
$this->show_form(_('Couldn\'t save Twitter preferences.')); $this->showForm(_('Couldn\'t save Twitter preferences.'));
return; return;
} }
$screen_name = $fuser->nickname; $screen_name = $fuser->nickname;
$original = clone($flink); $original = clone($flink);
$flink->set_flags($noticesync, $replysync, $friendsync); $flink->set_flags($noticesync, $replysync, $friendsync);
$result = $flink->update($original); $result = $flink->update($original);
if ($result === false) { if ($result === false) {
common_log_db_error($flink, 'UPDATE', __FILE__); common_log_db_error($flink, 'UPDATE', __FILE__);
$this->show_form(_('Couldn\'t save Twitter preferences.')); $this->showForm(_('Couldn\'t save Twitter preferences.'));
return; return;
} }
@ -336,12 +444,22 @@ class TwittersettingsAction extends SettingsAction
save_twitter_friends($user, $flink->foreign_id, $screen_name, $password); save_twitter_friends($user, $flink->foreign_id, $screen_name, $password);
} }
$this->show_form(_('Twitter preferences saved.'), true); $this->showForm(_('Twitter preferences saved.'), true);
} }
function verify_credentials($screen_name, $password) /**
* Verifies a username and password against Twitter's API
*
* @param string $screen_name Twitter user name
* @param string $password Twitter password
*
* @return boolean success flag
*/
function verifyCredentials($screen_name, $password)
{ {
$uri = 'http://twitter.com/account/verify_credentials.json'; $uri = 'http://twitter.com/account/verifyCredentials.json';
$data = get_twitter_data($uri, $screen_name, $password); $data = get_twitter_data($uri, $screen_name, $password);
if (!$data) { if (!$data) {
@ -354,7 +472,7 @@ class TwittersettingsAction extends SettingsAction
return false; return false;
} }
$twitter_id = $user->id; $twitter_id = $user->id;
if ($twitter_id) { if ($twitter_id) {
return $twitter_id; return $twitter_id;
@ -363,5 +481,4 @@ class TwittersettingsAction extends SettingsAction
return false; return false;
} }
} }