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 { class TwittersettingsAction extends SettingsAction {
function get_instructions() { 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.'); '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();
$profile = $user->getProfile(); $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); $this->form_header(_('Twitter settings'), $msg, $success);
common_element_start('form', array('method' => 'post', common_element_start('form', array('method' => 'post',
'id' => 'twittersettings', 'id' => 'twittersettings',
'action' => 'action' =>
common_local_url('twittersettings'))); common_local_url('twittersettings')));
if ($fuser) { if ($fuser) {
common_element_start('p'); 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', common_element('span', 'input_instructions',
_('Current verified Twitter User')); _('Current verified Twitter User'));
common_hidden('fuser_id', $fuser->id); common_hidden('flink_user_id', $flink->user_id);
common_element_end('p'); common_element_end('p');
common_submit('remove', _('Remove')); common_submit('remove', _('Remove'));
} else { } 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'), 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
common_password('twitter_password', _('Twitter Password')); common_password('twitter_password', _('Twitter Password'));
common_submit('add', _('Add'));
} }
common_element('h2', NULL, _('Preferences')); common_element('h2', NULL, _('Preferences'));
// XXX: these checkboxes don't do anything yet if ($flink) {
common_checkbox('repost', _('Automatically send my notices to Twitter.'), true); common_checkbox('noticesync', _('Automatically send my notices to Twitter.'),
common_checkbox('subscribe_friends', _('Subscribe to my Twitter friends here.'), true); ($flink->noticesync) ? true : false);
common_checkbox('friendsync', _('Subscribe to my Twitter friends here.'),
common_submit('save', _('Save')); ($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_element_end('form');
common_show_footer(); common_show_footer();
} }
function handle_post() { function handle_post() {
if ($this->arg('save')) { if ($this->arg('save')) {
$this->save_preferences(); $this->save_preferences();
} else if ($this->arg('add')) { } else if ($this->arg('add')) {
@ -92,13 +95,10 @@ class TwittersettingsAction extends SettingsAction {
} }
function add_twitter_acct() { function add_twitter_acct() {
$user = common_current_user();
$fuser = Foreign_user::getForeignUser($user->id, 1);
$twitter_username = $this->trimmed('twitter_username'); $twitter_username = $this->trimmed('twitter_username');
$twitter_password = $this->trimmed('twitter_password'); $twitter_password = $this->trimmed('twitter_password');
$noticesync = $this->boolean('noticesync');
$friendsync = $this->boolean('friendsync');
if (!Validate::string($twitter_username, array('min_length' => 1, if (!Validate::string($twitter_username, array('min_length' => 1,
'max_length' => 64, 'max_length' => 64,
@ -122,35 +122,57 @@ class TwittersettingsAction extends SettingsAction {
return; return;
} }
$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 (!$result) {
common_log_db_error($fuser, 'INSERT', __FILE__);
$this->show_form(_('Unable to save your Twitter settings!'));
return;
}
$user = common_current_user(); $user = common_current_user();
$fuser = Foreign_user::save( $flink = DB_DataObject::factory('foreign_link');
array( $flink->user_id = $user->id;
'id' => $twitter_id, $flink->foreign_id = $fuser->id;
'service' => '1', // 1 == Twitter $flink->service = 1; // Twitter
'uri' => "http://www.twitter.com/$twitter_username", $flink->credentials = $twitter_password;
'nickname' => $twitter_username, $flink->created = common_sql_now();
'user_id' => $user->id, $flink->noticesync = ($noticesync) ? 1 : 0;
'credentials' => $twitter_password $flink->friendsync = ($friendsync) ? 2 : 0;
)); $flink->profilesync = 0; // XXX: leave as default?
$flink_id = $flink->insert();
if (!$fuser) { if (!$flink_id) {
$this->show_form(_('Unable to save your Twitter credentials!')); common_log_db_error($flink, 'INSERT', __FILE__);
$this->show_form(_('Unable to save your Twitter settings!'));
return;
} }
$this->show_form(_('Twitter settings saved.'), true); $this->show_form(_('Twitter settings saved.'), true);
} }
function remove_twitter_acct() { function remove_twitter_acct() {
$user = common_current_user(); $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...? # Maybe an old tab open...?
if ($flink->user_id != $flink_user_id) {
if ($fuser->id != $fuser_id) { common_debug("flink user_id = " . $flink->user_id);
$this->show_form(_('That is not your Twitter account.')); $this->show_form(_('That is not your Twitter account.'));
return; return;
} }
@ -158,7 +180,15 @@ class TwittersettingsAction extends SettingsAction {
$result = $fuser->delete(); $result = $fuser->delete();
if (!$result) { 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.')); common_server_error(_('Couldn\'t remove Twitter user.'));
return; return;
} }
@ -167,17 +197,34 @@ class TwittersettingsAction extends SettingsAction {
} }
function save_preferences() { function save_preferences() {
$noticesync = $this->boolean('noticesync');
$friendsync = $this->boolean('friendsync');
$user = common_current_user(); $user = common_current_user();
$fuser = Foreign_user::getForeignUser($user->id, 1); $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; 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";
$data = $this->get_twitter_data($uri); $data = $this->get_twitter_data($uri);
@ -195,7 +242,6 @@ class TwittersettingsAction extends SettingsAction {
} }
function verify_credentials($user, $password) { function verify_credentials($user, $password) {
$uri = 'http://twitter.com/account/verify_credentials.json'; $uri = 'http://twitter.com/account/verify_credentials.json';
$data = $this->get_twitter_data($uri, $user, $password); $data = $this->get_twitter_data($uri, $user, $password);
@ -239,8 +285,8 @@ class TwittersettingsAction extends SettingsAction {
} }
curl_close($ch); curl_close($ch);
return $data; 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 */ /* the code above is auto generated do not remove the tag below */
###END_AUTOCODE ###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 */ /* the code above is auto generated do not remove the tag below */
###END_AUTOCODE ###END_AUTOCODE
function getForeignUser($user_id, $service) { // XXX: This only returns a 1->1 single obj mapping. Change? Or make
// a getForeignUsers() that returns more than one? --Zach
$fuser = DB_DataObject::factory('foreign_user'); static function getForeignUser($id, $service) {
$fuser = new Foreign_user();
$fuser->whereAdd("service = $service"); $fuser->whereAdd("service = $service");
$fuser->whereAdd("user_id = $user_id"); $fuser->whereAdd("id = $id");
$fuser->limit(1); $fuser->limit(1);
if ($fuser->find()) { if ($fuser->find()) {
@ -38,29 +39,4 @@ class Foreign_user extends DB_DataObject
return NULL; 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;
}
} }