<?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;
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']);
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;
}
&& 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;
}
['id' => $contact_id, '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;
}
$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']) {
/**
* 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
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, DI::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)) {
DFRN::import($atom, $target_importer);
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"
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]);
}