From f81192b4c3838480154301ac25cbbbed58593380 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 14 Dec 2023 04:58:43 +0000 Subject: [PATCH] Fix uddate issues and improve speed when displaying contact posts --- mod/update_contact.php | 4 +- src/Model/Contact.php | 156 +++++++++++++++------------ src/Module/Contact/Conversations.php | 2 +- src/Module/Contact/Follow.php | 2 +- src/Module/Contact/Media.php | 20 +++- src/Module/Contact/Posts.php | 2 +- src/Module/Contact/Unfollow.php | 2 +- src/Module/Profile/Media.php | 20 +++- 8 files changed, 134 insertions(+), 74 deletions(-) diff --git a/mod/update_contact.php b/mod/update_contact.php index 1a85018e6d..b97e9ec7be 100644 --- a/mod/update_contact.php +++ b/mod/update_contact.php @@ -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'] ?? ''); } } diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 1f9dd9782d..af9c631af9 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -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)); diff --git a/src/Module/Contact/Conversations.php b/src/Module/Contact/Conversations.php index 877c5405e4..853c4d8eac 100644 --- a/src/Module/Contact/Conversations.php +++ b/src/Module/Contact/Conversations.php @@ -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; } diff --git a/src/Module/Contact/Follow.php b/src/Module/Contact/Follow.php index 0199aca78f..f54baadac6 100644 --- a/src/Module/Contact/Follow.php +++ b/src/Module/Contact/Follow.php @@ -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; diff --git a/src/Module/Contact/Media.php b/src/Module/Contact/Media.php index fb0f2e07f4..2472cd3e20 100644 --- a/src/Module/Contact/Media.php +++ b/src/Module/Contact/Media.php @@ -21,19 +21,37 @@ 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; } diff --git a/src/Module/Contact/Posts.php b/src/Module/Contact/Posts.php index aef979b677..61785653be 100644 --- a/src/Module/Contact/Posts.php +++ b/src/Module/Contact/Posts.php @@ -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; } diff --git a/src/Module/Contact/Unfollow.php b/src/Module/Contact/Unfollow.php index 670fe57e69..ffdd0fdeb6 100644 --- a/src/Module/Contact/Unfollow.php +++ b/src/Module/Contact/Unfollow.php @@ -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; } diff --git a/src/Module/Profile/Media.php b/src/Module/Profile/Media.php index e2ee1e3d8a..9df82614bb 100644 --- a/src/Module/Profile/Media.php +++ b/src/Module/Profile/Media.php @@ -21,14 +21,32 @@ 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; } -- 2.39.5