friendica/src/Module/Conversation/Channel.php

184 lines
7 KiB
PHP
Raw Normal View History

<?php
/**
* @copyright Copyright (C) 2010-2023, 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/>.
2023-09-02 21:37:20 +02:00
*
*/
namespace Friendica\Module\Conversation;
2023-09-02 23:37:02 +02:00
use Friendica\App;
use Friendica\App\Mode;
use Friendica\Content\BoundariesPager;
use Friendica\Content\Conversation;
use Friendica\Content\Conversation\Entity\Channel as ChannelEntity;
2023-10-05 21:10:20 +02:00
use Friendica\Content\Conversation\Factory\UserDefinedChannel as UserDefinedChannelFactory;
use Friendica\Content\Conversation\Factory\Timeline as TimelineFactory;
use Friendica\Content\Conversation\Repository\UserDefinedChannel as ChannelRepository;
use Friendica\Content\Conversation\Factory\Channel as ChannelFactory;
use Friendica\Content\Conversation\Factory\Community as CommunityFactory;
use Friendica\Content\Conversation\Factory\Network as NetworkFactory;
use Friendica\Content\Feature;
use Friendica\Content\Nav;
use Friendica\Content\Text\HTML;
use Friendica\Content\Widget;
use Friendica\Content\Widget\TrendingTags;
2023-09-02 23:37:02 +02:00
use Friendica\Core\Cache\Capability\ICanCache;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\L10n;
use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
use Friendica\Core\Renderer;
2023-09-02 23:37:02 +02:00
use Friendica\Core\Session\Capability\IHandleUserSessions;
use Friendica\Module\Security\Login;
use Friendica\Network\HTTPException;
2023-09-02 23:48:55 +02:00
use Friendica\Database\Database;
2023-09-02 23:37:02 +02:00
use Friendica\Module\Response;
2023-09-03 07:23:49 +02:00
use Friendica\Navigation\SystemMessages;
2023-09-02 23:37:02 +02:00
use Friendica\Util\Profiler;
use Psr\Log\LoggerInterface;
class Channel extends Timeline
{
/** @var TimelineFactory */
protected $timeline;
2023-09-02 23:37:02 +02:00
/** @var Conversation */
2023-09-03 05:35:10 +02:00
protected $conversation;
/** @var App\Page */
protected $page;
/** @var SystemMessages */
protected $systemMessages;
/** @var ChannelFactory */
2023-10-06 13:38:29 +02:00
protected $channel;
2023-10-05 21:10:20 +02:00
/** @var UserDefinedChannelFactory */
protected $userDefinedChannel;
/** @var CommunityFactory */
protected $community;
/** @var NetworkFactory */
protected $networkFactory;
2023-10-05 21:10:20 +02:00
public function __construct(UserDefinedChannelFactory $userDefinedChannel, NetworkFactory $network, CommunityFactory $community, ChannelFactory $channelFactory, ChannelRepository $channel, TimelineFactory $timeline, Conversation $conversation, App\Page $page, SystemMessages $systemMessages, Mode $mode, 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 = [])
2023-09-02 23:37:02 +02:00
{
2023-09-19 11:05:28 +02:00
parent::__construct($channel, $mode, $session, $database, $pConfig, $config, $cache, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
2023-09-02 23:37:02 +02:00
2023-10-05 21:10:20 +02:00
$this->timeline = $timeline;
$this->conversation = $conversation;
$this->page = $page;
$this->systemMessages = $systemMessages;
2023-10-06 13:38:29 +02:00
$this->channel = $channelFactory;
2023-10-05 21:10:20 +02:00
$this->community = $community;
$this->networkFactory = $network;
$this->userDefinedChannel = $userDefinedChannel;
2023-09-02 23:37:02 +02:00
}
protected function content(array $request = []): string
{
2023-09-02 23:37:02 +02:00
if (!$this->session->getLocalUserId()) {
return Login::form();
}
2023-09-02 21:16:48 +02:00
$this->parseRequest($request);
$t = Renderer::getMarkupTemplate("community.tpl");
$o = Renderer::replaceMacros($t, [
'$content' => '',
2023-09-02 00:36:47 +02:00
'$header' => '',
]);
2023-09-02 23:37:02 +02:00
if ($this->pConfig->get($this->session->getLocalUserId(), 'system', 'infinite_scroll')) {
$tpl = Renderer::getMarkupTemplate('infinite_scroll_head.tpl');
2023-09-02 23:37:02 +02:00
$o .= Renderer::replaceMacros($tpl, ['$reload_uri' => $this->args->getQueryString()]);
}
2023-09-02 21:16:48 +02:00
if (empty($request['mode']) || ($request['mode'] != 'raw')) {
2023-10-06 13:38:29 +02:00
$tabs = $this->getTabArray($this->channel->getTimelines($this->session->getLocalUserId()), 'channel');
$tabs = array_merge($tabs, $this->getTabArray($this->channelRepository->selectByUid($this->session->getLocalUserId()), 'channel'));
$tabs = array_merge($tabs, $this->getTabArray($this->community->getTimelines(true), 'channel'));
2023-09-03 10:44:17 +02:00
$tab_tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
$o .= Renderer::replaceMacros($tab_tpl, ['$tabs' => $tabs]);
Nav::setSelected('channel');
2023-09-09 14:48:51 +02:00
$this->page['aside'] .= Widget::accountTypes('channel/' . $this->selectedTab, $this->accountTypeString);
if (!in_array($this->selectedTab, [ChannelEntity::FOLLOWERS, ChannelEntity::FORYOU]) && $this->config->get('system', 'community_no_sharer')) {
$this->page['aside'] .= $this->getNoSharerWidget('channel');
}
2023-09-02 23:37:02 +02:00
if (Feature::isEnabled($this->session->getLocalUserId(), 'trending_tags')) {
2023-09-09 14:48:51 +02:00
$this->page['aside'] .= TrendingTags::getHTML($this->selectedTab);
}
// We need the editor here to be able to reshare an item.
2023-09-02 23:37:02 +02:00
$o .= $this->conversation->statusEditor([], 0, true);
}
2023-10-06 13:38:29 +02:00
if ($this->channel->isTimeline($this->selectedTab) || $this->userDefinedChannel->isTimeline($this->selectedTab, $this->session->getLocalUserId())) {
$items = $this->getChannelItems();
$order = 'created';
} else {
$items = $this->getCommunityItems();
$order = 'commented';
}
2023-09-02 23:48:55 +02:00
if (!$this->database->isResult($items)) {
2023-09-02 23:37:02 +02:00
$this->systemMessages->addNotice($this->l10n->t('No results.'));
return $o;
}
$o .= $this->conversation->render($items, Conversation::MODE_CHANNEL, false, false, $order, $this->session->getLocalUserId());
$pager = new BoundariesPager(
2023-09-02 23:37:02 +02:00
$this->l10n,
$this->args->getQueryString(),
$items[0][$order],
$items[count($items) - 1][$order],
2023-09-09 14:48:51 +02:00
$this->itemsPerPage
);
2023-09-02 23:37:02 +02:00
if ($this->pConfig->get($this->session->getLocalUserId(), 'system', 'infinite_scroll')) {
$o .= HTML::scrollLoader();
} else {
$o .= $pager->renderMinimal(count($items));
}
return $o;
}
/**
* Computes module parameters from the request and local configuration
*
* @throws HTTPException\BadRequestException
* @throws HTTPException\ForbiddenException
*/
2023-09-02 21:16:48 +02:00
protected function parseRequest(array $request)
{
parent::parseRequest($request);
2023-09-09 14:48:51 +02:00
if (!$this->selectedTab) {
$this->selectedTab = ChannelEntity::FORYOU;
}
2023-10-06 13:38:29 +02:00
if (!$this->channel->isTimeline($this->selectedTab) && !$this->userDefinedChannel->isTimeline($this->selectedTab, $this->session->getLocalUserId()) && !$this->community->isTimeline($this->selectedTab)) {
2023-09-02 23:37:02 +02:00
throw new HTTPException\BadRequestException($this->l10n->t('Channel not available.'));
}
2023-09-09 15:02:20 +02:00
$this->maxId = $request['last_created'] ?? $this->maxId;
2023-09-16 06:20:38 +02:00
$this->minId = $request['first_created'] ?? $this->minId;
}
}