Merge pull request #4817 from annando/relais-duplicates

Avoid sending relay posts to servers that already received content
This commit is contained in:
Hypolite Petovan 2018-04-11 19:00:34 -04:00 committed by GitHub
commit 9d928417a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 22 deletions

View File

@ -49,10 +49,12 @@ class Diaspora
* *
* The list contains not only the official relays but also servers that we serve directly * 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 * @return array of relay servers
*/ */
public static function relayList($item_id) public static function relayList($item_id, $contacts = [])
{ {
$serverlist = []; $serverlist = [];
@ -99,15 +101,26 @@ class Diaspora
} }
// Now we are collecting all relay contacts // Now we are collecting all relay contacts
$contacts = [];
foreach ($serverlist as $server_url) { foreach ($serverlist as $server_url) {
// We don't send messages to ourselves // We don't send messages to ourselves
if (!link_compare($server_url, System::baseUrl())) { if (link_compare($server_url, System::baseUrl())) {
$cid = self::getRelayContactId($server_url); continue;
if (!is_bool($cid)) { }
$contacts[] = $cid; $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; return $contacts;
@ -119,7 +132,7 @@ class Diaspora
* @param string $server_url The url of the server * @param string $server_url The url of the server
* @return array with the contact * @return array with the contact
*/ */
private static function getRelayContactId($server_url) private static function getRelayContact($server_url)
{ {
$batch = $server_url . '/receive/public'; $batch = $server_url . '/receive/public';

View File

@ -479,15 +479,9 @@ class Notifier {
if ($public_message) { if ($public_message) {
$r0 = [];
$r1 = []; $r1 = [];
if ($diaspora_delivery) { 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` $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` != '' 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`", 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 // The function will ensure that there are no duplicates
$r1 = Diaspora::participantsForThread($item_id, $r1); $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` $condition = ['network' => NETWORK_DFRN, 'uid' => $owner['uid'], 'blocked' => false,
WHERE `network` in ('%s') AND `uid` = %d AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `rel` != %d", 'pending' => false, 'archive' => false, 'rel' => [CONTACT_IS_FOLLOWER, CONTACT_IS_FRIEND]];
dbesc(NETWORK_DFRN), $r2 = dba::inArray(dba::select('contact', ['id', 'name', 'network'], $condition));
intval($owner['uid']),
intval(CONTACT_IS_SHARING)
);
$r = array_merge($r2, $r1);
$r = array_merge($r2, $r1, $r0);
if (DBM::is_result($r)) { if (DBM::is_result($r)) {
logger('pubdeliver '.$target_item["guid"].': '.print_r($r,true), LOGGER_DEBUG); logger('pubdeliver '.$target_item["guid"].': '.print_r($r,true), LOGGER_DEBUG);