X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FProtocol%2FDiaspora.php;h=9e074e8a9a1c3a0b820938151fe7d18ac9ce6dae;hb=95827140891f45646ab53c6a850a3ea2389ce6e9;hp=50300d136c25e65704c5016551dcd3cb6e1fec00;hpb=53ffe5a2e18034c914faa24a8ed01126310aeffc;p=friendica.git diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index 50300d136c..9e074e8a9a 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -27,6 +27,7 @@ use Friendica\Model\Conversation; use Friendica\Model\GContact; use Friendica\Model\Group; use Friendica\Model\Item; +use Friendica\Model\ItemDeliveryData; use Friendica\Model\Mail; use Friendica\Model\Profile; use Friendica\Model\User; @@ -46,6 +47,36 @@ use SimpleXMLElement; */ class Diaspora { + /** + * Mark the relay contact of the given contact for archival + * This is called whenever there is a communication issue with the server. + * It avoids sending stuff to servers who don't exist anymore. + * The relay contact is a technical contact entry that exists once per server. + * + * @param array $contact of the relay contact + */ + public static function markRelayForArchival(array $contact) + { + if (!empty($contact['contact-type']) && ($contact['contact-type'] == Contact::TYPE_RELAY)) { + // This is already the relay contact, we don't need to fetch it + $relay_contact = $contact; + } elseif (empty($contact['baseurl'])) { + if (!empty($contact['batch'])) { + $condition = ['uid' => 0, 'network' => Protocol::FEDERATED, 'batch' => $contact['batch'], 'contact-type' => Contact::TYPE_RELAY]; + $relay_contact = DBA::selectFirst('contact', [], $condition); + } else { + return; + } + } else { + $relay_contact = self::getRelayContact($contact['baseurl'], []); + } + + if (!empty($relay_contact)) { + Logger::info('Relay contact will be marked for archival', ['id' => $relay_contact['id'], 'url' => $relay_contact['url']]); + Contact::markForArchival($relay_contact); + } + } + /** * @brief Return a list of relay servers * @@ -140,13 +171,12 @@ class Diaspora * @brief Return a contact for a given server address or creates a dummy entry * * @param string $server_url The url of the server + * @param array $fields Fieldlist * @return array with the contact * @throws \Exception */ - private static function getRelayContact($server_url) + private static function getRelayContact(string $server_url, array $fields = ['batch', 'id', 'name', 'network', 'protocol', 'archive', 'blocked']) { - $fields = ['batch', 'id', 'name', 'network', 'protocol', 'archive', 'blocked']; - // Fetch the relay contact $condition = ['uid' => 0, 'nurl' => Strings::normaliseLink($server_url), 'contact-type' => Contact::TYPE_RELAY]; @@ -200,7 +230,7 @@ class Diaspora DBA::update('contact', $fields, $condition, $old); } else { Logger::info('Create relay contact', ['fields' => $fields]); - DBA::insert('contact', $fields); + Contact::insert($fields); } } @@ -223,7 +253,7 @@ class Diaspora `fcontact`.`batch` AS `fbatch`, `fcontact`.`network` AS `fnetwork` FROM `participation` INNER JOIN `contact` ON `contact`.`id` = `participation`.`cid` INNER JOIN `fcontact` ON `fcontact`.`id` = `participation`.`fid` - WHERE `participation`.`iid` = ?", $thread); + WHERE `participation`.`iid` = ? AND NOT `contact`.`archive`", $thread); while ($contact = DBA::fetch($r)) { if (!empty($contact['fnetwork'])) { @@ -942,31 +972,41 @@ class Diaspora * @brief Fetches data for a given handle * * @param string $handle The handle + * @param boolean $update true = always update, false = never update, null = update when not found or outdated * * @return array the queried data * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \ImagickException */ - public static function personByHandle($handle) + public static function personByHandle($handle, $update = null) { - $update = false; - $person = DBA::selectFirst('fcontact', [], ['network' => Protocol::DIASPORA, 'addr' => $handle]); + if (!DBA::isResult($person)) { + $urls = [$handle, str_replace('http://', 'https://', $handle), Strings::normaliseLink($handle)]; + $person = DBA::selectFirst('fcontact', [], ['network' => Protocol::DIASPORA, 'url' => $urls]); + } + if (DBA::isResult($person)) { Logger::debug("In cache " . print_r($person, true)); - // update record occasionally so it doesn't get stale - $d = strtotime($person["updated"]." +00:00"); - if ($d < strtotime("now - 14 days")) { - $update = true; - } + if (is_null($update)) { + // update record occasionally so it doesn't get stale + $d = strtotime($person["updated"]." +00:00"); + if ($d < strtotime("now - 14 days")) { + $update = true; + } - if ($person["guid"] == "") { - $update = true; + if ($person["guid"] == "") { + $update = true; + } } + } elseif (is_null($update)) { + $update = !DBA::isResult($person); + } else { + $person = []; } - if (!DBA::isResult($person) || $update) { + if ($update) { Logger::log("create or refresh", Logger::DEBUG); $r = Probe::uri($handle, Protocol::DIASPORA); @@ -975,12 +1015,7 @@ class Diaspora if ($r && ($r["network"] === Protocol::DIASPORA)) { self::updateFContact($r); - // Fetch the updated or added contact - $person = DBA::selectFirst('fcontact', [], ['network' => Protocol::DIASPORA, 'addr' => $handle]); - if (!DBA::isResult($person)) { - $person = $r; - $person['id'] = 0; - } + $person = self::personByHandle($handle, false); } } @@ -1117,6 +1152,20 @@ class Diaspora return $contact; } + /** + * Checks if the given contact url does support ActivityPub + * + * @param string $url profile url + * @param boolean $update true = always update, false = never update, null = update when not found or outdated + * @return boolean + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + * @throws \ImagickException + */ + public static function isSupportedByContactUrl($url, $update = null) + { + return !empty(self::personByHandle($url, $update)); + } + /** * @brief Check if posting is allowed for this contact * @@ -1425,23 +1474,21 @@ class Diaspora */ public static function fetchByURL($url, $uid = 0) { - if (!preg_match("=([http|https].*)/(.*)/(.*)=ism", $url, $matches)) { + // Check for Diaspora (and Friendica) typical paths + if (!preg_match("=(https?://.+)/(?:posts|display)/([a-zA-Z0-9-_@.:%]+[a-zA-Z0-9])=i", $url, $matches)) { return false; } - // Check for Diaspora (and Friendica) typical path components - if (!in_array($matches[2], ['posts', 'display'])) { - return false; - } + $guid = urldecode($matches[2]); - $item = Item::selectFirst(['id'], ['guid' => $matches[3], 'uid' => $uid]); + $item = Item::selectFirst(['id'], ['guid' => $guid, 'uid' => $uid]); if (DBA::isResult($item)) { return $item['id']; } - self::storeByGuid($matches[3], $matches[1], $uid); + self::storeByGuid($guid, $matches[1], $uid); - $item = Item::selectFirst(['id'], ['guid' => $matches[3], 'uid' => $uid]); + $item = Item::selectFirst(['id'], ['guid' => $guid, 'uid' => $uid]); if (DBA::isResult($item)) { return $item['id']; } else { @@ -2191,7 +2238,9 @@ class Diaspora } Logger::info('Deliver participation', ['item' => $comment['id'], 'contact' => $contact_id]); - Worker::add(PRIORITY_HIGH, 'Delivery', Delivery::POST, $comment['id'], $contact_id); + if (Worker::add(PRIORITY_HIGH, 'Delivery', Delivery::POST, $comment['id'], $contact_id)) { + ItemDeliveryData::incrementQueueCount($comment['id'], 1); + } } DBA::close($comments); @@ -3164,8 +3213,6 @@ class Diaspora $logid = Strings::getRandomHex(4); - $dest_url = ($public_batch ? $contact["batch"] : $contact["notify"]); - // We always try to use the data from the fcontact table. // This is important for transmitting data to Friendica servers. if (!empty($contact['addr'])) { @@ -3175,6 +3222,10 @@ class Diaspora } } + if (empty($dest_url)) { + $dest_url = ($public_batch ? $contact["batch"] : $contact["notify"]); + } + if (!$dest_url) { Logger::log("no url for contact: ".$contact["id"]." batch mode =".$public_batch); return 0; @@ -3799,7 +3850,7 @@ class Diaspora return $result; } - $toplevel_item = Item::selectFirst(['guid', 'author-link'], ['id' => $item["parent"], 'parent' => $item["parent"]]); + $toplevel_item = Item::selectFirst(['guid', 'author-id', 'author-link'], ['id' => $item["parent"], 'parent' => $item["parent"]]); if (!DBA::isResult($toplevel_item)) { Logger::error('Missing parent conversation item', ['parent' => $item["parent"]]); return false; @@ -3807,12 +3858,18 @@ class Diaspora $thread_parent_item = $toplevel_item; if ($item['thr-parent'] != $item['parent-uri']) { - $thread_parent_item = Item::selectFirst(['guid', 'author-link'], ['uri' => $item['thr-parent'], 'uid' => $item['uid']]); + $thread_parent_item = Item::selectFirst(['guid', 'author-id', 'author-link'], ['uri' => $item['thr-parent'], 'uid' => $item['uid']]); } $body = $item["body"]; - if ((empty($item['uid']) || !Feature::isEnabled($item['uid'], 'explicit_mentions')) + // The replied to autor mention is prepended for clarity if: + // - Item replied isn't yours + // - Item is public or explicit mentions are disabled + // - Implicit mentions are enabled + if ( + $item['author-id'] != $thread_parent_item['author-id'] + && (empty($item['uid']) || !Feature::isEnabled($item['uid'], 'explicit_mentions')) && !Config::get('system', 'disable_implicit_mentions') ) { $body = self::prependParentAuthorMention($body, $thread_parent_item['author-link']);