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