]> git.mxchange.org Git - friendica.git/blobdiff - mod/item.php
Merge pull request #13142 from annando/collapse-author
[friendica.git] / mod / item.php
index b7ae4d2123471039992d5cd567caabdbc0abe9cb..369b0888a29449bbac8239ea1c16c92d7c2c642b 100644 (file)
@@ -19,7 +19,7 @@
  *
  * This is the POST destination for most all locally posted
  * text stuff. This function handles status, wall-to-wall status,
- * local comments, and remote coments that are posted on this site
+ * local comments, and remote comments that are posted on this site
  * (as opposed to being delivered in a feed).
  * Also processed here are posts and comments coming through the
  * statusnet/twitter API.
@@ -29,6 +29,7 @@
  */
 
 use Friendica\App;
+use Friendica\Content\Conversation;
 use Friendica\Content\Text\BBCode;
 use Friendica\Core\Hook;
 use Friendica\Core\Logger;
@@ -40,16 +41,14 @@ use Friendica\DI;
 use Friendica\Model\Contact;
 use Friendica\Model\Item;
 use Friendica\Model\ItemURI;
-use Friendica\Model\Photo;
 use Friendica\Model\Post;
 use Friendica\Network\HTTPException;
-use Friendica\Protocol\Activity;
 use Friendica\Util\DateTimeFormat;
 
 function item_post(App $a) {
        $uid = DI::userSession()->getLocalUserId();
 
-       if (!DI::userSession()->isAuthenticated() || !$uid) {
+       if (!$uid) {
                throw new HTTPException\ForbiddenException();
        }
 
@@ -68,11 +67,11 @@ function item_post(App $a) {
         * after it's been previewed
         */
        if (!$preview && !empty($_REQUEST['post_id_random'])) {
-               if (!empty($_SESSION['post-random']) && $_SESSION['post-random'] == $_REQUEST['post_id_random']) {
+               if (DI::session()->get('post-random') == $_REQUEST['post_id_random']) {
                        Logger::warning('duplicate post');
                        item_post_return(DI::baseUrl(), $return_path);
                } else {
-                       $_SESSION['post-random'] = $_REQUEST['post_id_random'];
+                       DI::session()->set('post-random', $_REQUEST['post_id_random']);
                }
        }
 
@@ -90,16 +89,15 @@ function item_drop(int $uid, string $dropitems)
                Item::deleteForUser(['id' => $item], $uid);
        }
 
-       $json = ['success' => 1];
-       System::jsonExit($json);
+       System::jsonExit(['success' => 1]);
 }
 
 function item_edit(int $uid, array $request, bool $preview, string $return_path)
 {
        $post = Post::selectFirst(Item::ITEM_FIELDLIST, ['id' => $request['post_id'], 'uid' => $uid]);
        if (!DBA::isResult($post)) {
-               DI::sysmsg()->addNotice(DI::l10n()->t('Unable to locate original post.'));
                if ($return_path) {
+                       DI::sysmsg()->addNotice(DI::l10n()->t('Unable to locate original post.'));
                        DI::baseUrl()->redirect($return_path);
                }
                throw new HTTPException\NotFoundException(DI::l10n()->t('Unable to locate original post.'));
@@ -108,6 +106,7 @@ function item_edit(int $uid, array $request, bool $preview, string $return_path)
        $post['edit'] = $post;
        $post['file'] = Post\Category::getTextByURIId($post['uri-id'], $post['uid']);   
 
+       Post\Media::deleteByURIId($post['uri-id'], [Post\Media::AUDIO, Post\Media::VIDEO, Post\Media::IMAGE, Post\Media::HTML]);
        $post = item_process($post, $request, $preview, $return_path);
 
        $fields = [
@@ -141,15 +140,12 @@ function item_edit(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']  ?? '');
-
        $post = ['uid' => $uid];
        $post = DI::contentItem()->initializePost($post);
 
        $post['edit']      = null;
        $post['post-type'] = $request['post_type'] ?? '';
        $post['wall']      = $request['wall'] ?? true;
-       $post['parent']    = intval($request['parent'] ?? 0);
        $post['pubmail']   = $request['pubmail_enable'] ?? false;
        $post['created']   = $request['created_at'] ?? DateTimeFormat::utcNow();
        $post['edited']    = $post['changed'] = $post['commented'] = $post['created'];
@@ -158,25 +154,20 @@ function item_insert(int $uid, array $request, bool $preview, string $return_pat
        $post['postopts']  = '';
        $post['file']      = '';
 
-       if ($post['parent']) {
-               if ($post['parent']) {
-                       $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)) {
-                       // The URI and the contact is taken from the direct parent which needn't to be the top parent
-                       $post['thr-parent'] = $parent_item['uri'];
-                       $toplevel_item = $parent_item;
-
+       if (!empty($request['parent'])) {
+               $parent_item = Post::selectFirst(Item::ITEM_FIELDLIST, ['id' => $request['parent']]);
+               if ($parent_item) {
+                       // if this isn't the top-level parent of the conversation, find it
                        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)) {
-                       DI::sysmsg()->addNotice(DI::l10n()->t('Unable to locate original post.'));
+               if (empty($toplevel_item)) {
                        if ($return_path) {
+                               DI::sysmsg()->addNotice(DI::l10n()->t('Unable to locate original post.'));
                                DI::baseUrl()->redirect($return_path);
                        }
                        throw new HTTPException\NotFoundException(DI::l10n()->t('Unable to locate original post.'));
@@ -187,13 +178,11 @@ function item_insert(int $uid, array $request, bool $preview, string $return_pat
                if ($toplevel_item['uid'] == 0) {
                        $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]);
-                       if ($stored) {
-                               $toplevel_item = Post::selectFirst(Item::ITEM_FIELDLIST, ['id' => $stored]);
-                       }
                }
 
                $post['parent']      = $toplevel_item['id'];
                $post['gravity']     = Item::GRAVITY_COMMENT;
+               $post['thr-parent']  = $parent_item['uri'];
                $post['wall']        = $toplevel_item['wall'];
        } else {
                $parent_item         = [];
@@ -210,8 +199,8 @@ function item_insert(int $uid, array $request, bool $preview, string $return_pat
 
        $post_id = Item::insert($post);
        if (!$post_id) {
-               DI::sysmsg()->addNotice(DI::l10n()->t('Item wasn\'t stored.'));
                if ($return_path) {
+                       DI::sysmsg()->addNotice(DI::l10n()->t('Item wasn\'t stored.'));
                        DI::baseUrl()->redirect($return_path);
                }
 
@@ -219,7 +208,7 @@ function item_insert(int $uid, array $request, bool $preview, string $return_pat
        }
 
        $post = Post::selectFirst(Item::ITEM_FIELDLIST, ['id' => $post_id]);
-       if (!DBA::isResult($post)) {
+       if (!$post) {
                Logger::error('Item couldn\'t be fetched.', ['post_id' => $post_id]);
                if ($return_path) {
                        DI::baseUrl()->redirect($return_path);
@@ -228,7 +217,7 @@ function item_insert(int $uid, array $request, bool $preview, string $return_pat
                throw new HTTPException\InternalServerErrorException(DI::l10n()->t('Item couldn\'t be fetched.'));
        }
 
-       $recipients = explode(',', $emailcc);
+       $recipients = explode(',', $request['emailcc'] ?? '');
 
        DI::contentItem()->postProcessPost($post, $recipients);
 
@@ -250,14 +239,6 @@ function item_process(array $post, array $request, bool $preview, string $return
 
        $post = DI::contentItem()->addCategories($post, $request['category'] ?? '');
 
-       if (!$preview) {
-               if (Photo::setPermissionFromBody($post['body'], $post['uid'], $post['contact-id'], $post['allow_cid'], $post['allow_gid'], $post['deny_cid'], $post['deny_gid'])) {
-                       $post['object-type'] = Activity\ObjectType::IMAGE;
-               }
-
-               $post = DI::contentItem()->moveAttachmentsFromBodyToAttach($post);
-       }
-
        // Add the attachment to the body.
        if (!empty($request['has_attachment'])) {
                $post['body'] .= DI::contentItem()->storeAttachmentFromRequest($request);
@@ -270,8 +251,8 @@ function item_process(array $post, array $request, bool $preview, string $return
                        System::jsonExit(['preview' => '']);
                }
 
-               DI::sysmsg()->addNotice(DI::l10n()->t('Empty post discarded.'));
                if ($return_path) {
+                       DI::sysmsg()->addNotice(DI::l10n()->t('Empty post discarded.'));
                        DI::baseUrl()->redirect($return_path);
                }
 
@@ -293,7 +274,7 @@ function item_process(array $post, array $request, bool $preview, string $return
                $post['body']           = BBCode::removeSharedData(Item::setHashtags($post['body']));
                $post['writable']       = true;
 
-               $o = DI::conversation()->create([$post], 'search', false, true);
+               $o = DI::conversation()->create([$post], Conversation::MODE_SEARCH, false, true);
 
                System::jsonExit(['preview' => $o]);
        }
@@ -387,6 +368,38 @@ function item_content(App $a)
 
                        Contact\User::setBlocked($item['author-id'], DI::userSession()->getLocalUserId(), true);
 
+                       if (DI::mode()->isAjax()) {
+                               // ajax return: [<item id>, 0 (no perm) | <owner id>]
+                               System::jsonExit([intval($args->get(2)), DI::userSession()->getLocalUserId()]);
+                       } else {
+                               item_redirect_after_action($item, $args->get(3));
+                       }
+                       break;
+
+               case 'ignore':
+                       $item = Post::selectFirstForUser(DI::userSession()->getLocalUserId(), ['guid', 'author-id', 'parent', 'gravity'], ['id' => $args->get(2)]);
+                       if (empty($item['author-id'])) {
+                               throw new HTTPException\NotFoundException('Item not found');
+                       }
+
+                       Contact\User::setIgnored($item['author-id'], DI::userSession()->getLocalUserId(), true);
+
+                       if (DI::mode()->isAjax()) {
+                               // ajax return: [<item id>, 0 (no perm) | <owner id>]
+                               System::jsonExit([intval($args->get(2)), DI::userSession()->getLocalUserId()]);
+                       } else {
+                               item_redirect_after_action($item, $args->get(3));
+                       }
+                       break;
+
+               case 'collapse':
+                       $item = Post::selectFirstForUser(DI::userSession()->getLocalUserId(), ['guid', 'author-id', 'parent', 'gravity'], ['id' => $args->get(2)]);
+                       if (empty($item['author-id'])) {
+                               throw new HTTPException\NotFoundException('Item not found');
+                       }
+
+                       Contact\User::setCollapsed($item['author-id'], DI::userSession()->getLocalUserId(), true);
+
                        if (DI::mode()->isAjax()) {
                                // ajax return: [<item id>, 0 (no perm) | <owner id>]
                                System::jsonExit([intval($args->get(2)), DI::userSession()->getLocalUserId()]);