1
0
Fork 0

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.
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) {
$tags = BBCode::getTags($body);
// Convert mentions in the body to a unified format
$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) {
$tag_type = substr($tag, 0, 1);
if ($tag_type == Tag::TAG_CHARACTER[Tag::HASHTAG]) {
continue;
}
/* 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'];
}
}
if (!$toplevel_item_id || empty($contact['cid']) || ($contact['contact-type'] != Contact::TYPE_COMMUNITY)) {
continue;
}
return $body;
});
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]]);
}
}
Logger::info('Got inform', ['inform' => $inform]);
}
$original_contact_id = $contact_id;

View file

@ -135,7 +135,6 @@ class Item
* the appropriate link.
*
* @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 string $tag the tag to replace
* @param string $network The network of the post
@ -144,7 +143,7 @@ class Item
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException
*/
public static function replaceTag(&$body, &$inform, $profile_uid, $tag, $network = '')
public static function replaceTag(&$body, $profile_uid, $tag, $network = '')
{
$replaced = false;
@ -218,16 +217,6 @@ class Item
// Check if $contact has been successfully loaded
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'];
$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 $profile_uid
@ -2358,11 +2358,10 @@ class BBCode
public static function setMentions($body, $profile_uid = 0, $network = '')
{
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);
$tagged = [];
$inform = '';
foreach ($tags as $tag) {
$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;
}
}

View file

@ -206,6 +206,27 @@ class Tag
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
*
@ -216,13 +237,10 @@ class Tag
*/
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()]);
if (!preg_match_all("/([" . $tags . "])\[url\=([^\[\]]*)\]([^\[\]]*)\[\/url\]/ism", $body, $result, PREG_SET_ORDER)) {
$result = self::getFromBody($body, $tags);
if (empty($result)) {
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);
}
}