diff --git a/include/delivery.php b/include/delivery.php index 8318be4dd..a9e629fcf 100644 --- a/include/delivery.php +++ b/include/delivery.php @@ -1,5 +1,6 @@ get_curl_headers(),'retry-after')))) { logger('diaspora_transmit: queue message'); // queue message for redelivery - q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`,`batch`) - VALUES ( %d, '%s', '%s', '%s', %d) ", - intval($contact['id']), - dbesc(datetime_convert()), - dbesc(datetime_convert()), - dbesc($slap), - intval($public_batch) - ); + add_to_queue($contact['id'],NETWORK_DIASPORA,$slap,$public_batch); } diff --git a/include/notifier.php b/include/notifier.php index 18ad07012..d12119b05 100644 --- a/include/notifier.php +++ b/include/notifier.php @@ -1,6 +1,7 @@ $handle, '$first' => $first, @@ -100,14 +100,6 @@ function profile_change() { $msgtosend = diaspora_msg_build($msg,$a->user,null,$a->user['prvkey'],null,true); foreach($recips as $recip) { - q("insert into queue (`cid`,`network`,`created`,`last`,`content`,`batch`) - values(%d,'%s','%s','%s','%s',%d)", - intval($recip['id']), - dbesc(NETWORK_DIASPORA), - dbesc(datetime_convert()), - dbesc(datetime_convert()), - dbesc($msgtosend), - intval(1) - ); + add_to_queue($recip['id'],NETWORK_DIASPORA,$msgtosend,true); } -} \ No newline at end of file +} diff --git a/include/queue_fn.php b/include/queue_fn.php index bc47ceffd..2168d42d7 100644 --- a/include/queue_fn.php +++ b/include/queue_fn.php @@ -14,3 +14,38 @@ function remove_queue_item($id) { intval($id) ); } + + +function add_to_queue($cid,$network,$msg,$batch = false) { + + $max_queue = get_config('system','max_contact_queue'); + if($max_queue < 1) + $max_queue = 500; + + $batch_queue = get_config('system','max_batch_queue'); + if($batch_queue < 1) + $batch_queue = 1000; + + $r = q("SELECT COUNT(*) AS `total` FROM `queue` left join `contact` WHERE ``queue`.`cid` = %d AND `contact`.`self` = 0 ", + intval($cid) + ); + if($r && count($r)) { + if($batch && ($r[0]['total'] > $batch_queue)) { + logger('add_to_queue: too many queued items for batch server ' . $cid . ' - discarding message'); + return; + } + elseif((! $batch) && ($r[0]['total'] > $max_queue)) { + logger('add_to_queue: too many queued items for contact ' . $cid . ' - discarding message'); + return; + } + } + + q("INSERT INTO `queue` ( `cid`, `network`, `created`, `last`, `content`, `batch`) + VALUES ( %d, '%s', '%s', '%s', '%s', %d) ", + intval($cid), + dbesc(datetime_convert()), + dbesc(datetime_convert()), + dbesc($msg), + intval(($batch) ? 1: 0) + ); +}