1
0
Fork 0

Merge pull request #8740 from MrPetovan/task/frio-improve-share-display

[frio] Improve share blocks display
This commit is contained in:
Michael Vogel 2020-06-11 07:32:29 +02:00 committed by GitHub
commit 9b85d0b16e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 228 additions and 155 deletions

View file

@ -21,7 +21,10 @@
namespace Friendica\Content;
use Friendica\Database\DBA;
use Friendica\Model\Contact;
use Friendica\Model\FileTag;
use Friendica\Model\Tag;
/**
* A content helper class for displaying items
@ -100,4 +103,123 @@ class Item
return [$categories, $folders];
}
/**
* This function removes the tag $tag from the text $body and replaces it with
* 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
*
* @return array|bool ['replaced' => $replaced, 'contact' => $contact];
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException
*/
public static function replaceTag(&$body, &$inform, $profile_uid, $tag, $network = '')
{
$replaced = false;
//is it a person tag?
if (Tag::isType($tag, Tag::MENTION, Tag::IMPLICIT_MENTION, Tag::EXCLUSIVE_MENTION)) {
$tag_type = substr($tag, 0, 1);
//is it already replaced?
if (strpos($tag, '[url=')) {
// Checking for the alias that is used for OStatus
$pattern = '/[@!]\[url\=(.*?)\](.*?)\[\/url\]/ism';
if (preg_match($pattern, $tag, $matches)) {
$data = Contact::getDetailsByURL($matches[1]);
if ($data['alias'] != '') {
$newtag = '@[url=' . $data['alias'] . ']' . $data['nick'] . '[/url]';
}
}
return $replaced;
}
//get the person's name
$name = substr($tag, 1);
// Sometimes the tag detection doesn't seem to work right
// This is some workaround
$nameparts = explode(' ', $name);
$name = $nameparts[0];
// Try to detect the contact in various ways
if (strpos($name, 'http://')) {
// At first we have to ensure that the contact exists
Contact::getIdForURL($name);
// Now we should have something
$contact = Contact::getDetailsByURL($name, $profile_uid);
} elseif (strpos($name, '@')) {
// This function automatically probes when no entry was found
$contact = Contact::getDetailsByAddr($name, $profile_uid);
} else {
$contact = false;
$fields = ['id', 'url', 'nick', 'name', 'alias', 'network', 'forum', 'prv'];
if (strrpos($name, '+')) {
// Is it in format @nick+number?
$tagcid = intval(substr($name, strrpos($name, '+') + 1));
$contact = DBA::selectFirst('contact', $fields, ['id' => $tagcid, 'uid' => $profile_uid]);
}
// select someone by nick or attag in the current network
if (!DBA::isResult($contact) && ($network != '')) {
$condition = ["(`nick` = ? OR `attag` = ?) AND `network` = ? AND `uid` = ?",
$name, $name, $network, $profile_uid];
$contact = DBA::selectFirst('contact', $fields, $condition);
}
//select someone by name in the current network
if (!DBA::isResult($contact) && ($network != '')) {
$condition = ['name' => $name, 'network' => $network, 'uid' => $profile_uid];
$contact = DBA::selectFirst('contact', $fields, $condition);
}
// select someone by nick or attag in any network
if (!DBA::isResult($contact)) {
$condition = ["(`nick` = ? OR `attag` = ?) AND `uid` = ?", $name, $name, $profile_uid];
$contact = DBA::selectFirst('contact', $fields, $condition);
}
// select someone by name in any network
if (!DBA::isResult($contact)) {
$condition = ['name' => $name, 'uid' => $profile_uid];
$contact = DBA::selectFirst('contact', $fields, $condition);
}
}
// 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'];
}
//if there is an url for this persons profile
if (isset($profile) && ($newname != '')) {
$replaced = true;
// create profile link
$profile = str_replace(',', '%2c', $profile);
$newtag = $tag_type.'[url=' . $profile . ']' . $newname . '[/url]';
$body = str_replace($tag_type . $name, $newtag, $body);
}
}
return ['replaced' => $replaced, 'contact' => $contact];
}
}

View file

@ -24,6 +24,8 @@ namespace Friendica\Content\Text;
use DOMDocument;
use DOMXPath;
use Exception;
use Friendica\Content\ContactSelector;
use Friendica\Content\Item;
use Friendica\Content\OEmbed;
use Friendica\Content\Smilies;
use Friendica\Core\Hook;
@ -35,6 +37,7 @@ use Friendica\DI;
use Friendica\Model\Contact;
use Friendica\Model\Event;
use Friendica\Model\Photo;
use Friendica\Model\Tag;
use Friendica\Network\Probe;
use Friendica\Object\Image;
use Friendica\Protocol\Activity;
@ -1073,14 +1076,21 @@ class BBCode
default:
$text = ($is_quote_share? "\n" : '');
$authorId = Contact::getIdForURL($attributes['profile']);
$contact = Contact::getById($authorId, ['network']);
$tpl = Renderer::getMarkupTemplate('shared_content.tpl');
$text .= Renderer::replaceMacros($tpl, [
'$profile' => $attributes['profile'],
'$avatar' => $attributes['avatar'],
'$author' => $attributes['author'],
'$link' => $attributes['link'],
'$posted' => $attributes['posted'],
'$content' => trim($content)
'$profile' => $attributes['profile'],
'$avatar' => $attributes['avatar'],
'$author' => $attributes['author'],
'$link' => $attributes['link'],
'$link_title' => DI::l10n()->t('link to source'),
'$posted' => $attributes['posted'],
'$network_name' => ContactSelector::networkToName($contact['network'], $attributes['profile']),
'$network_icon' => ContactSelector::networkToIcon($contact['network'], $attributes['profile']),
'$content' => self::setMentions(trim($content), 0, $contact['network']),
]);
break;
}
@ -2165,4 +2175,52 @@ class BBCode
return Strings::performWithEscapedBlocks($text, '#\[(?:' . implode('|', $tagList) . ').*?\[/(?:' . implode('|', $tagList) . ')]#ism', $callback);
}
/**
* Replaces mentions in the provided message body for the provided user and network if any
*
* @param $body
* @param $profile_uid
* @param $network
* @return string
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException
*/
public static function setMentions($body, $profile_uid = 0, $network = '')
{
BBCode::performWithEscapedTags($body, ['noparse', 'pre', 'code'], function ($body) use ($profile_uid, $network) {
$tags = BBCode::getTags($body);
$tagged = [];
$inform = '';
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;
}
}
$success = Item::replaceTag($body, $inform, $profile_uid, $tag, $network);
if ($success['replaced']) {
$tagged[] = $tag;
}
}
return $body;
});
return $body;
}
}

View file

@ -1168,7 +1168,7 @@ class Contact
if (!DBA::isResult($r)) {
$data = Probe::uri($addr);
$profile = self::getDetailsByURL($data['url'], $uid);
$profile = self::getDetailsByURL($data['url'], $uid, $data);
} else {
$profile = $r[0];
}

View file

@ -441,7 +441,7 @@ class Probe
}
}
if (!empty(self::$baseurl)) {
if (empty($data['baseurl']) && !empty(self::$baseurl)) {
$data['baseurl'] = self::$baseurl;
}
@ -736,13 +736,6 @@ class Probe
Logger::log($uri." is ".$result["network"], Logger::DEBUG);
if (empty($result["baseurl"]) && ($result["network"] != Protocol::PHANTOM)) {
$pos = strpos($result["url"], $host);
if ($pos) {
$result["baseurl"] = substr($result["url"], 0, $pos).$host;
}
}
return $result;
}