Twitter: Attachments are stripped from the body / Posting connectors: Add attachments to the body

This commit is contained in:
Michael 2021-05-01 08:59:14 +00:00
parent f0d88c6370
commit 85554bb6fd
9 changed files with 56 additions and 79 deletions

View file

@ -18,6 +18,7 @@ use Friendica\Core\Session;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Core\Worker; use Friendica\Core\Worker;
use Friendica\DI; use Friendica\DI;
use Friendica\Model\Post;
function diaspora_install() function diaspora_install()
{ {
@ -197,6 +198,8 @@ function diaspora_send(App $a, array &$b)
return; return;
} }
$b['body'] = Post\Media::addAttachmentsToBody($b['uri-id'], $b['body']);
// Dont't post if the post doesn't belong to us. // Dont't post if the post doesn't belong to us.
// This is a check for forum postings // This is a check for forum postings
$self = DBA::selectFirst('contact', ['id'], ['uid' => $b['uid'], 'self' => true]); $self = DBA::selectFirst('contact', ['id'], ['uid' => $b['uid'], 'self' => true]);

View file

@ -14,6 +14,7 @@ use Friendica\Core\Hook;
use Friendica\Core\Logger; use Friendica\Core\Logger;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\DI; use Friendica\DI;
use Friendica\Model\Post;
use Friendica\Model\Tag; use Friendica\Model\Tag;
use Friendica\Util\DateTimeFormat; use Friendica\Util\DateTimeFormat;
use Friendica\Util\XML; use Friendica\Util\XML;
@ -159,6 +160,8 @@ function dwpost_send(App $a, array &$b)
return; return;
} }
$b['body'] = Post\Media::addAttachmentsToBody($b['uri-id'], $b['body']);
/* /*
* dreamwidth post in the LJ user's timezone. * dreamwidth post in the LJ user's timezone.
* Hopefully the person's Friendica account * Hopefully the person's Friendica account

View file

@ -12,6 +12,7 @@ use Friendica\Core\Hook;
use Friendica\Core\Logger; use Friendica\Core\Logger;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\DI; use Friendica\DI;
use Friendica\Model\Post;
function libertree_install() function libertree_install()
{ {
@ -191,6 +192,8 @@ function libertree_send(&$a,&$b) {
return; return;
} }
$b['body'] = Post\Media::addAttachmentsToBody($b['uri-id'], $b['body']);
$ltree_api_token = DI::pConfig()->get($b['uid'],'libertree','libertree_api_token'); $ltree_api_token = DI::pConfig()->get($b['uid'],'libertree','libertree_api_token');
$ltree_url = DI::pConfig()->get($b['uid'],'libertree','libertree_url'); $ltree_url = DI::pConfig()->get($b['uid'],'libertree','libertree_url');
$ltree_blog = "$ltree_url/api/v1/posts/create/?token=$ltree_api_token"; $ltree_blog = "$ltree_url/api/v1/posts/create/?token=$ltree_api_token";

View file

@ -12,6 +12,7 @@ use Friendica\Content\Text\BBCode;
use Friendica\Core\Hook; use Friendica\Core\Hook;
use Friendica\Core\Logger; use Friendica\Core\Logger;
use Friendica\DI; use Friendica\DI;
use Friendica\Model\Post;
use Friendica\Model\Tag; use Friendica\Model\Tag;
use Friendica\Util\DateTimeFormat; use Friendica\Util\DateTimeFormat;
use Friendica\Util\XML; use Friendica\Util\XML;
@ -153,6 +154,8 @@ function ljpost_send(&$a,&$b) {
if($b['parent'] != $b['id']) if($b['parent'] != $b['id'])
return; return;
$b['body'] = Post\Media::addAttachmentsToBody($b['uri-id'], $b['body']);
// LiveJournal post in the LJ user's timezone. // LiveJournal post in the LJ user's timezone.
// Hopefully the person's Friendica account // Hopefully the person's Friendica account
// will be set to the same thing. // will be set to the same thing.

View file

@ -436,6 +436,8 @@ function pumpio_send(App $a, array &$b)
Logger::log("pumpio_send: parameter ".print_r($b, true), Logger::DATA); Logger::log("pumpio_send: parameter ".print_r($b, true), Logger::DATA);
$b['body'] = Post\Media::addAttachmentsToBody($b['uri-id'], $b['body']);
if ($b['parent'] != $b['id']) { if ($b['parent'] != $b['id']) {
// Looking if its a reply to a pumpio post // Looking if its a reply to a pumpio post
$condition = ['id' => $b['parent'], 'network' => Protocol::PUMPIO]; $condition = ['id' => $b['parent'], 'network' => Protocol::PUMPIO];

View file

@ -481,6 +481,8 @@ function statusnet_post_hook(App $a, &$b)
return; return;
} }
$b['body'] = Post\Media::addAttachmentsToBody($b['uri-id'], $b['body']);
$api = DI::pConfig()->get($b["uid"], 'statusnet', 'baseapi'); $api = DI::pConfig()->get($b["uid"], 'statusnet', 'baseapi');
$hostname = preg_replace("=https?://([\w\.]*)/.*=ism", "$1", $api); $hostname = preg_replace("=https?://([\w\.]*)/.*=ism", "$1", $api);

View file

@ -16,6 +16,7 @@ use Friendica\Core\Logger;
use Friendica\Core\Renderer; use Friendica\Core\Renderer;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\DI; use Friendica\DI;
use Friendica\Model\Post;
use Friendica\Model\Tag; use Friendica\Model\Tag;
use Friendica\Util\Strings; use Friendica\Util\Strings;
@ -366,6 +367,8 @@ function tumblr_send(App $a, array &$b) {
return; return;
} }
$b['body'] = Post\Media::addAttachmentsToBody($b['uri-id'], $b['body']);
$oauth_token = DI::pConfig()->get($b['uid'], "tumblr", "oauth_token"); $oauth_token = DI::pConfig()->get($b['uid'], "tumblr", "oauth_token");
$oauth_token_secret = DI::pConfig()->get($b['uid'], "tumblr", "oauth_token_secret"); $oauth_token_secret = DI::pConfig()->get($b['uid'], "tumblr", "oauth_token_secret");
$page = DI::pConfig()->get($b['uid'], "tumblr", "page"); $page = DI::pConfig()->get($b['uid'], "tumblr", "page");

View file

@ -66,8 +66,6 @@ use Abraham\TwitterOAuth\TwitterOAuth;
use Abraham\TwitterOAuth\TwitterOAuthException; use Abraham\TwitterOAuth\TwitterOAuthException;
use Codebird\Codebird; use Codebird\Codebird;
use Friendica\App; use Friendica\App;
use Friendica\Content\OEmbed;
use Friendica\Content\PageInfo;
use Friendica\Content\Text\BBCode; use Friendica\Content\Text\BBCode;
use Friendica\Content\Text\Plaintext; use Friendica\Content\Text\Plaintext;
use Friendica\Core\Hook; use Friendica\Core\Hook;
@ -544,6 +542,8 @@ function twitter_post_hook(App $a, array &$b)
return; return;
} }
$b['body'] = Post\Media::addAttachmentsToBody($b['uri-id'], $b['body']);
$thr_parent = null; $thr_parent = null;
if ($b['parent'] != $b['id']) { if ($b['parent'] != $b['id']) {
@ -1356,13 +1356,13 @@ function twitter_fetchuser($screen_name)
* *
* @param string $body * @param string $body
* @param stdClass $status * @param stdClass $status
* @param string $picture
* @return array * @return array
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/ */
function twitter_expand_entities($body, stdClass $status, $picture) function twitter_expand_entities($body, stdClass $status)
{ {
$plain = $body; $plain = $body;
$contains_urls = false;
$taglist = []; $taglist = [];
@ -1403,40 +1403,17 @@ function twitter_expand_entities($body, stdClass $status, $picture)
continue; continue;
} }
$contains_urls = true;
$expanded_url = $url->expanded_url; $expanded_url = $url->expanded_url;
$final_url = DI::httpRequest()->finalUrl($url->expanded_url);
$oembed_data = OEmbed::fetchURL($final_url);
$type = $oembed_data->type ?? '';
// Quickfix: Workaround for URL with '[' and ']' in it // Quickfix: Workaround for URL with '[' and ']' in it
if (strpos($expanded_url, '[') || strpos($expanded_url, ']')) { if (strpos($expanded_url, '[') || strpos($expanded_url, ']')) {
$expanded_url = $url->url; $expanded_url = $url->url;
} }
if ($type === 'photo' && !empty($oembed_data->url)) {
$replace = '[url=' . $expanded_url . '][img]' . $oembed_data->url . '[/img][/url]';
} elseif ($type === 'link') {
$curlResult = DI::httpRequest()->head($final_url, ['timeout' => 4]);
if ($curlResult->isSuccess()) {
$mimetype = $curlResult->getHeader('Content-Type');
} else {
$mimetype = '';
}
if (substr($mimetype, 0, 6) == 'image/') {
$replace = '[img]' . $final_url . '[/img]';
} else {
$replace = '[url=' . $expanded_url . ']' . $url->display_url . '[/url]';
}
} else {
$replace = '[url=' . $expanded_url . ']' . $url->display_url . '[/url]';
}
$replacementList[$url->indices[0]] = [ $replacementList[$url->indices[0]] = [
'replace' => $replace, 'replace' => '[url=' . $expanded_url . ']' . $url->display_url . '[/url]',
'length' => $url->indices[1] - $url->indices[0], 'length' => $url->indices[1] - $url->indices[0],
]; ];
} }
@ -1450,14 +1427,7 @@ function twitter_expand_entities($body, stdClass $status, $picture)
$body = trim($body); $body = trim($body);
// Footer will be taken care of with a share block in the case of a quote return ['body' => trim($body), 'plain' => trim($plain), 'taglist' => $taglist, 'urls' => $contains_urls];
if (empty($status->quoted_status)) {
if ($picture) {
$body .= "\n\n[img]" . $picture . "[/img]\n";
}
}
return ['body' => trim($body), 'plain' => trim($plain), 'taglist' => $taglist];
} }
/** /**
@ -1474,15 +1444,9 @@ function twitter_store_attachments(int $uriid, $post)
case 'photo': case 'photo':
$attachment = ['uri-id' => $uriid, 'type' => Post\Media::IMAGE]; $attachment = ['uri-id' => $uriid, 'type' => Post\Media::IMAGE];
// @todo In the future store the large picture. $attachment['url'] = $medium->media_url_https . '?name=large';
// This can be done when we don't embed the pictures in the body anymore. $attachment['width'] = $medium->sizes->large->w;
//$attachment['url'] = $medium->media_url_https . '?name=large'; $attachment['height'] = $medium->sizes->large->h;
//$attachment['width'] = $medium->sizes->large->w;
//$attachment['height'] = $medium->sizes->large->h;
$attachment['url'] = $medium->media_url_https;
$attachment['width'] = $medium->sizes->medium->w;
$attachment['height'] = $medium->sizes->medium->h;
if ($medium->sizes->small->w != $attachment['width']) { if ($medium->sizes->small->w != $attachment['width']) {
$attachment['preview'] = $medium->media_url_https . '?name=small'; $attachment['preview'] = $medium->media_url_https . '?name=small';
@ -1535,35 +1499,17 @@ function twitter_store_attachments(int $uriid, $post)
/** /**
* @brief Fetch media entities and add media links to the body * @brief Fetch media entities and add media links to the body
* *
* @param object $post Twitter object with the post * @param object $post Twitter object with the post
* @param array $postarray Array of the item that is about to be posted * @param array $postarray Array of the item that is about to be posted
* * @param integer $uriid URI Id used to store tags. -1 = don't store tags for this post.
* @return $picture string Image URL or empty string
*/ */
function twitter_media_entities($post, array &$postarray) function twitter_media_entities($post, array &$postarray, int $uriid = -1)
{ {
// There are no media entities? So we quit. // There are no media entities? So we quit.
if (empty($post->extended_entities->media)) { if (empty($post->extended_entities->media)) {
return ''; return;
} }
// When the post links to an external page, we only take one picture.
// We only do this when there is exactly one media.
if ((count($post->entities->urls) > 0) && (count($post->extended_entities->media) == 1)) {
$medium = $post->extended_entities->media[0];
$picture = '';
foreach ($post->entities->urls as $link) {
// Let's make sure the external link url matches the media url
if ($medium->url == $link->url && isset($medium->media_url_https)) {
$picture = $medium->media_url_https;
$postarray['body'] = str_replace($medium->url, '', $postarray['body']);
return $picture;
}
}
}
// This is a pure media post, first search for all media urls // This is a pure media post, first search for all media urls
$media = []; $media = [];
foreach ($post->extended_entities->media AS $medium) { foreach ($post->extended_entities->media AS $medium) {
@ -1583,7 +1529,9 @@ function twitter_media_entities($post, array &$postarray)
$postarray['post-type'] = Item::PT_IMAGE; $postarray['post-type'] = Item::PT_IMAGE;
break; break;
case 'video': case 'video':
$postarray['post-type'] = Item::PT_VIDEO; // Currently deactivated, since this causes the video to be display before the content
// We have to figure out a better way for declaring the post type and the display style.
//$postarray['post-type'] = Item::PT_VIDEO;
case 'animated_gif': case 'animated_gif':
if (!empty($medium->ext_alt_text)) { if (!empty($medium->ext_alt_text)) {
Logger::info('Got text description', ['alt_text' => $medium->ext_alt_text]); Logger::info('Got text description', ['alt_text' => $medium->ext_alt_text]);
@ -1604,18 +1552,20 @@ function twitter_media_entities($post, array &$postarray)
} }
} }
break; break;
// The following code will only be activated for test reasons
//default:
// $postarray['body'] .= print_r($medium, true);
} }
} }
if ($uriid != -1) {
foreach ($media AS $key => $value) {
$postarray['body'] = str_replace($key, '', $postarray['body']);
}
return;
}
// Now we replace the media urls. // Now we replace the media urls.
foreach ($media AS $key => $value) { foreach ($media AS $key => $value) {
$postarray['body'] = str_replace($key, "\n" . $value . "\n", $postarray['body']); $postarray['body'] = str_replace($key, "\n" . $value . "\n", $postarray['body']);
} }
return '';
} }
/** /**
@ -1737,9 +1687,15 @@ function twitter_createpost(App $a, $uid, $post, array $self, $create_user, $onl
} }
// Search for media links // Search for media links
$picture = twitter_media_entities($post, $postarray); twitter_media_entities($post, $postarray, $uriid);
$converted = twitter_expand_entities($postarray['body'], $post);
// When the post contains external links then images or videos are just "decorations".
if (!empty($converted['urls'])) {
$postarray['post-type'] = Item::PT_NOTE;
}
$converted = twitter_expand_entities($postarray['body'], $post, $picture);
$postarray['body'] = $converted['body']; $postarray['body'] = $converted['body'];
$postarray['created'] = DateTimeFormat::utc($post->created_at); $postarray['created'] = DateTimeFormat::utc($post->created_at);
$postarray['edited'] = DateTimeFormat::utc($post->created_at); $postarray['edited'] = DateTimeFormat::utc($post->created_at);

View file

@ -12,7 +12,7 @@ use Friendica\Core\Hook;
use Friendica\Core\Logger; use Friendica\Core\Logger;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\DI; use Friendica\DI;
use Friendica\Util\Strings; use Friendica\Model\Post;
use Friendica\Util\XML; use Friendica\Util\XML;
function wppost_install() function wppost_install()
@ -223,6 +223,8 @@ function wppost_send(&$a, &$b)
return; return;
} }
$b['body'] = Post\Media::addAttachmentsToBody($b['uri-id'], $b['body']);
$wp_username = XML::escape(DI::pConfig()->get($b['uid'], 'wppost', 'wp_username')); $wp_username = XML::escape(DI::pConfig()->get($b['uid'], 'wppost', 'wp_username'));
$wp_password = XML::escape(DI::pConfig()->get($b['uid'], 'wppost', 'wp_password')); $wp_password = XML::escape(DI::pConfig()->get($b['uid'], 'wppost', 'wp_password'));
$wp_blog = DI::pConfig()->get($b['uid'],'wppost','wp_blog'); $wp_blog = DI::pConfig()->get($b['uid'],'wppost','wp_blog');