From 8594ce0faedef96bbef2ac01166d8cdc3bbb07aa Mon Sep 17 00:00:00 2001 From: swaggboi Date: Mon, 25 Sep 2023 23:00:12 -0400 Subject: [PATCH] Implement RSS feed for remarks --- lib/PostText.pm | 4 +++ lib/PostText/Controller/Remark.pm | 57 +++++++++++++++++++++++++++++++ lib/PostText/Model/Remark.pm | 15 ++++++++ t/remark.t | 5 +++ 4 files changed, 81 insertions(+) diff --git a/lib/PostText.pm b/lib/PostText.pm index 620e59e..f8e2ca9 100644 --- a/lib/PostText.pm +++ b/lib/PostText.pm @@ -187,6 +187,10 @@ sub startup($self) { ->to('remark#flag') ->name('flag_remark'); + $remark->get('feed', [format => [qw{rss xml}]]) + ->to('remark#feed') + ->name('remarks_feed'); + # Login/out $r->any([qw{GET POST}], '/login') ->to('moderator#login') diff --git a/lib/PostText/Controller/Remark.pm b/lib/PostText/Controller/Remark.pm index c66d7c0..4cbe8d4 100644 --- a/lib/PostText/Controller/Remark.pm +++ b/lib/PostText/Controller/Remark.pm @@ -1,6 +1,8 @@ package PostText::Controller::Remark; use Mojo::Base 'Mojolicious::Controller', -signatures; +use Date::Format; +use XML::RSS; sub by_id($self) { my $remark_id = $self->param('remark_id'); @@ -99,4 +101,59 @@ sub flag($self) { $self->redirect_to($redirect_url); } +sub feed($self) { + my $remarks = $self->remark->feed; + my $rss = XML::RSS->new(version => '2.0'); + my $chan_link = $self->url_for(threads_list => {list_page => 1} )->to_abs; + my $rss_link = $self->url_for(remarks_feed => {format => 'rss'})->to_abs; + my $rss_title = 'Post::Text Remarks'; + my $rss_image = $self->url_for('/images/icon_small.png')->to_abs; + + $rss->add_module( + prefix => 'atom', + uri => 'http://www.w3.org/2005/Atom' + ); + + $rss->channel( + title => $rss_title, + description => 'In UTF-8 we trust. 🫡', + link => $chan_link, + lastBuildDate => time2str('%a, %d %b %Y %X %z', time), + atom => { + link => { + href => "$rss_link", # This has to be quoted idk why + rel => 'self', + type => 'application/rss+xml' + } + }); + + $rss->image( + title => $rss_title, + url => $rss_image, + link => $chan_link, + width => 144, + height => 144, + description => 'A small nerdy anime girl' + ); + + for my $remark (@{$remarks}) { + my $description = $self->markdown($remark->{'body'}); + my $item_link = $self->url_for( + single_remark => {remark_id => $remark->{'id'}} + )->to_abs; + + $rss->add_item( + # Maybe do like Re: $thread_title but that's too much + # effort tonight am lazy + #title => $remark->{'title'}, + link => $item_link, + permaLink => $item_link, + description => $description, + pubDate => $remark->{'date'} + ); + } + + $self->render(text => $rss->as_string); +} + 1; diff --git a/lib/PostText/Model/Remark.pm b/lib/PostText/Model/Remark.pm index 7b492a4..d74fdc2 100644 --- a/lib/PostText/Model/Remark.pm +++ b/lib/PostText/Model/Remark.pm @@ -116,4 +116,19 @@ sub unflag($self, $remark_id) { END_SQL } +sub feed($self) { + my $date_format = $self->date_format; + + $self->pg->db->query(<<~'END_SQL', $date_format)->hashes; + SELECT remark_id AS id, + TO_CHAR(remark_date, ?) AS date, + remark_body AS body + FROM remarks + WHERE NOT hidden_status + GROUP BY remark_id + ORDER BY remark_date DESC + LIMIT 15; + END_SQL +} + 1; diff --git a/t/remark.t b/t/remark.t index 3473f33..67a2bf9 100644 --- a/t/remark.t +++ b/t/remark.t @@ -28,6 +28,11 @@ subtest 'View single remark', sub { ->content_type_like(qr{text/plain}); }; +subtest 'Remarks feed', sub { + $t->get_ok('/remark/feed.rss')->status_is(200) + ->content_type_is('application/rss+xml') +}; + $t->ua->max_redirects(1); subtest 'Post new remark', sub {