Merge pull request #12865 from annando/fix-links

Fix missing attached links in posts
This commit is contained in:
Hypolite Petovan 2023-03-04 17:57:38 -05:00 committed by GitHub
commit 27cc346f8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 81 additions and 2 deletions

View File

@ -259,10 +259,12 @@ class Status extends BaseFactory
}
}
$item['body'] = $this->contentItem->addSharedPost($item);
if (!is_null($item['raw-body'])) {
$item['raw-body'] = $this->contentItem->addSharedPost($item, $item['raw-body']);
$item['raw-body'] = Post\Media::addHTMLLinkToBody($uriId, $item['raw-body']);
} else {
$item['body'] = $this->contentItem->addSharedPost($item);
$item['body'] = Post\Media::addHTMLLinkToBody($uriId, $item['body']);
}
}

View File

@ -124,6 +124,7 @@ class Status extends BaseFactory
*/
private function createFromArray(array $item, int $uid, bool $include_entities): \Friendica\Object\Api\Twitter\Status
{
$item = Post\Media::addHTMLAttachmentToItem($item);
$author = $this->twitterUser->createFromContactId($item['author-id'], $uid, true);
if (!empty($item['causer-id']) && ($item['post-reason'] == Item::PR_ANNOUNCEMENT)) {

View File

@ -21,6 +21,7 @@
namespace Friendica\Model\Post;
use Friendica\Content\PageInfo;
use Friendica\Content\Text\BBCode;
use Friendica\Core\Logger;
use Friendica\Core\Protocol;
@ -895,6 +896,72 @@ class Media
return $body;
}
public static function addHTMLAttachmentToBody(int $uriid, string $body): string
{
if (preg_match("/.*(\[attachment.*?\].*?\[\/attachment\]).*/ism", $body, $match)) {
return $body;
}
$links = self::getByURIId($uriid, [self::HTML]);
if (empty($links)) {
return $body;
}
$data = [
'type' => 'link',
'url' => $links[0]['url'],
'title' => $links[0]['name'],
'text' => $links[0]['description'],
'publisher_name' => $links[0]['publisher-name'],
'publisher_url' => $links[0]['publisher-url'],
'publisher_img' => $links[0]['publisher-image'],
'author_name' => $links[0]['author-name'],
'author_url' => $links[0]['author-url'],
'author_img' => $links[0]['author-image'],
'images' => [[
'src' => $links[0]['preview'],
'height' => $links[0]['preview-height'],
'width' => $links[0]['preview-width'],
]]
];
$body .= "\n" . PageInfo::getFooterFromData($data);
return $body;
}
public static function addHTMLLinkToBody(int $uriid, string $body): string
{
$links = self::getByURIId($uriid, [self::HTML]);
if (empty($links)) {
return $body;
}
if (strpos($body, $links[0]['url'])) {
return $body;
}
if (!empty($links[0]['name']) && ($links[0]['name'] != $links[0]['url'])) {
return $body . "\n[url=" . $links[0]['url'] . ']' . $links[0]['name'] . "[/url]";
} else {
return $body . "\n[url]" . $links[0]['url'] . "[/url]";
}
}
public static function addHTMLAttachmentToItem(array $item): array
{
if (($item['gravity'] == Item::GRAVITY_ACTIVITY) || empty($item['uri-id'])) {
return $item;
}
$item['body'] = self::addHTMLAttachmentToBody($item['uri-id'], $item['body']);
if (!empty($item['raw-body'])) {
$item['raw-body'] = self::addHTMLLinkToBody($item['uri-id'], $item['raw-body']);
}
return $item;
}
/**
* Get preview link for given media id
*

View File

@ -116,6 +116,7 @@ class Edit extends BaseModule
}
$item['body'] = Post\Media::addAttachmentsToBody($item['uri-id'], $item['body']);
$item = Post\Media::addHTMLAttachmentToItem($item);
$jotplugins = '';

View File

@ -1639,6 +1639,8 @@ class Transmitter
$real_quote = false;
$item = Post\Media::addHTMLAttachmentToItem($item);
$body = $item['body'];
if ($type == 'Note') {

View File

@ -774,6 +774,7 @@ class DFRN
}
$body = Post\Media::addAttachmentsToBody($item['uri-id'], DI::contentItem()->addSharedPost($item));
$body = Post\Media::addHTMLAttachmentToBody($item['uri-id'], $body);
if ($item['private'] == Item::PRIVATE) {
$body = Item::fixPrivatePhotos($body, $owner['uid'], $item, $cid);

View File

@ -3326,6 +3326,7 @@ class Diaspora
$title = $item['title'];
$body = Post\Media::addAttachmentsToBody($item['uri-id'], DI::contentItem()->addSharedPost($item), $attach_media);
$body = Post\Media::addHTMLLinkToBody($item['uri-id'], $body);
// Fetch the title from an attached link - if there is one
if (empty($item['title']) && DI::pConfig()->get($owner['uid'], 'system', 'attach_link_title')) {
@ -3585,6 +3586,7 @@ class Diaspora
}
$body = Post\Media::addAttachmentsToBody($item['uri-id'], DI::contentItem()->addSharedPost($item));
$body = Post\Media::addHTMLLinkToBody($item['uri-id'], $body);
// The replied to autor mention is prepended for clarity if:
// - Item replied isn't yours

View File

@ -1124,6 +1124,7 @@ class Feed
XML::addElement($doc, $entry, 'title', html_entity_decode($title, ENT_QUOTES, 'UTF-8'));
$body = Post\Media::addAttachmentsToBody($item['uri-id'], DI::contentItem()->addSharedPost($item));
$body = Post\Media::addHTMLLinkToBody($item['uri-id'], $body);
$body = BBCode::convertForUriId($item['uri-id'], $body, BBCode::ACTIVITYPUB);

View File

@ -1515,6 +1515,7 @@ class OStatus
XML::addElement($doc, $entry, 'title', html_entity_decode($title, ENT_QUOTES, 'UTF-8'));
$body = Post\Media::addAttachmentsToBody($item['uri-id'], DI::contentItem()->addSharedPost($item));
$body = Post\Media::addHTMLLinkToBody($item['uri-id'], $body);
if (!empty($item['title'])) {
$body = '[b]' . $item['title'] . "[/b]\n\n" . $body;

View File

@ -114,6 +114,7 @@ class Notifier
// find ancestors
$condition = ['id' => $target_id, 'visible' => true];
$target_item = Post::selectFirst(Item::DELIVER_FIELDLIST, $condition);
$target_item = Post\Media::addHTMLAttachmentToItem($target_item);
if (!DBA::isResult($target_item) || !intval($target_item['parent'])) {
Logger::info('No target item', ['cmd' => $cmd, 'target' => $target_id]);