Use centralized function to set items seen

This commit is contained in:
Michael 2023-09-09 14:13:58 +00:00
parent ab235e24ad
commit f9740e180e
2 changed files with 35 additions and 30 deletions

View File

@ -43,11 +43,9 @@ use Friendica\Core\Renderer;
use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\Core\Session\Capability\IHandleUserSessions;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Database\Database; use Friendica\Database\Database;
use Friendica\DI;
use Friendica\Model\Contact; use Friendica\Model\Contact;
use Friendica\Model\Circle; use Friendica\Model\Circle;
use Friendica\Model\Item; use Friendica\Model\Item;
use Friendica\Model\Post;
use Friendica\Model\Profile; use Friendica\Model\Profile;
use Friendica\Model\Verb; use Friendica\Model\Verb;
use Friendica\Module\Contact as ModuleContact; use Friendica\Module\Contact as ModuleContact;
@ -79,6 +77,8 @@ class Network extends Timeline
/** @var string */ /** @var string */
protected static $order; protected static $order;
/** @var App */
protected $app;
/** @var ICanCache */ /** @var ICanCache */
protected $cache; protected $cache;
/** @var IManageConfigValues The config */ /** @var IManageConfigValues The config */
@ -96,10 +96,11 @@ class Network extends Timeline
/** @var TimelineFactory */ /** @var TimelineFactory */
protected $timeline; protected $timeline;
public function __construct(TimelineFactory $timeline, SystemMessages $systemMessages, Mode $mode, Conversation $conversation, App\Page $page, IHandleUserSessions $session, Database $database, IManagePersonalConfigValues $pConfig, IManageConfigValues $config, ICanCache $cache, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = []) public function __construct(App $app, TimelineFactory $timeline, SystemMessages $systemMessages, Mode $mode, Conversation $conversation, App\Page $page, IHandleUserSessions $session, Database $database, IManagePersonalConfigValues $pConfig, IManageConfigValues $config, ICanCache $cache, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
{ {
parent::__construct($mode, $session, $database, $pConfig, $config, $cache, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); parent::__construct($mode, $session, $database, $pConfig, $config, $cache, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
$this->app = $app;
$this->timeline = $timeline; $this->timeline = $timeline;
$this->systemMessages = $systemMessages; $this->systemMessages = $systemMessages;
$this->conversation = $conversation; $this->conversation = $conversation;
@ -268,26 +269,6 @@ class Network extends Timeline
} }
} }
/**
* Sets items as seen
*
* @param array $condition The array with the SQL condition
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
private static function setItemsSeenByCondition(array $condition)
{
if (empty($condition)) {
return;
}
$unseen = Post::exists($condition);
if ($unseen) {
/// @todo handle huge "unseen" updates in the background to avoid timeout errors
Item::update(['unseen' => false], $condition);
}
}
/** /**
* Get the network tabs menu * Get the network tabs menu
* *
@ -316,7 +297,7 @@ class Network extends Timeline
self::$groupContactId = (int)($this->parameters['contact_id'] ?? 0); self::$groupContactId = (int)($this->parameters['contact_id'] ?? 0);
if (!$this->selectedTab) { if (!$this->selectedTab) {
$this->selectedTab = self::getTimelineOrderBySession(DI::userSession(), $this->pConfig); $this->selectedTab = self::getTimelineOrderBySession($this->session, $this->pConfig);
} elseif (!$this->timeline->isChannel($this->selectedTab) && !$this->timeline->isCommunity($this->selectedTab)) { } elseif (!$this->timeline->isChannel($this->selectedTab) && !$this->timeline->isCommunity($this->selectedTab)) {
throw new HTTPException\BadRequestException($this->l10n->t('Network feed not available.')); throw new HTTPException\BadRequestException($this->l10n->t('Network feed not available.'));
} }
@ -402,10 +383,10 @@ class Network extends Timeline
} }
if (self::$dateFrom) { if (self::$dateFrom) {
$conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` <= ? ", DateTimeFormat::convert(self::$dateFrom, 'UTC', DI::app()->getTimeZone())]); $conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` <= ? ", DateTimeFormat::convert(self::$dateFrom, 'UTC', $this->app->getTimeZone())]);
} }
if (self::$dateTo) { if (self::$dateTo) {
$conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` >= ? ", DateTimeFormat::convert(self::$dateTo, 'UTC', DI::app()->getTimeZone())]); $conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` >= ? ", DateTimeFormat::convert(self::$dateTo, 'UTC', $this->app->getTimeZone())]);
} }
if (self::$circleId) { if (self::$circleId) {
@ -484,10 +465,10 @@ class Network extends Timeline
// at the top level network page just mark everything seen. // at the top level network page just mark everything seen.
if (!self::$circleId && !self::$groupContactId && !self::$star && !self::$mention) { if (!self::$circleId && !self::$groupContactId && !self::$star && !self::$mention) {
$condition = ['unseen' => true, 'uid' => $this->session->getLocalUserId()]; $condition = ['unseen' => true, 'uid' => $this->session->getLocalUserId()];
self::setItemsSeenByCondition($condition); $this->setItemsSeenByCondition($condition);
} elseif (!empty($parents)) { } elseif (!empty($parents)) {
$condition = ['unseen' => true, 'uid' => $this->session->getLocalUserId(), 'parent-uri-id' => $parents]; $condition = ['unseen' => true, 'uid' => $this->session->getLocalUserId(), 'parent-uri-id' => $parents];
self::setItemsSeenByCondition($condition); $this->setItemsSeenByCondition($condition);
} }
return $items; return $items;

View File

@ -65,7 +65,7 @@ class Timeline extends BaseModule
/** @var App\Mode $mode */ /** @var App\Mode $mode */
protected $mode; protected $mode;
/** @var UserSession */ /** @var IHandleUserSessions */
protected $session; protected $session;
/** @var Database */ /** @var Database */
protected $database; protected $database;
@ -267,7 +267,8 @@ class Timeline extends BaseModule
$items = array_reverse($items); $items = array_reverse($items);
} }
Item::update(['unseen' => false], ['unseen' => true, 'uid' => $uid, 'uri-id' => array_column($items, 'uri-id')]); $condition = ['unseen' => true, 'uid' => $uid, 'parent-uri-id' => array_column($items, 'uri-id')];
$this->setItemsSeenByCondition($condition);
return $items; return $items;
} }
@ -406,6 +407,9 @@ class Timeline extends BaseModule
$selected_items = $items; $selected_items = $items;
} }
$condition = ['unseen' => true, 'uid' => $this->session->getLocalUserId(), 'parent-uri-id' => array_column($selected_items, 'uri-id')];
$this->setItemsSeenByCondition($condition);
return $selected_items; return $selected_items;
} }
@ -471,4 +475,24 @@ class Timeline extends BaseModule
return $items; return $items;
} }
/**
* Sets items as seen
*
* @param array $condition The array with the SQL condition
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
protected function setItemsSeenByCondition(array $condition)
{
if (empty($condition)) {
return;
}
$unseen = Post::exists($condition);
if ($unseen) {
/// @todo handle huge "unseen" updates in the background to avoid timeout errors
Item::update(['unseen' => false], $condition);
}
}
} }