Merge pull request #5710 from annando/fix-api

Fix for timeout issues when posting to the API
This commit is contained in:
Tobias Diekershoff 2018-09-02 14:52:09 +02:00 committed by GitHub
commit d5a3b0bd5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 23 deletions

View File

@ -1059,10 +1059,10 @@ function api_statuses_mediap($type)
// now that we have the img url in bbcode we can add it to the status and insert the wall item. // now that we have the img url in bbcode we can add it to the status and insert the wall item.
$_REQUEST['body'] = $txt . "\n\n" . '[url=' . $picture["albumpage"] . '][img]' . $picture["preview"] . "[/img][/url]"; $_REQUEST['body'] = $txt . "\n\n" . '[url=' . $picture["albumpage"] . '][img]' . $picture["preview"] . "[/img][/url]";
item_post($a); $item_id = item_post($a);
// this should output the last post (the one we just posted). // output the post that we just posted.
return api_status_show($type); return api_status_show($type, $item_id);
} }
/// @TODO move this to top of file or somewhere better! /// @TODO move this to top of file or somewhere better!
@ -1078,7 +1078,6 @@ api_register_func('api/statuses/mediap', 'api_statuses_mediap', true, API_METHOD
*/ */
function api_statuses_update($type) function api_statuses_update($type)
{ {
$a = get_app(); $a = get_app();
if (api_user() === false) { if (api_user() === false) {
@ -1132,8 +1131,8 @@ function api_statuses_update($type)
if ($throttle_day > 0) { if ($throttle_day > 0) {
$datefrom = date(DateTimeFormat::MYSQL, time() - 24*60*60); $datefrom = date(DateTimeFormat::MYSQL, time() - 24*60*60);
$condition = ["`uid` = ? AND `wall` AND `created` > ? AND `id` = `parent`", api_user(), $datefrom]; $condition = ["`uid` = ? AND `wall` AND `created` > ?", api_user(), $datefrom];
$posts_day = DBA::count('item', $condition); $posts_day = DBA::count('thread', $condition);
if ($posts_day > $throttle_day) { if ($posts_day > $throttle_day) {
logger('Daily posting limit reached for user '.api_user(), LOGGER_DEBUG); logger('Daily posting limit reached for user '.api_user(), LOGGER_DEBUG);
@ -1146,8 +1145,8 @@ function api_statuses_update($type)
if ($throttle_week > 0) { if ($throttle_week > 0) {
$datefrom = date(DateTimeFormat::MYSQL, time() - 24*60*60*7); $datefrom = date(DateTimeFormat::MYSQL, time() - 24*60*60*7);
$condition = ["`uid` = ? AND `wall` AND `created` > ? AND `id` = `parent`", api_user(), $datefrom]; $condition = ["`uid` = ? AND `wall` AND `created` > ?", api_user(), $datefrom];
$posts_week = DBA::count('item', $condition); $posts_week = DBA::count('thread', $condition);
if ($posts_week > $throttle_week) { if ($posts_week > $throttle_week) {
logger('Weekly posting limit reached for user '.api_user(), LOGGER_DEBUG); logger('Weekly posting limit reached for user '.api_user(), LOGGER_DEBUG);
@ -1160,8 +1159,8 @@ function api_statuses_update($type)
if ($throttle_month > 0) { if ($throttle_month > 0) {
$datefrom = date(DateTimeFormat::MYSQL, time() - 24*60*60*30); $datefrom = date(DateTimeFormat::MYSQL, time() - 24*60*60*30);
$condition = ["`uid` = ? AND `wall` AND `created` > ? AND `id` = `parent`", api_user(), $datefrom]; $condition = ["`uid` = ? AND `wall` AND `created` > ?", api_user(), $datefrom];
$posts_month = DBA::count('item', $condition); $posts_month = DBA::count('thread', $condition);
if ($posts_month > $throttle_month) { if ($posts_month > $throttle_month) {
logger('Monthly posting limit reached for user '.api_user(), LOGGER_DEBUG); logger('Monthly posting limit reached for user '.api_user(), LOGGER_DEBUG);
@ -1203,10 +1202,10 @@ function api_statuses_update($type)
} }
// call out normal post function // call out normal post function
item_post($a); $item_id = item_post($a);
// this should output the last post (the one we just posted). // output the post that we just posted.
return api_status_show($type); return api_status_show($type, $item_id);
} }
/// @TODO move to top of file or somewhere better /// @TODO move to top of file or somewhere better
@ -1263,7 +1262,7 @@ api_register_func('api/media/upload', 'api_media_upload', true, API_METHOD_POST)
* *
* @return array|string * @return array|string
*/ */
function api_status_show($type) function api_status_show($type, $item_id = 0)
{ {
$a = get_app(); $a = get_app();
@ -1277,9 +1276,14 @@ function api_status_show($type)
$privacy_sql = ""; $privacy_sql = "";
} }
// get last public wall message if (!empty($item_id)) {
$condition = ['owner-id' => $user_info['pid'], 'uid' => api_user(), // Get the item with the given id
'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT]]; $condition = ['id' => $item_id];
} else {
// get last public wall message
$condition = ['owner-id' => $user_info['pid'], 'uid' => api_user(),
'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT]];
}
$lastwall = Item::selectFirst(Item::ITEM_FIELDLIST, $condition, ['order' => ['id' => true]]); $lastwall = Item::selectFirst(Item::ITEM_FIELDLIST, $condition, ['order' => ['id' => true]]);
if (DBA::isResult($lastwall)) { if (DBA::isResult($lastwall)) {
@ -1996,14 +2000,14 @@ function api_statuses_repeat($type)
$_REQUEST["source"] = api_source(); $_REQUEST["source"] = api_source();
} }
item_post($a); $item_id = item_post($a);
} else { } else {
throw new ForbiddenException(); throw new ForbiddenException();
} }
// this should output the last post (the one we just posted). // output the post that we just posted.
$called_api = []; $called_api = [];
return api_status_show($type); return api_status_show($type, $item_id);
} }
/// @TODO move to top of file or somewhere better /// @TODO move to top of file or somewhere better

View File

@ -39,7 +39,7 @@ require_once 'include/items.php';
function item_post(App $a) { function item_post(App $a) {
if (!local_user() && !remote_user()) { if (!local_user() && !remote_user()) {
return; return 0;
} }
require_once 'include/security.php'; require_once 'include/security.php';
@ -154,7 +154,7 @@ function item_post(App $a) {
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("Message with URI ".$message_id." already exists for user ".$profile_uid, LOGGER_DEBUG); logger("Message with URI ".$message_id." already exists for user ".$profile_uid, LOGGER_DEBUG);
return; return 0;
} }
} }
@ -183,7 +183,7 @@ function item_post(App $a) {
$user = DBA::selectFirst('user', [], ['uid' => $profile_uid]); $user = DBA::selectFirst('user', [], ['uid' => $profile_uid]);
if (!DBA::isResult($user) && !$parent) { if (!DBA::isResult($user) && !$parent) {
return; return 0;
} }
$categories = ''; $categories = '';
@ -843,6 +843,10 @@ function item_post(App $a) {
logger('post_complete'); logger('post_complete');
if ($api_source) {
return $post_id;
}
item_post_return(System::baseUrl(), $api_source, $return_path); item_post_return(System::baseUrl(), $api_source, $return_path);
// NOTREACHED // NOTREACHED
} }