Merge pull request #10170 from annando/media-handling

Improved media handling
This commit is contained in:
Hypolite Petovan 2021-04-26 12:36:01 -04:00 committed by GitHub
commit 123e2060a3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 495 additions and 208 deletions

View file

@ -1,6 +1,6 @@
-- ------------------------------------------
-- Friendica 2021.03-rc (Red Hot Poker)
-- DB_UPDATE_VERSION 1413
-- Friendica 2021.06-dev (Siberian Iris)
-- DB_UPDATE_VERSION 1414
-- ------------------------------------------
@ -1037,6 +1037,13 @@ CREATE TABLE IF NOT EXISTS `post-media` (
`preview-height` smallint unsigned COMMENT 'Height of the preview picture',
`preview-width` smallint unsigned COMMENT 'Width of the preview picture',
`description` text COMMENT '',
`name` varchar(255) COMMENT 'Name of the media',
`author-url` varbinary(255) COMMENT 'URL of the author of the media',
`author-name` varchar(255) COMMENT 'Name of the author of the media',
`author-image` varbinary(255) COMMENT 'Image of the author of the media',
`publisher-url` varbinary(255) COMMENT 'URL of the publisher of the media',
`publisher-name` varchar(255) COMMENT 'Name of the publisher of the media',
`publisher-image` varbinary(255) COMMENT 'Image of the publisher of the media',
PRIMARY KEY(`id`),
UNIQUE INDEX `uri-id-url` (`uri-id`,`url`),
FOREIGN KEY (`uri-id`) REFERENCES `item-uri` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE

View file

@ -2508,7 +2508,8 @@ function api_format_messages($item, $recipient, $sender)
*/
function api_convert_item($item)
{
$body = $item['body'];
$body = api_add_attachments_to_body($item);
$entities = api_get_entitities($statustext, $body);
// Add pictures to the attachment array and remove them from the body
@ -2576,6 +2577,52 @@ function api_convert_item($item)
];
}
/**
* Add media attachments to the body
*
* @param array $item
* @return string body with added media
*/
function api_add_attachments_to_body(array $item)
{
$body = $item['body'];
foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::IMAGE, Post\Media::AUDIO, Post\Media::VIDEO]) as $media) {
if (Item::containsLink($item['body'], $media['url'])) {
continue;
}
if ($media['type'] == Post\Media::IMAGE) {
if (!empty($media['description'])) {
$body .= "\n[img=" . $media['url'] . ']' . $media['description'] .'[/img]';
} else {
$body .= "\n[img]" . $media['url'] .'[/img]';
}
} elseif ($media['type'] == Post\Media::AUDIO) {
$body .= "\n[audio]" . $media['url'] . "[/audio]\n";
} elseif ($media['type'] == Post\Media::VIDEO) {
$body .= "\n[video]" . $media['url'] . "[/video]\n";
}
}
if (strpos($body, '[/img]') !== false) {
return $body;
}
foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::HTML]) as $media) {
if (!empty($media['preview'])) {
$description = $media['description'] ?: $media['name'];
if (!empty($description)) {
$body .= "\n[img=" . $media['preview'] . ']' . $description .'[/img]';
} else {
$body .= "\n[img]" . $media['preview'] .'[/img]';
}
}
}
return $body;
}
/**
*
* @param string $body
@ -2604,7 +2651,11 @@ function api_get_attachments(&$body)
$imagedata = Images::getInfoFromURLCached($image);
if ($imagedata) {
if (DI::config()->get("system", "proxy_disabled")) {
$attachments[] = ["url" => $image, "mimetype" => $imagedata["mime"], "size" => $imagedata["size"]];
} else {
$attachments[] = ["url" => ProxyUtils::proxifyUrl($image, false), "mimetype" => $imagedata["mime"], "size" => $imagedata["size"]];
}
}
}
@ -2628,7 +2679,7 @@ function api_get_entitities(&$text, $bbcode)
preg_match_all("/\[img](.*?)\[\/img\]/ism", $bbcode, $images);
foreach ($images[1] as $image) {
$replace = ProxyUtils::proxifyUrl($image);
$replace = ProxyUtils::proxifyUrl($image, false);
$text = str_replace($image, $replace, $text);
}
return [];
@ -2743,7 +2794,7 @@ function api_get_entitities(&$text, $bbcode)
// If image cache is activated, then use the following sizes:
// thumb (150), small (340), medium (600) and large (1024)
if (!DI::config()->get("system", "proxy_disabled")) {
$media_url = ProxyUtils::proxifyUrl($url);
$media_url = ProxyUtils::proxifyUrl($url, false);
$sizes = [];
$scale = Images::getScalingDimensions($image[0], $image[1], 150);

View file

@ -45,6 +45,7 @@ use Friendica\Model\Contact;
use Friendica\Model\Conversation;
use Friendica\Model\FileTag;
use Friendica\Model\Item;
use Friendica\Model\ItemURI;
use Friendica\Model\Notification;
use Friendica\Model\Photo;
use Friendica\Model\Post;
@ -613,6 +614,8 @@ function item_post(App $a) {
$datarray['origin'] = $origin;
$datarray['object'] = $object;
$datarray["uri-id"] = ItemURI::getIdByURI($datarray["uri"]);
/*
* These fields are for the convenience of addons...
* 'self' if true indicates the owner is posting on their own wall

View file

@ -73,7 +73,7 @@ class PageInfo
// Additional link attachments are prepended before the existing [attachment] tag
$body = substr_replace($body, "\n[bookmark=" . $data['url'] . ']' . $linkTitle . "[/bookmark]\n", $existingAttachmentPos, 0);
} else {
$footer = PageInfo::getFooterFromData($data, $no_photos);
$footer = self::getFooterFromData($data, $no_photos);
$body = self::stripTrailingUrlFromBody($body, $data['url']);
$body .= "\n" . $footer;
}
@ -246,7 +246,7 @@ class PageInfo
* @param bool $searchNakedUrls Whether we should pick a naked URL (outside of BBCode tags) as a last resort
* @return string|null
*/
protected static function getRelevantUrlFromBody(string $body, bool $searchNakedUrls = false)
public static function getRelevantUrlFromBody(string $body, bool $searchNakedUrls = false)
{
$URLSearchString = 'https?://[^\[\]]*';

View file

@ -50,7 +50,7 @@ use Friendica\Util\XML;
class BBCode
{
// Update this value to the current date whenever changes are made to BBCode::convert
const VERSION = '2021-04-07';
const VERSION = '2021-04-24';
const INTERNAL = 0;
const API = 2;
@ -61,6 +61,7 @@ class BBCode
const BACKLINK = 8;
const ACTIVITYPUB = 9;
const ANCHOR = '<br class="anchor">';
/**
* Fetches attachment data that were generated the old way
*
@ -155,6 +156,7 @@ class BBCode
'image' => null,
'url' => '',
'author_name' => '',
'author_url' => '',
'provider_name' => '',
'provider_url' => '',
'title' => '',
@ -939,6 +941,26 @@ class BBCode
return $newbody;
}
/**
*
* @param string $text A BBCode string
* @return array share attributes
*/
public static function fetchShareAttributes($text)
{
$attributes = [];
if (!preg_match("/(.*?)\[share(.*?)\](.*)\[\/share\]/ism", $text, $matches)) {
return $attributes;
}
$attribute_string = $matches[2];
foreach (['author', 'profile', 'avatar', 'link', 'posted', 'guid'] as $field) {
preg_match("/$field=(['\"])(.+?)\\1/ism", $attribute_string, $matches);
$attributes[$field] = html_entity_decode($matches[2] ?? '', ENT_QUOTES, 'UTF-8');
}
return $attributes;
}
/**
* This function converts a [share] block to text according to a provided callback function whose signature is:
*
@ -1064,7 +1086,7 @@ class BBCode
'$guid' => $attributes['guid'],
'$network_name' => ContactSelector::networkToName($network, $attributes['profile']),
'$network_icon' => ContactSelector::networkToIcon($network, $attributes['profile']),
'$content' => self::setMentions(trim($content), 0, $network),
'$content' => self::setMentions(trim($content), 0, $network) . self::ANCHOR,
]);
break;
}

View file

@ -26,7 +26,6 @@ use Friendica\BaseFactory;
use Friendica\Content\Text\BBCode;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Model\Item;
use Friendica\Model\Post;
use Friendica\Model\Verb;
use Friendica\Network\HTTPException;

View file

@ -21,6 +21,7 @@
namespace Friendica\Model;
use Friendica\Content\PageInfo;
use Friendica\Content\Text\BBCode;
use Friendica\Content\Text\HTML;
use Friendica\Core\Hook;
@ -34,6 +35,7 @@ use Friendica\Core\Worker;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Model\Post;
use Friendica\Model\Post\Media;
use Friendica\Protocol\Activity;
use Friendica\Protocol\ActivityPub;
use Friendica\Protocol\Diaspora;
@ -177,6 +179,8 @@ class Item
while ($item = DBA::fetch($items)) {
if (!empty($fields['body'])) {
Post\Media::insertFromAttachmentData($item['uri-id'], $fields['body']);
$content_fields = ['raw-body' => trim($fields['raw-body'] ?? $fields['body'])];
// Remove all media attachments from the body and store them in the post-media table
@ -508,7 +512,7 @@ class Item
public static function isValid(array $item)
{
// When there is no content then we don't post it
if ($item['body'] . $item['title'] == '') {
if (($item['body'] . $item['title'] == '') && !Post\Media::existsByURIId($item['uri-id'])) {
Logger::notice('No body, no title.');
return false;
}
@ -955,6 +959,8 @@ class Item
self::setOwnerforResharedItem($item);
}
Post\Media::insertFromAttachmentData($item['uri-id'], $item['body']);
// Remove all media attachments from the body and store them in the post-media table
$item['raw-body'] = Post\Media::insertFromBody($item['uri-id'], $item['raw-body']);
$item['raw-body'] = self::setHashtags($item['raw-body']);
@ -2633,7 +2639,10 @@ class Item
unset($hook_data);
}
$orig_body = $item['body'];
$item['body'] = preg_replace("/\s*\[attachment .*\].*?\[\/attachment\]\s*/ism", '', $item['body']);
self::putInCache($item);
$item['body'] = $orig_body;
$s = $item["rendered-html"];
$hook_data = [
@ -2653,7 +2662,24 @@ class Item
return $s;
}
$s = self::addMediaAttachments($item, $s);
$shared = BBCode::fetchShareAttributes($item['body']);
if (!empty($shared['guid'])) {
$shared_item = Post::selectFirst(['uri-id', 'plink'], ['guid' => $shared['guid']]);
$shared_uri_id = $shared_item['uri-id'] ?? 0;
$shared_plink = $shared_item['plink'] ?? '';
$attachments = Post\Media::splitAttachments($shared_uri_id);
$s = self::addVisualAttachments($attachments, $item, $s, true);
$s = self::addLinkAttachment($attachments, $item, $s, true, '');
$s = self::addNonVisualAttachments($attachments, $item, $s, true);
} else {
$shared_uri_id = 0;
$shared_plink = '';
}
$attachments = Post\Media::splitAttachments($item['uri-id']);
$s = self::addVisualAttachments($attachments, $item, $s, false);
$s = self::addLinkAttachment($attachments, $item, $s, false, $shared_plink);
$s = self::addNonVisualAttachments($attachments, $item, $s, false);
// Map.
if (strpos($s, '<div class="map">') !== false && !empty($item['coord'])) {
@ -2678,45 +2704,55 @@ class Item
}
/**
* Add media attachments to the content
* Check if the body contains a link
*
* @param string $body
* @param string $url
* @return bool
*/
public static function containsLink(string $body, string $url)
{
if (strpos($body, $url)) {
return true;
}
foreach ([0, 1, 2] as $size) {
if (preg_match('#/photo/.*-' . $size . '\.#ism', $url) &&
strpos(preg_replace('#(/photo/.*)-[012]\.#ism', '$1-' . $size . '.', $body), $url)) {
return true;
}
}
return false;
}
/**
* Add visual attachments to the content
*
* @param array $attachments
* @param array $item
* @param string $content
* @return modified content
* @return string modified content
*/
private static function addMediaAttachments(array $item, string $content)
private static function addVisualAttachments(array $attachments, array $item, string $content, bool $shared)
{
$leading = '';
$trailing = '';
// currently deactivated the request for Post\Media::VIDEO since it creates mutliple videos from Peertube
foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::AUDIO,
Post\Media::DOCUMENT, Post\Media::TORRENT, Post\Media::UNKNOWN]) as $attachment) {
if (in_array($attachment['type'], [Post\Media::AUDIO, Post\Media::VIDEO]) && strpos($item['body'], $attachment['url'])) {
foreach ($attachments['visual'] as $attachment) {
if (self::containsLink($item['body'], $attachment['url'])) {
continue;
}
$mime = $attachment['mimetype'];
$author = ['uid' => 0, 'id' => $item['author-id'],
'network' => $item['author-network'], 'url' => $item['author-link']];
$the_url = Contact::magicLinkByContact($author, $attachment['url']);
$filetype = strtolower(substr($mime, 0, strpos($mime, '/')));
if ($filetype) {
$filesubtype = strtolower(substr($mime, strpos($mime, '/') + 1));
$filesubtype = str_replace('.', '-', $filesubtype);
} else {
$filetype = 'unkn';
$filesubtype = 'unkn';
}
if (($filetype == 'video')) {
if (($attachment['filetype'] == 'video')) {
/// @todo Move the template to /content as well
$media = Renderer::replaceMacros(Renderer::getMarkupTemplate('video_top.tpl'), [
'$video' => [
'id' => $item['author-id'],
'src' => $the_url,
'mime' => $mime,
'mime' => $attachment['mimetype'],
],
]);
if ($item['post-type'] == Item::PT_VIDEO) {
@ -2724,12 +2760,12 @@ class Item
} else {
$trailing .= $media;
}
} elseif ($filetype == 'audio') {
} elseif ($attachment['filetype'] == 'audio') {
$media = Renderer::replaceMacros(Renderer::getMarkupTemplate('content/audio.tpl'), [
'$audio' => [
'id' => $item['author-id'],
'src' => $the_url,
'mime' => $mime,
'mime' => $attachment['mimetype'],
],
]);
if ($item['post-type'] == Item::PT_AUDIO) {
@ -2737,7 +2773,110 @@ class Item
} else {
$trailing .= $media;
}
} elseif ($attachment['filetype'] == 'image') {
$media = Renderer::replaceMacros(Renderer::getMarkupTemplate('content/image.tpl'), [
'$image' => [
'src' => $the_url,
'attachment' => $attachment,
],
]);
$trailing .= $media;
}
}
if ($shared) {
$content = str_replace(BBCode::ANCHOR, '<div class="body-attach">' . $leading . '<div class="clear"></div></div>' . BBCode::ANCHOR, $content);
$content = str_replace(BBCode::ANCHOR, BBCode::ANCHOR . '<div class="body-attach">' . $trailing . '<div class="clear"></div></div>', $content);
} else {
if ($leading != '') {
$content = '<div class="body-attach">' . $leading . '<div class="clear"></div></div>' . $content;
}
if ($trailing != '') {
$content .= '<div class="body-attach">' . $trailing . '<div class="clear"></div></div>';
}
}
return $content;
}
/**
* Add link attachment to the content
*
* @param array $attachments
* @param array $item
* @param string $content
* @param bool $shared
* @return string modified content
*/
private static function addLinkAttachment(array $attachments, array $item, string $content, bool $shared, string $ignore_link)
{
// @ToDo Check only for audio and video
$preview = empty($attachments['visual']);
if (!empty($attachments['link'])) {
foreach ($attachments['link'] as $link) {
if (!Strings::compareLink($link['url'], $ignore_link)) {
$attachment = $link;
}
}
}
if (!empty($attachment)) {
$data = [
'author_img' => $attachment['author-image'] ?? '',
'author_name' => $attachment['author-name'] ?? '',
'author_url' => $attachment['author-url'] ?? '',
'publisher_img' => $attachment['publisher-image'] ?? '',
'publisher_name' => $attachment['publisher-name'] ?? '',
'publisher_url' => $attachment['publisher-url'] ?? '',
'text' => $attachment['description'] ?? '',
'title' => $attachment['name'] ?? '',
'type' => 'link',
'url' => $attachment['url'] ?? '',
];
if ($preview && !empty($attachment['preview']) && !empty($attachment['preview-height']) && !empty($attachment['preview-width'])) {
$data['images'][] = ['src' => $attachment['preview'],
'width' => $attachment['preview-width'], 'height' => $attachment['preview-height']];
}
$footer = PageInfo::getFooterFromData($data);
} elseif (preg_match("/.*(\[attachment.*?\].*?\[\/attachment\]).*/ism", $item['body'], $match)) {
$footer = $match[1];
}
if (!empty($footer)) {
// @todo Use a template
$rendered = BBCode::convert($footer);
if ($shared) {
return str_replace(BBCode::ANCHOR, BBCode::ANCHOR . $rendered, $content);
} else {
return $content . $rendered;
}
}
return $content;
}
/**
* Add non visual attachments to the content
*
* @param array $attachments
* @param array $item
* @param string $content
* @return string modified content
*/
private static function addNonVisualAttachments(array $attachments, array $item, string $content)
{
$trailing = '';
foreach ($attachments['additional'] as $attachment) {
if (strpos($item['body'], $attachment['url'])) {
continue;
}
$author = ['uid' => 0, 'id' => $item['author-id'],
'network' => $item['author-network'], 'url' => $item['author-link']];
$the_url = Contact::magicLinkByContact($author, $attachment['url']);
$title = Strings::escapeHtml(trim(($attachment['description'] ?? '') ?: $attachment['url']));
if (!empty($attachment['size'])) {
@ -2745,14 +2884,9 @@ class Item
}
/// @todo Use a template
$icon = '<div class="attachtype icon s22 type-' . $filetype . ' subtype-' . $filesubtype . '"></div>';
$icon = '<div class="attachtype icon s22 type-' . $attachment['filetype'] . ' subtype-' . $attachment['subtype'] . '"></div>';
$trailing .= '<a href="' . strip_tags($the_url) . '" title="' . $title . '" class="attachlink" target="_blank" rel="noopener noreferrer" >' . $icon . '</a>';
}
}
if ($leading != '') {
$content = '<div class="body-attach">' . $leading . '<div class="clear"></div></div>' . $content;
}
if ($trailing != '') {
$content .= '<div class="body-attach">' . $trailing . '<div class="clear"></div></div>';

View file

@ -76,6 +76,27 @@ class ItemURI
return self::insert(['uri' => $uri]);
}
return $itemuri['id'];
}
/**
* Searched for an id of a given guid.
*
* @param string $guid
*
* @return integer item-uri id
* @throws \Exception
*/
public static function getIdByGUID($guid)
{
// If the GUID gets too long we only take the first parts and hope for best
$guid = substr($guid, 0, 255);
$itemuri = DBA::selectFirst('item-uri', ['id'], ['guid' => $guid]);
if (!DBA::isResult($itemuri)) {
return 0;
}
return $itemuri['id'];
}
}

View file

@ -21,12 +21,15 @@
namespace Friendica\Model\Post;
use Friendica\Content\PageInfo;
use Friendica\Content\Text\BBCode;
use Friendica\Core\Logger;
use Friendica\Core\System;
use Friendica\Database\Database;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Util\Images;
use Friendica\Util\ParseUrl;
/**
* Class Media
@ -188,6 +191,20 @@ class Media
$media = self::addType($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['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;
}
@ -274,7 +291,7 @@ class Media
}
$body = str_replace($picture[0], '', $body);
$image = str_replace('-1.', '-0.', $picture[2]);
$attachments[] = ['uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $image,
$attachments[$image] = ['uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $image,
'preview' => $picture[2], 'description' => $picture[3]];
}
}
@ -282,7 +299,7 @@ class Media
if (preg_match_all("/\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures, PREG_SET_ORDER)) {
foreach ($pictures as $picture) {
$body = str_replace($picture[0], '', $body);
$attachments[] = ['uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $picture[1], 'description' => $picture[2]];
$attachments[$picture[1]] = ['uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $picture[1], 'description' => $picture[2]];
}
}
@ -293,7 +310,7 @@ class Media
}
$body = str_replace($picture[0], '', $body);
$image = str_replace('-1.', '-0.', $picture[2]);
$attachments[] = ['uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $image,
$attachments[$image] = ['uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $image,
'preview' => $picture[2], 'description' => null];
}
}
@ -301,24 +318,30 @@ class Media
if (preg_match_all("/\[img\]([^\[\]]*)\[\/img\]/ism", $body, $pictures, PREG_SET_ORDER)) {
foreach ($pictures as $picture) {
$body = str_replace($picture[0], '', $body);
$attachments[] = ['uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $picture[1]];
$attachments[$picture[1]] = ['uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $picture[1]];
}
}
if (preg_match_all("/\[audio\]([^\[\]]*)\[\/audio\]/ism", $body, $audios, PREG_SET_ORDER)) {
foreach ($audios as $audio) {
$body = str_replace($audio[0], '', $body);
$attachments[] = ['uri-id' => $uriid, 'type' => self::AUDIO, 'url' => $audio[1]];
$attachments[$audio[1]] = ['uri-id' => $uriid, 'type' => self::AUDIO, 'url' => $audio[1]];
}
}
if (preg_match_all("/\[video\]([^\[\]]*)\[\/video\]/ism", $body, $videos, PREG_SET_ORDER)) {
foreach ($videos as $video) {
$body = str_replace($video[0], '', $body);
$attachments[] = ['uri-id' => $uriid, 'type' => self::VIDEO, 'url' => $video[1]];
$attachments[$video[1]] = ['uri-id' => $uriid, 'type' => self::VIDEO, 'url' => $video[1]];
}
}
$url = PageInfo::getRelevantUrlFromBody($body);
if (!empty($url)) {
Logger::debug('Got page url', ['url' => $url]);
$attachments[$url] = ['uri-id' => $uriid, 'type' => self::UNKNOWN, 'url' => $url];
}
foreach ($attachments as $attachment) {
self::insert($attachment);
}
@ -326,6 +349,38 @@ class Media
return trim($body);
}
/**
* Add media links from the attachment field
*
* @param integer $uriid
* @param string $body
*/
public static function insertFromAttachmentData(int $uriid, string $body)
{
$data = BBCode::getAttachmentData($body);
if (empty($data)) {
return;
}
Logger::info('Adding attachment data', ['data' => $data]);
$attachment = [
'uri-id' => $uriid,
'type' => self::HTML,
'url' => $data['url'],
'preview' => $data['preview'] ?? null,
'description' => $data['description'] ?? null,
'name' => $data['title'] ?? null,
'author-url' => $data['author_url'] ?? null,
'author-name' => $data['author_name'] ?? null,
'publisher-url' => $data['provider_url'] ?? null,
'publisher-name' => $data['provider_name'] ?? null,
];
if (!empty($data['image'])) {
$attachment['preview'] = $data['image'];
}
self::insert($attachment);
}
/**
* Add media links from the attach field
*
@ -369,4 +424,67 @@ class Media
return DBA::selectToArray('post-media', [], $condition);
}
/**
* Checks if media attachments are associated with the provided item ID.
*
* @param int $uri_id
* @param array $types
* @return array
* @throws \Exception
*/
public static function existsByURIId(int $uri_id, array $types = [])
{
$condition = ['uri-id' => $uri_id];
if (!empty($types)) {
$condition = DBA::mergeConditions($condition, ['type' => $types]);
}
return DBA::exists('post-media', $condition);
}
/**
* Split the attachment media in the three segments "visual", "link" and "additional"
*
* @param int $uri_id
* @return array attachments
*/
public static function splitAttachments(int $uri_id)
{
$attachments = ['visual' => [], 'link' => [], 'additional' => []];
$media = self::getByURIId($uri_id);
if (empty($media)) {
return $attachments;
}
foreach ($media as $medium) {
$type = explode('/', current(explode(';', $medium['mimetype'])));
if (count($type) < 2) {
Logger::info('Unknown MimeType', ['type' => $type, 'media' => $medium]);
$filetype = 'unkn';
$subtype = 'unkn';
} else {
$filetype = strtolower($type[0]);
$subtype = strtolower($type[1]);
}
$medium['filetype'] = $filetype;
$medium['subtype'] = $subtype;
if ($medium['type'] == self::HTML || (($filetype == 'text') && ($subtype == 'html'))) {
$attachments['link'][] = $medium;
continue;
}
if (in_array($medium['type'], [self::AUDIO, self::VIDEO, self::IMAGE]) ||
in_array($filetype, ['audio', 'video', 'image'])) {
$attachments['visual'][] = $medium;
} else {
$attachments['additional'][] = $medium;
}
}
return $attachments;
}
}

View file

@ -135,114 +135,21 @@ class Processor
}
/**
* Add attachment data to the item array
* Stire attachment data
*
* @param array $activity
* @param array $item
*
* @return array array
*/
private static function constructAttachList($activity, $item)
private static function storeAttachments($activity, $item)
{
if (empty($activity['attachments'])) {
return $item;
return;
}
$leading = '';
$trailing = '';
foreach ($activity['attachments'] as $attach) {
switch ($attach['type']) {
case 'link':
$data = [
'url' => $attach['url'],
'type' => $attach['type'],
'title' => $attach['title'] ?? '',
'text' => $attach['desc'] ?? '',
'image' => $attach['image'] ?? '',
'images' => [],
'keywords' => [],
];
$item['body'] = PageInfo::appendDataToBody($item['body'], $data);
break;
default:
self::storeAttachmentAsMedia($item['uri-id'], $attach);
$filetype = strtolower(substr($attach['mediaType'], 0, strpos($attach['mediaType'], '/')));
if ($filetype == 'image') {
if (!empty($activity['source'])) {
foreach ([0, 1, 2] as $size) {
if (preg_match('#/photo/.*-' . $size . '\.#ism', $attach['url']) &&
strpos(preg_replace('#(/photo/.*)-[012]\.#ism', '$1-' . $size . '.', $activity['source']), $attach['url'])) {
continue 3;
}
}
if (strpos($activity['source'], $attach['url'])) {
continue 2;
}
}
// image is the preview/thumbnail URL
if (!empty($attach['image'])) {
$media = '[url=' . $attach['url'] . ']';
$attach['url'] = $attach['image'];
} else {
$media = '';
}
if (empty($attach['name'])) {
$media .= '[img]' . $attach['url'] . '[/img]';
} else {
$media .= '[img=' . $attach['url'] . ']' . $attach['name'] . '[/img]';
}
if (!empty($attach['image'])) {
$media .= '[/url]';
}
if ($item['post-type'] == Item::PT_IMAGE) {
$leading .= $media;
} else {
$trailing .= $media;
}
} elseif ($filetype == 'audio') {
if (!empty($activity['source']) && strpos($activity['source'], $attach['url'])) {
continue 2;
}
if ($item['post-type'] == Item::PT_AUDIO) {
$leading .= '[audio]' . $attach['url'] . "[/audio]\n";
} else {
$trailing .= '[audio]' . $attach['url'] . "[/audio]\n";
}
} elseif ($filetype == 'video') {
if (!empty($activity['source']) && strpos($activity['source'], $attach['url'])) {
continue 2;
}
if ($item['post-type'] == Item::PT_VIDEO) {
$leading .= '[video]' . $attach['url'] . "[/video]\n";
} else {
$trailing .= '[video]' . $attach['url'] . "[/video]\n";
}
}
}
}
if (!empty($leading) && !empty(trim($item['body']))) {
$item['body'] = $leading . "[hr]\n" . $item['body'];
} elseif (!empty($leading)) {
$item['body'] = $leading;
}
if (!empty($trailing) && !empty(trim($item['body']))) {
$item['body'] = $item['body'] . "\n[hr]" . $trailing;
} elseif (!empty($trailing)) {
$item['body'] = $trailing;
}
return $item;
}
/**
* Updates a message
@ -265,7 +172,7 @@ class Processor
$item = self::processContent($activity, $item);
$item = self::constructAttachList($activity, $item);
self::storeAttachments($activity, $item);
if (empty($item)) {
return;
@ -399,7 +306,7 @@ class Processor
$item['plink'] = $activity['alternate-url'] ?? $item['uri'];
$item = self::constructAttachList($activity, $item);
self::storeAttachments($activity, $item);
// We received the post via AP, so we set the protocol of the server to AP
$contact = Contact::getById($item['author-id'], ['gsid']);
@ -863,12 +770,12 @@ class Processor
$object = ActivityPub::fetchContent($url, $uid);
if (empty($object)) {
Logger::log('Activity ' . $url . ' was not fetchable, aborting.');
Logger::notice('Activity was not fetchable, aborting.', ['url' => $url]);
return '';
}
if (empty($object['id'])) {
Logger::log('Activity ' . $url . ' has got not id, aborting. ' . json_encode($object));
Logger::notice('Activity has got not id, aborting. ', ['url' => $url, 'object' => $object]);
return '';
}
@ -1019,7 +926,7 @@ class Processor
DBA::update('contact', ['hub-verify' => $activity['id'], 'protocol' => Protocol::ACTIVITYPUB], ['id' => $cid]);
}
Logger::log('Follow user ' . $uid . ' from contact ' . $cid . ' with id ' . $activity['id']);
Logger::notice('Follow user ' . $uid . ' from contact ' . $cid . ' with id ' . $activity['id']);
}
/**

View file

@ -22,7 +22,6 @@
namespace Friendica\Protocol;
use Friendica\Content\Feature;
use Friendica\Content\PageInfo;
use Friendica\Content\Text\BBCode;
use Friendica\Content\Text\Markdown;
use Friendica\Core\Cache\Duration;
@ -1067,7 +1066,7 @@ class Diaspora
* 'key' => The public key of the author
* @throws \Exception
*/
private static function message($guid, $server, $level = 0)
public static function message($guid, $server, $level = 0)
{
if ($level > 5) {
return false;
@ -2303,9 +2302,6 @@ class Diaspora
$item["body"] = self::replacePeopleGuid($item["body"], $item["author-link"]);
// Add OEmbed and other information to the body
$item["body"] = PageInfo::searchAndAppendToBody($item["body"], false, true);
return $item;
} else {
return $item;
@ -2489,7 +2485,6 @@ class Diaspora
Tag::storeFromBody($datarray['uri-id'], $datarray["body"]);
Post\Media::copy($original_item['uri-id'], $datarray['uri-id']);
$datarray["app"] = $original_item["app"];
$datarray["plink"] = self::plink($author, $guid);
@ -2733,8 +2728,6 @@ class Diaspora
if ($data->photo) {
foreach ($data->photo as $photo) {
self::storePhotoAsMedia($datarray['uri-id'], $photo);
$body = "[img]".XML::unescape($photo->remote_photo_path).
XML::unescape($photo->remote_photo_name)."[/img]\n".$body;
}
$datarray["object-type"] = Activity\ObjectType::IMAGE;
@ -2742,11 +2735,6 @@ class Diaspora
} else {
$datarray["object-type"] = Activity\ObjectType::NOTE;
$datarray["post-type"] = Item::PT_NOTE;
// Add OEmbed and other information to the body
if (!self::isHubzilla($contact["url"])) {
$body = PageInfo::searchAndAppendToBody($body, false, true);
}
}
/// @todo enable support for polls
@ -3378,6 +3366,36 @@ class Diaspora
return $eventdata;
}
/**
* Add media attachments to the body
*
* @param array $item
* @return string body
*/
private static function addAttachments(array $item)
{
$body = $item['body'];
foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::IMAGE, Post\Media::AUDIO, Post\Media::VIDEO]) as $media) {
if (Item::containsLink($item['body'], $media['url'])) {
continue;
}
if ($media['type'] == Post\Media::IMAGE) {
if (!empty($media['description'])) {
$body .= "\n[img=" . $media['url'] . ']' . $media['description'] .'[/img]';
} else {
$body .= "\n[img]" . $media['url'] .'[/img]';
}
} elseif ($media['type'] == Post\Media::AUDIO) {
$body .= "\n[audio]" . $media['url'] . "[/audio]\n";
} elseif ($media['type'] == Post\Media::VIDEO) {
$body .= "\n[video]" . $media['url'] . "[/video]\n";
}
}
return $body;
}
/**
* Create a post (status message or reshare)
*
@ -3418,7 +3436,7 @@ class Diaspora
$type = "reshare";
} else {
$title = $item["title"];
$body = $item["body"];
$body = self::addAttachments($item);
// Fetch the title from an attached link - if there is one
if (empty($item["title"]) && DI::pConfig()->get($owner['uid'], 'system', 'attach_link_title')) {
@ -3632,7 +3650,7 @@ class Diaspora
$thread_parent_item = Post::selectFirst(['guid', 'author-id', 'author-link', 'gravity'], ['uri' => $item['thr-parent'], 'uid' => $item['uid']]);
}
$body = $item["body"];
$body = self::addAttachments($item);
// The replied to autor mention is prepended for clarity if:
// - Item replied isn't yours

View file

@ -23,7 +23,6 @@ namespace Friendica\Protocol;
use DOMDocument;
use DOMXPath;
use Friendica\Content\PageInfo;
use Friendica\Content\Text\BBCode;
use Friendica\Content\Text\HTML;
use Friendica\Core\Cache\Duration;
@ -673,11 +672,6 @@ class OStatus
$item["body"] .= $add_body;
// Only add additional data when there is no picture in the post
if (!strstr($item["body"], '[/img]')) {
$item["body"] = PageInfo::searchAndAppendToBody($item["body"]);
}
Tag::storeFromBody($item['uri-id'], $item['body']);
// Mastodon Content Warning
@ -1098,7 +1092,9 @@ class OStatus
if (($item["object-type"] == Activity\ObjectType::QUESTION)
|| ($item["object-type"] == Activity\ObjectType::EVENT)
) {
$item["body"] .= "\n" . PageInfo::getFooterFromUrl($attribute['href']);
Post\Media::insert(['uri-id' => $item['uri-id'], 'type' => Post\Media::UNKNOWN,
'url' => $attribute['href'], 'mimetype' => $attribute['type'] ?? null,
'size' => $attribute['length'] ?? null, 'description' => $attribute['title'] ?? null]);
}
break;
case "ostatus:conversation":
@ -1125,7 +1121,9 @@ class OStatus
}
$link_data['related'] = $attribute['href'];
} else {
$item["body"] .= "\n" . PageInfo::getFooterFromUrl($attribute['href']);
Post\Media::insert(['uri-id' => $item['uri-id'], 'type' => Post\Media::UNKNOWN,
'url' => $attribute['href'], 'mimetype' => $attribute['type'] ?? null,
'size' => $attribute['length'] ?? null, 'description' => $attribute['title'] ?? null]);
}
break;
case "self":

View file

@ -197,7 +197,7 @@ class ParseUrl
];
if ($count > 10) {
Logger::log('Endless loop detected for ' . $url, Logger::DEBUG);
Logger::notice('Endless loop detected', ['url' => $url]);
return $siteinfo;
}
@ -297,7 +297,7 @@ class ParseUrl
// See https://github.com/friendica/friendica/issues/5470#issuecomment-418351211
$charset = str_ireplace('latin-1', 'latin1', $charset);
Logger::log('detected charset ' . $charset, Logger::DEBUG);
Logger::info('detected charset', ['charset' => $charset]);
$body = iconv($charset, 'UTF-8//TRANSLIT', $body);
}

View file

@ -55,7 +55,7 @@
use Friendica\Database\DBA;
if (!defined('DB_UPDATE_VERSION')) {
define('DB_UPDATE_VERSION', 1413);
define('DB_UPDATE_VERSION', 1414);
}
return [
@ -1092,6 +1092,13 @@ return [
"preview-height" => ["type" => "smallint unsigned", "comment" => "Height of the preview picture"],
"preview-width" => ["type" => "smallint unsigned", "comment" => "Width of the preview picture"],
"description" => ["type" => "text", "comment" => ""],
"name" => ["type" => "varchar(255)", "comment" => "Name of the media"],
"author-url" => ["type" => "varbinary(255)", "comment" => "URL of the author of the media"],
"author-name" => ["type" => "varchar(255)", "comment" => "Name of the author of the media"],
"author-image" => ["type" => "varbinary(255)", "comment" => "Image of the author of the media"],
"publisher-url" => ["type" => "varbinary(255)", "comment" => "URL of the publisher of the media"],
"publisher-name" => ["type" => "varchar(255)", "comment" => "Name of the publisher of the media"],
"publisher-image" => ["type" => "varbinary(255)", "comment" => "Image of the publisher of the media"],
],
"indexes" => [
"PRIMARY" => ["id"],

View file

@ -0,0 +1,2 @@
<img src="{{$image.src}}" alt="{{$image.attachment.description}}" title="{{$image.attachment.description}}">
<br>