]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/Diaspora.php
Fetch Diaspora posts by url
[friendica.git] / src / Protocol / Diaspora.php
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
         *