change foreign links flags code to use bitmasks

Zach was doing a lot of integer comparisons on the Foreign_link sync
fields. I switched them to use named bitmasks instead. I also switched
the semantics of bit 3 to be the opposite of what Zach had -- I find
lots of double-negatives in a checkbox to be hard to read.

darcs-hash:20080924150834-5ed1f-54cd945f61e43bc06768037c60c1e6180a8feead.gz
This commit is contained in:
Evan Prodromou 2008-09-24 11:08:34 -04:00
parent 845db5ec16
commit 392137b403
3 changed files with 48 additions and 48 deletions

View File

@ -69,21 +69,19 @@ class TwittersettingsAction extends SettingsAction {
common_element('h2', NULL, _('Preferences')); common_element('h2', NULL, _('Preferences'));
common_checkbox('noticesync', _('Automatically send my notices to Twitter.'),
($flink) ? ($flink->noticesync & FOREIGN_NOTICE_SEND) : true);
common_checkbox('replysync', _('Send local "@" replies to Twitter.'),
($flink) ? ($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) : false);
common_checkbox('friendsync', _('Subscribe to my Twitter friends here.'),
($flink) ? ($flink->friendsync & FOREIGN_FRIEND_RECV) : false,
NULL, 'true', true);
if ($flink) { if ($flink) {
common_checkbox('noticesync', _('Automatically send my notices to Twitter.'),
($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.'),
($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('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'));
} }
@ -163,18 +161,8 @@ class TwittersettingsAction extends SettingsAction {
$flink->credentials = $twitter_password; $flink->credentials = $twitter_password;
$flink->created = common_sql_now(); $flink->created = common_sql_now();
if ($noticesync) { $this->set_flags($flink, $noticesync, $replysync, $friendsync);
if ($replysync) {
$flink->noticesync = 3;
} else {
$flink->noticesync = 1;
}
} else {
$flink->noticesync = 0;
}
$flink->friendsync = ($friendsync) ? 2 : 0;
$flink->profilesync = 0; // XXX: leave as default?
$flink_id = $flink->insert(); $flink_id = $flink->insert();
if (!$flink_id) { if (!$flink_id) {
@ -241,19 +229,8 @@ class TwittersettingsAction extends SettingsAction {
$flink->query('BEGIN'); $flink->query('BEGIN');
$original = clone($flink); $original = clone($flink);
if ($noticesync) { $this->set_flags($flink, $noticesync, $replysync, $friendsync);
if ($replysync) {
$flink->noticesync = 3;
} else {
$flink->noticesync = 1;
}
} else {
$flink->noticesync = 0;
}
$flink->friendsync = ($friendsync) ? 2 : 0;
// $flink->profilesync = 0; // XXX: leave as default?
$result = $flink->update($original); $result = $flink->update($original);
@ -335,4 +312,25 @@ class TwittersettingsAction extends SettingsAction {
return $data; return $data;
} }
function set_flags(&$flink, $noticesync, $replysync, $friendsync) {
if ($noticesync) {
$flink->noticesync |= FOREIGN_NOTICE_SEND;
} else {
$flink->noticesync &= ~FOREIGN_NOTICE_SEND;
}
if ($replysync) {
$flink->noticesync |= FOREIGN_NOTICE_SEND_REPLY;
} else {
$flink->noticesync &= ~FOREIGN_NOTICE_SEND_REPLY;
}
if ($friendsync) {
$flink->friendsync |= FOREIGN_FRIEND_RECV;
} else {
$flink->friendsync &= ~FOREIGN_FRIEND_RECV;
}
$flink->profilesync = 0; // XXX: leave as default?
}
} }

View File

@ -28,6 +28,13 @@ define('MAX_AVATAR_SIZE', 256 * 1024);
define('NOTICES_PER_PAGE', 20); define('NOTICES_PER_PAGE', 20);
define('FOREIGN_NOTICE_SEND', 1);
define('FOREIGN_NOTICE_RECV', 2);
define('FOREIGN_NOTICE_SEND_REPLY', 4);
define('FOREIGN_FRIEND_SEND', 1);
define('FOREIGN_FRIEND_RECV', 2);
define_syslog_variables(); define_syslog_variables();
# append our extlib dir as the last-resort place to find libs # append our extlib dir as the last-resort place to find libs

View File

@ -1110,17 +1110,12 @@ 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 & FOREIGN_NOTICE_SEND) {
$ok_to_send = true;
// If it's not a reply, or if the user WANTS to send replies...
// Check to see whether user wants to filter @-replies
if ($flink->noticesync == 3) { if (!$notice->reply_to || ($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY)) {
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); $result = common_twitter_broadcast($notice, $flink);
if (!$result) { if (!$result) {