]> git.mxchange.org Git - friendica.git/blob - src/Worker/PollContacts.php
e73801f2b6ec42728d2732d960f1d4f61ed1b6ba
[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\Addon;
25 use Friendica\Core\Logger;
26 use Friendica\Core\Protocol;
27 use Friendica\Core\Worker;
28 use Friendica\Database\DBA;
29 use Friendica\DI;
30 use Friendica\Model\Contact;
31 use Friendica\Util\DateTimeFormat;
32
33 /**
34  * Poll contacts for unreceived messages
35  */
36 class PollContacts
37 {
38         public static function execute()
39         {
40                 Addon::reload();
41
42                 $sql = "SELECT `contact`.`id`, `contact`.`nick`, `contact`.`name`, `contact`.`network`, `contact`.`archive`,
43                                         `contact`.`last-update`, `contact`.`priority`, `contact`.`rating`, `contact`.`rel`, `contact`.`subhub`
44                                 FROM `user`
45                                 STRAIGHT_JOIN `contact`
46                                 ON `contact`.`uid` = `user`.`uid` AND `contact`.`poll` != ''
47                                         AND `contact`.`network` IN (?, ?, ?, ?, ?)
48                                         AND NOT `contact`.`self` AND NOT `contact`.`blocked`
49                                         AND `contact`.`rel` != ?
50                                 WHERE NOT `user`.`account_expired` AND NOT `user`.`account_removed`";
51
52                 $parameters = [Protocol::DFRN, Protocol::ACTIVITYPUB, Protocol::OSTATUS, Protocol::FEED, Protocol::MAIL, Contact::FOLLOWER];
53
54                 // Only poll from those with suitable relationships,
55                 // and which have a polling address and ignore Diaspora since
56                 // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
57                 $abandon_days = intval(DI::config()->get('system', 'account_abandon_days'));
58                 if ($abandon_days < 1) {
59                         $abandon_days = 0;
60                 }
61
62                 if (!empty($abandon_days)) {
63                         $sql .= " AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL ? DAY";
64                         $parameters[] = $abandon_days;
65                 }
66
67                 $contacts = DBA::p($sql, $parameters);
68
69                 if (!DBA::isResult($contacts)) {
70                         return;
71                 }
72
73                 while ($contact = DBA::fetch($contacts)) {
74                         $ratings = [0, 3, 7, 8, 9, 10];
75                         if (DI::config()->get('system', 'adjust_poll_frequency') && ($contact['network'] == Protocol::FEED)) {
76                                 $rating = $contact['rating'];
77                         } elseif (array_key_exists($contact['priority'], $ratings)) {
78                                 $rating = $ratings[$contact['priority']];
79                         } else {
80                                 $rating = -1;
81                         }
82
83                         // Friendica and OStatus are checked once a day
84                         if (in_array($contact['network'], [Protocol::DFRN, Protocol::OSTATUS])) {
85                                 $rating = 8;
86                         }
87
88                         // ActivityPub is checked once a week
89                         if ($contact['network'] == Protocol::ACTIVITYPUB) {
90                                 $rating = 9;
91                         }
92
93                         // Check archived contacts once a month
94                         if ($contact['archive']) {
95                                 $rating = 10;
96                         }
97
98                         if ($rating < 0) {
99                                 continue;
100                         }
101                         /*
102                          * Based on $contact['priority'], should we poll this site now? Or later?
103                          */
104
105                         $min_poll_interval = DI::config()->get('system', 'min_poll_interval');
106
107                         $poll_intervals = [$min_poll_interval . ' minute', '15 minute', '30 minute',
108                                 '1 hour', '2 hour', '3 hour', '6 hour', '12 hour' ,'1 day', '1 week', '1 month'];
109
110                         $now = DateTimeFormat::utcNow();
111                         $next_update = DateTimeFormat::utc($contact['last-update'] . ' + ' . $poll_intervals[$rating]);
112
113                         if (empty($poll_intervals[$rating]) || ($now < $next_update))  {
114                                 Logger::debug('No update', ['cid' => $contact['id'], 'rating' => $rating, 'next' => $next_update, 'now' => $now]);
115                                 continue;
116                         }
117
118                         if ((($contact['network'] == Protocol::FEED) && ($contact['priority'] <= 3)) || ($contact['network'] == Protocol::MAIL)) {
119                                 $priority = PRIORITY_MEDIUM;
120                         } elseif ($contact['archive']) {
121                                 $priority = PRIORITY_NEGLIGIBLE;
122                         } else {
123                                 $priority = PRIORITY_LOW;
124                         }
125
126                         Logger::notice("Polling " . $contact["network"] . " " . $contact["id"] . " " . $contact['priority'] . " " . $contact["nick"] . " " . $contact["name"]);
127
128                         Worker::add(['priority' => $priority, 'dont_fork' => true, 'force_priority' => true], 'OnePoll', (int)$contact['id']);
129                 }
130                 DBA::close($contacts);
131         }
132 }