From 8810cc8f84e24593f00c354bbf9eda2e661c652e Mon Sep 17 00:00:00 2001 From: Mike Lundahl Date: Sat, 21 Sep 2024 23:21:49 +0200 Subject: [PATCH 1/9] Now sending tags within categories. Also included flarum/tags as dev dependency --- composer.json | 5 ++++- .../DiscussionsActivityFeedController.php | 17 ++++++++++++++++- views/atom.blade.php | 3 +++ views/rss.blade.php | 3 +++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 5d29a4b..d128481 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "blomstra/syndication", + "name": "ianm/syndication", "description": "Brings RSS and Atom feeds to Flarum", "license": "CECILL-B", "type": "flarum-extension", @@ -53,5 +53,8 @@ "psr-4": { "IanM\\FlarumFeeds\\": "src/" } + }, + "require-dev": { + "flarum/tags": "*" } } diff --git a/src/Controller/DiscussionsActivityFeedController.php b/src/Controller/DiscussionsActivityFeedController.php index e2835b7..5befdcd 100644 --- a/src/Controller/DiscussionsActivityFeedController.php +++ b/src/Controller/DiscussionsActivityFeedController.php @@ -43,10 +43,12 @@ use Flarum\Http\UrlGenerator; use Flarum\Settings\SettingsRepositoryInterface; use Flarum\User\User; +use Flarum\Tags\Tag; use Illuminate\Contracts\View\Factory; use Illuminate\Support\Arr; use Psr\Http\Message\ServerRequestInterface as Request; use Symfony\Contracts\Translation\TranslatorInterface; +use IanM\FlarumFeeds\Models\DiscussionTag; /** * Displays feeds for topics, either last updated or created, possibly filtered by tag. @@ -88,7 +90,9 @@ protected function getFeedContent(Request $request) $sort = Arr::pull($queryParams, 'sort'); $q = Arr::pull($queryParams, 'q'); - $tags = $this->getTags($request); + + //FIXME: not sure this should be used? + $tags = []; if ($tags != null) { $tags_search = []; @@ -114,10 +118,18 @@ protected function getFeedContent(Request $request) $lastModified = null; foreach ((array) $last_discussions->data as $discussion) { + if ($discussion->type != 'discussions') { continue; } + $tagIds = DiscussionTag::appliedTags($discussion->id)->pluck('tag_id')->toArray(); + $tagNames = Tag::select('name')->whereIn('id', $tagIds)->pluck('name')->toArray(); + + if( !empty($tagNames) ) { + $tags = $tagNames; + } + if ($this->lastTopics && isset($discussion->relationships->firstPost)) { $content = $this->getRelationship($last_discussions, $discussion->relationships->firstPost); } elseif (isset($discussion->relationships->lastPost)) { @@ -132,6 +144,7 @@ protected function getFeedContent(Request $request) } else { $author = isset($discussion->relationships->lastPostedUser) ? $this->getRelationship($last_discussions, $discussion->relationships->lastPostedUser)->username : '[deleted]'; } + $entries[] = [ 'title' => $discussion->attributes->title, 'content' => $this->summarize($this->stripHTML($content->contentHtml)), @@ -139,6 +152,7 @@ protected function getFeedContent(Request $request) 'link' => $this->url->to('forum')->route('discussion', ['id' => $discussion->attributes->slug, 'near' => $content->number]), 'pubdate' => $this->parseDate($this->lastTopics ? $discussion->attributes->createdAt : $discussion->attributes->lastPostedAt), 'author' => $author, + 'tags' => $tagNames, ]; $modified = $this->parseDate($this->lastTopics ? $discussion->attributes->createdAt : $discussion->attributes->lastPostedAt); @@ -149,6 +163,7 @@ protected function getFeedContent(Request $request) } // TODO real tag names + // FIXME: tags shouldn't be in the feed description, it should be in each item if ($this->lastTopics) { if (empty($tags)) { $title = $this->translator->trans('ianm-syndication.forum.feeds.titles.main_d_title', ['{forum_name}' => $forum->attributes->title, '{forum_desc}' => $forum->attributes->description]); diff --git a/views/atom.blade.php b/views/atom.blade.php index 86076e2..f3115c9 100644 --- a/views/atom.blade.php +++ b/views/atom.blade.php @@ -19,6 +19,9 @@ @if ($html) type="html" @endif> + @foreach ($entry['tags'] as $tag) + + @endforeach diff --git a/views/rss.blade.php b/views/rss.blade.php index 72f5588..a77b915 100644 --- a/views/rss.blade.php +++ b/views/rss.blade.php @@ -16,6 +16,9 @@ {{ $entry['id'] }} {{ $entry['pubdate']->format(DateTime::RSS) }} + @foreach ($entry['tags'] as $tag) + {{ $tag }} + @endforeach @endforeach From 0e20fd0f61a36f8e9e2a2475761e073312775c95 Mon Sep 17 00:00:00 2001 From: Mike Lundahl Date: Mon, 23 Sep 2024 23:29:15 +0200 Subject: [PATCH 2/9] Include tags settings implemented --- js/src/admin/index.tsx | 6 ++++++ resources/locale/en.yml | 3 +++ src/Controller/DiscussionsActivityFeedController.php | 12 +++++++----- views/rss.blade.php | 2 +- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/js/src/admin/index.tsx b/js/src/admin/index.tsx index b6558fd..bd6cfe3 100644 --- a/js/src/admin/index.tsx +++ b/js/src/admin/index.tsx @@ -20,6 +20,12 @@ app.initializers.add('ianm-syndication', () => { type: 'boolean', help: app.translator.trans('ianm-syndication.admin.settings.html.help'), }) + .registerSetting({ + label: app.translator.trans('ianm-syndication.admin.settings.include-tags.label'), + setting: 'ianm-syndication.plugin.include-tags', + type: 'boolean', + help: app.translator.trans('ianm-syndication.admin.settings.include-tags.help'), + }) .registerSetting({ label: app.translator.trans('ianm-syndication.admin.settings.entries-count'), setting: 'ianm-syndication.plugin.entries-count', diff --git a/resources/locale/en.yml b/resources/locale/en.yml index ac2633b..daa6fc0 100644 --- a/resources/locale/en.yml +++ b/resources/locale/en.yml @@ -38,6 +38,9 @@ ianm-syndication: html: label: "Formatted Feeds" help: "Check this to keep the HTML in the generated feeds. If disabled, the feeds will be in plain text." + include-tags: + label: "Include tags" + help: "If enabled, tags will be included as categories." entries-count: "How many entries per feed?" forum-icons: label: Show Atom/RSS feed links on the forum diff --git a/src/Controller/DiscussionsActivityFeedController.php b/src/Controller/DiscussionsActivityFeedController.php index 5befdcd..16261b6 100644 --- a/src/Controller/DiscussionsActivityFeedController.php +++ b/src/Controller/DiscussionsActivityFeedController.php @@ -113,6 +113,7 @@ protected function getFeedContent(Request $request) $actor = $this->getActor($request); $forum = $this->getForumDocument($request, $actor); $last_discussions = $this->getDocument($request, $actor, $params); + $includeTags = $this->getSetting('include-tags'); $entries = []; $lastModified = null; @@ -123,12 +124,13 @@ protected function getFeedContent(Request $request) continue; } - $tagIds = DiscussionTag::appliedTags($discussion->id)->pluck('tag_id')->toArray(); - $tagNames = Tag::select('name')->whereIn('id', $tagIds)->pluck('name')->toArray(); + $tagIds = $includeTags ? DiscussionTag::appliedTags($discussion->id)->pluck('tag_id')->toArray() : []; + $tagNames = $includeTags ? Tag::select('name')->whereIn('id', $tagIds)->pluck('name')->toArray() : []; + $tagText = 'In tags: ' . implode(', ', $tagNames); - if( !empty($tagNames) ) { - $tags = $tagNames; - } + //if( !empty($tagNames) ) { + // $tags = $tagNames; + //} if ($this->lastTopics && isset($discussion->relationships->firstPost)) { $content = $this->getRelationship($last_discussions, $discussion->relationships->firstPost); diff --git a/views/rss.blade.php b/views/rss.blade.php index a77b915..c72072f 100644 --- a/views/rss.blade.php +++ b/views/rss.blade.php @@ -17,7 +17,7 @@ {{ $entry['id'] }} {{ $entry['pubdate']->format(DateTime::RSS) }} @foreach ($entry['tags'] as $tag) - {{ $tag }} + {{ $tag }} @endforeach @endforeach From c805e3621eef53301d97d8bcb31dc05f87b651b1 Mon Sep 17 00:00:00 2001 From: Mike Lundahl Date: Mon, 23 Sep 2024 23:31:44 +0200 Subject: [PATCH 3/9] model for DisussionTag --- src/Models/DiscussionTag.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 src/Models/DiscussionTag.php diff --git a/src/Models/DiscussionTag.php b/src/Models/DiscussionTag.php new file mode 100644 index 0000000..1db530d --- /dev/null +++ b/src/Models/DiscussionTag.php @@ -0,0 +1,14 @@ +get(); + } +} From e618968376ca90ea046fec5656fcc34c915515c2 Mon Sep 17 00:00:00 2001 From: Mike Lundahl Date: Tue, 24 Sep 2024 00:14:20 +0200 Subject: [PATCH 4/9] cleanup --- src/Controller/DiscussionsActivityFeedController.php | 11 ++++------- src/Models/DiscussionTag.php | 1 + 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/Controller/DiscussionsActivityFeedController.php b/src/Controller/DiscussionsActivityFeedController.php index 16261b6..522f525 100644 --- a/src/Controller/DiscussionsActivityFeedController.php +++ b/src/Controller/DiscussionsActivityFeedController.php @@ -91,7 +91,6 @@ protected function getFeedContent(Request $request) $sort = Arr::pull($queryParams, 'sort'); $q = Arr::pull($queryParams, 'q'); - //FIXME: not sure this should be used? $tags = []; if ($tags != null) { @@ -125,12 +124,11 @@ protected function getFeedContent(Request $request) } $tagIds = $includeTags ? DiscussionTag::appliedTags($discussion->id)->pluck('tag_id')->toArray() : []; - $tagNames = $includeTags ? Tag::select('name')->whereIn('id', $tagIds)->pluck('name')->toArray() : []; - $tagText = 'In tags: ' . implode(', ', $tagNames); + $tagNames = []; - //if( !empty($tagNames) ) { - // $tags = $tagNames; - //} + if( !empty($tagIds) ) { + $tagNames = $includeTags ? Tag::select('name')->whereIn('id', $tagIds)->pluck('name')->toArray() : []; + } if ($this->lastTopics && isset($discussion->relationships->firstPost)) { $content = $this->getRelationship($last_discussions, $discussion->relationships->firstPost); @@ -165,7 +163,6 @@ protected function getFeedContent(Request $request) } // TODO real tag names - // FIXME: tags shouldn't be in the feed description, it should be in each item if ($this->lastTopics) { if (empty($tags)) { $title = $this->translator->trans('ianm-syndication.forum.feeds.titles.main_d_title', ['{forum_name}' => $forum->attributes->title, '{forum_desc}' => $forum->attributes->description]); diff --git a/src/Models/DiscussionTag.php b/src/Models/DiscussionTag.php index 1db530d..5d021d4 100644 --- a/src/Models/DiscussionTag.php +++ b/src/Models/DiscussionTag.php @@ -7,6 +7,7 @@ class DiscussionTag extends AbstractModel { protected $table = 'discussion_tag'; + public static function appliedTags($id) { return static::where('discussion_id', $id)->get(); From f4b931aff5c26df9398f358ebde64da3f5dd3057 Mon Sep 17 00:00:00 2001 From: Mike Lundahl Date: Tue, 24 Sep 2024 00:24:48 +0200 Subject: [PATCH 5/9] reverted back to previous variable. --- src/Controller/DiscussionsActivityFeedController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Controller/DiscussionsActivityFeedController.php b/src/Controller/DiscussionsActivityFeedController.php index 522f525..893abf4 100644 --- a/src/Controller/DiscussionsActivityFeedController.php +++ b/src/Controller/DiscussionsActivityFeedController.php @@ -91,7 +91,7 @@ protected function getFeedContent(Request $request) $sort = Arr::pull($queryParams, 'sort'); $q = Arr::pull($queryParams, 'q'); - $tags = []; + $tags = $this->getTags($request); if ($tags != null) { $tags_search = []; From 959149ba05e118bb6ad2bee94845f9825c6968c3 Mon Sep 17 00:00:00 2001 From: Mike Lundahl Date: Tue, 24 Sep 2024 00:25:58 +0200 Subject: [PATCH 6/9] removed space --- src/Controller/DiscussionsActivityFeedController.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Controller/DiscussionsActivityFeedController.php b/src/Controller/DiscussionsActivityFeedController.php index 893abf4..782a321 100644 --- a/src/Controller/DiscussionsActivityFeedController.php +++ b/src/Controller/DiscussionsActivityFeedController.php @@ -90,7 +90,6 @@ protected function getFeedContent(Request $request) $sort = Arr::pull($queryParams, 'sort'); $q = Arr::pull($queryParams, 'q'); - $tags = $this->getTags($request); if ($tags != null) { From 784ffde4d35530d88ba9e1d3fd472b7c693edae3 Mon Sep 17 00:00:00 2001 From: Mike Lundahl Date: Tue, 24 Sep 2024 14:32:00 +0200 Subject: [PATCH 7/9] fix for when tags are undefined --- views/atom.blade.php | 2 ++ views/rss.blade.php | 2 ++ 2 files changed, 4 insertions(+) diff --git a/views/atom.blade.php b/views/atom.blade.php index f3115c9..688900e 100644 --- a/views/atom.blade.php +++ b/views/atom.blade.php @@ -19,9 +19,11 @@ @if ($html) type="html" @endif> + @if (isset($entry['tags'])) @foreach ($entry['tags'] as $tag) @endforeach + @endif diff --git a/views/rss.blade.php b/views/rss.blade.php index c72072f..1ec589f 100644 --- a/views/rss.blade.php +++ b/views/rss.blade.php @@ -16,9 +16,11 @@ {{ $entry['id'] }} {{ $entry['pubdate']->format(DateTime::RSS) }} + @if (isset($entry['tags'])) @foreach ($entry['tags'] as $tag) {{ $tag }} @endforeach + @endif @endforeach From 142f523f6dacc24eede5a71dd3d1871015f1d2b5 Mon Sep 17 00:00:00 2001 From: Mike Lundahl Date: Tue, 24 Sep 2024 15:48:34 +0200 Subject: [PATCH 8/9] Tags included in single discussion thread feed --- src/Controller/DiscussionFeedController.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Controller/DiscussionFeedController.php b/src/Controller/DiscussionFeedController.php index 90084f4..86557a1 100644 --- a/src/Controller/DiscussionFeedController.php +++ b/src/Controller/DiscussionFeedController.php @@ -43,7 +43,9 @@ use Flarum\Http\Exception\RouteNotFoundException; use Flarum\Http\UrlGenerator; use Flarum\Settings\SettingsRepositoryInterface; +use Flarum\Tags\Tag; use Flarum\User\User; +use IanM\FlarumFeeds\Models\DiscussionTag; use Illuminate\Contracts\View\Factory; use Illuminate\Support\Arr; use Psr\Http\Message\ServerRequestInterface as Request; @@ -93,12 +95,20 @@ protected function getFeedContent(Request $request) $entries = []; $lastModified = null; + $includeTags = $this->getSetting('include-tags'); foreach ($posts->data as $post) { if ($post->attributes->contentType != 'comment') { continue; } + $tagIds = $includeTags ? DiscussionTag::appliedTags($discussion->id)->pluck('tag_id')->toArray() : []; + $tagNames = []; + + if( !empty($tagIds) ) { + $tagNames = $includeTags ? Tag::select('name')->whereIn('id', $tagIds)->pluck('name')->toArray() : []; + } + $entries[] = [ 'title' => $discussion->attributes->title, 'content' => $this->summarize($this->stripHTML($post->attributes->contentHtml)), @@ -106,6 +116,7 @@ protected function getFeedContent(Request $request) 'id' => $this->url->to('forum')->route('discussion', ['id' => $discussion->id, 'near' => $post->attributes->number]), 'pubdate' => $this->parseDate($post->attributes->createdAt), 'author' => isset($post->relationships->user) ? $this->getRelationship($posts, $post->relationships->user)->username : '[deleted]', + 'tags' => $tagNames, ]; $modified = $this->parseDate(Arr::get($post->attributes, 'editedAt', $post->attributes->createdAt)); From 17e0689ca84cf73edf8144f52645445ceeb9d25e Mon Sep 17 00:00:00 2001 From: Mike Lundahl Date: Tue, 24 Sep 2024 16:09:30 +0200 Subject: [PATCH 9/9] Changed back the name to blomstra --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index d128481..9817d8a 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "ianm/syndication", + "name": "blomstra/syndication", "description": "Brings RSS and Atom feeds to Flarum", "license": "CECILL-B", "type": "flarum-extension",