/**
* Checks if the given server is reachable
*
- * @param string $profile URL of the given profile
- * @param string $server URL of the given server (If empty, taken from profile)
- * @param string $network Network value that is used, when detection failed
- * @param boolean $force Force an update.
+ * @param array $contact Contact that should be checked
*
* @return boolean 'true' if server seems vital
*/
- public static function reachable(string $profile, string $server = '', string $network = '', bool $force = false): bool
+ public static function reachable(array $contact): bool
{
- if ($server == '') {
- $contact = Contact::getByURL($profile, null, ['baseurl', 'network']);
- if (!empty($contact['baseurl'])) {
- $server = $contact['baseurl'];
- } elseif ($contact['network'] == Protocol::DIASPORA) {
- $parts = parse_url($profile);
- unset($parts['path']);
- $server = (string)Uri::fromParts($parts);
- }
+ if (!empty($contact['gsid'])) {
+ $gsid = $contact['gsid'];
+ } elseif (!empty($contact['baseurl'])) {
+ $server = $contact['baseurl'];
+ } elseif ($contact['network'] == Protocol::DIASPORA) {
+ $parts = parse_url($contact['url']);
+ unset($parts['path']);
+ $server = (string)Uri::fromParts($parts);
+ } else {
+ return true;
}
- if ($server == '') {
- return true;
+ if (!empty($gsid)) {
+ $condition = ['id' => $gsid];
+ } else {
+ $condition = ['nurl' => Strings::normaliseLink($server)];
+ }
+
+ $gserver = DBA::selectFirst('gserver', ['url', 'next_contact', 'failed'], $condition);
+ if (empty($gserver)) {
+ $reachable = true;
+ } else {
+ $reachable = !$gserver['failed'];
+ $server = $gserver['url'];
+ }
+
+ if (!empty($server) && (empty($gserver) || strtotime($gserver['next_contact']) < time())) {
+ Worker::add(Worker::PRIORITY_LOW, 'UpdateGServer', $server, false);
}
- return self::check($server, $network, $force);
+ return $reachable;
}
/**
$uid = $target_id;
$condition = ['uid' => $target_id, 'self' => false, 'network' => [Protocol::DFRN, Protocol::DIASPORA]];
- $delivery_contacts_stmt = DBA::select('contact', ['id', 'url', 'addr', 'network', 'protocol', 'baseurl', 'batch'], $condition);
+ $delivery_contacts_stmt = DBA::select('contact', ['id', 'url', 'addr', 'network', 'protocol', 'baseurl', 'gsid', 'batch'], $condition);
} else {
$post = Post::selectFirst(['id'], ['uri-id' => $post_uriid, 'uid' => $sender_uid]);
if (!DBA::isResult($post)) {
if (!empty($networks)) {
$condition['network'] = $networks;
}
- $delivery_contacts_stmt = DBA::select('contact', ['id', 'addr', 'url', 'network', 'protocol', 'baseurl', 'batch'], $condition);
+ $delivery_contacts_stmt = DBA::select('contact', ['id', 'addr', 'url', 'network', 'protocol', 'baseurl', 'gsid', 'batch'], $condition);
}
$conversants = [];
if ($diaspora_delivery && !$unlisted) {
$batch_delivery = true;
- $participants = DBA::selectToArray('contact', ['batch', 'network', 'protocol', 'baseurl', 'id', 'url', 'name'],
+ $participants = DBA::selectToArray('contact', ['batch', 'network', 'protocol', 'baseurl', 'gsid', 'id', 'url', 'name'],
["`network` = ? AND `batch` != '' AND `uid` = ? AND `rel` != ? AND NOT `blocked` AND NOT `pending` AND NOT `archive`", Protocol::DIASPORA, $owner['uid'], Contact::SHARING],
['group_by' => ['batch', 'network', 'protocol']]);
$condition = ['network' => Protocol::DFRN, 'uid' => $owner['uid'], 'blocked' => false,
'pending' => false, 'archive' => false, 'rel' => [Contact::FOLLOWER, Contact::FRIEND]];
- $contacts = DBA::selectToArray('contact', ['id', 'url', 'addr', 'name', 'network', 'protocol', 'baseurl'], $condition);
+ $contacts = DBA::selectToArray('contact', ['id', 'url', 'addr', 'name', 'network', 'protocol', 'baseurl', 'gsid'], $condition);
$conversants = array_merge($contacts, $participants);
continue;
}
- if (!GServer::reachable($contact['url'], $contact['baseurl'], $contact['network'])) {
+ if (!GServer::reachable($contact)) {
Logger::info('Server is not reachable', ['id' => $post_uriid, 'uid' => $sender_uid, 'contact' => $contact]);
continue;
}