Merge pull request #11196 from annando/issue-10966-send

Issue 10966: Perform forum handling with the unified format
This commit is contained in:
Hypolite Petovan 2022-01-31 06:53:43 -05:00 committed by GitHub
commit 6b93becf7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 104 additions and 59 deletions

View File

@ -393,48 +393,38 @@ function item_post(App $a) {
// Personal notes must never be altered to a forum post. // Personal notes must never be altered to a forum post.
if ($posttype != Item::PT_PERSONAL_NOTE) { 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
$tags = BBCode::getTags($body); $body = BBCode::setMentions($body, local_user() ? local_user() : $profile_uid, $network);
$tagged = []; // Search for forum mentions
foreach (Tag::getFromBody($body, Tag::TAG_CHARACTER[Tag::MENTION] . Tag::TAG_CHARACTER[Tag::EXCLUSIVE_MENTION]) as $tag) {
$contact = Contact::getByURLForUser($tag[2], $profile_uid);
if (!empty($inform)) {
$inform .= ',';
}
$inform .= 'cid:' . $contact['id'];
foreach ($tags as $tag) { if (!$toplevel_item_id || empty($contact['cid']) || ($contact['contact-type'] != Contact::TYPE_COMMUNITY)) {
$tag_type = substr($tag, 0, 1);
if ($tag_type == Tag::TAG_CHARACTER[Tag::HASHTAG]) {
continue; continue;
} }
/* If we already tagged 'Robert Johnson', don't try and tag 'Robert'. if (!empty($contact['prv']) || ($tag[1] == Tag::TAG_CHARACTER[Tag::EXCLUSIVE_MENTION])) {
* Robert Johnson should be first in the $tags array $private_forum = $contact['prv'];
*/ $only_to_forum = ($tag[1] == Tag::TAG_CHARACTER[Tag::EXCLUSIVE_MENTION]);
foreach ($tagged as $nextTag) { $private_id = $contact['id'];
if (stristr($nextTag, $tag . ' ')) { $forum_contact = $contact;
continue 2; Logger::info('Private forum or exclusive mention', ['url' => $tag[2], 'mention' => $tag[1]]);
} } elseif ($str_contact_allow == '<' . $contact['id'] . '>') {
}
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; $private_forum = false;
$only_to_forum = true; $only_to_forum = true;
$private_id = $success['contact']['id']; $private_id = $contact['id'];
$forum_contact = $success['contact']; $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]]);
} }
} }
} Logger::info('Got inform', ['inform' => $inform]);
return $body;
});
} }
$original_contact_id = $contact_id; $original_contact_id = $contact_id;

View File

@ -135,7 +135,6 @@ class Item
* the appropriate link. * the appropriate link.
* *
* @param string $body the text to replace the tag in * @param string $body the text to replace the tag in
* @param string $inform a comma-seperated string containing everybody to inform
* @param integer $profile_uid the user id to replace the tag for (0 = anyone) * @param integer $profile_uid the user id to replace the tag for (0 = anyone)
* @param string $tag the tag to replace * @param string $tag the tag to replace
* @param string $network The network of the post * @param string $network The network of the post
@ -144,7 +143,7 @@ class Item
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException * @throws \ImagickException
*/ */
public static function replaceTag(&$body, &$inform, $profile_uid, $tag, $network = '') public static function replaceTag(&$body, $profile_uid, $tag, $network = '')
{ {
$replaced = false; $replaced = false;
@ -218,16 +217,6 @@ class Item
// Check if $contact has been successfully loaded // Check if $contact has been successfully loaded
if (DBA::isResult($contact)) { if (DBA::isResult($contact)) {
if (strlen($inform) && (isset($contact['notify']) || isset($contact['id']))) {
$inform .= ',';
}
if (isset($contact['id'])) {
$inform .= 'cid:' . $contact['id'];
} elseif (isset($contact['notify'])) {
$inform .= $contact['notify'];
}
$profile = $contact['url']; $profile = $contact['url'];
$newname = ($contact['name'] ?? '') ?: $contact['nick']; $newname = ($contact['name'] ?? '') ?: $contact['nick'];
} }

View File

@ -2346,7 +2346,7 @@ class BBCode
} }
/** /**
* Replaces mentions in the provided message body for the provided user and network if any * Replaces mentions in the provided message body in BBCode links for the provided user and network if any
* *
* @param $body * @param $body
* @param $profile_uid * @param $profile_uid
@ -2358,11 +2358,10 @@ class BBCode
public static function setMentions($body, $profile_uid = 0, $network = '') public static function setMentions($body, $profile_uid = 0, $network = '')
{ {
DI::profiler()->startRecording('rendering'); DI::profiler()->startRecording('rendering');
self::performWithEscapedTags($body, ['noparse', 'pre', 'code', 'img'], function ($body) use ($profile_uid, $network) { $body = self::performWithEscapedTags($body, ['noparse', 'pre', 'code', 'img'], function ($body) use ($profile_uid, $network) {
$tags = self::getTags($body); $tags = self::getTags($body);
$tagged = []; $tagged = [];
$inform = '';
foreach ($tags as $tag) { foreach ($tags as $tag) {
$tag_type = substr($tag, 0, 1); $tag_type = substr($tag, 0, 1);
@ -2381,7 +2380,7 @@ class BBCode
} }
} }
if (($success = Item::replaceTag($body, $inform, $profile_uid, $tag, $network)) && $success['replaced']) { if (($success = Item::replaceTag($body, $profile_uid, $tag, $network)) && $success['replaced']) {
$tagged[] = $tag; $tagged[] = $tag;
} }
} }

View File

@ -206,6 +206,27 @@ class Tag
self::store($uriid, $type, $name, $url, $probing); self::store($uriid, $type, $name, $url, $probing);
} }
/**
* Get tags and mentions from the body
*
* @param string $body Body of the post
* @param string $tags Accepted tags
*
* @return array Tag list
*/
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 * Store tags and mentions from the body
* *
@ -216,13 +237,10 @@ class Tag
*/ */
public static function storeFromBody(int $uriid, string $body, string $tags = null, $probing = true) public static function storeFromBody(int $uriid, string $body, string $tags = null, $probing = true)
{ {
if (is_null($tags)) {
$tags = self::TAG_CHARACTER[self::HASHTAG] . self::TAG_CHARACTER[self::MENTION] . self::TAG_CHARACTER[self::EXCLUSIVE_MENTION];
}
Logger::info('Check for tags', ['uri-id' => $uriid, 'hash' => $tags, 'callstack' => System::callstack()]); 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; return;
} }

View File

@ -0,0 +1,49 @@
<?php
/**
* @copyright Copyright (C) 2010-2022, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Test\src\Model;
use Friendica\Model\Tag;
use PHPUnit\Framework\TestCase;
class TagTest extends TestCase
{
/**
*
*/
public function testGetFromBody()
{
$body = '![url=https://pirati.ca/profile/test1]Testgruppe 1b[/url] Test, please ignore';
$tags = Tag::getFromBody($body);
$expected = [
[
'![url=https://pirati.ca/profile/test1]Testgruppe 1b[/url]',
'!',
'https://pirati.ca/profile/test1',
'Testgruppe 1b'
]
];
self::assertEquals($expected, $tags);
}
}