]> git.mxchange.org Git - friendica.git/commitdiff
Issue 10966: Perform forum handling with the unified format
authorMichael <heluecht@pirati.ca>
Sun, 30 Jan 2022 04:48:22 +0000 (04:48 +0000)
committerMichael <heluecht@pirati.ca>
Sun, 30 Jan 2022 04:48:22 +0000 (04:48 +0000)
mod/item.php
src/Model/Tag.php

index 7d9cdb0286b6e4772c2d438cf0cde3b283de9b23..30cb010608de8fd8eba4fe476607b20990652edd 100644 (file)
@@ -393,7 +393,8 @@ function item_post(App $a) {
 
        // Personal notes must never be altered to a forum post.
        if ($posttype != Item::PT_PERSONAL_NOTE) {
-               $body = BBCode::performWithEscapedTags($body, ['noparse', 'pre', 'code', 'img'], function ($body) use ($profile_uid, $network, $str_contact_allow, &$inform, &$private_forum, &$private_id, &$only_to_forum, &$forum_contact) {
+               // Convert mentions in the body to a unified format
+               $body = BBCode::performWithEscapedTags($body, ['noparse', 'pre', 'code', 'img'], function ($body) use ($profile_uid, $network, &$inform) {
                        $tags = BBCode::getTags($body);
 
                        $tagged = [];
@@ -405,36 +406,45 @@ function item_post(App $a) {
                                        continue;
                                }
 
-                               /* If we already tagged 'Robert Johnson', don't try and tag 'Robert'.
-                               * Robert Johnson should be first in the $tags array
-                               */
+                               // If we already tagged 'Robert Johnson', don't try and tag 'Robert'.
+                               // Robert Johnson should be first in the $tags array
                                foreach ($tagged as $nextTag) {
                                        if (stristr($nextTag, $tag . ' ')) {
                                                continue 2;
                                        }
                                }
 
-                               if ($success = ItemHelper::replaceTag($body, $inform, local_user() ? local_user() : $profile_uid, $tag, $network)) {
-                                       if ($success['replaced']) {
-                                               $tagged[] = $tag;
-                                       }
-                                       // When the forum is private or the forum is addressed with a "!" make the post private
-                                       if (!empty($success['contact']['prv']) || ($tag_type == Tag::TAG_CHARACTER[Tag::EXCLUSIVE_MENTION])) {
-                                               $private_forum = $success['contact']['prv'];
-                                               $only_to_forum = ($tag_type == Tag::TAG_CHARACTER[Tag::EXCLUSIVE_MENTION]);
-                                               $private_id = $success['contact']['id'];
-                                               $forum_contact = $success['contact'];
-                                       } elseif (!empty($success['contact']['forum']) && ($str_contact_allow == '<' . $success['contact']['id'] . '>')) {
-                                               $private_forum = false;
-                                               $only_to_forum = true;
-                                               $private_id = $success['contact']['id'];
-                                               $forum_contact = $success['contact'];
-                                       }
-                               }
+                               ItemHelper::replaceTag($body, $inform, local_user() ? local_user() : $profile_uid, $tag, $network);
                        }
 
                        return $body;
                });
+
+               // Search for forum mentions
+               if (!$toplevel_item_id) {
+                       foreach (Tag::getFromBody($body) as $tag) {
+                               $contact = Contact::getByURL($tag[2], false, [], $profile_uid);
+                               if ($contact['contact-type'] != Contact::TYPE_COMMUNITY) {
+                                       continue;
+                               }
+
+                               if (!empty($contact['prv']) || ($tag[1] == Tag::TAG_CHARACTER[Tag::EXCLUSIVE_MENTION])) {
+                                       $private_forum = $contact['prv'];
+                                       $only_to_forum = ($tag[1] == Tag::TAG_CHARACTER[Tag::EXCLUSIVE_MENTION]);
+                                       $private_id = $contact['id'];
+                                       $forum_contact = $contact;
+                                       Logger::info('Private forum or exclusive mention', ['url' => $tag[2], 'mention' => $tag[1]]);
+                               } elseif ($str_contact_allow == '<' . $contact['id'] . '>') {
+                                       $private_forum = false;
+                                       $only_to_forum = true;
+                                       $private_id = $contact['id'];
+                                       $forum_contact = $contact;
+                                       Logger::info('Public forum', ['url' => $tag[2], 'mention' => $tag[1]]);
+                               } else {
+                                       Logger::info('Post with forum mention will not be converted to a forum post', ['url' => $tag[2], 'mention' => $tag[1]]);
+                               }
+                       }
+               }
        }
 
        $original_contact_id = $contact_id;
index 3020a2f2934839e0108779bda564314752e09bcb..17a68f120f4b5ab643277ac387571410b6d93944 100644 (file)
@@ -207,22 +207,40 @@ class Tag
        }
 
        /**
-        * Store tags and mentions from the body
+        * Get tags and mentions from the body
         * 
-        * @param integer $uriid   URI-Id
         * @param string  $body    Body of the post
         * @param string  $tags    Accepted tags
-        * @param boolean $probing Perform a probing for contacts, adding them if needed
+        *
+        * @return array Tag list
         */
-       public static function storeFromBody(int $uriid, string $body, string $tags = null, $probing = true)
+       public static function getFromBody(string $body, string $tags = null)
        {
                if (is_null($tags)) {
                        $tags =  self::TAG_CHARACTER[self::HASHTAG] . self::TAG_CHARACTER[self::MENTION] . self::TAG_CHARACTER[self::EXCLUSIVE_MENTION];
                }
 
+               if (!preg_match_all("/([" . $tags . "])\[url\=([^\[\]]*)\]([^\[\]]*)\[\/url\]/ism", $body, $result, PREG_SET_ORDER)) {
+                       return [];
+               }
+
+               return $result;
+       }
+
+       /**
+        * Store tags and mentions from the body
+        * 
+        * @param integer $uriid   URI-Id
+        * @param string  $body    Body of the post
+        * @param string  $tags    Accepted tags
+        * @param boolean $probing Perform a probing for contacts, adding them if needed
+        */
+       public static function storeFromBody(int $uriid, string $body, string $tags = null, $probing = true)
+       {
                Logger::info('Check for tags', ['uri-id' => $uriid, 'hash' => $tags, 'callstack' => System::callstack()]);
 
-               if (!preg_match_all("/([" . $tags . "])\[url\=([^\[\]]*)\]([^\[\]]*)\[\/url\]/ism", $body, $result, PREG_SET_ORDER)) {
+               $result = self::getFromBody($body, $tags);
+               if (empty($result)) {
                        return;
                }