Set posts "seen" by a worker if too much rows are unseen

This commit is contained in:
Michael 2024-06-23 21:24:19 +00:00
parent c380a28039
commit 53f3b65887
2 changed files with 50 additions and 14 deletions

View file

@ -47,10 +47,12 @@ use Friendica\Core\L10n;
use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
use Friendica\Core\Renderer;
use Friendica\Core\Session\Capability\IHandleUserSessions;
use Friendica\Core\Worker;
use Friendica\Database\DBA;
use Friendica\Database\Database;
use Friendica\Model\Contact;
use Friendica\Model\Circle;
use Friendica\Model\Post;
use Friendica\Model\Profile;
use Friendica\Module\Response;
use Friendica\Module\Security\Login;
@ -231,7 +233,7 @@ class Network extends Timeline
} else {
$items = $this->getItems();
}
$o .= $this->conversation->render($items, Conversation::MODE_NETWORK, false, false, $this->getOrder(), $this->session->getLocalUserId());
} catch (\Exception $e) {
$o .= $this->l10n->t('Error %d (%s) while fetching the timeline.', $e->getCode(), $e->getMessage());
@ -470,23 +472,20 @@ class Network extends Timeline
$items = array_reverse($items);
}
if ($this->database->isResult($items)) {
$parents = array_column($items, 'uri-id');
} else {
$parents = [];
if ($this->ping || !$this->database->isResult($items)) {
return $items;
}
// We aren't going to try and figure out at the item, circle, and page
// level which items you've seen and which you haven't. If you're looking
// at the top level network page just mark everything seen.
if (!$this->circleId && !$this->star && !$this->mention) {
$condition = ['unseen' => true, 'uid' => $this->session->getLocalUserId()];
$this->setItemsSeenByCondition($condition);
} elseif (!empty($parents)) {
$condition = ['unseen' => true, 'uid' => $this->session->getLocalUserId(), 'parent-uri-id' => $parents];
$this->setItemsSeenByCondition($condition);
$this->setItemsSeenByCondition(['unseen' => true, 'uid' => $this->session->getLocalUserId(), 'parent-uri-id' => array_column($items, 'uri-id')]);
$posts = Post::selectToArray(['uri-id'], ['unseen' => true, 'uid' => $this->session->getLocalUserId()], ['limit' => 100]);
if (!empty($posts)) {
$this->setItemsSeenByCondition(['unseen' => true, 'uid' => $this->session->getLocalUserId(), 'uri-id' => array_column($posts, 'uri-id')]);
}
if (count($posts) == 100) {
Worker::add(Worker::PRIORITY_MEDIUM, 'SetSeen', $this->session->getLocalUserId());
}
return $items;
}

37
src/Worker/SetSeen.php Normal file
View file

@ -0,0 +1,37 @@
<?php
/**
* @copyright Copyright (C) 2010-2024, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Worker;
use Friendica\Core\Logger;
use Friendica\Model\Item;
/**
* Set posts seen for the given user.
*/
class SetSeen
{
public static function execute(int $uid)
{
$ret = Item::update(['unseen' => false], ['unseen' => true, 'uid' => $uid]);
Logger::debug('Set seen', ['uid' => $uid, 'ret' => $ret]);
}
}