Skip to content

Commit

Permalink
Update for flarum 0.1.0-beta.8
Browse files Browse the repository at this point in the history
  • Loading branch information
jtojnar committed Dec 8, 2018
1 parent 88fd56c commit cfd15ca
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 57 deletions.
4 changes: 2 additions & 2 deletions composer.json
Expand Up @@ -4,13 +4,13 @@
"license": "CECILL-B",
"type": "flarum-extension",
"require": {
"flarum/core": "^0.1.0-beta.6"
"flarum/core": "^0.1.0-beta.8"
},
"extra": {
"flarum-extension": {
"title": "Syndication",
"icon": {
"name": "rss",
"name": "fas fa-rss",
"backgroundColor": "#e28f12",
"backgroundRepeat": "no-repeat",
"backgroundPosition": "center",
Expand Down
18 changes: 11 additions & 7 deletions bootstrap.php → extend.php
Expand Up @@ -35,15 +35,19 @@
* knowledge of the CeCILL-B license and that you accept its terms.
*/

use Flarum\Extend\Locales;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\View\Factory;

use AmauryCarrade\FlarumFeeds\Listener;

return function (Dispatcher $events, Factory $views)
{
$events->subscribe(Listener\AddFeedsRoutes::class);
$events->subscribe(Listener\AddClientAssetsAndLinks::class);

$views->addNamespace('flarum-feeds', __DIR__.'/views');
};
return [
new Locales(__DIR__ . '/resources/locale'),
function (Dispatcher $events) {
$events->subscribe(Listener\AddFeedsRoutes::class);
$events->subscribe(Listener\AddClientAssetsAndLinks::class);
},
function (Factory $views) {
$views->addNamespace('flarum-feeds', __DIR__.'/views');
},
];
File renamed without changes.
File renamed without changes.
30 changes: 16 additions & 14 deletions src/Controller/AbstractFeedController.php
Expand Up @@ -40,11 +40,13 @@
use DateTime;
use Flarum\Http\Exception\RouteNotFoundException;
use Psr\Http\Message\ServerRequestInterface as Request;
use Flarum\Core\User;
use Flarum\Forum\UrlGenerator;
use Flarum\Http\Controller\ControllerInterface;
use Flarum\User\User;
use Flarum\Http\UrlGenerator;
use Flarum\Api\Client as ApiClient;
use Illuminate\Contracts\View\Factory;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Symfony\Component\Translation\TranslatorInterface;
use Zend\Diactoros\Response;

Expand All @@ -54,7 +56,7 @@
*
* @package AmauryCarrade\FlarumFeeds\Controller
*/
abstract class AbstractFeedController implements ControllerInterface
abstract class AbstractFeedController implements RequestHandlerInterface
{
/**
* @var ApiClient
Expand Down Expand Up @@ -98,14 +100,14 @@ public function __construct(Factory $view, ApiClient $api, TranslatorInterface $
$this->api = $api;
$this->translator = $translator;

$this->url = app('Flarum\Forum\UrlGenerator');
$this->url = app('Flarum\Http\UrlGenerator');
}

/**
* @param Request $request
* @return \Zend\Diactoros\Response
* @param ServerRequestInterface $request
* @return ResponseInterface
*/
public function handle(Request $request)
public function handle(ServerRequestInterface $request): ResponseInterface
{
$feed_type = $this->getFeedType($request);
$feed_type = in_array($feed_type, ['rss', 'atom']) ? $feed_type : 'rss';
Expand All @@ -122,10 +124,10 @@ public function handle(Request $request)
}

/**
* @param Request $request A request.
* @param ServerRequestInterface $request A request.
* @return User The actor for this request.
*/
protected function getActor(Request $request)
protected function getActor(ServerRequestInterface $request)
{
return $request->getAttribute('actor');
}
Expand Down Expand Up @@ -207,16 +209,16 @@ protected function parseDate($date)
}

/**
* @param Request $request
* @param ServerRequestInterface $request
* @return array
*/
abstract protected function getFeedContent(Request $request);
abstract protected function getFeedContent(ServerRequestInterface $request);

/**
* @param Request $request The request
* @param ServerRequestInterface $request The request
* @return string 'rss' or 'atom', defaults to 'rss'.
*/
protected function getFeedType(Request $request)
protected function getFeedType(ServerRequestInterface $request)
{
$path = strtolower($request->getUri()->getPath());
return starts_with($path, '/atom') ? 'atom' : 'rss';
Expand Down
10 changes: 5 additions & 5 deletions src/Controller/DiscussionFeedController.php
Expand Up @@ -38,7 +38,7 @@
namespace AmauryCarrade\FlarumFeeds\Controller;

use Flarum\Api\Client as ApiClient;
use Flarum\Core\User;
use Flarum\User\User;
use Flarum\Http\Exception\RouteNotFoundException;
use Illuminate\Contracts\View\Factory;
use Symfony\Component\Translation\TranslatorInterface;
Expand Down Expand Up @@ -84,7 +84,7 @@ protected function getFeedContent(Request $request)
'offset' => 0,
'limit' => 20
],
'sort' => '-time'
'sort' => '-createdAt'
]);

$entries = [];
Expand All @@ -98,16 +98,16 @@ protected function getFeedContent(Request $request)
'title' => $discussion->attributes->title,
'description' => $this->summary($post->attributes->contentHtml),
'content' => $post->attributes->contentHtml,
'permalink' => $this->url->toRoute('discussion', ['id' => $discussion->id . '-' . $discussion->attributes->slug, 'near' => $post->attributes->number]) . '/' . $post->attributes->number, // TODO check out why the near parameter refuses to work
'pubdate' => $this->parseDate($post->attributes->time),
'permalink' => $this->url->to('forum')->route('discussion', ['id' => $discussion->id . '-' . $discussion->attributes->slug, 'near' => $post->attributes->number]) . '/' . $post->attributes->number, // TODO check out why the near parameter refuses to work
'pubdate' => $this->parseDate($post->attributes->createdAt),
'author' => $this->getRelationship($posts, $post->relationships->user)->username
];
}

return [
'title' => $this->translator->trans('amaurycarrade-syndication.forum.feeds.titles.discussion_title', ['{discussion_name}' => $discussion->attributes->title]),
'description' => $this->translator->trans('amaurycarrade-syndication.forum.feeds.titles.discussion_subtitle', ['{discussion_name}' => $discussion->attributes->title]),
'link' => $this->url->toRoute('discussion', ['id' => $discussion->id . '-' . $discussion->attributes->slug]),
'link' => $this->url->to('forum')->route('discussion', ['id' => $discussion->id . '-' . $discussion->attributes->slug]),
'pubDate' => new \DateTime(),
'entries' => $entries
];
Expand Down
24 changes: 12 additions & 12 deletions src/Controller/DiscussionsActivityFeedController.php
Expand Up @@ -38,7 +38,7 @@
namespace AmauryCarrade\FlarumFeeds\Controller;

use Psr\Http\Message\ServerRequestInterface as Request;
use Flarum\Core\User;
use Flarum\User\User;
use Flarum\Api\Client as ApiClient;
use Illuminate\Contracts\View\Factory;
use Symfony\Component\Translation\TranslatorInterface;
Expand All @@ -59,10 +59,10 @@ class DiscussionsActivityFeedController extends AbstractFeedController
* @var array
*/
private $sortMap = [
'latest' => '-lastTime',
'top' => '-commentsCount',
'newest' => '-startTime',
'oldest' => 'startTime'
'latest' => '-lastPostedAt',
'top' => '-commentCount',
'newest' => '-createdAt',
'oldest' => 'createdAt'
];

/**
Expand Down Expand Up @@ -108,7 +108,7 @@ protected function getFeedContent(Request $request)
'sort' => $sort && isset($this->sortMap[$sort]) ? $this->sortMap[$sort] : ($this->lastTopics ? $this->sortMap['newest'] : $this->sortMap['latest']),
'filter' => compact('q'),
'page' => ['offset' => 0, 'limit' => 20],
'include' => $this->lastTopics ? 'startPost,startUser' : 'lastPost,lastUser'
'include' => $this->lastTopics ? 'firstPost,user' : 'lastPost,lastPostedUser'
];

$actor = $this->getActor($request);
Expand All @@ -121,9 +121,9 @@ protected function getFeedContent(Request $request)
{
if ($discussion->type != 'discussions') continue;

if ($this->lastTopics && isset($discussion->relationships->startPost))
if ($this->lastTopics && isset($discussion->relationships->firstPost))
{
$content = $this->getRelationship($last_discussions, $discussion->relationships->startPost);
$content = $this->getRelationship($last_discussions, $discussion->relationships->firstPost);
}
else if (isset($discussion->relationships->lastPost))
{
Expand All @@ -139,10 +139,10 @@ protected function getFeedContent(Request $request)
'title' => $discussion->attributes->title,
'description' => $this->summary($content->contentHtml),
'content' => $content->contentHtml,
'id' => $this->url->toRoute('discussion', ['id' => $discussion->id . '-' . $discussion->attributes->slug]),
'permalink' => $this->url->toRoute('discussion', ['id' => $discussion->id . '-' . $discussion->attributes->slug, 'near' => $content->number]) . '/' . $content->number, // TODO same than DiscussionFeedController
'pubdate' => $this->parseDate($this->lastTopics ? $discussion->attributes->startTime : $discussion->attributes->lastTime),
'author' => $this->getRelationship($last_discussions, $this->lastTopics ? $discussion->relationships->startUser : $discussion->relationships->lastUser)->username
'id' => $this->url->to('forum')->route('discussion', ['id' => $discussion->id . '-' . $discussion->attributes->slug]),
'permalink' => $this->url->to('forum')->route('discussion', ['id' => $discussion->id . '-' . $discussion->attributes->slug, 'near' => $content->number]) . '/' . $content->number, // TODO same than DiscussionFeedController
'pubdate' => $this->parseDate($this->lastTopics ? $discussion->attributes->createdAt : $discussion->attributes->lastPostedAt),
'author' => $this->getRelationship($last_discussions, $this->lastTopics ? $discussion->relationships->user : $discussion->relationships->lastPostedUser)->username
];
}

Expand Down
21 changes: 4 additions & 17 deletions src/Listener/AddClientAssetsAndLinks.php
Expand Up @@ -40,7 +40,7 @@
use DirectoryIterator;
use Illuminate\Contracts\Events\Dispatcher;
use Flarum\Event\ConfigureLocales;
use Flarum\Event\ConfigureWebApp;
use Flarum\Frontend\Event\Rendering;
use Symfony\Component\Translation\TranslatorInterface;


Expand All @@ -55,23 +55,10 @@ public function subscribe(Dispatcher $events)
{
$this->translator = app('translator');

$events->listen(ConfigureWebApp::class, [$this, 'addFeedLinks']);
$events->listen(ConfigureLocales::class, [$this, 'addLocales']);

}

public function addLocales(ConfigureLocales $event)
{
foreach (new DirectoryIterator(__DIR__.'/../../locale') as $file)
{
if ($file->isFile() && in_array($file->getExtension(), ['yml', 'yaml']))
{
$event->locales->addTranslations($file->getBasename('.'.$file->getExtension()), $file->getPathname());
}
}
$events->listen(Rendering::class, [$this, 'addFeedLinks']);
}

public function addFeedLinks(ConfigureWebApp $event)
public function addFeedLinks(Rendering $event)
{
if ($event->isForum())
{
Expand Down Expand Up @@ -100,7 +87,7 @@ public function addFeedLinks(ConfigureWebApp $event)
}
}

private function addAtomFeed(ConfigureWebApp $event, $url, $title)
private function addAtomFeed(Rendering $event, $url, $title)
{
$event->view->addHeadString('<link rel="alternate" type="application/atom+xml" title="' . $title . '" href="' . app()->url() . '/' . $url . '" />');
}
Expand Down

0 comments on commit cfd15ca

Please sign in to comment.