Twitter integration - Foreign_user::getForeignUser() to retrieve

Foreign_user by multi-part key: user_id + service

darcs-hash:20080827005444-7b5ce-2b53a2c1a917539248b68f21f94e0f9f5fe810f2.gz
This commit is contained in:
Zach Copley 2008-08-26 20:54:44 -04:00
parent 803bdff3f6
commit 3cf6cef9b9
2 changed files with 63 additions and 49 deletions

View File

@ -24,14 +24,15 @@ require_once(INSTALLDIR.'/lib/settingsaction.php');
class TwittersettingsAction extends SettingsAction { class TwittersettingsAction extends SettingsAction {
function get_instructions() { function get_instructions() {
return _('Enter your Twitter credentials to automatically send your notices to Twitter, ' . return _('Add your Twitter account credentials to automatically send your notices to Twitter, ' .
'and subscribe to Twitter friends already here.'); 'and subscribe to Twitter friends already here.');
} }
function show_form($msg=NULL, $success=false) { function show_form($msg=NULL, $success=false) {
$user = common_current_user(); $user = common_current_user();
$fuser = Foreign_user::staticGet('user_id', $user->id); $profile = $user->getProfile();
$fuser = Foreign_user::getForeignUser($user->id, 0);
$this->form_header(_('Twitter settings'), $msg, $success); $this->form_header(_('Twitter settings'), $msg, $success);
@ -54,6 +55,7 @@ class TwittersettingsAction extends SettingsAction {
} else { } else {
// XXX: Should we make an educated guess as to the twitter accnt name? -- Zach
common_input('twitter_username', _('Twitter Username'), common_input('twitter_username', _('Twitter Username'),
($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
@ -66,14 +68,12 @@ class TwittersettingsAction extends SettingsAction {
common_element('h2', NULL, _('Preferences')); common_element('h2', NULL, _('Preferences'));
// these checkboxes don't do anything yet // XXX: these checkboxes don't do anything yet
common_checkbox('repost', _('Automatically send my notices to Twitter.'), true); common_checkbox('repost', _('Automatically send my notices to Twitter.'), true);
common_checkbox('subscribe_friends', _('Subscribe to my Twitter friends here.'), true); common_checkbox('subscribe_friends', _('Subscribe to my Twitter friends here.'), true);
common_submit('save', _('Save')); common_submit('save', _('Save'));
common_element_end('form'); common_element_end('form');
common_show_footer(); common_show_footer();
} }
@ -83,55 +83,18 @@ class TwittersettingsAction extends SettingsAction {
if ($this->arg('save')) { if ($this->arg('save')) {
$this->save_preferences(); $this->save_preferences();
} else if ($this->arg('add')) { } else if ($this->arg('add')) {
$this->add_twitter_user(); $this->add_twitter_acct();
} else if ($this->arg('remove')) { } else if ($this->arg('remove')) {
$this->remove_twitter_user(); $this->remove_twitter_acct();
} else { } else {
$this->show_form(_('Unexpected form submission.')); $this->show_form(_('Unexpected form submission.'));
} }
} }
function remove_twitter_user() {
$user = common_current_user(); function add_twitter_acct() {
$fuser = Foreign_user::staticGet('user_id', $user->id);
$fuser_id = $this->arg('fuser_id');
# Maybe an old tab open...?
if ($fuser->id != $fuser_id) {
$this->show_form(_('That is not your Twitter account.'));
return;
}
$result = $fuser->delete();
if (!$result) {
common_log_db_error($user, 'UPDATE', __FILE__);
common_server_error(_('Couldn\'t remove Twitter user.'));
return;
}
$this->show_form(_('Twitter account removed.'), TRUE);
}
function save_preferences() {
$user = common_current_user(); $user = common_current_user();
$fuser = Foreign_user::staticGet('user_id', $user->id); $fuser = Foreign_user::getForeignUser($user->id, 0);
$this->show_form(_('Save doesn\'t do anything yet.'));
return;
}
function add_twitter_user() {
$user = common_current_user();
$fuser = Foreign_user::staticGet('user_id', $user->id);
$twitter_username = $this->trimmed('twitter_username'); $twitter_username = $this->trimmed('twitter_username');
@ -151,7 +114,7 @@ class TwittersettingsAction extends SettingsAction {
} }
// Now that we have a valid Twitter user, we have to make another api call to // Now that we have a valid Twitter user, we have to make another api call to
// find its Twitter ID. // find its Twitter ID. Dumb, but true.
$twitter_id = $this->get_twitter_id($twitter_username); $twitter_id = $this->get_twitter_id($twitter_username);
if (!$twitter_id) { if (!$twitter_id) {
@ -178,6 +141,41 @@ class TwittersettingsAction extends SettingsAction {
$this->show_form(_('Twitter settings saved.'), true); $this->show_form(_('Twitter settings saved.'), true);
} }
function remove_twitter_acct() {
$user = common_current_user();
$fuser = Foreign_user::getForeignUser($user->id, 0);
$fuser_id = $this->arg('fuser_id');
# Maybe an old tab open...?
if ($fuser->id != $fuser_id) {
$this->show_form(_('That is not your Twitter account.'));
return;
}
$result = $fuser->delete();
if (!$result) {
common_log_db_error($user, 'UPDATE', __FILE__);
common_server_error(_('Couldn\'t remove Twitter user.'));
return;
}
$this->show_form(_('Twitter account removed.'), TRUE);
}
function save_preferences() {
$user = common_current_user();
$fuser = Foreign_user::getForeignUser($user->id, 0);
$this->show_form(_('Save doesn\'t do anything yet.'));
return;
}
function get_twitter_id($twitter_username) { function get_twitter_id($twitter_username) {
$uri = "http://twitter.com/users/show/$twitter_username.json"; $uri = "http://twitter.com/users/show/$twitter_username.json";

View File

@ -23,6 +23,22 @@ class Foreign_user extends DB_DataObject
/* the code above is auto generated do not remove the tag below */ /* the code above is auto generated do not remove the tag below */
###END_AUTOCODE ###END_AUTOCODE
function getForeignUser($user_id, $service) {
$fuser = DB_DataObject::factory('foreign_user');
$fuser->whereAdd("service = $service");
$fuser->whereAdd("user_id = $user_id");
$fuser->limit(1);
if ($fuser->find()) {
$fuser->fetch();
return $fuser;
}
return NULL;
}
static function save($fields) { static function save($fields) {
extract($fields); extract($fields);