Simplified code

This commit is contained in:
Michael 2023-01-07 18:07:54 +00:00
parent d22712956b
commit 8654b39e98

View file

@ -142,6 +142,7 @@ function item_edit(int $uid, array $request, bool $preview, string $return_path)
function item_insert(int $uid, array $request, bool $preview, string $return_path) function item_insert(int $uid, array $request, bool $preview, string $return_path)
{ {
$emailcc = trim($request['emailcc'] ?? ''); $emailcc = trim($request['emailcc'] ?? '');
$parent_id = intval($request['parent'] ?? 0);
$post = ['uid' => $uid]; $post = ['uid' => $uid];
$post = DI::contentItem()->initializePost($post); $post = DI::contentItem()->initializePost($post);
@ -149,7 +150,6 @@ function item_insert(int $uid, array $request, bool $preview, string $return_pat
$post['edit'] = null; $post['edit'] = null;
$post['post-type'] = $request['post_type'] ?? ''; $post['post-type'] = $request['post_type'] ?? '';
$post['wall'] = $request['wall'] ?? true; $post['wall'] = $request['wall'] ?? true;
$post['parent'] = intval($request['parent'] ?? 0);
$post['pubmail'] = $request['pubmail_enable'] ?? false; $post['pubmail'] = $request['pubmail_enable'] ?? false;
$post['created'] = $request['created_at'] ?? DateTimeFormat::utcNow(); $post['created'] = $request['created_at'] ?? DateTimeFormat::utcNow();
$post['edited'] = $post['changed'] = $post['commented'] = $post['created']; $post['edited'] = $post['changed'] = $post['commented'] = $post['created'];
@ -158,23 +158,18 @@ function item_insert(int $uid, array $request, bool $preview, string $return_pat
$post['postopts'] = ''; $post['postopts'] = '';
$post['file'] = ''; $post['file'] = '';
if ($post['parent']) { if ($parent_id) {
if ($post['parent']) { $parent_item = Post::selectFirst(Item::ITEM_FIELDLIST, ['id' => $parent_id]);
$parent_item = Post::selectFirst(Item::ITEM_FIELDLIST, ['id' => $post['parent']]);
}
// if this isn't the top-level parent of the conversation, find it
if (DBA::isResult($parent_item)) { if (DBA::isResult($parent_item)) {
// The URI and the contact is taken from the direct parent which needn't to be the top parent // if this isn't the top-level parent of the conversation, find it
$post['thr-parent'] = $parent_item['uri'];
$toplevel_item = $parent_item;
if ($parent_item['gravity'] != Item::GRAVITY_PARENT) { if ($parent_item['gravity'] != Item::GRAVITY_PARENT) {
$toplevel_item = Post::selectFirst(Item::ITEM_FIELDLIST, ['id' => $toplevel_item['parent']]); $toplevel_item = Post::selectFirst(Item::ITEM_FIELDLIST, ['id' => $parent_item['parent']]);
} else {
$toplevel_item = $parent_item;
} }
} }
if (!DBA::isResult($toplevel_item)) { if (empty($toplevel_item)) {
DI::sysmsg()->addNotice(DI::l10n()->t('Unable to locate original post.')); DI::sysmsg()->addNotice(DI::l10n()->t('Unable to locate original post.'));
if ($return_path) { if ($return_path) {
DI::baseUrl()->redirect($return_path); DI::baseUrl()->redirect($return_path);
@ -187,17 +182,13 @@ function item_insert(int $uid, array $request, bool $preview, string $return_pat
if ($toplevel_item['uid'] == 0) { if ($toplevel_item['uid'] == 0) {
$stored = Item::storeForUserByUriId($toplevel_item['uri-id'], $post['uid'], ['post-reason' => Item::PR_ACTIVITY]); $stored = Item::storeForUserByUriId($toplevel_item['uri-id'], $post['uid'], ['post-reason' => Item::PR_ACTIVITY]);
Logger::info('Public item stored for user', ['uri-id' => $toplevel_item['uri-id'], 'uid' => $post['uid'], 'stored' => $stored]); Logger::info('Public item stored for user', ['uri-id' => $toplevel_item['uri-id'], 'uid' => $post['uid'], 'stored' => $stored]);
if ($stored) {
$toplevel_item = Post::selectFirst(Item::ITEM_FIELDLIST, ['id' => $stored]);
}
} }
$post['parent'] = $toplevel_item['id'];
$post['gravity'] = Item::GRAVITY_COMMENT; $post['gravity'] = Item::GRAVITY_COMMENT;
$post['thr-parent'] = $parent_item['uri'];
$post['wall'] = $toplevel_item['wall']; $post['wall'] = $toplevel_item['wall'];
} else { } else {
$parent_item = []; $parent_item = [];
$post['parent'] = 0;
$post['gravity'] = Item::GRAVITY_PARENT; $post['gravity'] = Item::GRAVITY_PARENT;
$post['thr-parent'] = $post['uri']; $post['thr-parent'] = $post['uri'];
} }