Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/include tags #1

Merged
merged 9 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,8 @@
"psr-4": {
"IanM\\FlarumFeeds\\": "src/"
}
},
"require-dev": {
"flarum/tags": "*"
}
}
6 changes: 6 additions & 0 deletions js/src/admin/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
3 changes: 3 additions & 0 deletions resources/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions src/Controller/DiscussionFeedController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -93,19 +95,28 @@ 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)),
'link' => $this->url->to('forum')->route('discussion', ['id' => $discussion->attributes->slug, 'near' => $post->attributes->number]),
'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));
Expand Down
13 changes: 13 additions & 0 deletions src/Controller/DiscussionsActivityFeedController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -109,15 +111,24 @@ 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;

foreach ((array) $last_discussions->data as $discussion) {

if ($discussion->type != 'discussions') {
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() : [];
}

if ($this->lastTopics && isset($discussion->relationships->firstPost)) {
$content = $this->getRelationship($last_discussions, $discussion->relationships->firstPost);
} elseif (isset($discussion->relationships->lastPost)) {
Expand All @@ -132,13 +143,15 @@ 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)),
'id' => $this->url->to('forum')->route('discussion', ['id' => $discussion->id, 'near' => $content->number]),
'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);
Expand Down
15 changes: 15 additions & 0 deletions src/Models/DiscussionTag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace IanM\FlarumFeeds\Models;

use Flarum\Database\AbstractModel;

class DiscussionTag extends AbstractModel
{
protected $table = 'discussion_tag';

public static function appliedTags($id)
{
return static::where('discussion_id', $id)->get();
}
}
5 changes: 5 additions & 0 deletions views/atom.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
@if ($html)
type="html"
@endif><![CDATA[{!! $entry['content'] !!}]]></content>
@if (isset($entry['tags']))
@foreach ($entry['tags'] as $tag)
<category term="{{ $tag }}" />
@endforeach
@endif
<author>
<name><![CDATA[{{ $entry['author'] }}]]></name>
</author>
Expand Down
5 changes: 5 additions & 0 deletions views/rss.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
<description><![CDATA[{!! $entry['content'] !!}]]></description>
<guid isPermaLink="true">{{ $entry['id'] }}</guid>
<pubDate>{{ $entry['pubdate']->format(DateTime::RSS) }}</pubDate>
@if (isset($entry['tags']))
@foreach ($entry['tags'] as $tag)
<category>{{ $tag }}</category>
@endforeach
@endif
</item>
@endforeach

Expand Down
Loading