Issue 12345: No link preview on DFRN posts

This commit is contained in:
Michael 2022-12-08 05:49:25 +00:00
parent b9cb94c432
commit 7d51fa47d5
6 changed files with 72 additions and 26 deletions

View File

@ -220,7 +220,7 @@ class Item
$content_fields['raw-body'] = Post\Media::insertFromBody($item['uri-id'], $content_fields['raw-body']);
$content_fields['raw-body'] = self::setHashtags($content_fields['raw-body']);
Post\Media::insertFromRelevantUrl($item['uri-id'], $content_fields['raw-body']);
Post\Media::insertFromRelevantUrl($item['uri-id'], $content_fields['raw-body'], $fields['body'], $item['author-network']);
Post\Content::update($item['uri-id'], $content_fields);
}
@ -1188,7 +1188,8 @@ class Item
$item['raw-body'] = Post\Media::insertFromBody($item['uri-id'], $item['raw-body']);
$item['raw-body'] = self::setHashtags($item['raw-body']);
Post\Media::insertFromRelevantUrl($item['uri-id'], $item['raw-body']);
$author = Contact::getById($item['author-id'], ['network']);
Post\Media::insertFromRelevantUrl($item['uri-id'], $item['raw-body'], $item['body'], $author['network'] ?? '');
// Check for hashtags in the body and repair or add hashtag links
$item['body'] = self::setHashtags($item['body']);

View File

@ -66,6 +66,7 @@ class Media
* Insert a post-media record
*
* @param array $media
* @param bool $force
* @return void
*/
public static function insert(array $media, bool $force = false)
@ -229,20 +230,9 @@ class Media
}
if ($media['type'] == self::HTML) {
$data = ParseUrl::getSiteinfoCached($media['url'], false);
$media['preview'] = $data['images'][0]['src'] ?? null;
$media['preview-height'] = $data['images'][0]['height'] ?? null;
$media['preview-width'] = $data['images'][0]['width'] ?? null;
$media['blurhash'] = $data['images'][0]['blurhash'] ?? null;
$media['description'] = $data['text'] ?? null;
$media['name'] = $data['title'] ?? null;
$media['author-url'] = $data['author_url'] ?? null;
$media['author-name'] = $data['author_name'] ?? null;
$media['author-image'] = $data['author_img'] ?? null;
$media['publisher-url'] = $data['publisher_url'] ?? null;
$media['publisher-name'] = $data['publisher_name'] ?? null;
$media['publisher-image'] = $data['publisher_img'] ?? null;
$media = self::addPage($media);
}
return $media;
}
@ -345,6 +335,31 @@ class Media
return $media;
}
/**
* Add page infos for HTML entries
*
* @param array $media
* @return array
*/
private static function addPage(array $media): array
{
$data = ParseUrl::getSiteinfoCached($media['url'], false);
$media['preview'] = $data['images'][0]['src'] ?? null;
$media['preview-height'] = $data['images'][0]['height'] ?? null;
$media['preview-width'] = $data['images'][0]['width'] ?? null;
$media['blurhash'] = $data['images'][0]['blurhash'] ?? null;
$media['description'] = $data['text'] ?? null;
$media['name'] = $data['title'] ?? null;
$media['author-url'] = $data['author_url'] ?? null;
$media['author-name'] = $data['author_name'] ?? null;
$media['author-image'] = $data['author_img'] ?? null;
$media['publisher-url'] = $data['publisher_url'] ?? null;
$media['publisher-name'] = $data['publisher_name'] ?? null;
$media['publisher-image'] = $data['publisher_img'] ?? null;
return $media;
}
/**
* Fetch media data from local resources
* @param array $media
@ -543,7 +558,7 @@ class Media
* @param string $body
* @return void
*/
public static function insertFromRelevantUrl(int $uriid, string $body)
public static function insertFromRelevantUrl(int $uriid, string $body, string $fullbody, string $network)
{
// Remove all hashtags and mentions
$body = preg_replace("/([#@!])\[url\=(.*?)\](.*?)\[\/url\]/ism", '', $body);
@ -552,7 +567,10 @@ class Media
if (preg_match_all("/\[url\](https?:.*?)\[\/url\]/ism", $body, $matches)) {
foreach ($matches[1] as $url) {
Logger::info('Got page url (link without description)', ['uri-id' => $uriid, 'url' => $url]);
self::insert(['uri-id' => $uriid, 'type' => self::UNKNOWN, 'url' => $url]);
self::insert(['uri-id' => $uriid, 'type' => self::UNKNOWN, 'url' => $url], false, $network);
if ($network == Protocol::DFRN) {
self::revertHTMLType($uriid, $url, $fullbody);
}
}
}
@ -560,10 +578,30 @@ class Media
if (preg_match_all("/\[url\=(https?:.*?)\].*?\[\/url\]/ism", $body, $matches)) {
foreach ($matches[1] as $url) {
Logger::info('Got page url (link with description)', ['uri-id' => $uriid, 'url' => $url]);
self::insert(['uri-id' => $uriid, 'type' => self::UNKNOWN, 'url' => $url]);
self::insert(['uri-id' => $uriid, 'type' => self::UNKNOWN, 'url' => $url], false, $network);
if ($network == Protocol::DFRN) {
self::revertHTMLType($uriid, $url, $fullbody);
}
}
}
}
/**
* Revert the media type of links to UNKNOWN for DFRN posts when they aren't attached
*
* @param integer $uriid
* @param string $url
* @param string $body
* @return void
*/
private static function revertHTMLType(int $uriid, string $url, string $body)
{
$attachment = BBCode::getAttachmentData($body);
if (!empty($attachment['url']) && Network::getUrlMatch($attachment['url'], $url)) {
return;
}
DBA::update('post-media', ['type' => self::UNKNOWN], ['uri-id' => $uriid, 'type' => self::HTML, 'url' => $url]);
}
/**
* Add media links from the attachment field
@ -633,7 +671,7 @@ class Media
*/
public static function getByURIId(int $uri_id, array $types = [])
{
$condition = ['uri-id' => $uri_id];
$condition = ["`uri-id` = ? AND `type` != ?", $uri_id, self::UNKNOWN];
if (!empty($types)) {
$condition = DBA::mergeConditions($condition, ['type' => $types]);
@ -652,7 +690,7 @@ class Media
*/
public static function existsByURIId(int $uri_id, array $types = []): bool
{
$condition = ['uri-id' => $uri_id];
$condition = ["`uri-id` = ? AND `type` != ?", $uri_id, self::UNKNOWN];
if (!empty($types)) {
$condition = DBA::mergeConditions($condition, ['type' => $types]);

View File

@ -734,6 +734,9 @@ class Image
public function getBlurHash(): string
{
$image = New Image($this->asString());
if (empty($image)) {
return '';
}
$width = $image->getWidth();
$height = $image->getHeight();
@ -749,7 +752,11 @@ class Image
$row = [];
for ($x = 0; $x < $width; ++$x) {
if ($image->isImagick()) {
try {
$colors = $image->image->getImagePixelColor($x, $y)->getColor();
} catch (\Throwable $th) {
return '';
}
$row[] = [$colors['r'], $colors['g'], $colors['b']];
} else {
$index = imagecolorat($image->image, $x, $y);

View File

@ -711,7 +711,7 @@ class DFRN
*/
private static function getAttachment($doc, $root, array $item)
{
foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::DOCUMENT, Post\Media::TORRENT, Post\Media::UNKNOWN]) as $attachment) {
foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::DOCUMENT, Post\Media::TORRENT]) as $attachment) {
$attributes = ['rel' => 'enclosure',
'href' => $attachment['url'],
'type' => $attachment['mimetype']];

View File

@ -499,7 +499,7 @@ class Diaspora
}
if (!($fields = self::validPosting($msg))) {
Logger::warning('Invalid posting');
Logger::warning('Invalid posting', ['msg' => $msg]);
return false;
}
@ -534,7 +534,7 @@ class Diaspora
if (is_null($fields)) {
$private = true;
if (!($fields = self::validPosting($msg))) {
Logger::warning('Invalid posting');
Logger::warning('Invalid posting', ['msg' => $msg]);
return false;
}
} else {
@ -3387,7 +3387,7 @@ class Diaspora
$body = '### ' . html_entity_decode($title) . "\n\n" . $body;
}
$attachments = Post\Media::getByURIId($item['uri-id'], [Post\Media::DOCUMENT, Post\Media::TORRENT, Post\Media::UNKNOWN]);
$attachments = Post\Media::getByURIId($item['uri-id'], [Post\Media::DOCUMENT, Post\Media::TORRENT]);
if (!empty($attachments)) {
$body .= "\n[hr]\n";
foreach ($attachments as $attachment) {

View File

@ -1186,7 +1186,7 @@ class OStatus
}
}
foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::DOCUMENT, Post\Media::TORRENT, Post\Media::UNKNOWN]) as $attachment) {
foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::DOCUMENT, Post\Media::TORRENT]) as $attachment) {
$attributes = ['rel' => 'enclosure',
'href' => $attachment['url'],
'type' => $attachment['mimetype']];