From a6322b1c4ed5067a6f883e83b7f87d9e451d3baa Mon Sep 17 00:00:00 2001 From: rabuzarus Date: Fri, 1 Feb 2019 23:42:36 +0100 Subject: [PATCH] attachment preview: some backend work in the item module: - convert attachment to bbcode and - add the attachment bbcode to the item body --- mod/item.php | 40 +++++++++++++++++++++++++++++++++++-- view/js/linkPreview.js | 45 ++++++++++++++++++++++++++++++++---------- 2 files changed, 73 insertions(+), 12 deletions(-) diff --git a/mod/item.php b/mod/item.php index 7cb2849a89..40c01da20f 100644 --- a/mod/item.php +++ b/mod/item.php @@ -40,6 +40,8 @@ use Friendica\Util\Emailer; use Friendica\Util\Security; use Friendica\Util\Strings; +require_once 'include/items.php'; + function item_post(App $a) { if (!local_user() && !remote_user()) { return 0; @@ -188,6 +190,40 @@ function item_post(App $a) { $categories = ''; $postopts = ''; $emailcc = ''; + $body = defaults($_REQUEST, 'body', ''); + $has_attachment = defaults($_REQUEST, 'has_attachment', 0); + + // If we have a speparate attachment, we need to add it to the body. + if (!empty($has_attachment)) { + $attachment_type = defaults($_REQUEST, 'attachment_type', ''); + $attachment_title = defaults($_REQUEST, 'attachment_title', ''); + $attachment_text = defaults($_REQUEST, 'attachment_text', ''); + + $attachment_url = hex2bin(defaults($_REQUEST, 'attachment_url', '')); + $attachment_img_src = hex2bin(defaults($_REQUEST, 'attachment_img_src', '')); + + $attachment_img_width = defaults($_REQUEST, 'attachment_img_width', 0); + $attachment_img_height = defaults($_REQUEST, 'attachment_img_height', 0); + $attachment = [ + 'type' => $attachment_type, + 'title' => $attachment_title, + 'text' => $attachment_text, + 'url' => $attachment_url, + ]; + + if (!empty($attachment_img_src)) { + $attachment['images'] = [ + 0 => [ + 'src' => $attachment_img_src, + 'width' => $attachment_img_width, + 'height' => $attachment_img_height + ] + ]; + } + + $att_bbcode = add_page_info_data($attachment); + $body .= $att_bbcode; + } if (!empty($orig_post)) { $str_group_allow = $orig_post['allow_gid']; @@ -201,7 +237,7 @@ function item_post(App $a) { $app = $orig_post['app']; $categories = $orig_post['file']; $title = Strings::escapeTags(trim($_REQUEST['title'])); - $body = Strings::escapeHtml(trim($_REQUEST['body'])); + $body = Strings::escapeHtml(trim($body)); $private = $orig_post['private']; $pubmail_enabled = $orig_post['pubmail']; $network = $orig_post['network']; @@ -237,7 +273,7 @@ function item_post(App $a) { $coord = Strings::escapeTags(trim(defaults($_REQUEST, 'coord' , ''))); $verb = Strings::escapeTags(trim(defaults($_REQUEST, 'verb' , ''))); $emailcc = Strings::escapeTags(trim(defaults($_REQUEST, 'emailcc' , ''))); - $body = Strings::escapeHtml(trim(defaults($_REQUEST, 'body' , ''))); + $body = Strings::escapeHtml(trim($body)); $network = Strings::escapeTags(trim(defaults($_REQUEST, 'network' , Protocol::DFRN))); $guid = System::createUUID(); diff --git a/view/js/linkPreview.js b/view/js/linkPreview.js index dcc1677cc5..ab5091c7e7 100644 --- a/view/js/linkPreview.js +++ b/view/js/linkPreview.js @@ -16,8 +16,13 @@ selector = selector.substr(1); var previewTpl = '\ -
{1}
\ -
'; +
\ + {1}\ + \ + \ + \ +
'; + var attachmentTpl = '\
\
\ @@ -32,6 +37,9 @@
\
\ \ + \ + \ + \
\
\

\ @@ -73,8 +81,7 @@ }); }; var resetPreview = function() { - $('#previewChangeImg_' + selector).removeClass('buttonChangeActive'); - $('#previewChangeImg_' + selector).addClass('buttonChangeDeactive'); + $('#hasAttachment_' + selector).val(0); photoNumber = 0; images = ""; } @@ -90,6 +97,12 @@ isExtern = true; } + // Don't procces the textarea input if we have already + // an attachment preview. + if (!isExtern && isActive) { + return; + } + if (trim(text) !== "") { if (block === false && urlRegex.test(text)) { binurl = bin2hex(text); @@ -186,10 +199,12 @@ // Note: if we finish the Preview of other media content type, // we can move this condition to the beggining of crawlText(); if (isActive) { + $('#profile-rotator').hide(); return; } if (json.type != 'link' && json.type != 'video' && json.type != 'photo' || json.url == json.title) { + $('#profile-rotator').hide(); return; } @@ -203,7 +218,13 @@ // Load and add the template if it isn't allready loaded. if ($('#preview_' + selector).length == 0) { - var tpl = previewTpl.format(typeClass, attachmentTpl); + var tpl = previewTpl.format( + typeClass, + attachmentTpl, + 1, + bin2hex(json.url), + json.type + ); $('#' + selector).after(tpl); } @@ -215,13 +236,12 @@ $('#previewTitle_' + selector).html("\ " + escapeHTML(json.title) + "\ - " + " ); - $('#previewDescription_' + selector).html("\ " + escapeHTML(description) + "\n\ - " + " ); if (json.url) { @@ -235,6 +255,9 @@ if (Array.isArray(images)) { $('#previewImages_' + selector).show(); + $('#attachmentImageSrc_' + selector).val(bin2hex(images[photoNumber].src)); + $('#attachmentImageWidth_' + selector).val(images[photoNumber].width); + $('#attachmentImageHeight_' + selector).val(images[photoNumber].height); } else { $('#previewImages_' + selector).hide(); } @@ -259,7 +282,7 @@ $('#previewImage_' + selector).html(appendImage + ""); - // more than just one image. + // More than just one image. if (images.length > 1) { // Enable the the button to change the preview pictures. $('#previewChangeImg_' + selector).show(); @@ -270,7 +293,6 @@ $('#previewChangeImg_' + selector).unbind('click').click(function (e) { e.stopPropagation(); if (images.length > 1) { -// photoNumber = parseInt($('#photoNumber_' + selector).val()); $('#imagePreview_' + selector + '_' + photoNumber).css({ 'display': 'none' }); @@ -285,6 +307,9 @@ 'display': 'block' }); $('#photoNumber_' + selector).val(photoNumber); + $('#attachmentImageSrc_' + selector).val(bin2hex(images[photoNumber].src)); + $('#attachmentImageWidth_' + selector).val(images[photoNumber].width); + $('#attachmentImageHeight_' + selector).val(images[photoNumber].height); } }); }