Twitter integration - support for new foreign_link table and prefs now save/update

darcs-hash:20080828072205-7b5ce-16510357343f1d9a3acc696f39a28a723005f3a4.gz
This commit is contained in:
Zach Copley 2008-08-28 03:22:05 -04:00
parent 8ff52da169
commit bf14709fe2
3 changed files with 123 additions and 83 deletions

View File

@ -24,62 +24,65 @@ require_once(INSTALLDIR.'/lib/settingsaction.php');
class TwittersettingsAction extends SettingsAction {
function get_instructions() {
return _('Add your Twitter account credentials to automatically send your notices to Twitter, ' .
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) {
$user = common_current_user();
$profile = $user->getProfile();
$fuser = Foreign_user::getForeignUser($user->id, 1);
$fuser = NULL;
$flink = Foreign_link::getForeignLink($user->id, 1); // 1 == Twitter
if ($flink) {
$fuser = Foreign_user::staticGet('user_id', $flink->user_id);
}
$this->form_header(_('Twitter settings'), $msg, $success);
common_element_start('form', array('method' => 'post',
'id' => 'twittersettings',
'action' =>
common_local_url('twittersettings')));
if ($fuser) {
common_element_start('p');
common_element('span', 'Twitter User', $fuser->uri);
common_element('span', 'twitter_user', $fuser->nickname);
common_element('a', array('href' => $fuser->uri), $fuser->uri);
common_element('span', 'input_instructions',
_('Current verified Twitter User'));
common_hidden('fuser_id', $fuser->id);
common_hidden('flink_user_id', $flink->user_id);
common_element_end('p');
common_submit('remove', _('Remove'));
} else {
// XXX: Should we make an educated guess as to the twitter accnt name? -- Zach
// XXX: Should we make an educated guess as to the twitter acct name? -- Zach
common_input('twitter_username', _('Twitter Username'),
($this->arg('twitter_username')) ? $this->arg('twitter_username') : $profile->nickname,
_('No spaces, please.')); // hey, it's what Twitter says
common_password('twitter_password', _('Twitter Password'));
common_submit('add', _('Add'));
}
common_element('h2', NULL, _('Preferences'));
// XXX: these checkboxes don't do anything yet
common_checkbox('repost', _('Automatically send my notices to Twitter.'), true);
common_checkbox('subscribe_friends', _('Subscribe to my Twitter friends here.'), true);
common_submit('save', _('Save'));
if ($flink) {
common_checkbox('noticesync', _('Automatically send my notices to Twitter.'),
($flink->noticesync) ? true : false);
common_checkbox('friendsync', _('Subscribe to my Twitter friends here.'),
($flink->friendsync) ? true : false);
common_submit('save', _('Save'));
} else {
common_checkbox('noticesync', _('Automatically send my notices to Twitter.'), true);
common_checkbox('friendsync', _('Subscribe to my Twitter friends here.'), true);
common_submit('add', _('Add'));
}
common_element_end('form');
common_show_footer();
}
function handle_post() {
if ($this->arg('save')) {
$this->save_preferences();
} else if ($this->arg('add')) {
@ -92,13 +95,10 @@ class TwittersettingsAction extends SettingsAction {
}
function add_twitter_acct() {
$user = common_current_user();
$fuser = Foreign_user::getForeignUser($user->id, 1);
$twitter_username = $this->trimmed('twitter_username');
$twitter_password = $this->trimmed('twitter_password');
$noticesync = $this->boolean('noticesync');
$friendsync = $this->boolean('friendsync');
if (!Validate::string($twitter_username, array('min_length' => 1,
'max_length' => 64,
@ -121,36 +121,58 @@ class TwittersettingsAction extends SettingsAction {
$this->show_form(sprintf(_('Unable to retrieve account information for "%s" from Twitter.'), $twitter_username));
return;
}
$user = common_current_user();
$fuser = Foreign_user::save(
array(
'id' => $twitter_id,
'service' => '1', // 1 == Twitter
'uri' => "http://www.twitter.com/$twitter_username",
'nickname' => $twitter_username,
'user_id' => $user->id,
'credentials' => $twitter_password
));
$fuser = DB_DataObject::factory('foreign_user');
$fuser->id = $twitter_id;
$fuser->service = 1; // Twitter
$fuser->uri = "http://www.twitter.com/$twitter_username";
$fuser->nickname = $twitter_username;
$fuser->created = common_sql_now();
$result = $fuser->insert();
if (!$fuser) {
$this->show_form(_('Unable to save your Twitter credentials!'));
if (!$result) {
common_log_db_error($fuser, 'INSERT', __FILE__);
$this->show_form(_('Unable to save your Twitter settings!'));
return;
}
$user = common_current_user();
$flink = DB_DataObject::factory('foreign_link');
$flink->user_id = $user->id;
$flink->foreign_id = $fuser->id;
$flink->service = 1; // Twitter
$flink->credentials = $twitter_password;
$flink->created = common_sql_now();
$flink->noticesync = ($noticesync) ? 1 : 0;
$flink->friendsync = ($friendsync) ? 2 : 0;
$flink->profilesync = 0; // XXX: leave as default?
$flink_id = $flink->insert();
if (!$flink_id) {
common_log_db_error($flink, 'INSERT', __FILE__);
$this->show_form(_('Unable to save your Twitter settings!'));
return;
}
$this->show_form(_('Twitter settings saved.'), true);
}
function remove_twitter_acct() {
$user = common_current_user();
$fuser = Foreign_user::getForeignUser($user->id, 1);
$fuser_id = $this->arg('fuser_id');
// For now we assume one Twitter acct per Laconica acct
$flink = Foreign_link::getForeignLink($user->id, 1);
$fuser = Foreign_user::getForeignUser($flink->foreign_id, 1);
$flink_user_id = $this->arg('flink_user_id');
if (!$flink) {
common_debug("couldn't get flink");
}
# Maybe an old tab open...?
if ($fuser->id != $fuser_id) {
if ($flink->user_id != $flink_user_id) {
common_debug("flink user_id = " . $flink->user_id);
$this->show_form(_('That is not your Twitter account.'));
return;
}
@ -158,7 +180,15 @@ class TwittersettingsAction extends SettingsAction {
$result = $fuser->delete();
if (!$result) {
common_log_db_error($user, 'UPDATE', __FILE__);
common_log_db_error($flink, 'DELETE', __FILE__);
$this->show_form(_('Couldn\'t remove Twitter user.'));
return;
}
$result = $flink->delete();
if (!$result) {
common_log_db_error($flink, 'DELETE', __FILE__);
common_server_error(_('Couldn\'t remove Twitter user.'));
return;
}
@ -167,17 +197,34 @@ class TwittersettingsAction extends SettingsAction {
}
function save_preferences() {
$user = common_current_user();
$fuser = Foreign_user::getForeignUser($user->id, 1);
$noticesync = $this->boolean('noticesync');
$friendsync = $this->boolean('friendsync');
$user = common_current_user();
$flink = Foreign_link::getForeignLink($user->id, 1);
$this->show_form(_('Save doesn\'t do anything yet.'));
if (!$flink) {
common_log_db_error($flink, 'SELECT', __FILE__);
$this->show_form(_('Couldn\'t save Twitter preferences.'));
return;
}
$flink->noticesync = ($noticesync) ? 1 : 0;
$flink->friendsync = ($friendsync) ? 2 : 0;
// $flink->profilesync = 0; // XXX: leave as default?
$result = $flink->update();
if (!$result) {
common_log_db_error($flink, 'UPDATE', __FILE__);
$this->show_form(_('Couldn\'t save Twitter preferences.'));
return;
}
$this->show_form(_('Twitter preferences saved.'));
return;
}
function get_twitter_id($twitter_username) {
function get_twitter_id($twitter_username) {
$uri = "http://twitter.com/users/show/$twitter_username.json";
$data = $this->get_twitter_data($uri);
@ -194,8 +241,7 @@ class TwittersettingsAction extends SettingsAction {
return $user->id;
}
function verify_credentials($user, $password) {
function verify_credentials($user, $password) {
$uri = 'http://twitter.com/account/verify_credentials.json';
$data = $this->get_twitter_data($uri, $user, $password);
@ -239,8 +285,8 @@ class TwittersettingsAction extends SettingsAction {
}
curl_close($ch);
return $data;
}
}

View File

@ -25,4 +25,22 @@ class Foreign_link extends DB_DataObject
/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
// XXX: This only returns a 1->1 single obj mapping. Change? Or make
// a getForeignUsers() that returns more than one? --Zach
static function getForeignLink($user_id, $service) {
$flink = new Foreign_link();
$flink->whereAdd("service = $service");
$flink->whereAdd("user_id = $user_id");
$flink->limit(1);
if ($flink->find()) {
$flink->fetch();
return $flink;
}
return NULL;
}
}

View File

@ -23,11 +23,12 @@ class Foreign_user extends DB_DataObject
/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
function getForeignUser($user_id, $service) {
$fuser = DB_DataObject::factory('foreign_user');
// XXX: This only returns a 1->1 single obj mapping. Change? Or make
// a getForeignUsers() that returns more than one? --Zach
static function getForeignUser($id, $service) {
$fuser = new Foreign_user();
$fuser->whereAdd("service = $service");
$fuser->whereAdd("user_id = $user_id");
$fuser->whereAdd("id = $id");
$fuser->limit(1);
if ($fuser->find()) {
@ -38,29 +39,4 @@ class Foreign_user extends DB_DataObject
return NULL;
}
static function save($fields) {
extract($fields);
$fuser = new Foreign_user();
$fuser->id = $id;
$fuser->service = $service;
$fuser->uri = $uri;
$fuser->nickname = $nickname;
$fuser->user_id = $user_id;
$fuser->credentials = $credentials;
$fuser->created = common_sql_now();
$result = $fuser->insert();
if (!$result) {
common_log_db_error($fuser, 'INSERT', __FILE__);
return FALSE;
}
return $fuser;
}
}