Merge pull request #8203 from MrPetovan/task/4599-frio-jot-async

[frio] Add new asynchronous submission of modal form
This commit is contained in:
Philipp 2020-01-30 06:42:12 +01:00 committed by GitHub
commit 4c07c725b6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 107 additions and 48 deletions

View file

@ -1214,6 +1214,7 @@ function status_editor(App $a, $x, $notes_cid = 0, $popup = false)
'$return_path' => $query_str, '$return_path' => $query_str,
'$action' => 'item', '$action' => 'item',
'$share' => ($x['button'] ?? '') ?: DI::l10n()->t('Share'), '$share' => ($x['button'] ?? '') ?: DI::l10n()->t('Share'),
'$loading' => DI::l10n()->t('Loading...'),
'$upload' => DI::l10n()->t('Upload photo'), '$upload' => DI::l10n()->t('Upload photo'),
'$shortupload' => DI::l10n()->t('upload photo'), '$shortupload' => DI::l10n()->t('upload photo'),
'$attach' => DI::l10n()->t('Attach file'), '$attach' => DI::l10n()->t('Attach file'),

View file

@ -70,6 +70,7 @@ function editpost_content(App $a)
'$return_path' => '/display/' . $item['guid'], '$return_path' => '/display/' . $item['guid'],
'$action' => 'item', '$action' => 'item',
'$share' => DI::l10n()->t('Save'), '$share' => DI::l10n()->t('Save'),
'$loading' => DI::l10n()->t('Loading...'),
'$upload' => DI::l10n()->t('Upload photo'), '$upload' => DI::l10n()->t('Upload photo'),
'$shortupload' => DI::l10n()->t('upload photo'), '$shortupload' => DI::l10n()->t('upload photo'),
'$attach' => DI::l10n()->t('Attach file'), '$attach' => DI::l10n()->t('Attach file'),

View file

@ -33,6 +33,7 @@ use Friendica\Model\FileTag;
use Friendica\Model\Item; use Friendica\Model\Item;
use Friendica\Model\Photo; use Friendica\Model\Photo;
use Friendica\Model\Term; use Friendica\Model\Term;
use Friendica\Network\HTTPException;
use Friendica\Object\EMail\ItemCCEMail; use Friendica\Object\EMail\ItemCCEMail;
use Friendica\Protocol\Activity; use Friendica\Protocol\Activity;
use Friendica\Protocol\Diaspora; use Friendica\Protocol\Diaspora;
@ -45,7 +46,7 @@ require_once __DIR__ . '/../include/items.php';
function item_post(App $a) { function item_post(App $a) {
if (!Session::isAuthenticated()) { if (!Session::isAuthenticated()) {
return 0; throw new HTTPException\ForbiddenException();
} }
$uid = local_user(); $uid = local_user();
@ -54,13 +55,12 @@ function item_post(App $a) {
$arr_drop = explode(',', $_REQUEST['dropitems']); $arr_drop = explode(',', $_REQUEST['dropitems']);
drop_items($arr_drop); drop_items($arr_drop);
$json = ['success' => 1]; $json = ['success' => 1];
echo json_encode($json); System::jsonExit($json);
exit();
} }
Hook::callAll('post_local_start', $_REQUEST); Hook::callAll('post_local_start', $_REQUEST);
Logger::log('postvars ' . print_r($_REQUEST, true), Logger::DATA); Logger::debug('postvars', ['_REQUEST' => $_REQUEST]);
$api_source = $_REQUEST['api_source'] ?? false; $api_source = $_REQUEST['api_source'] ?? false;
@ -76,7 +76,7 @@ function item_post(App $a) {
*/ */
if (!$preview && !empty($_REQUEST['post_id_random'])) { if (!$preview && !empty($_REQUEST['post_id_random'])) {
if (!empty($_SESSION['post-random']) && $_SESSION['post-random'] == $_REQUEST['post_id_random']) { if (!empty($_SESSION['post-random']) && $_SESSION['post-random'] == $_REQUEST['post_id_random']) {
Logger::log("item post: duplicate post", Logger::DEBUG); Logger::info('item post: duplicate post');
item_post_return(DI::baseUrl(), $api_source, $return_path); item_post_return(DI::baseUrl(), $api_source, $return_path);
} else { } else {
$_SESSION['post-random'] = $_REQUEST['post_id_random']; $_SESSION['post-random'] = $_REQUEST['post_id_random'];
@ -119,11 +119,11 @@ function item_post(App $a) {
} }
if (!DBA::isResult($toplevel_item)) { if (!DBA::isResult($toplevel_item)) {
notice(DI::l10n()->t('Unable to locate original post.') . EOL); notice(DI::l10n()->t('Unable to locate original post.'));
if (!empty($_REQUEST['return'])) { if ($return_path) {
DI::baseUrl()->redirect($return_path); DI::baseUrl()->redirect($return_path);
} }
exit(); throw new HTTPException\NotFoundException(DI::l10n()->t('Unable to locate original post.'));
} }
$toplevel_item_id = $toplevel_item['id']; $toplevel_item_id = $toplevel_item['id'];
@ -133,7 +133,7 @@ function item_post(App $a) {
} }
if ($toplevel_item_id) { if ($toplevel_item_id) {
Logger::info('mod_item: item_post parent=' . $toplevel_item_id); Logger::info('mod_item: item_post', ['parent' => $toplevel_item_id]);
} }
$post_id = intval($_REQUEST['post_id'] ?? 0); $post_id = intval($_REQUEST['post_id'] ?? 0);
@ -156,7 +156,7 @@ function item_post(App $a) {
// Check for multiple posts with the same message id (when the post was created via API) // Check for multiple posts with the same message id (when the post was created via API)
if (($message_id != '') && ($profile_uid != 0)) { if (($message_id != '') && ($profile_uid != 0)) {
if (Item::exists(['uri' => $message_id, 'uid' => $profile_uid])) { if (Item::exists(['uri' => $message_id, 'uid' => $profile_uid])) {
Logger::log("Message with URI ".$message_id." already exists for user ".$profile_uid, Logger::DEBUG); Logger::info('Message already exists for user', ['uri' => $message_id, 'uid' => $profile_uid]);
return 0; return 0;
} }
} }
@ -166,13 +166,12 @@ function item_post(App $a) {
// Now check that valid personal details have been provided // Now check that valid personal details have been provided
if (!Security::canWriteToUserWall($profile_uid) && !$allow_comment) { if (!Security::canWriteToUserWall($profile_uid) && !$allow_comment) {
notice(DI::l10n()->t('Permission denied.') . EOL); notice(DI::l10n()->t('Permission denied.'));
if ($return_path) {
if (!empty($_REQUEST['return'])) {
DI::baseUrl()->redirect($return_path); DI::baseUrl()->redirect($return_path);
} }
exit(); throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
} }
// Init post instance // Init post instance
@ -319,13 +318,15 @@ function item_post(App $a) {
if (!strlen($body)) { if (!strlen($body)) {
if ($preview) { if ($preview) {
exit(); System::jsonExit(['preview' => '']);
} }
info(DI::l10n()->t('Empty post discarded.') . EOL);
if (!empty($_REQUEST['return'])) { info(DI::l10n()->t('Empty post discarded.'));
if ($return_path) {
DI::baseUrl()->redirect($return_path); DI::baseUrl()->redirect($return_path);
} }
exit();
throw new HTTPException\BadRequestException(DI::l10n()->t('Empty post discarded.'));
} }
} }
@ -502,9 +503,6 @@ function item_post(App $a) {
$body = DI::bbCodeVideo()->transform($body); $body = DI::bbCodeVideo()->transform($body);
// Fold multi-line [code] sequences
$body = preg_replace('/\[\/code\]\s*\[code\]/ism', "\n", $body);
$body = BBCode::scaleExternalImages($body); $body = BBCode::scaleExternalImages($body);
// Setting the object type if not defined before // Setting the object type if not defined before
@ -664,15 +662,14 @@ function item_post(App $a) {
$datarray["author-network"] = Protocol::DFRN; $datarray["author-network"] = Protocol::DFRN;
$o = conversation($a, [array_merge($contact_record, $datarray)], new Pager(DI::args()->getQueryString()), 'search', false, true); $o = conversation($a, [array_merge($contact_record, $datarray)], new Pager(DI::args()->getQueryString()), 'search', false, true);
Logger::log('preview: ' . $o);
echo json_encode(['preview' => $o]); System::jsonExit(['preview' => $o]);
exit();
} }
Hook::callAll('post_local',$datarray); Hook::callAll('post_local',$datarray);
if (!empty($datarray['cancel'])) { if (!empty($datarray['cancel'])) {
Logger::log('mod_item: post cancelled by addon.'); Logger::info('mod_item: post cancelled by addon.');
if ($return_path) { if ($return_path) {
DI::baseUrl()->redirect($return_path); DI::baseUrl()->redirect($return_path);
} }
@ -682,8 +679,7 @@ function item_post(App $a) {
$json['reload'] = DI::baseUrl() . '/' . $_REQUEST['jsreload']; $json['reload'] = DI::baseUrl() . '/' . $_REQUEST['jsreload'];
} }
echo json_encode($json); System::jsonExit($json);
exit();
} }
if ($orig_post) { if ($orig_post) {
@ -707,11 +703,12 @@ function item_post(App $a) {
// update filetags in pconfig // update filetags in pconfig
FileTag::updatePconfig($uid, $categories_old, $categories_new, 'category'); FileTag::updatePconfig($uid, $categories_old, $categories_new, 'category');
if (!empty($_REQUEST['return']) && strlen($return_path)) { info(DI::l10n()->t('Post updated.'));
Logger::log('return: ' . $return_path); if ($return_path) {
DI::baseUrl()->redirect($return_path); DI::baseUrl()->redirect($return_path);
} }
exit();
throw new HTTPException\OKException(DI::l10n()->t('Post updated.'));
} }
unset($datarray['edit']); unset($datarray['edit']);
@ -728,15 +725,23 @@ function item_post(App $a) {
$post_id = Item::insert($datarray); $post_id = Item::insert($datarray);
if (!$post_id) { if (!$post_id) {
Logger::log("Item wasn't stored."); info(DI::l10n()->t('Item wasn\'t stored.'));
DI::baseUrl()->redirect($return_path); if ($return_path) {
DI::baseUrl()->redirect($return_path);
}
throw new HTTPException\InternalServerErrorException(DI::l10n()->t('Item wasn\'t stored.'));
} }
$datarray = Item::selectFirst(Item::ITEM_FIELDLIST, ['id' => $post_id]); $datarray = Item::selectFirst(Item::ITEM_FIELDLIST, ['id' => $post_id]);
if (!DBA::isResult($datarray)) { if (!DBA::isResult($datarray)) {
Logger::log("Item with id ".$post_id." couldn't be fetched."); Logger::error('Item couldn\'t be fetched.', ['post_id' => $post_id]);
DI::baseUrl()->redirect($return_path); if ($return_path) {
DI::baseUrl()->redirect($return_path);
}
throw new HTTPException\InternalServerErrorException(DI::l10n()->t('Item couldn\'t be fetched.'));
} }
// update filetags in pconfig // update filetags in pconfig
@ -811,21 +816,19 @@ function item_post(App $a) {
Worker::add(['priority' => PRIORITY_HIGH, 'dont_fork' => false], "Notifier", Delivery::POST, $post_id); Worker::add(['priority' => PRIORITY_HIGH, 'dont_fork' => false], "Notifier", Delivery::POST, $post_id);
} }
Logger::log('post_complete'); Logger::info('post_complete');
if ($api_source) { if ($api_source) {
return $post_id; return $post_id;
} }
info(DI::l10n()->t('Post published.'));
item_post_return(DI::baseUrl(), $api_source, $return_path); item_post_return(DI::baseUrl(), $api_source, $return_path);
// NOTREACHED // NOTREACHED
} }
function item_post_return($baseurl, $api_source, $return_path) function item_post_return($baseurl, $api_source, $return_path)
{ {
// figure out how to return, depending on from whence we came
$a = DI::app();
if ($api_source) { if ($api_source) {
return; return;
} }
@ -839,10 +842,9 @@ function item_post_return($baseurl, $api_source, $return_path)
$json['reload'] = $baseurl . '/' . $_REQUEST['jsreload']; $json['reload'] = $baseurl . '/' . $_REQUEST['jsreload'];
} }
Logger::log('post_json: ' . print_r($json, true), Logger::DEBUG); Logger::info('post_json', ['json' => $json]);
echo json_encode($json); System::jsonExit($json);
exit();
} }
function item_content(App $a) function item_content(App $a)
@ -867,8 +869,7 @@ function item_content(App $a)
if (DI::mode()->isAjax()) { if (DI::mode()->isAjax()) {
// ajax return: [<item id>, 0 (no perm) | <owner id>] // ajax return: [<item id>, 0 (no perm) | <owner id>]
echo json_encode([intval($a->argv[2]), intval($o)]); System::jsonExit([intval($a->argv[2]), intval($o)]);
exit();
} }
} }
@ -889,7 +890,7 @@ function item_content(App $a)
* *
* @return array|bool ['replaced' => $replaced, 'contact' => $contact]; * @return array|bool ['replaced' => $replaced, 'contact' => $contact];
* @throws ImagickException * @throws ImagickException
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws HTTPException\InternalServerErrorException
*/ */
function handle_tag(&$body, &$inform, &$str_tags, $profile_uid, $tag, $network = "") function handle_tag(&$body, &$inform, &$str_tags, $profile_uid, $tag, $network = "")
{ {

View file

@ -16,26 +16,32 @@ class Logger
{ {
/** /**
* @see Logger::error() * @see Logger::error()
* @deprecated since 2019.01
*/ */
const WARNING = LogLevel::ERROR; const WARNING = LogLevel::ERROR;
/** /**
* @see Logger::warning() * @see Logger::warning()
* @deprecated since 2019.01
*/ */
const INFO = LogLevel::WARNING; const INFO = LogLevel::WARNING;
/** /**
* @see Logger::notice() * @see Logger::notice()
* @deprecated since 2019.01
*/ */
const TRACE = LogLevel::NOTICE; const TRACE = LogLevel::NOTICE;
/** /**
* @see Logger::info() * @see Logger::info()
* @deprecated since 2019.01
*/ */
const DEBUG = LogLevel::INFO; const DEBUG = LogLevel::INFO;
/** /**
* @see Logger::debug() * @see Logger::debug()
* @deprecated since 2019.01
*/ */
const DATA = LogLevel::DEBUG; const DATA = LogLevel::DEBUG;
/** /**
* @see Logger::debug() * @see Logger::debug()
* @deprecated since 2019.01
*/ */
const ALL = LogLevel::DEBUG; const ALL = LogLevel::DEBUG;

View file

@ -67,11 +67,53 @@
jotTextOpenUI(document.getElementById("profile-jot-text")); jotTextOpenUI(document.getElementById("profile-jot-text"));
jotActive(); jotActive();
addeditortext(embedcode); addeditortext(embedcode);
}); })
$('body').on('fbrowser.file.main', function(e, filename, embedcode, id) { .on('fbrowser.file.main', function(e, filename, embedcode, id) {
jotTextOpenUI(document.getElementById("profile-jot-text")); jotTextOpenUI(document.getElementById("profile-jot-text"));
jotActive(); jotActive();
addeditortext(embedcode); addeditortext(embedcode);
})
// Asynchronous jot submission
.on('submit', '#profile-jot-form', function (e) {
e.preventDefault();
// Disable jot submit buttons during processing
let $share = $('#profile-jot-submit').button('loading');
let $sharePreview = $('#profile-jot-preview-submit').button('loading');
let formData = new FormData(e.target);
// This cancels the automatic redirection after item submission
formData.delete('return');
$.ajax({
url: 'item',
data: formData,
processData: false,
contentType: false,
type: 'POST',
})
.then(function () {
// Reset to form for jot reuse in the same page
e.target.reset();
$('#jot-modal').modal('hide');
})
.always(function() {
// Reset the post_id_random to avoid duplicate post errors
let new_post_id_random = Math.floor(Math.random() * (Number.MAX_SAFE_INTEGER - (Number.MAX_SAFE_INTEGER / 10))) + Number.MAX_SAFE_INTEGER / 10;
$('#profile-jot-form [name=post_id_random]').val(new_post_id_random);
// Reset jot submit button state
$share.button('reset');
$sharePreview.button('reset');
// Force the display update of the edited post/comment
if (formData.get('post_id')) {
force_update = true;
update_item = formData.get('post_id');
}
NavUpdate();
})
}); });
$('#wall-image-upload').on('click', function(){ $('#wall-image-upload').on('click', function(){

View file

@ -104,7 +104,11 @@
<li><button type="button" class="btn-link" id="profile-nolocation" onclick="jotClearLocation();" title="{{$noloc}}">{{$shortnoloc}}</button></li> <li><button type="button" class="btn-link" id="profile-nolocation" onclick="jotClearLocation();" title="{{$noloc}}">{{$shortnoloc}}</button></li>
--> -->
<li role="presentation" class="pull-right"><button class="btn btn-primary" type="submit" id="profile-jot-submit" name="submit" ><i class="fa fa-paper-plane fa-fw" aria-hidden="true"></i> {{$share}}</button></li> <li role="presentation" class="pull-right">
<button class="btn btn-primary" type="submit" id="profile-jot-submit" name="submit" data-loading-text="{{$loading}}">
<i class="fa fa-paper-plane fa-fw" aria-hidden="true"></i> {{$share}}
</button>
</li>
<li role="presentation" id="character-counter" class="grey jothidden text-info pull-right"></li> <li role="presentation" id="character-counter" class="grey jothidden text-info pull-right"></li>
<li role="presentation" id="profile-rotator-wrapper" class="pull-right" style="display: {{$visitor}};" > <li role="presentation" id="profile-rotator-wrapper" class="pull-right" style="display: {{$visitor}};" >
<img role="presentation" id="profile-rotator" src="images/rotator.gif" alt="{{$wait}}" title="{{$wait}}" style="display: none;" /> <img role="presentation" id="profile-rotator" src="images/rotator.gif" alt="{{$wait}}" title="{{$wait}}" style="display: none;" />
@ -124,7 +128,11 @@
<div id="jot-preview-share" class="minimize" aria-labelledby="jot-preview-lnk" role="tabpanel" aria-hidden="true"> <div id="jot-preview-share" class="minimize" aria-labelledby="jot-preview-lnk" role="tabpanel" aria-hidden="true">
<ul id="profile-jot-preview-submit-wrapper" class="jothidden nav nav-pills"> <ul id="profile-jot-preview-submit-wrapper" class="jothidden nav nav-pills">
<li role="presentation" class="pull-right"><button class="btn btn-primary" type="submit" id="profile-jot-peview-submit" name="submit" ><i class="fa fa-paper-plane fa-fw" aria-hidden="true"></i> {{$share}}</button></li> <li role="presentation" class="pull-right">
<button class="btn btn-primary" type="submit" id="profile-jot-preview-submit" name="submit" data-loading-text="{{$loading}}">
<i class="fa fa-paper-plane fa-fw" aria-hidden="true"></i> {{$share}}
</button>
</li>
</ul> </ul>
</div> </div>