1
0
Fork 0

Move mod/display.php "feed-item" to a src\Module\Item\Feed.php

This commit is contained in:
Philipp Holzer 2022-11-14 21:56:41 +01:00
parent 744d0fdde4
commit 5c05a3b0f6
Signed by: nupplaPhil
GPG key ID: 24A7501396EB5432
3 changed files with 95 additions and 18 deletions

View file

@ -36,7 +36,6 @@ use Friendica\Module\Response;
use Friendica\Network\HTTPException; use Friendica\Network\HTTPException;
use Friendica\Protocol\ActivityPub; use Friendica\Protocol\ActivityPub;
use Friendica\Protocol\DFRN; use Friendica\Protocol\DFRN;
use Friendica\Protocol\Diaspora;
use Friendica\Util\DateTimeFormat; use Friendica\Util\DateTimeFormat;
function display_init(App $a) function display_init(App $a)
@ -49,8 +48,6 @@ function display_init(App $a)
return; return;
} }
$nick = ((DI::args()->getArgc() > 1) ? DI::args()->getArgv()[1] : '');
$item = null; $item = null;
$item_user = DI::userSession()->getLocalUserId(); $item_user = DI::userSession()->getLocalUserId();
@ -58,14 +55,9 @@ function display_init(App $a)
// If there is only one parameter, then check if this parameter could be a guid // If there is only one parameter, then check if this parameter could be a guid
if (DI::args()->getArgc() == 2) { if (DI::args()->getArgc() == 2) {
$nick = '';
// Does the local user have this item? // Does the local user have this item?
if (DI::userSession()->getLocalUserId()) { if (DI::userSession()->getLocalUserId()) {
$item = Post::selectFirstForUser(DI::userSession()->getLocalUserId(), $fields, ['guid' => DI::args()->getArgv()[1], 'uid' => DI::userSession()->getLocalUserId()]); $item = Post::selectFirstForUser(DI::userSession()->getLocalUserId(), $fields, ['guid' => DI::args()->getArgv()[1], 'uid' => DI::userSession()->getLocalUserId()]);
if (DBA::isResult($item)) {
$nick = $a->getLoggedInUserNickname();
}
} }
// Is this item private but could be visible to the remove visitor? // Is this item private but could be visible to the remove visitor?
@ -84,22 +76,12 @@ function display_init(App $a)
if (!DBA::isResult($item)) { if (!DBA::isResult($item)) {
$item = Post::selectFirstForUser(DI::userSession()->getLocalUserId(), $fields, ['guid' => DI::args()->getArgv()[1], 'private' => [Item::PUBLIC, Item::UNLISTED], 'uid' => 0]); $item = Post::selectFirstForUser(DI::userSession()->getLocalUserId(), $fields, ['guid' => DI::args()->getArgv()[1], 'private' => [Item::PUBLIC, Item::UNLISTED], 'uid' => 0]);
} }
} elseif (DI::args()->getArgc() >= 3 && $nick == 'feed-item') {
$uri_id = DI::args()->getArgv()[2];
if (substr($uri_id, -5) == '.atom') {
$uri_id = substr($uri_id, 0, -5);
}
$item = Post::selectFirstForUser(DI::userSession()->getLocalUserId(), $fields, ['uri-id' => $uri_id, 'private' => [Item::PUBLIC, Item::UNLISTED], 'uid' => 0]);
} }
if (!DBA::isResult($item)) { if (!DBA::isResult($item)) {
return; return;
} }
if (DI::args()->getArgc() >= 3 && $nick == 'feed-item') {
displayShowFeed($item['uri-id'], $item['uid'], DI::args()->getArgc() > 3 && DI::args()->getArgv()[3] == 'conversation.atom');
}
if (!empty($_SERVER['HTTP_ACCEPT']) && strstr($_SERVER['HTTP_ACCEPT'], 'application/atom+xml')) { if (!empty($_SERVER['HTTP_ACCEPT']) && strstr($_SERVER['HTTP_ACCEPT'], 'application/atom+xml')) {
Logger::debug('Directly serving XML', ['uri-id' => $item['uri-id']]); Logger::debug('Directly serving XML', ['uri-id' => $item['uri-id']]);
displayShowFeed($item['uri-id'], $item['uid'], false); displayShowFeed($item['uri-id'], $item['uid'], false);

92
src/Module/Item/Feed.php Normal file
View file

@ -0,0 +1,92 @@
<?php
/**
* @copyright Copyright (C) 2010-2022, 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/>.
*
* See update_profile.php for documentation
*/
namespace Friendica\Module\Item;
use Friendica\App;
use Friendica\BaseModule;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\L10n;
use Friendica\Core\Session\Capability\IHandleUserSessions;
use Friendica\Core\System;
use Friendica\Model\Item;
use Friendica\Model\Post;
use Friendica\Module\Response;
use Friendica\Protocol\DFRN;
use Friendica\Util\Profiler;
use Friendica\Network\HTTPException;
use Psr\Log\LoggerInterface;
/**
* Controller to display an item (or the whole conversation of an item) as an ATOM Feed
*/
class Feed extends BaseModule
{
/** @var IManageConfigValues */
protected $config;
/** @var IHandleUserSessions */
protected $session;
public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, IManageConfigValues $config, IHandleUserSessions $session, array $server, array $parameters = [])
{
parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
$this->config = $config;
$this->session = $session;
}
protected function rawContent(array $request = [])
{
if ($this->config->get('system', 'block_public') && !$this->session->isAuthenticated()) {
throw new HTTPException\UnauthorizedException($this->t('Access denied.'));
}
$uriId = $this->parameters['uri-id'];
$item = Post::selectFirstForUser($this->session->getLocalUserId(), [
'uri-id',
'parent-uri-id',
'author-id',
'author-link',
'body',
'uid',
'guid',
'gravity',
], [
'uri-id' => $uriId,
'private' => [Item::PUBLIC, Item::UNLISTED],
'uid' => 0,
]);
if (empty($item)) {
throw new HTTPException\BadRequestException($this->t('Item not found.', ['uri-id' => $uriId]));
}
$xml = DFRN::itemFeed($item['uri-id'], $item['uid'], ($this->parameters['mode'] ?? '') === 'conversation');
if (empty($xml)) {
throw new HTTPException\InternalServerErrorException($this->t('The feed for this item is unavailable.', ['uri-id' => $uriId]));
}
System::httpExit($xml, Response::TYPE_ATOM);
}
}

View file

@ -398,6 +398,9 @@ return [
'/dirfind' => [Module\Search\Directory::class, [R::GET]], '/dirfind' => [Module\Search\Directory::class, [R::GET]],
'/directory' => [Module\Directory::class, [R::GET]], '/directory' => [Module\Directory::class, [R::GET]],
'/display/feed-item/{uri-id}[.atom]' => [Module\Item\Feed::class, [R::GET]],
'/display/feed-item/{uri-id}/{mode:conversation}[.atom]' => [Module\Item\Feed::class, [R::GET]],
'/featured/{nickname}' => [Module\ActivityPub\Featured::class, [R::GET]], '/featured/{nickname}' => [Module\ActivityPub\Featured::class, [R::GET]],
'/feed' => [ '/feed' => [