1
0
Fork 0

Merge pull request #11999 from annando/quote-uri-id

Improve quote generation
This commit is contained in:
Hypolite Petovan 2022-10-15 09:11:22 -04:00 committed by GitHub
commit 1585f711ee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 12 deletions

View file

@ -574,9 +574,10 @@ class Item
*
* @param string $url
* @param integer $uid
* @param bool $add_media
* @return string
*/
public function createSharedPostByUrl(string $url, int $uid = 0): string
public function createSharedPostByUrl(string $url, int $uid = 0, bool $add_media = false): string
{
if (!empty($uid)) {
$id = ModelItem::searchByLink($url, $uid);
@ -599,7 +600,7 @@ class Item
return '';
}
return $this->createSharedBlockByArray($shared_item);
return $this->createSharedBlockByArray($shared_item, $add_media);
}
/**
@ -652,9 +653,10 @@ class Item
* Add a share block for the given item array
*
* @param array $item
* @param bool $add_media
* @return string
*/
public function createSharedBlockByArray(array $item): string
public function createSharedBlockByArray(array $item, bool $add_media = false): string
{
if ($item['network'] == Protocol::FEED) {
return PageInfo::getFooterFromUrl($item['plink']);
@ -662,6 +664,8 @@ class Item
$item['guid'] = '';
$item['uri'] = '';
$item['body'] = Post\Media::addAttachmentsToBody($item['uri-id'], $item['body']);
} elseif ($add_media) {
$item['body'] = Post\Media::addAttachmentsToBody($item['uri-id'], $item['body']);
}
$shared_content = BBCode::getShareOpeningTag($item['author-name'], $item['author-link'], $item['author-avatar'], $item['plink'], $item['created'], $item['guid'], $item['uri']);

View file

@ -1085,6 +1085,22 @@ class BBCode
);
}
/**
* Remove the share block
*
* @param string $body
* @return string
*/
public static function removeSharedData(string $body): string
{
return BBCode::convertShare(
$body,
function (array $attributes) {
return '';
}
);
}
/**
* This function converts a [share] block to text according to a provided callback function whose signature is:
*
@ -1134,7 +1150,7 @@ class BBCode
);
DI::profiler()->stopRecording();
return $return;
return trim($return);
}
/**

View file

@ -1123,7 +1123,7 @@ class Item
if (!empty($quote_id) && Post::exists(['uri-id' => $quote_id, 'network' => Protocol::FEDERATED])) {
$item['quote-uri-id'] = $quote_id;
$item['raw-body'] = BBCode::replaceSharedData($item['raw-body']);
$item['raw-body'] = BBCode::removeSharedData($item['raw-body']);
}
if (!DBA::exists('contact', ['id' => $item['author-id'], 'network' => Protocol::DFRN])) {
@ -3612,9 +3612,10 @@ class Item
* Improve the data in shared posts
*
* @param array $item
* @param bool $add_media
* @return string body
*/
public static function improveSharedDataInBody(array $item): string
public static function improveSharedDataInBody(array $item, bool $add_media = false): string
{
$shared = BBCode::fetchShareAttributes($item['body']);
if (empty($shared['guid']) && empty($shared['message_id'])) {
@ -3624,7 +3625,7 @@ class Item
$link = $shared['link'] ?: $shared['message_id'];
if (empty($shared_content)) {
$shared_content = DI::contentItem()->createSharedPostByUrl($link, $item['uid'] ?? 0);
$shared_content = DI::contentItem()->createSharedPostByUrl($link, $item['uid'] ?? 0, $add_media);
}
if (empty($shared_content)) {

View file

@ -40,6 +40,7 @@ use Friendica\Model\User;
use Friendica\Network\HTTPException;
use Friendica\Protocol\Activity;
use Friendica\Protocol\ActivityPub;
use Friendica\Protocol\Diaspora;
use Friendica\Protocol\Relay;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\HTTPSignature;
@ -1621,6 +1622,8 @@ class Transmitter
$permission_block = self::createPermissionBlockForItem($item, false);
$real_quote = false;
$body = $item['body'];
if ($type == 'Note') {
@ -1662,9 +1665,22 @@ class Transmitter
$body = BBCode::setMentionsToNicknames($body);
if (!empty($item['quote-uri'])) {
$body = BBCode::replaceSharedData($body);
if (!empty($item['quote-uri']) && Post::exists(['uri-id' => $item['quote-uri-id'], 'network' => [Protocol::ACTIVITYPUB, Protocol::DFRN]])) {
$real_quote = true;
if (Diaspora::isReshare($body, false)) {
$body = BBCode::replaceSharedData($body);
} elseif (strpos($body, $item['quote-uri']) === false) {
$body .= "\n" . $item['quote-uri'];
}
$data['quoteUrl'] = $item['quote-uri'];
} elseif (!empty($item['quote-uri']) && !Diaspora::isReshare($body, false)) {
$fields = ['uri-id', 'uri', 'body', 'title', 'author-name', 'author-link', 'author-avatar', 'guid', 'created', 'plink', 'network'];
$shared_item = Post::selectFirst($fields, ['uri-id' => $item['quote-uri-id']]);
if (!empty($shared_item['uri-id'])) {
$shared_item['body'] = Post\Media::addAttachmentsToBody($shared_item['uri-id'], $shared_item['body']);
$body .= "\n" . DI::contentItem()->createSharedBlockByArray($shared_item);
$item['body'] = Item::improveSharedDataInBody($item, true);
}
}
$data['content'] = BBCode::convertForUriId($item['uri-id'], $body, BBCode::ACTIVITYPUB);
@ -1677,9 +1693,11 @@ class Transmitter
if (!empty($language)) {
$richbody = BBCode::setMentionsToNicknames($item['body'] ?? '');
$shared = BBCode::fetchShareAttributes($richbody);
if (!empty($shared['link']) && !empty($shared['guid']) && !empty($shared['comment'])) {
$richbody = BBCode::replaceSharedData($richbody);
if ($real_quote) {
$shared = BBCode::fetchShareAttributes($richbody);
if (!empty($shared['link']) && !empty($shared['guid']) && !empty($shared['comment'])) {
$richbody = BBCode::replaceSharedData($richbody);
}
}
$richbody = BBCode::removeAttachment($richbody);