]> git.mxchange.org Git - friendica.git/commitdiff
Fetch Diaspora posts by url
authorMichael <heluecht@pirati.ca>
Sun, 21 Jul 2019 07:37:50 +0000 (07:37 +0000)
committerMichael <heluecht@pirati.ca>
Sun, 21 Jul 2019 07:37:50 +0000 (07:37 +0000)
src/Model/Item.php
src/Protocol/ActivityPub/Processor.php
src/Protocol/Diaspora.php

index ddfc5e0b35566a9fa4119dee016d473d46776923..81c60b30b038387f2711a32bc9551c70bcb61ce1 100644 (file)
@@ -20,6 +20,7 @@ use Friendica\Core\Renderer;
 use Friendica\Core\System;
 use Friendica\Core\Worker;
 use Friendica\Database\DBA;
+use Friendica\Protocol\ActivityPub;
 use Friendica\Protocol\Diaspora;
 use Friendica\Protocol\OStatus;
 use Friendica\Util\DateTimeFormat;
@@ -29,7 +30,6 @@ use Friendica\Util\Security;
 use Friendica\Util\Strings;
 use Friendica\Util\XML;
 use Friendica\Worker\Delivery;
-use Friendica\Protocol\ActivityPub;
 use Text_LanguageDetect;
 
 class Item extends BaseObject
@@ -3628,11 +3628,12 @@ class Item extends BaseObject
                        return $item_id;
                }
 
-               ActivityPub\Processor::fetchMissingActivity($uri);
-
-               /// @todo add Diaspora as well
+               if (ActivityPub\Processor::fetchMissingActivity($uri)) {
+                       $item_id = self::searchByLink($uri, $uid);
+               } else {
+                       $item_id = Diaspora::fetchByURL($uri);
+               }
 
-               $item_id = self::searchByLink($uri, $uid);
                if (!empty($item_id)) {
                        return $item_id;
                }
index c1ab6bc1af58cdd5381f0e1584d33ebf57493951..dadadecde9af1c97789404cce9d51292a2f64df6 100644 (file)
@@ -534,8 +534,9 @@ class Processor
        /**
         * Fetches missing posts
         *
-        * @param $url
-        * @param $child
+        * @param string $url message URL
+        * @param array $child activity array with the child of this message
+        * @return boolean success
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        public static function fetchMissingActivity($url, $child = [])
@@ -549,12 +550,12 @@ class Processor
                $object = ActivityPub::fetchContent($url, $uid);
                if (empty($object)) {
                        Logger::log('Activity ' . $url . ' was not fetchable, aborting.');
-                       return;
+                       return false;
                }
 
                if (empty($object['id'])) {
                        Logger::log('Activity ' . $url . ' has got not id, aborting. ' . json_encode($object));
-                       return;
+                       return false;
                }
 
                if (!empty($child['author'])) {
@@ -593,6 +594,8 @@ class Processor
 
                ActivityPub\Receiver::processActivity($ldactivity);
                Logger::log('Activity ' . $url . ' had been fetched and processed.');
+
+               return true;
        }
 
        /**
index 8e2e487796a300c373edddebc880cd9833e3af7f..50300d136c25e65704c5016551dcd3cb6e1fec00 100644 (file)
@@ -1414,6 +1414,41 @@ class Diaspora
                return $msg;
        }
 
+       /**
+        * @brief Fetches an item with a given URL
+        *
+        * @param string $url the message url
+        *
+        * @return int the message id of the stored message or false
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
+        */
+       public static function fetchByURL($url, $uid = 0)
+       {
+               if (!preg_match("=([http|https].*)/(.*)/(.*)=ism", $url, $matches)) {
+                       return false;
+               }
+
+               // Check for Diaspora (and Friendica) typical path components
+               if (!in_array($matches[2], ['posts', 'display'])) {
+                       return false;
+               }
+
+               $item = Item::selectFirst(['id'], ['guid' => $matches[3], 'uid' => $uid]);
+               if (DBA::isResult($item)) {
+                       return $item['id'];
+               }
+
+               self::storeByGuid($matches[3], $matches[1], $uid);
+
+               $item = Item::selectFirst(['id'], ['guid' => $matches[3], 'uid' => $uid]);
+               if (DBA::isResult($item)) {
+                       return $item['id'];
+               } else {
+                       return false;
+               }
+       }
+
        /**
         * @brief Fetches the item record of a given guid
         *