Fix uddate issues and improve speed when displaying contact posts

This commit is contained in:
Michael 2023-12-14 04:58:43 +00:00
parent 747a15f0e0
commit f81192b4c3
8 changed files with 135 additions and 75 deletions

View File

@ -31,7 +31,7 @@ use Friendica\Model\Contact;
function update_contact_content(App $a) function update_contact_content(App $a)
{ {
if (!empty(DI::args()->get(1)) && !empty($_GET['force'])) { 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'])) { if (DBA::isResult($contact) && empty($contact['deleted'])) {
DI::page()['aside'] = ''; DI::page()['aside'] = '';
@ -39,7 +39,7 @@ function update_contact_content(App $a)
$item = Post::selectFirst(['parent'], ['id' => $_GET['item']]); $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'] ?? '');
} }
} }

View File

@ -1566,23 +1566,20 @@ class Contact
* @return string posts in HTML * @return string posts in HTML
* @throws \Exception * @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 * Returns posts from a given contact id
* *
* @param int $cid 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 * @param bool $only_media Only display media content
* @return string posts in HTML * @return string posts in HTML
* @throws \Exception * @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]); $contact = DBA::selectFirst('contact', ['contact-type', 'network'], ['id' => $cid]);
if (!DBA::isResult($contact)) { if (!DBA::isResult($contact)) {
@ -1590,33 +1587,18 @@ class Contact
} }
if (empty($contact["network"]) || in_array($contact["network"], Protocol::FEDERATED)) { 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 { } else {
$sql = "`uid` = ?"; $condition = ["`uid` = ?", $uid];
} }
$contact_field = ((($contact["contact-type"] == self::TYPE_COMMUNITY) || ($contact['network'] == Protocol::MAIL)) ? 'owner-id' : 'author-id'); $contact_field = ((($contact["contact-type"] == self::TYPE_COMMUNITY) || ($contact['network'] == Protocol::MAIL)) ? 'owner-id' : 'author-id');
if ($thread_mode) { $condition = DBA::mergeConditions($condition, ["`$contact_field` = ? AND `gravity` IN (?, ?)", $cid, Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT]);
$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()
];
}
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)) { if (!empty($last_received)) {
$condition = DBA::mergeConditions($condition, ["`received` < ?", $last_received]); $condition = DBA::mergeConditions($condition, ["`received` < ?", $last_received]);
} }
}
if ($only_media) { if ($only_media) {
$condition = DBA::mergeConditions($condition, [ $condition = DBA::mergeConditions($condition, [
@ -1626,66 +1608,108 @@ class Contact
} }
if (DI::mode()->isMobile()) { if (DI::mode()->isMobile()) {
$itemsPerPage = DI::pConfig()->get( $itemsPerPage = DI::pConfig()->get($uid, 'system', 'itemspage_mobile_network', DI::config()->get('system', 'itemspage_network_mobile'));
DI::userSession()->getLocalUserId(),
'system',
'itemspage_mobile_network',
DI::config()->get('system', 'itemspage_network_mobile')
);
} else { } else {
$itemsPerPage = DI::pConfig()->get( $itemsPerPage = DI::pConfig()->get($uid, 'system', 'itemspage_network', DI::config()->get('system', 'itemspage_network'));
DI::userSession()->getLocalUserId(),
'system',
'itemspage_network',
DI::config()->get('system', 'itemspage_network')
);
} }
$pager = new Pager(DI::l10n(), DI::args()->getQueryString(), $itemsPerPage); $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), $itemsPerPage);
$params = ['order' => ['received' => true], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]]; $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'); $tpl = Renderer::getMarkupTemplate('infinite_scroll_head.tpl');
$o = Renderer::replaceMacros($tpl, ['$reload_uri' => DI::args()->getQueryString()]); $o = Renderer::replaceMacros($tpl, ['$reload_uri' => DI::args()->getQueryString()]);
} else { } else {
$o = ''; $o = '';
} }
if ($thread_mode) { $fields = array_merge(Item::DISPLAY_FIELDLIST, ['featured']);
$fields = ['uri-id', 'thr-parent-id', 'gravity', 'author-id', 'commented']; $items = Post::toArray(Post::selectForUser($uid, $fields, $condition, $params));
$items = Post::toArray(Post::selectForUser(DI::userSession()->getLocalUserId(), $fields, $condition, $params));
$o .= DI::conversation()->render($items, ConversationContent::MODE_CONTACT_POSTS);
if (DI::pConfig()->get($uid, 'system', 'infinite_scroll')) {
$o .= HTML::scrollLoader();
} else {
$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
]);
$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) { if ($pager->getStart() == 0) {
$cdata = self::getPublicAndUserContactID($cid, DI::userSession()->getLocalUserId()); $fields = ['uri-id', 'thr-parent-id', 'gravity', 'author-id', 'received'];
if (!empty($cdata['public'])) { $pinned = Post\Collection::selectToArrayForContact($cid, Post\Collection::FEATURED, $fields);
$pinned = Post\Collection::selectToArrayForContact($cdata['public'], Post\Collection::FEATURED, $fields);
$items = array_merge($items, $pinned); $items = array_merge($items, $pinned);
} }
}
$o .= DI::conversation()->render($items, ConversationContent::MODE_CONTACTS, $update, false, 'pinned_commented', DI::userSession()->getLocalUserId()); $o .= DI::conversation()->render($items, ConversationContent::MODE_CONTACTS, $update, false, 'pinned_received', $uid);
} 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 .= DI::conversation()->render($items, ConversationContent::MODE_CONTACT_POSTS, $update);
}
if (!$update) { if (!$update) {
if (DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'infinite_scroll')) { if (DI::pConfig()->get($uid, 'system', 'infinite_scroll')) {
$o .= HTML::scrollLoader(); $o .= HTML::scrollLoader();
} else { } else {
$o .= $pager->renderMinimal(count($items)); $o .= $pager->renderMinimal(count($items));

View File

@ -113,7 +113,7 @@ class Conversations extends BaseModule
$o = $this->conversation->statusEditor([], 0, true); $o = $this->conversation->statusEditor([], 0, true);
$o .= Contact::getTabsHTML($contact, Contact::TAB_CONVERSATIONS); $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; return $o;
} }

View File

@ -193,7 +193,7 @@ class Follow extends BaseModule
); );
// Show last public posts // Show last public posts
$output .= Contact::getPostsFromUrl($contact['url']); $output .= Contact::getPostsFromUrl($contact['url'], $this->session->getLocalUserId());
} }
return $output; return $output;

View File

@ -21,19 +21,37 @@
namespace Friendica\Module\Contact; namespace Friendica\Module\Contact;
use Friendica\App;
use Friendica\BaseModule; use Friendica\BaseModule;
use Friendica\Content\Widget; use Friendica\Content\Widget;
use Friendica\Core\L10n;
use Friendica\Core\Session\Capability\IHandleUserSessions;
use Friendica\DI; use Friendica\DI;
use Friendica\Model; use Friendica\Model;
use Friendica\Model\Contact as ModelContact; use Friendica\Model\Contact as ModelContact;
use Friendica\Module\Contact; use Friendica\Module\Contact;
use Friendica\Module\Response;
use Friendica\Network\HTTPException\BadRequestException; use Friendica\Network\HTTPException\BadRequestException;
use Friendica\Util\Profiler;
use Psr\Log\LoggerInterface;
/** /**
* GUI for media posts of a contact * GUI for media posts of a contact
*/ */
class Media extends BaseModule 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 protected function content(array $request = []): string
{ {
$cid = $this->parameters['id']; $cid = $this->parameters['id'];
@ -47,7 +65,7 @@ class Media extends BaseModule
$o = Contact::getTabsHTML($contact, Contact::TAB_MEDIA); $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; return $o;
} }

View File

@ -99,7 +99,7 @@ class Posts extends BaseModule
$o = Contact::getTabsHTML($contact, Contact::TAB_POSTS); $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; return $o;
} }

View File

@ -138,7 +138,7 @@ class Unfollow extends \Friendica\BaseModule
$o .= Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'), ['$title' => $this->t('Posts and Replies')]); $o .= Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'), ['$title' => $this->t('Posts and Replies')]);
// Show last public posts // Show last public posts
$o .= Contact::getPostsFromUrl($contact['url']); $o .= Contact::getPostsFromUrl($contact['url'], $this->userSession->getLocalUserId());
return $o; return $o;
} }

View File

@ -21,14 +21,32 @@
namespace Friendica\Module\Profile; namespace Friendica\Module\Profile;
use Friendica\App;
use Friendica\Core\L10n;
use Friendica\Core\Session\Capability\IHandleUserSessions;
use Friendica\DI; use Friendica\DI;
use Friendica\Model\Contact; use Friendica\Model\Contact;
use Friendica\Model\Profile as ProfileModel; use Friendica\Model\Profile as ProfileModel;
use Friendica\Module\BaseProfile; use Friendica\Module\BaseProfile;
use Friendica\Module\Response;
use Friendica\Network\HTTPException; use Friendica\Network\HTTPException;
use Friendica\Util\Profiler;
use Psr\Log\LoggerInterface;
class Media extends BaseProfile 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 protected function content(array $request = []): string
{ {
$a = DI::app(); $a = DI::app();
@ -46,7 +64,7 @@ class Media extends BaseProfile
$o = self::getTabsHTML('media', $is_owner, $profile['nickname'], $profile['hide-friends']); $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; return $o;
} }