]> git.mxchange.org Git - friendica.git/commitdiff
Fix uddate issues and improve speed when displaying contact posts
authorMichael <heluecht@pirati.ca>
Thu, 14 Dec 2023 04:58:43 +0000 (04:58 +0000)
committerMichael <heluecht@pirati.ca>
Thu, 14 Dec 2023 04:58:43 +0000 (04:58 +0000)
mod/update_contact.php
src/Model/Contact.php
src/Module/Contact/Conversations.php
src/Module/Contact/Follow.php
src/Module/Contact/Media.php
src/Module/Contact/Posts.php
src/Module/Contact/Unfollow.php
src/Module/Profile/Media.php

index 1a85018e6d23645842f9b261e3c4a00364014ef5..b97e9ec7be9df49a2cfa1b65d45f5f496f842f47 100644 (file)
@@ -31,7 +31,7 @@ use Friendica\Model\Contact;
 function update_contact_content(App $a)
 {
        if (!empty(DI::args()->get(1)) && !empty($_GET['force'])) {
-               $contact = Contact::getById(DI::args()->get(1), ['id', 'deleted']);
+               $contact = DBA::selectFirst('account-user-view', ['pid', 'deleted'], ['id' => DI::args()->get(1)]);
                if (DBA::isResult($contact) && empty($contact['deleted'])) {
                        DI::page()['aside'] = '';
 
@@ -39,7 +39,7 @@ function update_contact_content(App $a)
                                $item = Post::selectFirst(['parent'], ['id' => $_GET['item']]);
                        }
 
-                       $text = Contact::getPostsFromId($contact['id'], true, true, $item['parent'] ?? 0);
+                       $text = Contact::getThreadsFromId($contact['pid'], DI::userSession()->getLocalUserId(), true, $item['parent'] ?? 0, $_GET['last_received'] ?? '');
                }
        }
 
index 1f9dd9782d8963534633620888ca5793e3649b2a..af9c631af9fe3f14d4c63dd28ad2dc58a057d4c2 100644 (file)
@@ -1566,23 +1566,20 @@ class Contact
         * @return string posts in HTML
         * @throws \Exception
         */
-       public static function getPostsFromUrl(string $contact_url, bool $thread_mode = false, int $update = 0, int $parent = 0, bool $only_media = false): string
+       public static function getPostsFromUrl(string $contact_url, int $uid, bool $only_media = false): string
        {
-               return self::getPostsFromId(self::getIdForURL($contact_url), $thread_mode, $update, $parent, $only_media);
+               return self::getPostsFromId(self::getIdForURL($contact_url), $uid, $only_media);
        }
 
        /**
         * Returns posts from a given contact id
         *
         * @param int  $cid         Contact ID
-        * @param bool $thread_mode
-        * @param int  $update      Update mode
-        * @param int  $parent      Item parent ID for the update mode
         * @param bool $only_media  Only display media content
         * @return string posts in HTML
         * @throws \Exception
         */
-       public static function getPostsFromId(int $cid, bool $thread_mode = false, int $update = 0, int $parent = 0, bool $only_media = false): string
+       public static function getPostsFromId(int $cid, int $uid, bool $only_media = false, string $last_received = null): string
        {
                $contact = DBA::selectFirst('contact', ['contact-type', 'network'], ['id' => $cid]);
                if (!DBA::isResult($contact)) {
@@ -1590,32 +1587,17 @@ class Contact
                }
 
                if (empty($contact["network"]) || in_array($contact["network"], Protocol::FEDERATED)) {
-                       $sql = "(`uid` = 0 OR (`uid` = ? AND NOT `global`))";
+                       $condition = ["(`uid` = 0 OR (`uid` = ? AND NOT `global`))", $uid];
                } else {
-                       $sql = "`uid` = ?";
+                       $condition = ["`uid` = ?", $uid];
                }
 
                $contact_field = ((($contact["contact-type"] == self::TYPE_COMMUNITY) || ($contact['network'] == Protocol::MAIL)) ? 'owner-id' : 'author-id');
 
-               if ($thread_mode) {
-                       $condition = [
-                               "((`$contact_field` = ? AND `gravity` = ?) OR (`author-id` = ? AND `gravity` = ? AND `vid` = ? AND `protocol` != ? AND `thr-parent-id` = `parent-uri-id`)) AND " . $sql,
-                               $cid, Item::GRAVITY_PARENT, $cid, Item::GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE), Conversation::PARCEL_DIASPORA, DI::userSession()->getLocalUserId()
-                       ];
-               } else {
-                       $condition = [
-                               "`$contact_field` = ? AND `gravity` IN (?, ?) AND " . $sql,
-                               $cid, Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT, DI::userSession()->getLocalUserId()
-                       ];
-               }
+               $condition = DBA::mergeConditions($condition, ["`$contact_field` = ? AND `gravity` IN (?, ?)", $cid, Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT]);
 
-               if (!empty($parent)) {
-                       $condition = DBA::mergeConditions($condition, ['parent' => $parent]);
-               } else {
-                       $last_received = isset($_GET['last_received']) ? DateTimeFormat::utc($_GET['last_received']) : '';
-                       if (!empty($last_received)) {
-                               $condition = DBA::mergeConditions($condition, ["`received` < ?", $last_received]);
-                       }
+               if (!empty($last_received)) {
+                       $condition = DBA::mergeConditions($condition, ["`received` < ?", $last_received]);
                }
 
                if ($only_media) {
@@ -1626,66 +1608,108 @@ class Contact
                }
 
                if (DI::mode()->isMobile()) {
-                       $itemsPerPage = DI::pConfig()->get(
-                               DI::userSession()->getLocalUserId(),
-                               'system',
-                               'itemspage_mobile_network',
-                               DI::config()->get('system', 'itemspage_network_mobile')
-                       );
+                       $itemsPerPage = DI::pConfig()->get($uid, 'system', 'itemspage_mobile_network', DI::config()->get('system', 'itemspage_network_mobile'));
                } else {
-                       $itemsPerPage = DI::pConfig()->get(
-                               DI::userSession()->getLocalUserId(),
-                               'system',
-                               'itemspage_network',
-                               DI::config()->get('system', 'itemspage_network')
-                       );
+                       $itemsPerPage = DI::pConfig()->get($uid, 'system', 'itemspage_network', DI::config()->get('system', 'itemspage_network'));
                }
 
                $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), $itemsPerPage);
 
                $params = ['order' => ['received' => true], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
 
-               if (DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'infinite_scroll')) {
+               if (DI::pConfig()->get($uid, 'system', 'infinite_scroll')) {
                        $tpl = Renderer::getMarkupTemplate('infinite_scroll_head.tpl');
                        $o = Renderer::replaceMacros($tpl, ['$reload_uri' => DI::args()->getQueryString()]);
                } else {
                        $o = '';
                }
 
-               if ($thread_mode) {
-                       $fields = ['uri-id', 'thr-parent-id', 'gravity', 'author-id', 'commented'];
-                       $items = Post::toArray(Post::selectForUser(DI::userSession()->getLocalUserId(), $fields, $condition, $params));
+               $fields = array_merge(Item::DISPLAY_FIELDLIST, ['featured']);
+               $items = Post::toArray(Post::selectForUser($uid, $fields, $condition, $params));
 
-                       if ($pager->getStart() == 0) {
-                               $cdata = self::getPublicAndUserContactID($cid, DI::userSession()->getLocalUserId());
-                               if (!empty($cdata['public'])) {
-                                       $pinned = Post\Collection::selectToArrayForContact($cdata['public'], Post\Collection::FEATURED, $fields);
-                                       $items = array_merge($items, $pinned);
-                               }
-                       }
+               $o .= DI::conversation()->render($items, ConversationContent::MODE_CONTACT_POSTS);
 
-                       $o .= DI::conversation()->render($items, ConversationContent::MODE_CONTACTS, $update, false, 'pinned_commented', DI::userSession()->getLocalUserId());
+               if (DI::pConfig()->get($uid, 'system', 'infinite_scroll')) {
+                       $o .= HTML::scrollLoader();
                } else {
-                       $fields = array_merge(Item::DISPLAY_FIELDLIST, ['featured']);
-                       $items = Post::toArray(Post::selectForUser(DI::userSession()->getLocalUserId(), $fields, $condition, $params));
-
-                       if ($pager->getStart() == 0) {
-                               $cdata = self::getPublicAndUserContactID($cid, DI::userSession()->getLocalUserId());
-                               if (!empty($cdata['public'])) {
-                                       $condition = [
-                                               "`uri-id` IN (SELECT `uri-id` FROM `collection-view` WHERE `cid` = ? AND `type` = ?)",
-                                               $cdata['public'], Post\Collection::FEATURED
-                                       ];
-                                       $pinned = Post::toArray(Post::selectForUser(DI::userSession()->getLocalUserId(), $fields, $condition, $params));
-                                       $items = array_merge($pinned, $items);
-                               }
-                       }
+                       $o .= $pager->renderMinimal(count($items));
+               }
+
+               return $o;
+       }
+
+       /**
+        * Returns threads from a given contact id
+        *
+        * @param int  $cid         Contact ID
+        * @param int  $update      Update mode
+        * @param int  $parent      Item parent ID for the update mode
+        * @return string posts in HTML
+        * @throws \Exception
+        */
+       public static function getThreadsFromId(int $cid, int $uid, int $update = 0, int $parent = 0, string $last_received = ''): string
+       {
+               $contact = DBA::selectFirst('contact', ['contact-type', 'network'], ['id' => $cid]);
+               if (!DBA::isResult($contact)) {
+                       return '';
+               }
+
+               if (empty($contact["network"]) || in_array($contact["network"], Protocol::FEDERATED)) {
+                       $condition = ["(`uid` = 0 OR (`uid` = ? AND NOT `global`))", $uid];
+               } else {
+                       $condition = ["`uid` = ?", $uid];
+               }
+
+               if (!empty($parent)) {
+                       $condition = DBA::mergeConditions($condition, ['parent' => $parent]);
+               } elseif (!empty($last_received)) {
+                       $condition = DBA::mergeConditions($condition, ["`received` < ?", $last_received]);
+               }
+
+               $contact_field = ((($contact["contact-type"] == self::TYPE_COMMUNITY) || ($contact['network'] == Protocol::MAIL)) ? 'owner-id' : 'author-id');
+
+               if (DI::mode()->isMobile()) {
+                       $itemsPerPage = DI::pConfig()->get($uid, 'system', 'itemspage_mobile_network', DI::config()->get('system', 'itemspage_network_mobile'));
+               } else {
+                       $itemsPerPage = DI::pConfig()->get($uid, 'system', 'itemspage_network', DI::config()->get('system', 'itemspage_network'));
+               }
+
+               $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), $itemsPerPage);
+
+               if (DI::pConfig()->get($uid, 'system', 'infinite_scroll')) {
+                       $tpl = Renderer::getMarkupTemplate('infinite_scroll_head.tpl');
+                       $o = Renderer::replaceMacros($tpl, ['$reload_uri' => DI::args()->getQueryString()]);
+               } else {
+                       $o = '';
+               }
+
+               $condition1 = DBA::mergeConditions($condition, ["`$contact_field` = ?", $cid]);
+
+               $condition2 = DBA::mergeConditions($condition, [
+                       "`author-id` = ? AND `gravity` = ? AND `vid` = ? AND `protocol` != ? AND `thr-parent-id` = `parent-uri-id`",
+                       $cid, Item::GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE), Conversation::PARCEL_DIASPORA
+               ]);
 
-                       $o .= DI::conversation()->render($items, ConversationContent::MODE_CONTACT_POSTS, $update);
+               $sql1 = "SELECT `uri-id`, `received` FROM `post-thread-user-view` WHERE " . array_shift($condition1);
+               $sql2 = "SELECT `thr-parent-id` AS `uri-id`, `received` FROM `post-user-view` WHERE " . array_shift($condition2);
+
+               $union = array_merge($condition1, $condition2);
+               $sql = $sql1 . " UNION " . $sql2;
+
+               $sql .= " ORDER BY `received` DESC LIMIT ?, ?";
+               $union = array_merge($union, [$pager->getStart(), $pager->getItemsPerPage()]);
+               $items = Post::toArray(DBA::p($sql, $union));
+
+               if ($pager->getStart() == 0) {
+                       $fields = ['uri-id', 'thr-parent-id', 'gravity', 'author-id', 'received'];
+                       $pinned = Post\Collection::selectToArrayForContact($cid, Post\Collection::FEATURED, $fields);
+                       $items = array_merge($items, $pinned);
                }
 
+               $o .= DI::conversation()->render($items, ConversationContent::MODE_CONTACTS, $update, false, 'pinned_received', $uid);
+
                if (!$update) {
-                       if (DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'infinite_scroll')) {
+                       if (DI::pConfig()->get($uid, 'system', 'infinite_scroll')) {
                                $o .= HTML::scrollLoader();
                        } else {
                                $o .= $pager->renderMinimal(count($items));
index 877c5405e40f265999fc302ff2415fcc0993baa3..853c4d8eac692cbebd40a7102d4a30b4818ccaba 100644 (file)
@@ -113,7 +113,7 @@ class Conversations extends BaseModule
                $o = $this->conversation->statusEditor([], 0, true);
 
                $o .= Contact::getTabsHTML($contact, Contact::TAB_CONVERSATIONS);
-               $o .= Model\Contact::getPostsFromId($contact['id'], true);
+               $o .= Model\Contact::getThreadsFromId($contact['id'], $this->userSession->getLocalUserId(), 0, 0, $request['last_received'] ?? '');
 
                return $o;
        }
index 0199aca78f46f455c85f6cc8506e74177428b018..f54baadac6312cc1bc17945b5003622d2a74fa39 100644 (file)
@@ -193,7 +193,7 @@ class Follow extends BaseModule
                        );
 
                        // Show last public posts
-                       $output .= Contact::getPostsFromUrl($contact['url']);
+                       $output .= Contact::getPostsFromUrl($contact['url'], $this->session->getLocalUserId());
                }
 
                return $output;
index fb0f2e07f449a6df54b28e26d5121ba6aaf6fbaa..2472cd3e207befd287c3482adf0645e806eb8699 100644 (file)
 
 namespace Friendica\Module\Contact;
 
+use Friendica\App;
 use Friendica\BaseModule;
 use Friendica\Content\Widget;
+use Friendica\Core\L10n;
+use Friendica\Core\Session\Capability\IHandleUserSessions;
 use Friendica\DI;
 use Friendica\Model;
 use Friendica\Model\Contact as ModelContact;
 use Friendica\Module\Contact;
+use Friendica\Module\Response;
 use Friendica\Network\HTTPException\BadRequestException;
+use Friendica\Util\Profiler;
+use Psr\Log\LoggerInterface;
 
 /**
  * GUI for media posts of a contact
  */
 class Media extends BaseModule
 {
+       /**
+        * @var IHandleUserSessions
+        */
+       private $userSession;
+
+       public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, IHandleUserSessions $userSession, $server, array $parameters = [])
+       {
+               parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
+
+               $this->userSession = $userSession;
+       }
+
        protected function content(array $request = []): string
        {
                $cid = $this->parameters['id'];
@@ -47,7 +65,7 @@ class Media extends BaseModule
 
                $o = Contact::getTabsHTML($contact, Contact::TAB_MEDIA);
 
-               $o .= ModelContact::getPostsFromUrl($contact['url'], false, 0, 0, true);
+               $o .= ModelContact::getPostsFromUrl($contact['url'], $this->userSession->getLocalUserId(), true);
 
                return $o;
        }
index aef979b67703de5b635ffb507f5793936289c99f..61785653be352f13ee933530fa6a03f24f6b25a9 100644 (file)
@@ -99,7 +99,7 @@ class Posts extends BaseModule
 
                $o = Contact::getTabsHTML($contact, Contact::TAB_POSTS);
 
-               $o .= Model\Contact::getPostsFromId($contact['id']);
+               $o .= Model\Contact::getPostsFromId($contact['id'], $this->userSession->getLocalUserId(), false, $request['last_received'] ?? '');
 
                return $o;
        }
index 670fe57e6907ecca51b8d13f636d1744a1589719..ffdd0fdeb684dba1c7923ec918401762a7347380 100644 (file)
@@ -138,7 +138,7 @@ class Unfollow extends \Friendica\BaseModule
                $o .= Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'), ['$title' => $this->t('Posts and Replies')]);
 
                // Show last public posts
-               $o .= Contact::getPostsFromUrl($contact['url']);
+               $o .= Contact::getPostsFromUrl($contact['url'], $this->userSession->getLocalUserId());
 
                return $o;
        }
index e2ee1e3d8ac7256245b0bd4eccb32f2cd35a47af..9df82614bbbbc22ecce1383a074b49df2686d3a1 100644 (file)
 
 namespace Friendica\Module\Profile;
 
+use Friendica\App;
+use Friendica\Core\L10n;
+use Friendica\Core\Session\Capability\IHandleUserSessions;
 use Friendica\DI;
 use Friendica\Model\Contact;
 use Friendica\Model\Profile as ProfileModel;
 use Friendica\Module\BaseProfile;
+use Friendica\Module\Response;
 use Friendica\Network\HTTPException;
+use Friendica\Util\Profiler;
+use Psr\Log\LoggerInterface;
 
 class Media extends BaseProfile
 {
+       /**
+        * @var IHandleUserSessions
+        */
+       private $userSession;
+
+       public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, IHandleUserSessions $userSession, $server, array $parameters = [])
+       {
+               parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
+
+               $this->userSession = $userSession;
+       }
+
        protected function content(array $request = []): string
        {
                $a = DI::app();
@@ -46,7 +64,7 @@ class Media extends BaseProfile
 
                $o = self::getTabsHTML('media', $is_owner, $profile['nickname'], $profile['hide-friends']);
 
-               $o .= Contact::getPostsFromUrl($profile['url'], false, 0, 0, true);
+               $o .= Contact::getPostsFromUrl($profile['url'], $this->userSession->getLocalUserId(), true);
 
                return $o;
        }