X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FWorker%2FDelivery.php;h=1d1e8702b28c60390eea80d29e67fe0d5aa1ed12;hb=bf8fb215a9cc554b5ec5b774168a52fb56fa43e6;hp=ed2ff2979da929eaebd9f70270dc3cc098ce06d3;hpb=50be94aa5ecdceb10de0d6b4764b8eeca78a826f;p=friendica.git diff --git a/src/Worker/Delivery.php b/src/Worker/Delivery.php index ed2ff2979d..1d1e8702b2 100644 --- a/src/Worker/Delivery.php +++ b/src/Worker/Delivery.php @@ -1,14 +1,28 @@ . + * */ + namespace Friendica\Worker; -use Friendica\Core\Config; -use Friendica\Core\L10n; use Friendica\Core\Logger; use Friendica\Core\Protocol; -use Friendica\Core\System; use Friendica\Database\DBA; use Friendica\DI; use Friendica\Model; @@ -19,6 +33,9 @@ use Friendica\Protocol\Activity; use Friendica\Util\Strings; use Friendica\Util\Network; use Friendica\Core\Worker; +use Friendica\Model\Conversation; +use Friendica\Model\FContact; +use Friendica\Protocol\Relay; class Delivery { @@ -44,14 +61,12 @@ class Delivery if ($cmd == self::MAIL) { $target_item = DBA::selectFirst('mail', [], ['id' => $target_id]); if (!DBA::isResult($target_item)) { - self::setFailedQueue($cmd, $target_id); return; } $uid = $target_item['uid']; } elseif ($cmd == self::SUGGESTION) { $target_item = DBA::selectFirst('fsuggest', [], ['id' => $target_id]); if (!DBA::isResult($target_item)) { - self::setFailedQueue($cmd, $target_id); return; } $uid = $target_item['uid']; @@ -61,7 +76,6 @@ class Delivery } else { $item = Model\Item::selectFirst(['parent'], ['id' => $target_id]); if (!DBA::isResult($item) || empty($item['parent'])) { - self::setFailedQueue($cmd, $target_id); return; } $parent_id = intval($item['parent']); @@ -71,6 +85,10 @@ class Delivery $itemdata = Model\Item::select([], $condition, $params); while ($item = Model\Item::fetch($itemdata)) { + if ($item['verb'] == Activity::ANNOUNCE) { + continue; + } + if ($item['id'] == $parent_id) { $parent = $item; } @@ -83,13 +101,12 @@ class Delivery if (empty($target_item)) { Logger::log('Item ' . $target_id . "wasn't found. Quitting here."); - self::setFailedQueue($cmd, $target_id); return; } if (empty($parent)) { Logger::log('Parent ' . $parent_id . ' for item ' . $target_id . "wasn't found. Quitting here."); - self::setFailedQueue($cmd, $target_id); + self::setFailedQueue($cmd, $target_item); return; } @@ -99,7 +116,7 @@ class Delivery $uid = $target_item['uid']; } else { Logger::log('Only public users for item ' . $target_id, Logger::DEBUG); - self::setFailedQueue($cmd, $target_id); + self::setFailedQueue($cmd, $target_item); return; } @@ -113,7 +130,7 @@ class Delivery if (!empty($contact_id) && Model\Contact::isArchived($contact_id)) { Logger::info('Contact is archived', ['id' => $contact_id, 'cmd' => $cmd, 'item' => $target_item['id']]); - self::setFailedQueue($cmd, $target_id); + self::setFailedQueue($cmd, $target_item); return; } @@ -124,13 +141,7 @@ class Delivery } } - // When commenting too fast after delivery, a post wasn't recognized as top level post. - // The count then showed more than one entry. The additional check should help. - // The check for the "count" should be superfluous, but I'm not totally sure by now, so we keep it. - if ((($parent['id'] == $target_id) || (count($items) == 1)) && ($parent['uri'] === $parent['parent-uri'])) { - Logger::log('Top level post'); - $top_level = true; - } + $top_level = $target_item['gravity'] == GRAVITY_PARENT; // This is IMPORTANT!!!! @@ -162,7 +173,7 @@ class Delivery && empty($parent['allow_gid']) && empty($parent['deny_cid']) && empty($parent['deny_gid']) - && !$parent["private"]) { + && ($parent["private"] != Model\Item::PRIVATE)) { $public_message = true; } } @@ -173,31 +184,37 @@ class Delivery $owner = Model\User::getOwnerDataById($uid); if (!DBA::isResult($owner)) { - self::setFailedQueue($cmd, $target_id); + self::setFailedQueue($cmd, $target_item); return; } - // We don't deliver our items to blocked or pending contacts, and not to ourselves either + // We don't deliver our items to blocked, archived or pending contacts, and not to ourselves either $contact = DBA::selectFirst('contact', [], - ['id' => $contact_id, 'blocked' => false, 'pending' => false, 'self' => false] + ['id' => $contact_id, 'archive' => false, 'blocked' => false, 'pending' => false, 'self' => false] ); if (!DBA::isResult($contact)) { - self::setFailedQueue($cmd, $target_id); + self::setFailedQueue($cmd, $target_item); return; } if (Network::isUrlBlocked($contact['url'])) { - self::setFailedQueue($cmd, $target_id); + self::setFailedQueue($cmd, $target_item); return; } // Transmit via Diaspora if the thread had started as Diaspora post. // Also transmit via Diaspora if this is a direct answer to a Diaspora comment. // This is done since the uri wouldn't match (Diaspora doesn't transmit it) - if (!empty($parent) && !empty($thr_parent) && in_array(Protocol::DIASPORA, [$parent['network'], $thr_parent['network']])) { + // Also transmit relayed posts from Diaspora contacts via Diaspora. + if (!empty($parent) && !empty($thr_parent) && in_array(Protocol::DIASPORA, [$parent['network'], $thr_parent['network'], $target_item['network']])) { $contact['network'] = Protocol::DIASPORA; } + // Ensure that local contacts are delivered locally + if (Model\Contact::isLocal($contact['url'])) { + $contact['network'] = Protocol::DFRN; + } + Logger::notice('Delivering', ['cmd' => $cmd, 'target' => $target_id, 'followup' => $followup, 'network' => $contact['network']]); switch ($contact['network']) { @@ -223,20 +240,20 @@ class Delivery /** * Increased the "failed" counter in the item delivery data * - * @param string $cmd Command - * @param integer $id Item id + * @param string $cmd Command + * @param array $item Item array */ - private static function setFailedQueue(string $cmd, int $id) + private static function setFailedQueue(string $cmd, array $item) { if (!in_array($cmd, [Delivery::POST, Delivery::POKE])) { return; } - Model\ItemDeliveryData::incrementQueueFailed($id); + Model\Post\DeliveryData::incrementQueueFailed($item['uri-id'] ?? $item['id']); } /** - * @brief Deliver content via DFRN + * Deliver content via DFRN * * @param string $cmd Command * @param array $contact Contact record of the receiver @@ -252,7 +269,7 @@ class Delivery private static function deliverDFRN($cmd, $contact, $owner, $items, $target_item, $public_message, $top_level, $followup) { // Transmit Diaspora reshares via Diaspora if the Friendica contact support Diaspora - if (Diaspora::isReshare($target_item['body']) && !empty(Diaspora::personByHandle($contact['addr'], false))) { + if (Diaspora::isReshare($target_item['body']) && !empty(FContact::getByURL($contact['addr'], false))) { Logger::info('Reshare will be transmitted via Diaspora', ['url' => $contact['url'], 'guid' => ($target_item['guid'] ?? '') ?: $target_item['id']]); self::deliverDiaspora($cmd, $contact, $owner, $items, $target_item, $public_message, $top_level, $followup); return; @@ -274,13 +291,17 @@ class Delivery $msgitems = [$target_item]; $atom = DFRN::entries($msgitems, $owner); } else { - $msgitems = []; - foreach ($items as $item) { - // Only add the parent when we don't delete other items. - if (($target_item['id'] == $item['id']) || ($cmd != self::DELETION)) { - $item["entry:comment-allow"] = true; - $item["entry:cid"] = ($top_level ? $contact['id'] : 0); - $msgitems[] = $item; + if ($target_item['deleted']) { + $msgitems = [$target_item]; + } else { + $msgitems = []; + foreach ($items as $item) { + // Only add the parent when we don't delete other items. + if (($target_item['id'] == $item['id']) || ($cmd != self::DELETION)) { + $item["entry:comment-allow"] = true; + $item["entry:cid"] = ($top_level ? $contact['id'] : 0); + $msgitems[] = $item; + } } } $atom = DFRN::entries($msgitems, $owner); @@ -288,11 +309,8 @@ class Delivery Logger::debug('Notifier entry: ' . $contact["url"] . ' ' . (($target_item['guid'] ?? '') ?: $target_item['id']) . ' entry: ' . $atom); - $basepath = implode('/', array_slice(explode('/', $contact['url']), 0, 3)); - // perform local delivery if we are on the same site - - if (Strings::compareLink($basepath, System::baseUrl())) { + if (Model\Contact::isLocal($contact['url'])) { $condition = ['nurl' => Strings::normaliseLink($contact['url']), 'self' => true]; $target_self = DBA::selectFirst('contact', ['uid'], $condition); if (!DBA::isResult($target_self)) { @@ -316,16 +334,16 @@ class Delivery return; } - DFRN::import($atom, $target_importer); + DFRN::import($atom, $target_importer, false, Conversation::PARCEL_LOCAL_DFRN); if (in_array($cmd, [Delivery::POST, Delivery::POKE])) { - Model\ItemDeliveryData::incrementQueueDone($target_item['id'], Model\ItemDeliveryData::DFRN); + Model\Post\DeliveryData::incrementQueueDone($target_item['uri-id'], Model\Post\DeliveryData::DFRN); } return; } - $protocol = Model\ItemDeliveryData::DFRN; + $protocol = Model\Post\DeliveryData::DFRN; // We don't have a relationship with contacts on a public post. // Se we transmit with the new method and via Diaspora as a fallback @@ -341,9 +359,9 @@ class Delivery if (in_array($cmd, [Delivery::POST, Delivery::POKE])) { if (($deliver_status >= 200) && ($deliver_status <= 299)) { - Model\ItemDeliveryData::incrementQueueDone($target_item['id'], $protocol); + Model\Post\DeliveryData::incrementQueueDone($target_item['uri-id'], $protocol); } else { - Model\ItemDeliveryData::incrementQueueFailed($target_item['id']); + Model\Post\DeliveryData::incrementQueueFailed($target_item['uri-id']); } } return; @@ -360,11 +378,11 @@ class Delivery if ($deliver_status < 200) { // Legacy DFRN $deliver_status = DFRN::deliver($owner, $contact, $atom); - $protocol = Model\ItemDeliveryData::LEGACY_DFRN; + $protocol = Model\Post\DeliveryData::LEGACY_DFRN; } } else { $deliver_status = DFRN::deliver($owner, $contact, $atom); - $protocol = Model\ItemDeliveryData::LEGACY_DFRN; + $protocol = Model\Post\DeliveryData::LEGACY_DFRN; } Logger::info('DFRN Delivery', ['cmd' => $cmd, 'url' => $contact['url'], 'guid' => ($target_item['guid'] ?? '') ?: $target_item['id'], 'return' => $deliver_status]); @@ -374,7 +392,7 @@ class Delivery Model\Contact::unmarkForArchival($contact); if (in_array($cmd, [Delivery::POST, Delivery::POKE])) { - Model\ItemDeliveryData::incrementQueueDone($target_item['id'], $protocol); + Model\Post\DeliveryData::incrementQueueDone($target_item['uri-id'], $protocol); } } else { // The message could not be delivered. We mark the contact as "dead" @@ -382,13 +400,13 @@ class Delivery Logger::info('Delivery failed: defer message', ['id' => ($target_item['guid'] ?? '') ?: $target_item['id']]); if (!Worker::defer() && in_array($cmd, [Delivery::POST, Delivery::POKE])) { - Model\ItemDeliveryData::incrementQueueFailed($target_item['id']); + Model\Post\DeliveryData::incrementQueueFailed($target_item['uri-id']); } } } /** - * @brief Deliver content via Diaspora + * Deliver content via Diaspora * * @param string $cmd Command * @param array $contact Contact record of the receiver @@ -414,7 +432,7 @@ class Delivery Logger::notice('Deliver via Diaspora', ['target' => $target_item['id'], 'guid' => $target_item['guid'], 'to' => $loc]); - if (Config::get('system', 'dfrn_only') || !Config::get('system', 'diaspora_enabled')) { + if (DI::config()->get('system', 'dfrn_only') || !DI::config()->get('system', 'diaspora_enabled')) { return; } @@ -459,7 +477,7 @@ class Delivery Model\Contact::unmarkForArchival($contact); if (in_array($cmd, [Delivery::POST, Delivery::POKE])) { - Model\ItemDeliveryData::incrementQueueDone($target_item['id'], Model\ItemDeliveryData::DIASPORA); + Model\Post\DeliveryData::incrementQueueDone($target_item['uri-id'], Model\Post\DeliveryData::DIASPORA); } } else { // The message could not be delivered. We mark the contact as "dead" @@ -467,23 +485,23 @@ class Delivery // When it is delivered to the public endpoint, we do mark the relay contact for archival as well if ($public_message) { - Diaspora::markRelayForArchival($contact); + Relay::markForArchival($contact); } if (empty($contact['contact-type']) || ($contact['contact-type'] != Model\Contact::TYPE_RELAY)) { Logger::info('Delivery failed: defer message', ['id' => ($target_item['guid'] ?? '') ?: $target_item['id']]); // defer message for redelivery if (!Worker::defer() && in_array($cmd, [Delivery::POST, Delivery::POKE])) { - Model\ItemDeliveryData::incrementQueueFailed($target_item['id']); + Model\Post\DeliveryData::incrementQueueFailed($target_item['uri-id']); } } elseif (in_array($cmd, [Delivery::POST, Delivery::POKE])) { - Model\ItemDeliveryData::incrementQueueFailed($target_item['id']); + Model\Post\DeliveryData::incrementQueueFailed($target_item['uri-id']); } } } /** - * @brief Deliver content via mail + * Deliver content via mail * * @param string $cmd Command * @param array $contact Contact record of the receiver @@ -495,7 +513,7 @@ class Delivery */ private static function deliverMail($cmd, $contact, $owner, $target_item, $thr_parent) { - if (Config::get('system','dfrn_only')) { + if (DI::config()->get('system','dfrn_only')) { return; } @@ -536,7 +554,7 @@ class Delivery $reply_to = $mailacct['reply_to']; } - $subject = ($target_item['title'] ? Email::encodeHeader($target_item['title'], 'UTF-8') : L10n::t("\x28no subject\x29")); + $subject = ($target_item['title'] ? Email::encodeHeader($target_item['title'], 'UTF-8') : DI::l10n()->t("\x28no subject\x29")); // only expose our real email address to true friends @@ -548,7 +566,8 @@ class Delivery $headers = 'From: ' . Email::encodeHeader($local_user['username'],'UTF-8') . ' <' . $local_user['email'] . '>' . "\n"; } } else { - $headers = 'From: '. Email::encodeHeader($local_user['username'], 'UTF-8') . ' getHostname() . '>' . "\n"; + $sender = DI::config()->get('config', 'sender_email', 'noreply@' . DI::baseUrl()->getHostname()); + $headers = 'From: '. Email::encodeHeader($local_user['username'], 'UTF-8') . ' <' . $sender . '>' . "\n"; } $headers .= 'Message-Id: <' . Email::iri2msgid($target_item['uri']) . '>' . "\n"; @@ -586,7 +605,7 @@ class Delivery Email::send($addr, $subject, $headers, $target_item); - Model\ItemDeliveryData::incrementQueueDone($target_item['id'], Model\ItemDeliveryData::MAIL); + Model\Post\DeliveryData::incrementQueueDone($target_item['uri-id'], Model\Post\DeliveryData::MAIL); Logger::info('Delivered via mail', ['guid' => $target_item['guid'], 'to' => $addr, 'subject' => $subject]); }