Merge pull request #13717 from annando/contacts-posts

Fix uddate issues and improve speed when displaying contact posts
This commit is contained in:
Hypolite Petovan 2023-12-14 11:48:31 -05:00 committed by GitHub
commit c2e4c0e1fa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 170 additions and 81 deletions

View file

@ -1,6 +1,6 @@
-- ------------------------------------------
-- Friendica 2023.09-rc (Giant Rhubarb)
-- DB_UPDATE_VERSION 1540
-- DB_UPDATE_VERSION 1541
-- ------------------------------------------
@ -1534,7 +1534,8 @@ CREATE TABLE IF NOT EXISTS `post-user` (
INDEX `event-id` (`event-id`),
INDEX `psid` (`psid`),
INDEX `author-id_uid` (`author-id`,`uid`),
INDEX `author-id_received` (`author-id`,`received`),
INDEX `author-id_created` (`author-id`,`created`),
INDEX `owner-id_created` (`owner-id`,`created`),
INDEX `parent-uri-id_uid` (`parent-uri-id`,`uid`),
INDEX `uid_wall_received` (`uid`,`wall`,`received`),
INDEX `uid_contactid` (`uid`,`contact-id`),
@ -1595,6 +1596,8 @@ CREATE TABLE IF NOT EXISTS `post-thread-user` (
INDEX `post-user-id` (`post-user-id`),
INDEX `commented` (`commented`),
INDEX `received` (`received`),
INDEX `author-id_created` (`author-id`,`created`),
INDEX `owner-id_created` (`owner-id`,`created`),
INDEX `uid_received` (`uid`,`received`),
INDEX `uid_wall_received` (`uid`,`wall`,`received`),
INDEX `uid_commented` (`uid`,`commented`),

View file

@ -50,6 +50,8 @@ Indexes
| post-user-id | post-user-id |
| commented | commented |
| received | received |
| author-id_created | author-id, created |
| owner-id_created | owner-id, created |
| uid_received | uid, received |
| uid_wall_received | uid, wall, received |
| uid_commented | uid, commented |

View file

@ -58,7 +58,8 @@ Indexes
| event-id | event-id |
| psid | psid |
| author-id_uid | author-id, uid |
| author-id_received | author-id, received |
| author-id_created | author-id, created |
| owner-id_created | owner-id, created |
| parent-uri-id_uid | parent-uri-id, uid |
| uid_wall_received | uid, wall, received |
| uid_contactid | uid, contact-id |

View 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'] ?? '');
}
}

View file

@ -1298,6 +1298,8 @@ class Conversation
usort($parents, [$this, 'sortThrFeaturedReceived']);
} elseif (stristr($order, 'pinned_commented')) {
usort($parents, [$this, 'sortThrFeaturedCommented']);
} elseif (stristr($order, 'pinned_created')) {
usort($parents, [$this, 'sortThrFeaturedCreated']);
} elseif (stristr($order, 'received')) {
usort($parents, [$this, 'sortThrReceived']);
} elseif (stristr($order, 'commented')) {
@ -1375,6 +1377,24 @@ class Conversation
return strcmp($b['commented'], $a['commented']);
}
/**
* usort() callback to sort item arrays by featured and the created key
*
* @param array $a
* @param array $b
* @return int
*/
private function sortThrFeaturedCreated(array $a, array $b): int
{
if ($b['featured'] && !$a['featured']) {
return 1;
} elseif (!$b['featured'] && $a['featured']) {
return -1;
}
return strcmp($b['created'], $a['created']);
}
/**
* usort() callback to sort item arrays by the received key
*

View 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_created = 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_created)) {
$condition = DBA::mergeConditions($condition, ["`created` < ?", $last_created]);
}
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()]];
$params = ['order' => ['created' => 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 .= DI::conversation()->render($items, ConversationContent::MODE_CONTACT_POSTS, $update);
$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_created = ''): 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_created)) {
$condition = DBA::mergeConditions($condition, ["`created` < ?", $last_created]);
}
$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` = ? AND `gravity` = ?", $cid, Item::GRAVITY_PARENT]);
$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`, `created` FROM `post-thread-user-view` WHERE " . array_shift($condition1);
$sql2 = "SELECT `thr-parent-id` AS `uri-id`, `created` FROM `post-user-view` WHERE " . array_shift($condition2);
$union = array_merge($condition1, $condition2);
$sql = $sql1 . " UNION " . $sql2;
$sql .= " ORDER BY `created` DESC LIMIT ?, ?";
$union = array_merge($union, [$pager->getStart(), $pager->getItemsPerPage()]);
$items = Post::toArray(DBA::p($sql, $union));
if (empty($last_created) && ($pager->getStart() == 0)) {
$fields = ['uri-id', 'thr-parent-id', 'gravity', 'author-id', 'created'];
$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_created', $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));

View 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_created'] ?? '');
return $o;
}

View 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;

View file

@ -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;
}

View 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_created'] ?? '');
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')]);
// Show last public posts
$o .= Contact::getPostsFromUrl($contact['url']);
$o .= Contact::getPostsFromUrl($contact['url'], $this->userSession->getLocalUserId());
return $o;
}

View file

@ -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;
}

View file

@ -56,7 +56,7 @@ use Friendica\Database\DBA;
// This file is required several times during the test in DbaDefinition which justifies this condition
if (!defined('DB_UPDATE_VERSION')) {
define('DB_UPDATE_VERSION', 1540);
define('DB_UPDATE_VERSION', 1541);
}
return [
@ -1551,7 +1551,8 @@ return [
"event-id" => ["event-id"],
"psid" => ["psid"],
"author-id_uid" => ["author-id", "uid"],
"author-id_received" => ["author-id", "received"],
"author-id_created" => ["author-id", "created"],
"owner-id_created" => ["owner-id", "created"],
"parent-uri-id_uid" => ["parent-uri-id", "uid"],
"uid_wall_received" => ["uid", "wall", "received"],
"uid_contactid" => ["uid", "contact-id"],
@ -1601,6 +1602,8 @@ return [
"post-user-id" => ["post-user-id"],
"commented" => ["commented"],
"received" => ["received"],
"author-id_created" => ["author-id", "created"],
"owner-id_created" => ["owner-id", "created"],
"uid_received" => ["uid", "received"],
"uid_wall_received" => ["uid", "wall", "received"],
"uid_commented" => ["uid", "commented"],