/**
* 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']);
- if (!empty($contact['baseurl'])) {
- $server = $contact['baseurl'];
- }
+ 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', 'network'], $condition);
+ if (empty($gserver)) {
+ $reachable = true;
+ } else {
+ $reachable = !$gserver['failed'] && in_array($gserver['network'], Protocol::FEDERATED);
+ $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;
}
/**