Implement RSS feed for remarks

This commit is contained in:
swaggboi 2023-09-25 23:00:12 -04:00
parent 9079fa2a46
commit 8594ce0fae
4 changed files with 81 additions and 0 deletions

View File

@ -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')

View File

@ -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;

View File

@ -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;

View File

@ -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 {