From cfd15ca747e0f282a968767c120d45e1d5a79a85 Mon Sep 17 00:00:00 2001 From: Jan Tojnar Date: Sat, 8 Dec 2018 05:27:02 +0100 Subject: [PATCH] Update for flarum 0.1.0-beta.8 --- composer.json | 4 +-- bootstrap.php => extend.php | 18 ++++++----- {locale => resources/locale}/en.yml | 0 {locale => resources/locale}/fr.yml | 0 src/Controller/AbstractFeedController.php | 30 ++++++++++--------- src/Controller/DiscussionFeedController.php | 10 +++---- .../DiscussionsActivityFeedController.php | 24 +++++++-------- src/Listener/AddClientAssetsAndLinks.php | 21 +++---------- 8 files changed, 50 insertions(+), 57 deletions(-) rename bootstrap.php => extend.php (83%) rename {locale => resources/locale}/en.yml (100%) rename {locale => resources/locale}/fr.yml (100%) diff --git a/composer.json b/composer.json index 7566512..65bc442 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/bootstrap.php b/extend.php similarity index 83% rename from bootstrap.php rename to extend.php index f38340f..6bbbfa1 100644 --- a/bootstrap.php +++ b/extend.php @@ -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'); + }, +]; diff --git a/locale/en.yml b/resources/locale/en.yml similarity index 100% rename from locale/en.yml rename to resources/locale/en.yml diff --git a/locale/fr.yml b/resources/locale/fr.yml similarity index 100% rename from locale/fr.yml rename to resources/locale/fr.yml diff --git a/src/Controller/AbstractFeedController.php b/src/Controller/AbstractFeedController.php index 6d3f7d5..bb9abbb 100644 --- a/src/Controller/AbstractFeedController.php +++ b/src/Controller/AbstractFeedController.php @@ -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; @@ -54,7 +56,7 @@ * * @package AmauryCarrade\FlarumFeeds\Controller */ -abstract class AbstractFeedController implements ControllerInterface +abstract class AbstractFeedController implements RequestHandlerInterface { /** * @var ApiClient @@ -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'; @@ -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'); } @@ -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'; diff --git a/src/Controller/DiscussionFeedController.php b/src/Controller/DiscussionFeedController.php index d88dcc8..b260241 100644 --- a/src/Controller/DiscussionFeedController.php +++ b/src/Controller/DiscussionFeedController.php @@ -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; @@ -84,7 +84,7 @@ protected function getFeedContent(Request $request) 'offset' => 0, 'limit' => 20 ], - 'sort' => '-time' + 'sort' => '-createdAt' ]); $entries = []; @@ -98,8 +98,8 @@ 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 ]; } @@ -107,7 +107,7 @@ protected function getFeedContent(Request $request) 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 ]; diff --git a/src/Controller/DiscussionsActivityFeedController.php b/src/Controller/DiscussionsActivityFeedController.php index 37f4655..29fa4f1 100644 --- a/src/Controller/DiscussionsActivityFeedController.php +++ b/src/Controller/DiscussionsActivityFeedController.php @@ -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; @@ -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' ]; /** @@ -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); @@ -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)) { @@ -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 ]; } diff --git a/src/Listener/AddClientAssetsAndLinks.php b/src/Listener/AddClientAssetsAndLinks.php index 33b34e5..3758ef7 100644 --- a/src/Listener/AddClientAssetsAndLinks.php +++ b/src/Listener/AddClientAssetsAndLinks.php @@ -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; @@ -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()) { @@ -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(''); }