Skip to content

Commit

Permalink
feat: user posts feed (#12)
Browse files Browse the repository at this point in the history
* feat: user posts feed

* chore: housekeeping
  • Loading branch information
SychO9 committed Sep 12, 2023
1 parent 5c5117d commit 9160a73
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 1 deletion.
5 changes: 4 additions & 1 deletion extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@
->get('/atom/t/{tag}', 'feeds.atom.tag', Controller\TagsFeedController::class)

->get('/rss/t/{tag}/discussions', 'feeds.rss.tag_discussions', Controller\LastDiscussionsByTagFeedController::class)
->get('/atom/t/{tag}/discussions', 'feeds.atom.tag_discussions', Controller\LastDiscussionsByTagFeedController::class),
->get('/atom/t/{tag}/discussions', 'feeds.atom.tag_discussions', Controller\LastDiscussionsByTagFeedController::class)

->get('/rss/u/{username}/posts', 'feeds.rss.user_posts', Controller\UserPostsFeedController::class)
->get('/atom/u/{username}/posts', 'feeds.atom.user_posts', Controller\UserPostsFeedController::class),

(new Extend\Frontend('forum'))
->content(Listener\AddClientLinks::class),
Expand Down
6 changes: 6 additions & 0 deletions resources/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ ianm-syndication:
tag_activity: "Activity for the {tag} tag"
tag_new_discussions: "Discussions in the {tag} tag"
discussion_last_posts: "This discussion"
user_last_posts: "Posts & comments by this user"
feeds:
entries:
user_posts:
title_reply: "Re: {discussion}"
titles:
main_title: "{forum_name}"
main_subtitle: "Last posts in the forum"
Expand All @@ -18,6 +22,8 @@ ianm-syndication:
tag_d_subtitle: "The newest discussions in the {tag} tag"
discussion_title: "{discussion_name}"
discussion_subtitle: "Last messages in this discussion"
user_title: "{username}"
user_subtitle: "Latest posts & comments by this user."
discussion:
feed_link: Feed
admin:
Expand Down
105 changes: 105 additions & 0 deletions src/Controller/UserPostsFeedController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

/*
* Copyright or © or Copr. flarum-ext-syndication contributor : Amaury
* Carrade (2016)
*
* https://amaury.carrade.eu
*
* This software is a computer program whose purpose is to provides RSS
* and Atom feeds to Flarum.
*
* This software is governed by the CeCILL-B license under French law and
* abiding by the rules of distribution of free software. You can use,
* modify and/ or redistribute the software under the terms of the CeCILL-B
* license as circulated by CEA, CNRS and INRIA at the following URL
* "http://www.cecill.info".
*
* As a counterpart to the access to the source code and rights to copy,
* modify and redistribute granted by the license, users are provided only
* with a limited warranty and the software's author, the holder of the
* economic rights, and the successive licensors have only limited
* liability.
*
* In this respect, the user's attention is drawn to the risks associated
* with loading, using, modifying and/or developing or reproducing the
* software by the user in light of its specific status of free software,
* that may mean that it is complicated to manipulate, and that also
* therefore means that it is reserved for developers and experienced
* professionals having in-depth computer knowledge. Users are therefore
* encouraged to load and test the software's suitability as regards their
* requirements in conditions enabling the security of their systems and/or
* data to be ensured and, more generally, to use and operate it in the
* same conditions as regards security.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL-B license and that you accept its terms.
*
*/

namespace IanM\FlarumFeeds\Controller;

use DateTime;
use Flarum\Post\CommentPost;
use Flarum\Post\PostRepository;
use Flarum\User\UserRepository;
use Illuminate\Support\Arr;
use Psr\Http\Message\ServerRequestInterface as Request;

/**
* Displays feed for a given topic.
*/
class UserPostsFeedController extends AbstractFeedController
{
protected $routeName = 'user_posts';

protected function getFeedContent(Request $request): array
{
$username = Arr::get($request->getQueryParams(), 'username');

$actor = $this->getActor($request);

$user = resolve(UserRepository::class)->findOrFailByUsername($username);
$posts = resolve(PostRepository::class)
->queryVisibleTo($actor)
->with(['discussion'])
->where('type', CommentPost::$type)
->where('user_id', $user->id)
->limit($this->getSetting('entries-count'))
->orderBy('created_at', 'desc')
->setModel(new CommentPost)
->get();

$entries = [];
$lastModified = null;

/** @var CommentPost $post */
foreach ($posts as $post) {
$entries[] = [
'title' => $post->discussion->user_id === $user->id
? $post->discussion->title
: $this->translator->trans('ianm-syndication.forum.feeds.entries.user_posts.title_reply', ['{discussion}' => $post->discussion->title]),
'content' => $this->summarize($this->stripHTML($post->formatContent($request))),
'link' => $this->url->to('forum')->route('discussion', ['id' => $post->discussion->slug, 'near' => $post->number]),
'id' => $this->url->to('forum')->route('discussion', ['id' => $post->discussion->id, 'near' => $post->number]),
'pubdate' => $this->parseDate($post->created_at->format(DateTime::RFC3339)),
'author' => $username,
];

$modified = $post->edited_at ?? $post->created_at;

if ($lastModified === null || $lastModified < $modified) {
$lastModified = $modified;
}
}

return [
'title' => $this->translator->trans('ianm-syndication.forum.feeds.titles.user_title', ['{username}' => $username]),
'description' => $this->translator->trans('ianm-syndication.forum.feeds.titles.user_subtitle', ['{username}' => $username]),
'link' => $this->url->to('forum')->route('user', ['username' => $username]),
'pubDate' => new \DateTime(),
'lastModified' => $lastModified,
'entries' => $entries,
];
}
}
5 changes: 5 additions & 0 deletions src/Listener/AddClientLinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use Flarum\Foundation\Config;
use Flarum\Frontend\Document;
use Flarum\Tags\Tag;
use Illuminate\Support\Arr;
use Psr\Http\Message\ServerRequestInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

Expand Down Expand Up @@ -84,6 +85,10 @@ public function __invoke(Document $view, ServerRequestInterface $request)

// TODO add discussion name?
$this->addAtomFeed($view, 'atom/d/'.$path_parts[2], $this->translator->trans('ianm-syndication.forum.autodiscovery.discussion_last_posts'));
} elseif ($route === 'user') {
$username = Arr::get($request->getQueryParams(), 'username');

$this->addAtomFeed($view, "atom/u/$username/posts", $this->translator->trans('ianm-syndication.forum.autodiscovery.user_last_posts'));
}
}

Expand Down

0 comments on commit 9160a73

Please sign in to comment.