]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/Diaspora.php
Merge remote-tracking branch 'upstream/develop' into term2tag
[friendica.git] / src / Protocol / Diaspora.php
index 828333b41f10656119947bf96dbd1aa7a9fc246d..a6369a2bfa43c5ca67a4d4d8fa9f7d5894f13776 100644 (file)
@@ -35,9 +35,11 @@ use Friendica\Model\Contact;
 use Friendica\Model\Conversation;
 use Friendica\Model\GContact;
 use Friendica\Model\Item;
+use Friendica\Model\ItemURI;
 use Friendica\Model\ItemDeliveryData;
 use Friendica\Model\Mail;
 use Friendica\Model\Profile;
+use Friendica\Model\Term;
 use Friendica\Model\User;
 use Friendica\Network\Probe;
 use Friendica\Util\Crypto;
@@ -123,7 +125,7 @@ class Diaspora
                        }
 
                        // All tags of the current post
-                       $condition = ['otype' => TERM_OBJ_POST, 'type' => TERM_HASHTAG, 'oid' => $parent['parent']];
+                       $condition = ['otype' => Term::OBJECT_TYPE_POST, 'type' => Term::HASHTAG, 'oid' => $parent['parent']];
                        $tags = DBA::select('term', ['term'], $condition);
                        $taglist = [];
                        while ($tag = DBA::fetch($tags)) {
@@ -1807,6 +1809,66 @@ class Diaspora
                return false;
        }
 
+       private static function storeMentions(int $uriid, string $text)
+       {
+               preg_match_all('/([@!]){(?:([^}]+?); ?)?([^} ]+)}/', $text, $matches, PREG_SET_ORDER);
+               if (empty($matches)) {
+                       return;
+               }
+
+               /*
+                * Matching values for the preg match
+                * [1] = mention type (@ or !)
+                * [2] = name (optional)
+                * [3] = profile URL
+                */
+
+               foreach ($matches as $match) {
+                       if (empty($match)) {
+                               continue;
+                       }
+
+                       $person = self::personByHandle($match[3]);
+                       if (empty($person)) {
+                               continue;
+                       }
+
+                       $fields = ['uri-id' => $uriid, 'name' => substr($person['addr'], 0, 64), 'url' => $person['url']];
+
+                       if ($match[1] == Term::TAG_CHARACTER[Term::MENTION]) {
+                               $fields['type'] = Term::MENTION;
+                       } elseif ($match[1] == Term::TAG_CHARACTER[Term::EXCLUSIVE_MENTION]) {
+                               $fields['type'] = Term::EXCLUSIVE_MENTION;
+                       } elseif ($match[1] == Term::TAG_CHARACTER[Term::IMPLICIT_MENTION]) {
+                               $fields['type'] = Term::IMPLICIT_MENTION;
+                       } else {
+                               continue;
+                       }
+
+                       DBA::insert('tag', $fields, true);
+                       Logger::info('Stored mention', ['uriid' => $uriid, 'match' => $match, 'fields' => $fields]);
+               }
+       }
+
+       private static function storeTags(int $uriid, string $body)
+       {
+               $tags = BBCode::getTags($body);
+               if (empty($tags)) {
+                       return;
+               }
+
+               foreach ($tags as $tag) {
+                       if ((substr($tag, 0, 1) != Term::TAG_CHARACTER[Term::HASHTAG]) || (strlen($tag) <= 1)) {
+                               Logger::info('Skip tag', ['uriid' => $uriid, 'tag' => $tag]);
+                               continue;
+                       }
+
+                       $fields = ['uri-id' => $uriid, 'name' => substr($tag, 1, 64), 'type' => Term::HASHTAG];
+                       DBA::insert('tag', $fields, true);
+                       Logger::info('Stored tag', ['uriid' => $uriid, 'tag' => $tag, 'fields' => $fields]);
+               }
+       }
+
        /**
         * Processes an incoming comment
         *
@@ -1877,6 +1939,7 @@ class Diaspora
 
                $datarray["guid"] = $guid;
                $datarray["uri"] = self::getUriFromGuid($author, $guid);
+               $datarray['uri-id'] = ItemURI::insert(['uri' => $datarray['uri'], 'guid' => $datarray['guid']]);
 
                $datarray["verb"] = Activity::POST;
                $datarray["gravity"] = GRAVITY_COMMENT;
@@ -1895,10 +1958,14 @@ class Diaspora
                $datarray["changed"] = $datarray["created"] = $datarray["edited"] = $created_at;
 
                $datarray["plink"] = self::plink($author, $guid, $parent_item['guid']);
+       
                $body = Markdown::toBBCode($text);
 
                $datarray["body"] = self::replacePeopleGuid($body, $person["url"]);
 
+               self::storeMentions($datarray['uri-id'], $text);
+               self::storeTags($datarray['uri-id'], $datarray["body"]);
+
                self::fetchGuid($datarray);
 
                // If we are the origin of the parent we store the original data.
@@ -2641,6 +2708,7 @@ class Diaspora
 
                $datarray['guid'] = $parent['guid'] . '-' . $guid;
                $datarray['uri'] = self::getUriFromGuid($author, $datarray['guid']);
+
                $datarray['parent-uri'] = $parent['uri'];
 
                $datarray['verb'] = $datarray['body'] = Activity::ANNOUNCE;
@@ -2715,6 +2783,7 @@ class Diaspora
 
                $datarray["guid"] = $guid;
                $datarray["uri"] = $datarray["parent-uri"] = self::getUriFromGuid($author, $guid);
+               $datarray['uri-id'] = ItemURI::insert(['uri' => $datarray['uri'], 'guid' => $datarray['guid']]);
 
                $datarray["verb"] = Activity::POST;
                $datarray["gravity"] = GRAVITY_PARENT;
@@ -2722,6 +2791,8 @@ class Diaspora
                $datarray["protocol"] = Conversation::PARCEL_DIASPORA;
                $datarray["source"] = $xml;
 
+               /// @todo Copy tag data from original post
+
                $prefix = share_header(
                        $original_item["author-name"],
                        $original_item["author-link"],
@@ -2958,6 +3029,7 @@ class Diaspora
 
                $datarray["guid"] = $guid;
                $datarray["uri"] = $datarray["parent-uri"] = self::getUriFromGuid($author, $guid);
+               $datarray['uri-id'] = ItemURI::insert(['uri' => $datarray['uri'], 'guid' => $datarray['guid']]);
 
                $datarray["verb"] = Activity::POST;
                $datarray["gravity"] = GRAVITY_PARENT;
@@ -2967,6 +3039,9 @@ class Diaspora
 
                $datarray["body"] = self::replacePeopleGuid($body, $contact["url"]);
 
+               self::storeMentions($datarray['uri-id'], $text);
+               self::storeTags($datarray['uri-id'], $datarray["body"]);
+
                if ($provider_display_name != "") {
                        $datarray["app"] = $provider_display_name;
                }
@@ -3941,30 +4016,24 @@ class Diaspora
 
                Logger::log("Got relayable data ".$type." for item ".$item["guid"]." (".$item["id"].")", Logger::DEBUG);
 
-               // Old way - is used by the internal Friendica functions
-               /// @todo Change all signatur storing functions to the new format
-               if ($item['signed_text'] && $item['signature'] && $item['signer']) {
-                       $message = self::messageFromSignature($item);
-               } else {// New way
-                       $msg = json_decode($item['signed_text'], true);
-
-                       $message = [];
-                       if (is_array($msg)) {
-                               foreach ($msg as $field => $data) {
-                                       if (!$item["deleted"]) {
-                                               if ($field == "diaspora_handle") {
-                                                       $field = "author";
-                                               }
-                                               if ($field == "target_type") {
-                                                       $field = "parent_type";
-                                               }
-                                       }
+               $msg = json_decode($item['signed_text'], true);
 
-                                       $message[$field] = $data;
+               $message = [];
+               if (is_array($msg)) {
+                       foreach ($msg as $field => $data) {
+                               if (!$item["deleted"]) {
+                                       if ($field == "diaspora_handle") {
+                                               $field = "author";
+                                       }
+                                       if ($field == "target_type") {
+                                               $field = "parent_type";
+                                       }
                                }
-                       } else {
-                               Logger::log("Signature text for item ".$item["guid"]." (".$item["id"].") couldn't be extracted: ".$item['signed_text'], Logger::DEBUG);
+
+                               $message[$field] = $data;
                        }
+               } else {
+                       Logger::log("Signature text for item ".$item["guid"]." (".$item["id"].") couldn't be extracted: ".$item['signed_text'], Logger::DEBUG);
                }
 
                $message["parent_author_signature"] = self::signature($owner, $message);