diff --git a/database.sql b/database.sql index 04a35634e..3cb87fc90 100644 --- a/database.sql +++ b/database.sql @@ -1,6 +1,6 @@ -- ------------------------------------------ -- Friendica 2020.03-dev (Dalmatian Bellflower) --- DB_UPDATE_VERSION 1334 +-- DB_UPDATE_VERSION 1335 -- ------------------------------------------ @@ -279,6 +279,7 @@ CREATE TABLE IF NOT EXISTS `conversation` ( `conversation-uri` varbinary(255) NOT NULL DEFAULT '' COMMENT 'GNU Social conversation URI', `conversation-href` varbinary(255) NOT NULL DEFAULT '' COMMENT 'GNU Social conversation link', `protocol` tinyint unsigned NOT NULL DEFAULT 255 COMMENT 'The protocol of the item', + `direction` tinyint unsigned NOT NULL DEFAULT 0 COMMENT 'How the message arrived here: 1=push, 2=pull', `source` mediumtext COMMENT 'Original source', `received` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT 'Receiving date', PRIMARY KEY(`item-uri`), diff --git a/src/Model/Conversation.php b/src/Model/Conversation.php index 661cfcd6a..1dcb6b0c2 100644 --- a/src/Model/Conversation.php +++ b/src/Model/Conversation.php @@ -41,6 +41,19 @@ class Conversation const PARCEL_TWITTER = 67; const PARCEL_UNKNOWN = 255; + /** + * Unknown message direction + */ + const UNKNOWN = 0; + /** + * The message had been pushed to this sytem + */ + const PUSH = 1; + /** + * The message had been fetched by our system + */ + const PULL = 2; + public static function getByItemUri($item_uri) { return DBA::selectFirst('conversation', [], ['item-uri' => $item_uri]); @@ -79,6 +92,10 @@ class Conversation $conversation['protocol'] = $arr['protocol']; } + if (isset($arr['direction'])) { + $conversation['direction'] = $arr['direction']; + } + if (isset($arr['source'])) { $conversation['source'] = $arr['source']; } @@ -118,6 +135,7 @@ class Conversation unset($arr['conversation-href']); unset($arr['protocol']); unset($arr['source']); + unset($arr['direction']); return $arr; } diff --git a/src/Object/Post.php b/src/Object/Post.php index 1c1f85e2a..76cf6b036 100644 --- a/src/Object/Post.php +++ b/src/Object/Post.php @@ -406,6 +406,15 @@ class Post $remote_comment = ''; } + $direction = []; + if (DI::config()->get('debug', 'show_direction')) { + $conversation = DBA::selectFirst('conversation', ['direction'], ['item-uri' => $item['uri']]); + if (!empty($conversation['direction']) && in_array($conversation['direction'], [1, 2])) { + $title = [1 => DI::l10n()->t('Pushed'), 2 => DI::l10n()->t('Pulled')]; + $direction = ['direction' => $conversation['direction'], 'title' => $title[$conversation['direction']]]; + } + } + $tmp_item = [ 'template' => $this->getTemplate(), 'type' => implode("", array_slice(explode("/", $item['verb']), -1)), @@ -482,6 +491,7 @@ class Post 'commented' => $item['commented'], 'created_date' => $item['created'], 'return' => (DI::args()->getCommand()) ? bin2hex(DI::args()->getCommand()) : '', + 'direction' => $direction, 'delivery' => [ 'queue_count' => $item['delivery_queue_count'], 'queue_done' => $item['delivery_queue_done'] + $item['delivery_queue_failed'], /// @todo Possibly display it separately in the future diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php index 2e3264e09..4fa2d33f7 100644 --- a/src/Protocol/ActivityPub/Processor.php +++ b/src/Protocol/ActivityPub/Processor.php @@ -461,6 +461,10 @@ class Processor $item['protocol'] = Conversation::PARCEL_ACTIVITYPUB; $item['conversation-href'] = $activity['context'] ?? ''; $item['conversation-uri'] = $activity['conversation'] ?? ''; + + if (isset($activity['push'])) { + $item['direction'] = $activity['push'] ? Conversation::PUSH : Conversation::PULL; + } } $isForum = false; @@ -683,7 +687,8 @@ class Processor $ldactivity['thread-completion'] = true; - ActivityPub\Receiver::processActivity($ldactivity); + ActivityPub\Receiver::processActivity($ldactivity, json_encode($activity)); + Logger::notice('Activity had been fetched and processed.', ['url' => $url, 'object' => $activity['id']]); return $activity['id']; diff --git a/src/Protocol/ActivityPub/Receiver.php b/src/Protocol/ActivityPub/Receiver.php index b9fd51ee8..21aba5094 100644 --- a/src/Protocol/ActivityPub/Receiver.php +++ b/src/Protocol/ActivityPub/Receiver.php @@ -126,7 +126,7 @@ class Receiver $trust_source = false; } - self::processActivity($ldactivity, $body, $uid, $trust_source); + self::processActivity($ldactivity, $body, $uid, $trust_source, true); } /** @@ -174,15 +174,16 @@ class Receiver /** * Prepare the object array * - * @param array $activity - * @param integer $uid User ID - * @param $trust_source + * @param array $activity Array with activity data + * @param integer $uid User ID + * @param boolean $push Message had been pushed to our system + * @param boolean $trust_source Do we trust the source? * * @return array with object data * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \ImagickException */ - private static function prepareObjectData($activity, $uid, &$trust_source) + private static function prepareObjectData($activity, $uid, $push, &$trust_source) { $actor = JsonLD::fetchElement($activity, 'as:actor', '@id'); if (empty($actor)) { @@ -230,8 +231,15 @@ class Receiver Logger::log("Object data couldn't be processed", Logger::DEBUG); return []; } + $object_data['object_id'] = $object_id; + if ($type == 'as:Announce') { + $object_data['push'] = false; + } else { + $object_data['push'] = $push; + } + // Test if it is an answer to a mail if (DBA::exists('mail', ['uri' => $object_data['reply-to-id']])) { $object_data['directmessage'] = true; @@ -309,9 +317,10 @@ class Receiver * @param string $body * @param integer $uid User ID * @param boolean $trust_source Do we trust the source? + * @param boolean $push Message had been pushed to our system * @throws \Exception */ - public static function processActivity($activity, $body = '', $uid = null, $trust_source = false) + public static function processActivity($activity, $body = '', $uid = null, $trust_source = false, $push = false) { $type = JsonLD::fetchElement($activity, '@type'); if (!$type) { @@ -341,7 +350,7 @@ class Receiver } // $trust_source is called by reference and is set to true if the content was retrieved successfully - $object_data = self::prepareObjectData($activity, $uid, $trust_source); + $object_data = self::prepareObjectData($activity, $uid, $push, $trust_source); if (empty($object_data)) { Logger::log('No object data found', Logger::DEBUG); return; @@ -352,7 +361,7 @@ class Receiver return; } - if (!empty($body)) { + if (!empty($body) && empty($object_data['raw'])) { $object_data['raw'] = $body; } @@ -390,6 +399,11 @@ class Receiver $announce_object_data['author'] = JsonLD::fetchElement($activity, 'as:actor', '@id'); $announce_object_data['object_id'] = $object_data['object_id']; $announce_object_data['object_type'] = $object_data['object_type']; + $announce_object_data['push'] = $push; + + if (!empty($body)) { + $announce_object_data['raw'] = $body; + } ActivityPub\Processor::createActivity($announce_object_data, Activity::ANNOUNCE); } @@ -779,7 +793,12 @@ class Receiver } if (in_array($type, self::CONTENT_TYPES)) { - return self::processObject($object); + $object_data = self::processObject($object); + + if (!empty($data)) { + $object_data['raw'] = json_encode($data); + } + return $object_data; } if ($type == 'as:Announce') { diff --git a/static/dbstructure.config.php b/static/dbstructure.config.php index 5b2e3bc09..0c0b28941 100755 --- a/static/dbstructure.config.php +++ b/static/dbstructure.config.php @@ -51,7 +51,7 @@ use Friendica\Database\DBA; if (!defined('DB_UPDATE_VERSION')) { - define('DB_UPDATE_VERSION', 1334); + define('DB_UPDATE_VERSION', 1335); } return [ @@ -342,6 +342,7 @@ return [ "conversation-uri" => ["type" => "varbinary(255)", "not null" => "1", "default" => "", "comment" => "GNU Social conversation URI"], "conversation-href" => ["type" => "varbinary(255)", "not null" => "1", "default" => "", "comment" => "GNU Social conversation link"], "protocol" => ["type" => "tinyint unsigned", "not null" => "1", "default" => "255", "comment" => "The protocol of the item"], + "direction" => ["type" => "tinyint unsigned", "not null" => "1", "default" => "0", "comment" => "How the message arrived here: 1=push, 2=pull"], "source" => ["type" => "mediumtext", "comment" => "Original source"], "received" => ["type" => "datetime", "not null" => "1", "default" => DBA::NULL_DATETIME, "comment" => "Receiving date"], ], diff --git a/static/defaults.config.php b/static/defaults.config.php index ab47eef19..110c016eb 100644 --- a/static/defaults.config.php +++ b/static/defaults.config.php @@ -496,6 +496,10 @@ return [ // Logs every call to /inbox as a JSON file in Friendica's temporary directory 'ap_inbox_log' => false, + // show_direction (Boolean) + // Display if a post had been fetched or had been pushed towards our server + 'show_direction' => false, + // total_ap_delivery (Boolean) // Deliver via AP to every possible receiver and we suppress the delivery to these contacts with other protocols 'total_ap_delivery' => false, diff --git a/view/theme/frio/templates/sub/direction.tpl b/view/theme/frio/templates/sub/direction.tpl new file mode 100644 index 000000000..be34a1edc --- /dev/null +++ b/view/theme/frio/templates/sub/direction.tpl @@ -0,0 +1,10 @@ +{{if $direction.direction > 0}} + + • + {{if $direction.direction == 1}} + + {{elseif $direction.direction == 2}} + + {{/if}} + +{{/if}} diff --git a/view/theme/frio/templates/wall_thread.tpl b/view/theme/frio/templates/wall_thread.tpl index 5a0cf57fa..a5a785af7 100644 --- a/view/theme/frio/templates/wall_thread.tpl +++ b/view/theme/frio/templates/wall_thread.tpl @@ -155,6 +155,9 @@ as the value of $top_child_total (this is done at the end of this file) {{if $item.owner_self}} {{include file="sub/delivery_count.tpl" delivery=$item.delivery}} {{/if}} + {{if $item.direction}} + {{include file="sub/direction.tpl" direction=$item.direction}} + {{/if}} {{if $item.pinned}} • {{$item.pinned}} @@ -183,6 +186,9 @@ as the value of $top_child_total (this is done at the end of this file) {{if $item.owner_self}} {{include file="sub/delivery_count.tpl" delivery=$item.delivery}} {{/if}} + {{if $item.direction}} + {{include file="sub/direction.tpl" direction=$item.direction}} + {{/if}}
@@ -202,6 +208,9 @@ as the value of $top_child_total (this is done at the end of this file) {{if $item.owner_self}} {{include file="sub/delivery_count.tpl" delivery=$item.delivery}} {{/if}} + {{if $item.direction}} + {{include file="sub/direction.tpl" direction=$item.direction}} + {{/if}} diff --git a/view/theme/vier/templates/sub/direction.tpl b/view/theme/vier/templates/sub/direction.tpl new file mode 100644 index 000000000..859102609 --- /dev/null +++ b/view/theme/vier/templates/sub/direction.tpl @@ -0,0 +1,10 @@ +{{if $direction.direction > 0}} + + • + {{if $direction.direction == 1}} + + {{elseif $direction.direction == 2}} + + {{/if}} + +{{/if}} diff --git a/view/theme/vier/templates/wall_thread.tpl b/view/theme/vier/templates/wall_thread.tpl index aef411bca..31ab92a3d 100644 --- a/view/theme/vier/templates/wall_thread.tpl +++ b/view/theme/vier/templates/wall_thread.tpl @@ -60,6 +60,9 @@ {{if $item.owner_self}} {{include file="sub/delivery_count.tpl" delivery=$item.delivery}} {{/if}} + {{if $item.direction}} + {{include file="sub/direction.tpl" direction=$item.direction}} + {{/if}} {{$item.pinned}} {{if $item.lock}}{{$item.lock}}{{/if}}