<?php
/**
- * @file src/Worker/Delivery.php
+ * @copyright Copyright (C) 2020, Friendica
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
*/
+
namespace Friendica\Worker;
-use Friendica\Core\Config;
-use Friendica\Core\L10n;
use Friendica\Core\Logger;
use Friendica\Core\Protocol;
use Friendica\Database\DBA;
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
{
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'];
} 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']);
$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;
}
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;
}
$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;
}
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;
}
}
}
- // 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!!!!
&& empty($parent['allow_gid'])
&& empty($parent['deny_cid'])
&& empty($parent['deny_gid'])
- && !$parent["private"]) {
+ && ($parent["private"] != Model\Item::PRIVATE)) {
$public_message = true;
}
}
$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;
}
/**
* 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
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;
$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);
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
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;
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]);
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"
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
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;
}
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"
// 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
*/
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;
}
$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
$headers = 'From: ' . Email::encodeHeader($local_user['username'],'UTF-8') . ' <' . $local_user['email'] . '>' . "\n";
}
} else {
- $headers = 'From: '. Email::encodeHeader($local_user['username'], 'UTF-8') . ' <noreply@' . DI::baseUrl()->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";
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]);
}