]> git.mxchange.org Git - friendica.git/blob - src/Worker/PollContacts.php
Merge pull request #9578 from annando/onepoll-failed
[friendica.git] / src / Worker / PollContacts.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Worker;
23
24 use Friendica\Core\Logger;
25 use Friendica\Core\Protocol;
26 use Friendica\Core\Worker;
27 use Friendica\Database\DBA;
28 use Friendica\DI;
29 use Friendica\Model\Contact;
30 use Friendica\Util\DateTimeFormat;
31
32 /**
33  * Poll contacts for unreceived messages
34  */
35 class PollContacts
36 {
37         public static function execute()
38         {
39                 $abandon_days = intval(DI::config()->get('system', 'account_abandon_days'));
40                 if ($abandon_days < 1) {
41                         $abandon_days = 0;
42                 }
43
44                 $condition = ['network' => [Protocol::DFRN, Protocol::ACTIVITYPUB, Protocol::OSTATUS, Protocol::FEED, 
45                         Protocol::MAIL, Protocol::ZOT, Protocol::PHANTOM], 'self' => false, 'blocked' => false];
46
47                 if (!empty($abandon_days)) {
48                         $condition = DBA::mergeConditions($condition,
49                                 ["`uid` != ? AND `uid` IN (SELECT `uid` FROM `user` WHERE NOT `account_expired` AND NOT `account_removed`  AND `login_date` > UTC_TIMESTAMP() - INTERVAL ? DAY)", 0, $abandon_days]);
50                 } else  {
51                         $condition = DBA::mergeConditions($condition,
52                                 ["`uid` != ? AND `uid` IN (SELECT `uid` FROM `user` WHERE NOT `account_expired` AND NOT `account_removed`)", 0]);
53                 }
54
55                 $contacts = DBA::select('contact', ['id', 'nick', 'name', 'network', 'archive', 'last-update', 'priority', 'rating'], $condition);
56                 if (!DBA::isResult($contacts)) {
57                         return;
58                 }
59
60                 while ($contact = DBA::fetch($contacts)) {
61                         if (in_array($contact['network'], [Protocol::MAIL, Protocol::FEED])) {
62                                 $ratings = [0, 3, 7, 8, 9, 10];
63                                 if (DI::config()->get('system', 'adjust_poll_frequency') && ($contact['network'] == Protocol::FEED)) {
64                                         $rating = $contact['rating'];
65                                 } elseif (array_key_exists($contact['priority'], $ratings)) {
66                                         $rating = $ratings[$contact['priority']];
67                                 } else {
68                                         $rating = -1;
69                                 }
70                         } else {
71                                 // Check once a week per default for all other networks
72                                 $rating = 9;
73                         }
74
75                         // Friendica and OStatus are checked once a day
76                         if (in_array($contact['network'], [Protocol::DFRN, Protocol::OSTATUS])) {
77                                 $rating = 8;
78                         }
79
80                         // Check archived contacts or contacts with unsupported protocols once a month
81                         if ($contact['archive'] || in_array($contact['network'], [Protocol::ZOT, Protocol::PHANTOM])) {
82                                 $rating = 10;
83                         }
84
85                         if ($rating < 0) {
86                                 continue;
87                         }
88                         /*
89                          * Based on $contact['priority'], should we poll this site now? Or later?
90                          */
91
92                         $min_poll_interval = DI::config()->get('system', 'min_poll_interval');
93
94                         $poll_intervals = [$min_poll_interval . ' minute', '15 minute', '30 minute',
95                                 '1 hour', '2 hour', '3 hour', '6 hour', '12 hour' ,'1 day', '1 week', '1 month'];
96
97                         $now = DateTimeFormat::utcNow();
98                         $next_update = DateTimeFormat::utc($contact['last-update'] . ' + ' . $poll_intervals[$rating]);
99
100                         if (empty($poll_intervals[$rating]) || ($now < $next_update))  {
101                                 Logger::debug('No update', ['cid' => $contact['id'], 'rating' => $rating, 'next' => $next_update, 'now' => $now]);
102                                 continue;
103                         }
104
105                         if ((($contact['network'] == Protocol::FEED) && ($contact['priority'] <= 3)) || ($contact['network'] == Protocol::MAIL)) {
106                                 $priority = PRIORITY_MEDIUM;
107                         } elseif ($contact['archive']) {
108                                 $priority = PRIORITY_NEGLIGIBLE;
109                         } else {
110                                 $priority = PRIORITY_LOW;
111                         }
112
113                         Logger::notice("Polling " . $contact["network"] . " " . $contact["id"] . " " . $contact['priority'] . " " . $contact["nick"] . " " . $contact["name"]);
114
115                         Worker::add(['priority' => $priority, 'dont_fork' => true, 'force_priority' => true], 'OnePoll', (int)$contact['id']);
116                 }
117                 DBA::close($contacts);
118         }
119 }