From edcf1466a749d59bc3bba8e5d1dc48508e10521a Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 11 Apr 2018 19:01:25 +0000 Subject: [PATCH] Relay: Avoid sending relay posts to servers that already received content --- src/Protocol/Diaspora.php | 29 +++++++++++++++++++++-------- src/Worker/Notifier.php | 22 ++++++++-------------- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index 0e5f0c27f..f8e4c11b2 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -49,10 +49,12 @@ class Diaspora * * The list contains not only the official relays but also servers that we serve directly * - * @param integer $item_id The id of the item that is sent + * @param integer $item_id The id of the item that is sent + * @param array $contacts The previously fetched contacts + * * @return array of relay servers */ - public static function relayList($item_id) + public static function relayList($item_id, $contacts = []) { $serverlist = []; @@ -99,15 +101,26 @@ class Diaspora } // Now we are collecting all relay contacts - $contacts = []; foreach ($serverlist as $server_url) { // We don't send messages to ourselves - if (!link_compare($server_url, System::baseUrl())) { - $cid = self::getRelayContactId($server_url); - if (!is_bool($cid)) { - $contacts[] = $cid; + if (link_compare($server_url, System::baseUrl())) { + continue; + } + $contact = self::getRelayContact($server_url); + if (is_bool($contact)) { + continue; + } + + $exists = false; + foreach ($contacts as $entry) { + if ($entry['batch'] == $contact['batch']) { + $exists = true; } } + + if (!$exists) { + $contacts[] = $contact; + } } return $contacts; @@ -119,7 +132,7 @@ class Diaspora * @param string $server_url The url of the server * @return array with the contact */ - private static function getRelayContactId($server_url) + private static function getRelayContact($server_url) { $batch = $server_url . '/receive/public'; diff --git a/src/Worker/Notifier.php b/src/Worker/Notifier.php index e4e720236..422adafed 100644 --- a/src/Worker/Notifier.php +++ b/src/Worker/Notifier.php @@ -479,15 +479,9 @@ class Notifier { if ($public_message) { - - $r0 = []; $r1 = []; if ($diaspora_delivery) { - if (!$followup) { - $r0 = Diaspora::relayList($item_id); - } - $r1 = q("SELECT `batch`, ANY_VALUE(`id`) AS `id`, ANY_VALUE(`name`) AS `name`, ANY_VALUE(`network`) AS `network` FROM `contact` WHERE `network` = '%s' AND `batch` != '' AND `uid` = %d AND `rel` != %d AND NOT `blocked` AND NOT `pending` AND NOT `archive` GROUP BY `batch`", @@ -500,17 +494,17 @@ class Notifier { // The function will ensure that there are no duplicates $r1 = Diaspora::participantsForThread($item_id, $r1); + // Add the relay to the list, avoid duplicates + if (!$followup) { + $r1 = Diaspora::relayList($item_id, $r1); + } } - $r2 = q("SELECT `id`, `name`,`network` FROM `contact` - WHERE `network` in ('%s') AND `uid` = %d AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `rel` != %d", - dbesc(NETWORK_DFRN), - intval($owner['uid']), - intval(CONTACT_IS_SHARING) - ); + $condition = ['network' => NETWORK_DFRN, 'uid' => $owner['uid'], 'blocked' => false, + 'pending' => false, 'archive' => false, 'rel' => [CONTACT_IS_FOLLOWER, CONTACT_IS_FRIEND]]; + $r2 = dba::inArray(dba::select('contact', ['id', 'name', 'network'], $condition)); - - $r = array_merge($r2, $r1, $r0); + $r = array_merge($r2, $r1); if (DBM::is_result($r)) { logger('pubdeliver '.$target_item["guid"].': '.print_r($r,true), LOGGER_DEBUG);