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
/*
* Laconica - a distributed open-source microblogging tool
* Copyright (C) 2008, Controlez-Vous, Inc.
/**
* Laconica, the distributed open-source microblogging tool
*
* 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
@ -15,35 +18,84 @@
*
* 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/>.
*
* @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);
/**
* 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
{
/**
* 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');
}
/**
* 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.');
}
function show_form($msg=null, $success=false)
/**
* 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();
$profile = $user->getProfile();
$fuser = null;
$flink = Foreign_link::getByUserID($user->id, 1); // 1 == Twitter
if ($flink) {
$fuser = $flink->getForeignUser();
}
$this->form_header(_('Twitter settings'), $msg, $success);
$this->elementStart('form', array('method' => 'post',
'id' => 'twittersettings',
'action' =>
@ -64,7 +116,9 @@ class TwittersettingsAction extends SettingsAction
$this->submit('remove', _('Remove'));
} else {
$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
$this->password('twitter_password', _('Twitter password'));
@ -72,14 +126,23 @@ class TwittersettingsAction extends SettingsAction
$this->element('h2', null, _('Preferences'));
$this->checkbox('noticesync', _('Automatically send my notices to Twitter.'),
($flink) ? ($flink->noticesync & FOREIGN_NOTICE_SEND) : true);
$this->checkbox('noticesync',
_('Automatically send my notices to Twitter.'),
($flink) ?
($flink->noticesync & FOREIGN_NOTICE_SEND) :
true);
$this->checkbox('replysync', _('Send local "@" replies to Twitter.'),
($flink) ? ($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) : true);
$this->checkbox('replysync',
_('Send local "@" replies to Twitter.'),
($flink) ?
($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) :
true);
$this->checkbox('friendsync', _('Subscribe to my Twitter friends here.'),
($flink) ? ($flink->friendsync & FOREIGN_FRIEND_RECV) : false);
$this->checkbox('friendsync',
_('Subscribe to my Twitter friends here.'),
($flink) ?
($flink->friendsync & FOREIGN_FRIEND_RECV) :
false);
if ($flink) {
$this->submit('save', _('Save'));
@ -87,14 +150,21 @@ class TwittersettingsAction extends SettingsAction
$this->submit('add', _('Add'));
}
$this->show_twitter_subscriptions();
$this->showTwitterSubscriptions();
$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();
@ -123,10 +193,20 @@ class TwittersettingsAction extends SettingsAction
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);
if ($friends_count > 0) {
@ -151,8 +231,14 @@ class TwittersettingsAction extends SettingsAction
'href' => $other->profileurl,
'rel' => 'contact',
'class' => 'subscription'));
$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,
'height' => AVATAR_MINI_SIZE,
'class' => 'avatar mini',
@ -168,47 +254,52 @@ class TwittersettingsAction extends SettingsAction
$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');
}
/**
* 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 handle_post()
function handlePost()
{
# CSRF protection
// CSRF protection
$token = $this->trimmed('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;
}
if ($this->arg('save')) {
$this->save_preferences();
$this->savePreferences();
} else if ($this->arg('add')) {
$this->add_twitter_acct();
$this->addTwitterAccount();
} else if ($this->arg('remove')) {
$this->remove_twitter_acct();
$this->removeTwitterAccount();
} 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');
$password = $this->trimmed('twitter_password');
$noticesync = $this->boolean('noticesync');
@ -219,32 +310,35 @@ class TwittersettingsAction extends SettingsAction
array('min_length' => 1,
'max_length' => 15,
'format' => VALIDATE_NUM.VALIDATE_ALPHA.'_'))) {
$this->show_form(
_('Username must have only numbers, upper- and lowercase letters, and underscore (_). 15 chars max.'));
$this->showForm(_('Username must have only numbers, '.
'upper- and lowercase letters, '.
'and underscore (_). 15 chars max.'));
return;
}
if (!$this->verify_credentials($screen_name, $password)) {
$this->show_form(_('Could not verify your Twitter credentials!'));
if (!$this->verifyCredentials($screen_name, $password)) {
$this->showForm(_('Could not verify your Twitter credentials!'));
return;
}
$twit_user = twitter_user_info($screen_name, $password);
if (!$twit_user) {
$this->show_form(sprintf(_('Unable to retrieve account information for "%s" from Twitter.'),
$this->showForm(sprintf(_('Unable to retrieve account information '.
'For "%s" from Twitter.'),
$screen_name));
return;
}
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;
}
$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->service = 1; // Twitter
@ -257,7 +351,7 @@ class TwittersettingsAction extends SettingsAction
if (!$flink_id) {
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;
}
@ -265,19 +359,26 @@ class TwittersettingsAction extends SettingsAction
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();
$flink = Foreign_link::getByUserID($user->id, 1);
$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) {
$this->show_form(_('That is not your Twitter account.'));
$this->showForm(_('That is not your Twitter account.'));
return;
}
@ -289,12 +390,17 @@ class TwittersettingsAction extends SettingsAction
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');
$friendsync = $this->boolean('friendsync');
$replysync = $this->boolean('replysync');
@ -305,7 +411,7 @@ class TwittersettingsAction extends SettingsAction
if (!$flink) {
common_log_db_error($flink, 'SELECT', __FILE__);
$this->show_form(_('Couldn\'t save Twitter preferences.'));
$this->showForm(_('Couldn\'t save Twitter preferences.'));
return;
}
@ -316,19 +422,21 @@ class TwittersettingsAction extends SettingsAction
if (!$fuser) {
common_log_db_error($fuser, 'SELECT', __FILE__);
$this->show_form(_('Couldn\'t save Twitter preferences.'));
$this->showForm(_('Couldn\'t save Twitter preferences.'));
return;
}
$screen_name = $fuser->nickname;
$original = clone($flink);
$flink->set_flags($noticesync, $replysync, $friendsync);
$result = $flink->update($original);
if ($result === false) {
common_log_db_error($flink, 'UPDATE', __FILE__);
$this->show_form(_('Couldn\'t save Twitter preferences.'));
$this->showForm(_('Couldn\'t save Twitter preferences.'));
return;
}
@ -336,12 +444,22 @@ class TwittersettingsAction extends SettingsAction
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);
if (!$data) {
@ -363,5 +481,4 @@ class TwittersettingsAction extends SettingsAction
return false;
}
}