Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.2 KiB

10 years ago
10 years ago
10 years ago
  1. <?php
  2. function update_queue_time($id) {
  3. logger('queue: requeue item ' . $id);
  4. q("UPDATE `queue` SET `last` = '%s' WHERE `id` = %d",
  5. dbesc(datetime_convert()),
  6. intval($id)
  7. );
  8. }
  9. function remove_queue_item($id) {
  10. logger('queue: remove queue item ' . $id);
  11. q("DELETE FROM `queue` WHERE `id` = %d",
  12. intval($id)
  13. );
  14. }
  15. /**
  16. * @brief Checks if the communication with a given contact had problems recently
  17. *
  18. * @param int $cid Contact id
  19. *
  20. * @return bool The communication with this contact has currently problems
  21. */
  22. function was_recently_delayed($cid) {
  23. $was_delayed = false;
  24. // Are there queue entries that were recently added?
  25. $r = q("SELECT `id` FROM `queue` WHERE `cid` = %d
  26. AND `last` > UTC_TIMESTAMP() - INTERVAL 15 MINUTE LIMIT 1",
  27. intval($cid)
  28. );
  29. $was_delayed = dbm::is_result($r);
  30. // We set "term-date" to a current date if the communication has problems.
  31. // If the communication works again we reset this value.
  32. if ($was_delayed) {
  33. $r = q("SELECT `term-date` FROM `contact` WHERE `id` = %d AND `term-date` <= '1000-01-01' LIMIT 1",
  34. intval($cid)
  35. );
  36. $was_delayed = !dbm::is_result($r);
  37. }
  38. return $was_delayed;
  39. }
  40. function add_to_queue($cid,$network,$msg,$batch = false) {
  41. $max_queue = get_config('system','max_contact_queue');
  42. if($max_queue < 1)
  43. $max_queue = 500;
  44. $batch_queue = get_config('system','max_batch_queue');
  45. if($batch_queue < 1)
  46. $batch_queue = 1000;
  47. $r = q("SELECT COUNT(*) AS `total` FROM `queue` INNER JOIN `contact` ON `queue`.`cid` = `contact`.`id`
  48. WHERE `queue`.`cid` = %d AND `contact`.`self` = 0 ",
  49. intval($cid)
  50. );
  51. if (dbm::is_result($r)) {
  52. if($batch && ($r[0]['total'] > $batch_queue)) {
  53. logger('add_to_queue: too many queued items for batch server ' . $cid . ' - discarding message');
  54. return;
  55. }
  56. elseif((! $batch) && ($r[0]['total'] > $max_queue)) {
  57. logger('add_to_queue: too many queued items for contact ' . $cid . ' - discarding message');
  58. return;
  59. }
  60. }
  61. q("INSERT INTO `queue` ( `cid`, `network`, `created`, `last`, `content`, `batch`)
  62. VALUES ( %d, '%s', '%s', '%s', '%s', %d) ",
  63. intval($cid),
  64. dbesc($network),
  65. dbesc(datetime_convert()),
  66. dbesc(datetime_convert()),
  67. dbesc($msg),
  68. intval(($batch) ? 1: 0)
  69. );
  70. }