Created function to search posts

This commit is contained in:
swagg boi 2023-10-27 16:57:21 -04:00
parent 7dc52a9655
commit f2e63b5ca3
3 changed files with 75 additions and 4 deletions

View file

@ -13,6 +13,7 @@ use HTML::Restrict;
use PostText::Model::Thread; use PostText::Model::Thread;
use PostText::Model::Remark; use PostText::Model::Remark;
use PostText::Model::Moderator; use PostText::Model::Moderator;
use PostText::Model::Page;
sub startup($self) { sub startup($self) {
$self->plugin('Config'); $self->plugin('Config');
@ -56,7 +57,11 @@ sub startup($self) {
state $moderator = PostText::Model::Moderator->new( state $moderator = PostText::Model::Moderator->new(
pg => $c->pg, pg => $c->pg,
authenticator => $c->authenticator authenticator => $c->authenticator
) )
});
$self->helper(page => sub ($c) {
state $moderator = PostText::Model::Page->new(pg => $c->pg)
}); });
$self->helper(truncate_text => sub ($c, $input_text) { $self->helper(truncate_text => sub ($c, $input_text) {
@ -85,7 +90,7 @@ sub startup($self) {
# Finish configuring some things # Finish configuring some things
$self->secrets($self->config->{'secrets'}) || die $@; $self->secrets($self->config->{'secrets'}) || die $@;
$self->pg->migrations->from_dir('migrations')->migrate(14); $self->pg->migrations->from_dir('migrations')->migrate(15);
if (my $threads_per_page = $self->config->{'threads_per_page'}) { if (my $threads_per_page = $self->config->{'threads_per_page'}) {
$self->thread->per_page($threads_per_page) $self->thread->per_page($threads_per_page)
@ -95,6 +100,10 @@ sub startup($self) {
$self->remark->per_page($remarks_per_page) $self->remark->per_page($remarks_per_page)
} }
if (my $results_per_page = $self->config->{'results_per_page'}) {
$self->page->per_page($results_per_page)
}
$self->asset->process; $self->asset->process;
push @{$self->commands->namespaces}, 'PostText::Command'; push @{$self->commands->namespaces}, 'PostText::Command';

View file

@ -7,7 +7,7 @@ sub flagged($self) {
my @post_links = map { my @post_links = map {
$self->url_for( $self->url_for(
'hidden_' . $_->{'type'}, $_->{'type'} . '_id' => $_->{'id'} 'hidden_' . $_->{'type'}, $_->{'type'} . '_id' => $_->{'id'}
) )
} @{$flagged_posts}; } @{$flagged_posts};
$self->stash(post_links => \@post_links); $self->stash(post_links => \@post_links);
@ -20,7 +20,7 @@ sub hidden($self) {
my @post_links = map { my @post_links = map {
$self->url_for( $self->url_for(
'hidden_' . $_->{'type'}, $_->{'type'} . '_id' => $_->{'id'} 'hidden_' . $_->{'type'}, $_->{'type'} . '_id' => $_->{'id'}
) )
} @{$hidden_posts}; } @{$hidden_posts};
$self->stash(post_links => \@post_links); $self->stash(post_links => \@post_links);

View file

@ -0,0 +1,62 @@
package PostText::Model::Page;
use Mojo::Base -base, -signatures;
has 'pg';
has per_page => 5;
has date_format => 'Dy, FMDD Mon YYYY HH24:MI:SS TZHTZM';
# args: date_format, search_query, limit, offset
# SELECT 'thread' AS post_type,
# thread_id AS post_id,
# TO_CHAR(thread_date, $1) AS post_date,
# thread_author AS post_author,
# thread_body AS post_body,
# ts_rank(search_tokens, plainto_tsquery('english', $2)) AS search_rank
# FROM threads
# WHERE search_tokens @@ plainto_tsquery('english', $2)
# UNION ALL
# SELECT 'remark',
# remark_id,
# remark_date,
# remark_author,
# remark_body,
# ts_rank(search_tokens, plainto_tsquery('english', $2))
# FROM remarks
# WHERE search_tokens @@ plainto_tsquery('english', $2)
# ORDER BY search_rank DESC, post_date DESC
# LIMIT $3 OFFSET $4;
sub search($self, $search_query, $this_page = 1) {
my $date_format = $self->date_format;
my $row_count = $self->per_page;
my $offset = ($this_page - 1) * $row_count;
my @data = ($date_format, $search_query, $row_count, $offset);
$self->pg->db->query(<<~'END_SQL', @data)->hashes;
SELECT 'thread' AS post_type,
thread_id AS post_id,
TO_CHAR(thread_date, $1) AS post_date,
thread_author AS post_author,
thread_body AS post_body,
ts_rank(search_tokens, plainto_tsquery('english', $2)) AS search_rank
FROM threads
WHERE search_tokens @@ plainto_tsquery('english', $2)
UNION ALL
SELECT 'remark',
remark_id,
TO_CHAR(remark_date, $1),
remark_author,
remark_body,
ts_rank(search_tokens, plainto_tsquery('english', $2))
FROM remarks
WHERE search_tokens @@ plainto_tsquery('english', $2)
ORDER BY search_rank DESC, post_date DESC
LIMIT $3 OFFSET $4;
END_SQL
}
1;