]> git.mxchange.org Git - friendica.git/commitdiff
Post processing centralized
authorMichael <heluecht@pirati.ca>
Wed, 4 Jan 2023 06:52:40 +0000 (06:52 +0000)
committerMichael <heluecht@pirati.ca>
Wed, 4 Jan 2023 06:52:40 +0000 (06:52 +0000)
mod/item.php
src/Content/Item.php
src/Model/Item.php

index 808641132e2908da951291cd0ed7ee846e07e54b..5b40a67de20b9ee2369d6b34414846d491d75652 100644 (file)
@@ -42,9 +42,7 @@ use Friendica\Model\Item;
 use Friendica\Model\ItemURI;
 use Friendica\Model\Photo;
 use Friendica\Model\Post;
-use Friendica\Model\Tag;
 use Friendica\Network\HTTPException;
-use Friendica\Object\EMail\ItemCCEMail;
 use Friendica\Protocol\Activity;
 use Friendica\Util\DateTimeFormat;
 
@@ -99,25 +97,24 @@ function item_post(App $a) {
 
        $post = DI::contentItem()->initializePost($post);
 
-       $post['self']        = true;
-       $post['api_source']  = false;
-       $post['edit']        = $orig_post;
-       $post['file']        = '';
-       $post['attach']      = '';
-       $post['inform']      = '';
-       $post['postopts']    = '';
-
-       $post['wall']        = $_REQUEST['wall'] ?? true;
-       $post['post-type']   = $_REQUEST['post_type'] ?? '';
-       $post['title']       = trim($_REQUEST['title'] ?? '');
-       $post['body']        = $_REQUEST['body'] ?? '';
-       $post['location']    = trim($_REQUEST['location'] ?? '');
-       $post['coord']       = trim($_REQUEST['coord'] ?? '');
-       $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'];
-       $post['app']         = '';
+       $post['edit']       = $orig_post;
+       $post['self']       = true;
+       $post['api_source'] = false;
+       $post['file']       = '';
+       $post['attach']     = '';
+       $post['inform']     = '';
+       $post['postopts']   = '';
+       $post['wall']       = $_REQUEST['wall'] ?? true;
+       $post['post-type']  = $_REQUEST['post_type'] ?? '';
+       $post['title']      = trim($_REQUEST['title'] ?? '');
+       $post['body']       = $_REQUEST['body'] ?? '';
+       $post['location']   = trim($_REQUEST['location'] ?? '');
+       $post['coord']      = trim($_REQUEST['coord'] ?? '');
+       $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'];
+       $post['app']        = '';
 
        if ($post['parent']) {
                if ($post['parent']) {
@@ -168,7 +165,6 @@ function item_post(App $a) {
        $post['pubmail'] = $post['pubmail'] && !$post['private'];
 
        if (!empty($orig_post)) {
-               $post['uri']  = $orig_post['uri'];
                $post['file'] = Post\Category::getTextByURIId($orig_post['uri-id'], $orig_post['uid']);
        }
 
@@ -202,26 +198,20 @@ function item_post(App $a) {
                throw new HTTPException\BadRequestException(DI::l10n()->t('Empty post discarded.'));
        }
 
-       // Check for hashtags in the body and repair or add hashtag links
-       if ($preview || $orig_post) {
-               $post['body'] = Item::setHashtags($post['body']);
-       }
-
        // preview mode - prepare the body for display and send it via json
        if ($preview) {
-               // We set the datarray ID to -1 because in preview mode the dataray
-               // doesn't have an ID.
-               $post['id'] = -1;
-               $post['uri-id'] = -1;
+               // We have to preset some fields, so that the conversation can be displayed
+               $post['id']             = -1;
+               $post['uri-id']         = -1;
                $post['author-network'] = Protocol::DFRN;
                $post['author-updated'] = '';
-               $post['author-gsid'] = 0;
-               $post['author-uri-id'] = ItemURI::getIdByURI($post['author-link']);
-               $post['owner-updated'] = '';
-               $post['has-media'] = false;
-               $post['quote-uri-id'] = Item::getQuoteUriId($post['body'], $post['uid']);
-               $post['body'] = BBCode::removeSharedData($post['body']);
-               $post['writable'] = true;
+               $post['author-gsid']    = 0;
+               $post['author-uri-id']  = ItemURI::getIdByURI($post['author-link']);
+               $post['owner-updated']  = '';
+               $post['has-media']      = false;
+               $post['quote-uri-id']   = Item::getQuoteUriId($post['body'], $post['uid']);
+               $post['body']           = BBCode::removeSharedData(Item::setHashtags($post['body']));
+               $post['writable']       = true;
 
                $o = DI::conversation()->create([$post], 'search', false, true);
 
@@ -230,6 +220,10 @@ function item_post(App $a) {
 
        Hook::callAll('post_local',$post);
 
+       unset($post['edit']);
+       unset($post['self']);
+       unset($post['api_source']);
+
        if (!empty($_REQUEST['scheduled_at'])) {
                $scheduled_at = DateTimeFormat::convert($_REQUEST['scheduled_at'], 'UTC', $a->getTimeZone());
                if ($scheduled_at > DateTimeFormat::utcNow()) {
@@ -238,9 +232,6 @@ function item_post(App $a) {
                        unset($post['commented']);
                        unset($post['received']);
                        unset($post['changed']);
-                       unset($post['edit']);
-                       unset($post['self']);
-                       unset($post['api_source']);
 
                        Post\Delayed::add($post['uri'], $post, Worker::PRIORITY_HIGH, Post\Delayed::PREPARED_NO_HOOK, $scheduled_at);
                        item_post_return(DI::baseUrl(), $return_path);
@@ -261,26 +252,26 @@ function item_post(App $a) {
                System::jsonExit($json);
        }
 
-       $post['uri-id'] = ItemURI::getIdByURI($post['uri']);
-
-       $quote_uri_id = Item::getQuoteUriId($post['body'], $post['uid']);
-       if (!empty($quote_uri_id)) {
-               $post['quote-uri-id'] = $quote_uri_id;
-               $post['body']         = BBCode::removeSharedData($post['body']);
-       }
-
        if ($orig_post) {
                $fields = [
-                       'title'   => $post['title'],
-                       'body'    => $post['body'],
-                       'attach'  => $post['attach'],
-                       'file'    => $post['file'],
-                       'edited'  => DateTimeFormat::utcNow(),
-                       'changed' => DateTimeFormat::utcNow()
+                       'title'        => $post['title'],
+                       'body'         => $post['body'],
+                       'attach'       => $post['attach'],
+                       'file'         => $post['file'],
+                       'edited'       => DateTimeFormat::utcNow(),
+                       'changed'      => DateTimeFormat::utcNow()
                ];
 
+               $fields['body'] = Item::setHashtags($fields['body']);
+
+               $quote_uri_id = Item::getQuoteUriId($fields['body'], $post['uid']);
+               if (!empty($quote_uri_id)) {
+                       $fields['quote-uri-id'] = $quote_uri_id;
+                       $fields['body']         = BBCode::removeSharedData($post['body']);
+               }
+
                Item::update($fields, ['id' => $post_id]);
-               Item::updateDisplayCache($post['uri-id']);
+               Item::updateDisplayCache($orig_post['uri-id']);
 
                if ($return_path) {
                        DI::baseUrl()->redirect($return_path);
@@ -289,10 +280,6 @@ function item_post(App $a) {
                throw new HTTPException\OKException(DI::l10n()->t('Post updated.'));
        }
 
-       unset($post['edit']);
-       unset($post['self']);
-       unset($post['api_source']);
-
        $post_id = Item::insert($post);
        if (!$post_id) {
                DI::sysmsg()->addNotice(DI::l10n()->t('Item wasn\'t stored.'));
@@ -313,26 +300,9 @@ function item_post(App $a) {
                throw new HTTPException\InternalServerErrorException(DI::l10n()->t('Item couldn\'t be fetched.'));
        }
 
-       if (!\Friendica\Content\Feature::isEnabled($post['uid'], 'explicit_mentions') && ($post['gravity'] == Item::GRAVITY_COMMENT)) {
-               Tag::createImplicitMentions($post['uri-id'], $post['thr-parent-id']);
-       }
-
-       Hook::callAll('post_local_end', $post);
-
        $recipients = explode(',', $emailcc);
-       if (count($recipients)) {
-               foreach ($recipients as $recipient) {
-                       $address = trim($recipient);
-                       if (!strlen($address)) {
-                               continue;
-                       }
-
-                       $author = DBA::selectFirst('contact', ['thumb'], ['uid' => $uid, 'self' => true]);
 
-                       DI::emailer()->send(new ItemCCEMail(DI::app(), DI::l10n(), DI::baseUrl(),
-                               $post, $address, $author['thumb'] ?? ''));
-               }
-       }
+       DI::contentItem()->postProcessPost($post, $recipients);
 
        Logger::debug('post_complete');
 
index 76a3a1f4a9a9c294bdc35b47d79d1543e640c236..c4af86e6b1dc2eee3f8a6a2321f44789bb04093b 100644 (file)
@@ -21,6 +21,7 @@
 
 namespace Friendica\Content;
 
+use Friendica\App;
 use Friendica\App\BaseURL;
 use Friendica\Content\Text\BBCode;
 use Friendica\Content\Text\BBCode\Video;
@@ -44,9 +45,11 @@ use Friendica\Model\Tag;
 use Friendica\Model\Post;
 use Friendica\Model\User;
 use Friendica\Network\HTTPException;
+use Friendica\Object\EMail\ItemCCEMail;
 use Friendica\Protocol\Activity;
 use Friendica\Util\ACLFormatter;
 use Friendica\Util\DateTimeFormat;
+use Friendica\Util\Emailer;
 use Friendica\Util\ParseUrl;
 use Friendica\Util\Profiler;
 use Friendica\Util\Proxy;
@@ -73,8 +76,12 @@ class Item
        private $pConfig;
        /** @var BaseURL */
        private $baseURL;
+       /** @var Emailer */
+       private $emailer;
+       /** @var App */
+       private $app;
 
-       public function __construct(Profiler $profiler, Activity $activity, L10n $l10n, IHandleUserSessions $userSession, Video $bbCodeVideo, ACLFormatter $aclFormatter, IManagePersonalConfigValues $pConfig, BaseURL $baseURL)
+       public function __construct(Profiler $profiler, Activity $activity, L10n $l10n, IHandleUserSessions $userSession, Video $bbCodeVideo, ACLFormatter $aclFormatter, IManagePersonalConfigValues $pConfig, BaseURL $baseURL, Emailer $emailer, App $app)
        {
                $this->profiler     = $profiler;
                $this->activity     = $activity;
@@ -84,6 +91,8 @@ class Item
                $this->aclFormatter = $aclFormatter;
                $this->baseURL      = $baseURL;
                $this->pConfig      = $pConfig;
+               $this->emailer      = $emailer;
+               $this->app          = $app;
        }
 
        /**
@@ -823,10 +832,10 @@ class Item
                        // get the "fileas" tags for this post
                        $filedas = FileTag::fileToArray($post['file']);
                }
-       
+
                $list_array = explode(',', trim($category));
                $post['file'] = FileTag::arrayToFile($list_array, 'category');
-       
+
                if (!empty($filedas) && is_array($filedas)) {
                        // append the fileas stuff to the new categories list
                        $post['file'] .= FileTag::arrayToFile($filedas);
@@ -850,12 +859,12 @@ class Item
                if (!DBA::isResult($user)) {
                        throw new HTTPException\NotFoundException($this->l10n->t('Unable to locate original post.'));
                }
-       
+
                $post['allow_cid'] = isset($request['contact_allow']) ? $this->aclFormatter->toString($request['contact_allow']) : $user['allow_cid'] ?? '';
                $post['allow_gid'] = isset($request['group_allow'])   ? $this->aclFormatter->toString($request['group_allow'])   : $user['allow_gid'] ?? '';
                $post['deny_cid']  = isset($request['contact_deny'])  ? $this->aclFormatter->toString($request['contact_deny'])  : $user['deny_cid']  ?? '';
                $post['deny_gid']  = isset($request['group_deny'])    ? $this->aclFormatter->toString($request['group_deny'])    : $user['deny_gid']  ?? '';
-       
+
                $visibility = $request['visibility'] ?? '';
                if ($visibility === 'public') {
                        // The ACL selector introduced in version 2019.12 sends ACL input data even when the Public visibility is selected
@@ -874,7 +883,7 @@ class Item
                } else {
                        $post['private'] = ItemModel::PUBLIC;
                }
-       
+
                return $post;
        }
 
@@ -947,7 +956,7 @@ class Item
                $post['guid']       = System::createUUID();
                $post['uri']        = ItemModel::newURI($post['guid']);
                $post['verb']       = Activity::POST;
-               $post['received']   = DateTimeFormat::utcNow(); 
+               $post['received']   = DateTimeFormat::utcNow();
                $owner = User::getOwnerDataById($post['uid']);
 
                if (empty($post['contact-id'])) {
@@ -991,4 +1000,25 @@ class Item
 
                return $post;
        }
+
+       public function postProcessPost(array $post, array $recipients = [])
+       {
+               if (!\Friendica\Content\Feature::isEnabled($post['uid'], 'explicit_mentions') && ($post['gravity'] == ItemModel::GRAVITY_COMMENT)) {
+                       Tag::createImplicitMentions($post['uri-id'], $post['thr-parent-id']);
+               }
+
+               Hook::callAll('post_local_end', $post);
+
+               $author = DBA::selectFirst('contact', ['thumb'], ['uid' => $post['uid'], 'self' => true]);
+
+               foreach ($recipients as $recipient) {
+                       $address = trim($recipient);
+                       if (!strlen($address)) {
+                               continue;
+                       }
+
+                       $this->emailer->send(new ItemCCEMail($this->app, $this->l10n, $this->baseURL,
+                               $post, $address, $author['thumb'] ?? ''));
+               }
+       }
 }
index d5a72c73e7c52792263dc29952073a050f6a67b5..f4bcb56e091d90382ed7d278ec97c4c74c9d5a5f 100644 (file)
@@ -820,6 +820,12 @@ class Item
        private static function prepareOriginPost(array $item): array
        {
                $item = DI::contentItem()->initializePost($item);
+
+               if (Photo::setPermissionFromBody($item['body'], $item['uid'], $item['contact-id'], $item['allow_cid'], $item['allow_gid'], $item['deny_cid'], $item['deny_gid'])) {
+                       $item['object-type'] = Activity\ObjectType::IMAGE;
+               }
+
+               $item = DI::contentItem()->moveAttachmentsFromBodyToAttach($item);
                $item = DI::contentItem()->finalizePost($item);
 
                return $item;
@@ -1301,10 +1307,7 @@ class Item
                }
 
                if ($notify) {
-                       if (!\Friendica\Content\Feature::isEnabled($posted_item['uid'], 'explicit_mentions') && ($posted_item['gravity'] == self::GRAVITY_COMMENT)) {
-                               Tag::createImplicitMentions($posted_item['uri-id'], $posted_item['thr-parent-id']);
-                       }
-                       Hook::callAll('post_local_end', $posted_item);
+                       DI::contentItem()->postProcessPost($posted_item);
                } else {
                        Hook::callAll('post_remote_end', $posted_item);
                }