From ebfad34e2fd9654ad5dce2fac1d22ba1da4c71b1 Mon Sep 17 00:00:00 2001 From: swag Date: Mon, 24 Apr 2023 14:25:32 -0400 Subject: [PATCH] Implement RSS feed --- README.md | 1 - cpanfile | 2 ++ lib/PostText.pm | 4 ++- lib/PostText/Controller/Thread.pm | 42 +++++++++++++++++++++++-------- t/thread.t | 5 ++++ templates/layouts/default.html.ep | 13 +++++----- 6 files changed, 48 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 7f99141..cbf9f3b 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,6 @@ Run the tests locally (against development environment): ### (Lord knows there's TODOs I could be working on...) -1. RSS feed!! 1. Support at least some Markdown, specifically the code blocks 1. Implement tripcodes (moving this down in priority due to complexity...) 1. Return a text response instead of HTML if a `.txt` extension [is diff --git a/cpanfile b/cpanfile index d8d50c5..b936369 100644 --- a/cpanfile +++ b/cpanfile @@ -5,3 +5,5 @@ requires 'Mojolicious::Plugin::AssetPack'; requires 'Crypt::Passphrase::Argon2'; requires 'Crypt::Passphrase::Bcrypt'; # Needed for old passphrases requires 'Perl::Critic::Community'; +requires 'Date::Format'; +requires 'XML::RSS'; diff --git a/lib/PostText.pm b/lib/PostText.pm index bcd3fb7..8557e87 100644 --- a/lib/PostText.pm +++ b/lib/PostText.pm @@ -115,7 +115,9 @@ sub startup($self) { ->to('thread#flag') ->name('flag_thread'); - #$thread->get('feed', [format => ['rss']])->to('thread#feed'); + $thread->get('feed', [format => [qw{rss xml}]]) + ->to('thread#feed') + ->name('threads_feed'); # Remark my $remark = $r->any('/remark'); diff --git a/lib/PostText/Controller/Thread.pm b/lib/PostText/Controller/Thread.pm index 68c88e7..9145f34 100644 --- a/lib/PostText/Controller/Thread.pm +++ b/lib/PostText/Controller/Thread.pm @@ -1,7 +1,8 @@ package PostText::Controller::Thread; use Mojo::Base 'Mojolicious::Controller', -signatures; -#use XML::RSS; +use Date::Format; +use XML::RSS; sub create($self) { my $v; @@ -91,16 +92,35 @@ sub by_page($self) { $self->render; } -#sub feed($self) { -# my $threads = $self->thread->by_page(1); -# my $rss = XML::RSS->new(version => '2.0'); -# -# $rss->channel( -# title => 'Post::Text', -# description => 'In UTF-8 we trust. 🫡', -# link => $self->url_for('thread_list')->to_abs, -# ); -#} +sub feed($self) { + my $threads = $self->thread->by_page(1); + my $rss = XML::RSS->new(version => '2.0'); + my $chan_link = $self->url_for(threads_list => {list_page => 1})->to_abs; + + $rss->channel( + title => 'Post::Text', + description => 'In UTF-8 we trust. 🫡', + link => $chan_link, + lastBuildDate => time2str('%a, %d %b %Y %X %Z', time) + ); + + for my $thread (@{$threads}) { + my $item_link = + $self->url_for( + single_thread => {thread_id => $thread->{'id'}} + )->to_abs; + + $rss->add_item( + title => $thread->{'title'}, + link => $item_link, + description => $self->truncate_text($thread->{'body'}), + author => $thread->{'author'}, + pubDate => $thread->{'date'} + ); + } + + $self->render(text => $rss->as_string); +} sub bump($self) { my $thread_id = $self->param('thread_id'); diff --git a/t/thread.t b/t/thread.t index 3ff44f7..cf06cef 100644 --- a/t/thread.t +++ b/t/thread.t @@ -38,6 +38,11 @@ subtest 'View single thread', sub { ->text_like(h2 => qr/Thread #1/); }; +subtest 'Threads feed', sub { + $t->get_ok('/thread/feed.rss')->status_is(200) + ->content_type_is('application/rss+xml') +}; + $t->ua->max_redirects(1); subtest 'Post new thread', sub { diff --git a/templates/layouts/default.html.ep b/templates/layouts/default.html.ep index 57bdf0e..0578d91 100644 --- a/templates/layouts/default.html.ep +++ b/templates/layouts/default.html.ep @@ -8,23 +8,24 @@

Post::Text


<% if (flash 'error') { =%>