edit throttling

darcs-hash:20081210174722-84dde-4c79d7f73230d008195bd19738bc9a6017b940e9.gz
This commit is contained in:
Evan Prodromou 2008-12-10 12:47:22 -05:00
parent 1c0a19b457
commit a143b64666
5 changed files with 98 additions and 56 deletions

View File

@ -93,6 +93,11 @@ class Notice extends Memcached_DataObject
static function saveNew($profile_id, $content, $source=NULL, $is_local=1, $reply_to=NULL, $uri=NULL) { static function saveNew($profile_id, $content, $source=NULL, $is_local=1, $reply_to=NULL, $uri=NULL) {
if (!Notice::checkEditThrottle($profile_id)) {
common_log(LOG_WARNING, 'Excessive posting by profile #' . $profile_id . '; throttled.');
return _('Too many notices too fast; take a breather and post again in a few minutes.');
}
$notice = new Notice(); $notice = new Notice();
$notice->profile_id = $profile_id; $notice->profile_id = $profile_id;
@ -147,6 +152,24 @@ class Notice extends Memcached_DataObject
return $notice; return $notice;
} }
static function checkEditThrottle($profile_id) {
$profile = Profile::staticGet($profile_id);
if (!$profile) {
return false;
}
# Get the Nth notice
$notice = $profile->getNotices(common_config('throttle', 'count') - 1, 1);
if ($notice && $notice->fetch()) {
# If the Nth notice was posted less than timespan seconds ago
if (time() - strtotime($notice->created) <= common_config('throttle', 'timespan')) {
# Then we throttle
return false;
}
}
# Either not N notices in the stream, OR the Nth was not posted within timespan seconds
return true;
}
function blowCaches($blowLast=false) { function blowCaches($blowLast=false) {
$this->blowSubsCache($blowLast); $this->blowSubsCache($blowLast);
$this->blowNoticeCache($blowLast); $this->blowNoticeCache($blowLast);
@ -197,9 +220,9 @@ class Notice extends Memcached_DataObject
if ($this->is_local) { if ($this->is_local) {
$cache = common_memcache(); $cache = common_memcache();
if ($cache) { if ($cache) {
$cache->delete(common_cache_key('user:notices:'.$this->profile_id)); $cache->delete(common_cache_key('profile:notices:'.$this->profile_id));
if ($blowLast) { if ($blowLast) {
$cache->delete(common_cache_key('user:notices:'.$this->profile_id.';last')); $cache->delete(common_cache_key('profile:notices:'.$this->profile_id.';last'));
} }
} }
} }

View File

@ -145,4 +145,15 @@ class Profile extends Memcached_DataObject
} }
return NULL; return NULL;
} }
function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0) {
$qry =
'SELECT * ' .
'FROM notice ' .
'WHERE profile_id = %d ';
return Notice::getStream(sprintf($qry, $this->id),
'profile:notices:'.$this->id,
$offset, $limit, $since_id, $before_id);
}
} }

View File

@ -342,14 +342,12 @@ class User extends Memcached_DataObject
} }
function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0) { function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0) {
$qry = $profile = $this->getProfile();
'SELECT * ' . if (!$profile) {
'FROM notice ' . return NULL;
'WHERE profile_id = %d '; } else {
return $profile->getNotices($offset, $limit, $since_id, $before_id);
return Notice::getStream(sprintf($qry, $this->id), }
'user:notices:'.$this->id,
$offset, $limit, $since_id, $before_id);
} }
function favoriteNotices($offset=0, $limit=NOTICES_PER_PAGE) { function favoriteNotices($offset=0, $limit=NOTICES_PER_PAGE) {

View File

@ -61,7 +61,6 @@ $config['sphinx']['enabled'] = false;
$config['sphinx']['server'] = 'localhost'; $config['sphinx']['server'] = 'localhost';
$config['sphinx']['port'] = 3312; $config['sphinx']['port'] = 3312;
# Users to populate the 'Featured' tab # Users to populate the 'Featured' tab
#$config['nickname']['featured'][] = 'scobleizer'; #$config['nickname']['featured'][] = 'scobleizer';
@ -93,7 +92,6 @@ $config['sphinx']['port'] = 3312;
#For incoming email, if enabled. Defaults to site server name. #For incoming email, if enabled. Defaults to site server name.
#$config['mail']['domain'] = 'incoming.example.net'; #$config['mail']['domain'] = 'incoming.example.net';
#exponential decay factor for tags, default 10 days #exponential decay factor for tags, default 10 days
#raise this if traffic is slow, lower it if it's fast #raise this if traffic is slow, lower it if it's fast
#$config['tag']['dropoff'] = 86400.0 * 10; #$config['tag']['dropoff'] = 86400.0 * 10;
@ -124,3 +122,11 @@ $config['sphinx']['port'] = 3312;
#Twitter integration source attribute. Note: default is Laconica #Twitter integration source attribute. Note: default is Laconica
#$config['integration']['source'] = 'Laconica'; #$config['integration']['source'] = 'Laconica';
# Edit throttling. Off by default. If turned on, you can only post 20 notices
# every 10 minutes. Admins may want to play with the settings to minimize inconvenience for
# real users without getting uncontrollable floods from spammers or runaway bots.
#$config['throttle']['enabled'] = true;
#$config['throttle']['count'] = 100;
#$config['throttle']['timespan'] = 3600;

View File

@ -92,6 +92,10 @@ $config =
'blacklist' => array()), 'blacklist' => array()),
'theme' => 'theme' =>
array('server' => NULL), array('server' => NULL),
'throttle' =>
array('enabled' => false, // whether to throttle edits; false by default
'count' => 20, // number of allowed messages in timespan
'timespan' => 600), // timespan for throttling
'xmpp' => 'xmpp' =>
array('enabled' => false, array('enabled' => false,
'server' => 'INVALID SERVER', 'server' => 'INVALID SERVER',