Twitter bridge - pref setting to turn off sending @-replies to Twitter

darcs-hash:20080924023615-7b5ce-c747e8236b3967824ed93f18285e1b6e0b46c3f0.gz
This commit is contained in:
Zach Copley 2008-09-23 22:36:15 -04:00
parent 285f062197
commit 06859cc14e
3 changed files with 58 additions and 22 deletions

View File

@ -71,13 +71,19 @@ class TwittersettingsAction extends SettingsAction {
if ($flink) { if ($flink) {
common_checkbox('noticesync', _('Automatically send my notices to Twitter.'), common_checkbox('noticesync', _('Automatically send my notices to Twitter.'),
($flink->noticesync) ? true : false); ($flink->noticesync > 0) ? true : false);
common_checkbox('replysync', _('Don\'t send local "@" replies to Twitter.'),
($flink->noticesync == 3) ? true : false, NULL, 'true');
common_checkbox('friendsync', _('Subscribe to my Twitter friends here.'), common_checkbox('friendsync', _('Subscribe to my Twitter friends here.'),
($flink->friendsync) ? true : false); ($flink->friendsync) ? true : false, NULL, 'true', true);
common_submit('save', _('Save')); common_submit('save', _('Save'));
} else { } else {
common_checkbox('noticesync', _('Automatically send my notices to Twitter.'), true); common_checkbox('noticesync', _('Automatically send my notices to Twitter.'), true);
common_checkbox('friendsync', _('Subscribe to my Twitter friends here.'), true); common_checkbox('replysync', _('Don\'t send local "@" replies to Twitter.'), false, NULL, 'true');
common_checkbox('friendsync', _('Subscribe to my Twitter friends here.'), false, NULL, 'true', true);
common_submit('add', _('Add')); common_submit('add', _('Add'));
} }
@ -109,12 +115,13 @@ class TwittersettingsAction extends SettingsAction {
$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'); $noticesync = $this->boolean('noticesync');
$replysync = $this->boolean('replysync');
$friendsync = $this->boolean('friendsync'); $friendsync = $this->boolean('friendsync');
if (!Validate::string($twitter_username, array('min_length' => 1, if (!Validate::string($twitter_username, array('min_length' => 1,
'max_length' => 15, 'max_length' => 15,
'format' => VALIDATE_NUM . VALIDATE_ALPHA . '_'))) { 'format' => VALIDATE_NUM . VALIDATE_ALPHA . '_'))) {
$this->show_form(_('Username must have only numbers, upper- and lowercase letters, and underscore (_).')); $this->show_form(_('Username must have only numbers, upper- and lowercase letters, and underscore (_). 15 chars max.'));
return; return;
} }
@ -155,7 +162,15 @@ class TwittersettingsAction extends SettingsAction {
$flink->service = 1; // Twitter $flink->service = 1; // Twitter
$flink->credentials = $twitter_password; $flink->credentials = $twitter_password;
$flink->created = common_sql_now(); $flink->created = common_sql_now();
$flink->noticesync = ($noticesync) ? 1 : 0;
if ($noticesync) {
if ($replysync) {
$flink->noticesync = 3;
} else {
$flink->noticesync = 1;
}
}
$flink->friendsync = ($friendsync) ? 2 : 0; $flink->friendsync = ($friendsync) ? 2 : 0;
$flink->profilesync = 0; // XXX: leave as default? $flink->profilesync = 0; // XXX: leave as default?
$flink_id = $flink->insert(); $flink_id = $flink->insert();
@ -210,6 +225,8 @@ class TwittersettingsAction extends SettingsAction {
function save_preferences() { function save_preferences() {
$noticesync = $this->boolean('noticesync'); $noticesync = $this->boolean('noticesync');
$friendsync = $this->boolean('friendsync'); $friendsync = $this->boolean('friendsync');
$replysync = $this->boolean('replysync');
$user = common_current_user(); $user = common_current_user();
$flink = Foreign_link::getForeignLink($user->id, 1); $flink = Foreign_link::getForeignLink($user->id, 1);
@ -219,7 +236,14 @@ class TwittersettingsAction extends SettingsAction {
return; return;
} }
$flink->noticesync = ($noticesync) ? 1 : 0; if ($noticesync) {
if ($replysync) {
$flink->noticesync = 3;
} else {
$flink->noticesync = 1;
}
}
$flink->friendsync = ($friendsync) ? 2 : 0; $flink->friendsync = ($friendsync) ? 2 : 0;
// $flink->profilesync = 0; // XXX: leave as default? // $flink->profilesync = 0; // XXX: leave as default?
$result = $flink->update(); $result = $flink->update();

View File

@ -281,7 +281,7 @@ create table foreign_link (
foreign_id int comment 'link ' references foreign_user(id), foreign_id int comment 'link ' references foreign_user(id),
service int not null comment 'foreign key to service' references foreign_service(id), service int not null comment 'foreign key to service' references foreign_service(id),
credentials varchar(255) comment 'authc credentials, typically a password', credentials varchar(255) comment 'authc credentials, typically a password',
noticesync tinyint not null default 1 comment 'notice synchronization, bit 1 = sync outgoing, bit 2 = sync incoming', noticesync tinyint not null default 1 comment 'notice synchronization, bit 1 = sync outgoing, bit 2 = sync incoming, bit 3 = filter local replies',
friendsync tinyint not null default 2 comment 'friend synchronization, bit 1 = sync outgoing, bit 2 = sync incoming', friendsync tinyint not null default 2 comment 'friend synchronization, bit 1 = sync outgoing, bit 2 = sync incoming',
profilesync tinyint not null default 1 comment 'profile synchronization, bit 1 = sync outgoing, bit 2 = sync incoming', profilesync tinyint not null default 1 comment 'profile synchronization, bit 1 = sync outgoing, bit 2 = sync incoming',
created datetime not null comment 'date this record was created', created datetime not null comment 'date this record was created',

View File

@ -1107,16 +1107,28 @@ function common_save_replies($notice) {
} }
function common_broadcast_notice($notice, $remote=false) { function common_broadcast_notice($notice, $remote=false) {
// Check to see if notice should go to Twitter // Check to see if notice should go to Twitter
$flink = Foreign_link::getForeignLink($notice->profile_id, 1); // 1 == Twitter $flink = Foreign_link::getForeignLink($notice->profile_id, 1); // 1 == Twitter
if ($flink->noticesync >= 1) {
if ($flink->noticesync == 1) { $ok_to_send = true;
if (!common_twitter_broadcast($notice, $flink)) {
common_debug('Unable to send notice: ' . $notice->id . ' to Twitter.', __FILE__); // Check to see whether user wants to filter @-replies
if ($flink->noticesync == 3) {
if (preg_match('/(?:^|\s)@([A-Za-z0-9_\-\.]{1,64})/', $notice->content)) {
$ok_to_send = false;
}
}
if ($ok_to_send) {
$result = common_twitter_broadcast($notice, $flink);
if (!$result) {
common_debug('Unable to send notice: ' . $notice->id . ' to Twitter.', __FILE__);
}
} }
} }
if (common_config('queue', 'enabled')) { if (common_config('queue', 'enabled')) {
# Do it later! # Do it later!
return common_enqueue_notice($notice); return common_enqueue_notice($notice);
@ -1125,17 +1137,17 @@ function common_broadcast_notice($notice, $remote=false) {
} }
} }
function common_twitter_broadcast($notice, $flink) { function common_twitter_broadcast($notice, $flink) {
global $config; global $config;
$success = true; $success = true;
$fuser = $flink->getForeignUser(); $fuser = $flink->getForeignUser();
$twitter_user = $fuser->nickname; $twitter_user = $fuser->nickname;
$twitter_password = $flink->credentials; $twitter_password = $flink->credentials;
$uri = 'http://www.twitter.com/statuses/update.json'; $uri = 'http://www.twitter.com/statuses/update.json';
// XXX: Hack to get around PHP cURL's use of @ being a a meta character // XXX: Hack to get around PHP cURL's use of @ being a a meta character
$statustxt = preg_replace('/^@/', ' @', $notice->content); $statustxt = preg_replace('/^@/', ' @', $notice->content);
$options = array( $options = array(
CURLOPT_USERPWD => "$twitter_user:$twitter_password", CURLOPT_USERPWD => "$twitter_user:$twitter_password",
CURLOPT_POST => true, CURLOPT_POST => true,
@ -1151,27 +1163,27 @@ function common_twitter_broadcast($notice, $flink) {
CURLOPT_CONNECTTIMEOUT => 120, // XXX: Scary!!!! How long should this be? CURLOPT_CONNECTTIMEOUT => 120, // XXX: Scary!!!! How long should this be?
CURLOPT_TIMEOUT => 120 CURLOPT_TIMEOUT => 120
); );
$ch = curl_init($uri); $ch = curl_init($uri);
curl_setopt_array($ch, $options); curl_setopt_array($ch, $options);
$data = curl_exec($ch); $data = curl_exec($ch);
$errmsg = curl_error($ch); $errmsg = curl_error($ch);
if ($errmsg) { if ($errmsg) {
common_debug("cURL error: $errmsg - trying to send notice for $twitter_user.", common_debug("cURL error: $errmsg - trying to send notice for $twitter_user.",
__FILE__); __FILE__);
$success = false; $success = false;
} }
curl_close($ch); curl_close($ch);
if (!$data) { if (!$data) {
common_debug("No data returned by Twitter's API trying to send update for $twitter_user", common_debug("No data returned by Twitter's API trying to send update for $twitter_user",
__FILE__); __FILE__);
$success = false; $success = false;
} }
// Twitter should return a status // Twitter should return a status
$status = json_decode($data); $status = json_decode($data);
if (!$status->id) { if (!$status->id) {
@ -1179,8 +1191,8 @@ function common_twitter_broadcast($notice, $flink) {
__FILE__); __FILE__);
$success = false; $success = false;
} }
return $status; return $success;
} }
# Stick the notice on the queue # Stick the notice on the queue