From f2e63b5ca3e7164b541d165b3038b16d4113bedd Mon Sep 17 00:00:00 2001 From: swaggboi Date: Fri, 27 Oct 2023 16:57:21 -0400 Subject: [PATCH] Created function to search posts --- lib/PostText.pm | 13 +++++- lib/PostText/Controller/Moderator.pm | 4 +- lib/PostText/Model/Page.pm | 62 ++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 lib/PostText/Model/Page.pm diff --git a/lib/PostText.pm b/lib/PostText.pm index 4afb10f..dcbbce5 100644 --- a/lib/PostText.pm +++ b/lib/PostText.pm @@ -13,6 +13,7 @@ use HTML::Restrict; use PostText::Model::Thread; use PostText::Model::Remark; use PostText::Model::Moderator; +use PostText::Model::Page; sub startup($self) { $self->plugin('Config'); @@ -56,7 +57,11 @@ sub startup($self) { state $moderator = PostText::Model::Moderator->new( pg => $c->pg, 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) { @@ -85,7 +90,7 @@ sub startup($self) { # Finish configuring some things $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'}) { $self->thread->per_page($threads_per_page) @@ -95,6 +100,10 @@ sub startup($self) { $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; push @{$self->commands->namespaces}, 'PostText::Command'; diff --git a/lib/PostText/Controller/Moderator.pm b/lib/PostText/Controller/Moderator.pm index 1d8891d..a3c65e2 100644 --- a/lib/PostText/Controller/Moderator.pm +++ b/lib/PostText/Controller/Moderator.pm @@ -7,7 +7,7 @@ sub flagged($self) { my @post_links = map { $self->url_for( 'hidden_' . $_->{'type'}, $_->{'type'} . '_id' => $_->{'id'} - ) + ) } @{$flagged_posts}; $self->stash(post_links => \@post_links); @@ -20,7 +20,7 @@ sub hidden($self) { my @post_links = map { $self->url_for( 'hidden_' . $_->{'type'}, $_->{'type'} . '_id' => $_->{'id'} - ) + ) } @{$hidden_posts}; $self->stash(post_links => \@post_links); diff --git a/lib/PostText/Model/Page.pm b/lib/PostText/Model/Page.pm new file mode 100644 index 0000000..6a24eb8 --- /dev/null +++ b/lib/PostText/Model/Page.pm @@ -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;