PostText/lib/PostText.pm

317 lines
9.0 KiB
Perl
Raw Permalink Normal View History

package PostText;
2022-09-15 19:14:40 -04:00
2023-01-08 23:54:58 -05:00
# Sep 22 2022
2022-09-15 19:14:40 -04:00
use Mojo::Base 'Mojolicious', -signatures;
2023-08-05 03:14:09 -04:00
use Mojo::Util qw{b64_encode gzip};
2022-09-15 19:14:40 -04:00
use Mojo::Pg;
use Crypt::Passphrase;
use Text::Markdown qw{markdown};
use HTML::Restrict;
# The local libs
2022-09-15 19:14:40 -04:00
use PostText::Model::Thread;
use PostText::Model::Remark;
use PostText::Model::Moderator;
2023-10-27 16:57:21 -04:00
use PostText::Model::Page;
2022-09-15 19:14:40 -04:00
sub startup($self) {
$self->plugin('Config');
$self->plugin('TagHelpers::Pagination');
2024-08-09 21:13:02 -04:00
# Alpha testing Slapbird APM
if (my $slapbirdapm_api_key = $self->config->{'slapbirdapm_api_key'}) {
$self->plugin('SlapbirdAPM', key => $slapbirdapm_api_key)
if $self->mode eq 'production'
}
# Helpers
2022-10-12 23:58:46 -04:00
$self->helper(pg => sub ($c) {
state $pg = Mojo::Pg->new($c->config->{$self->mode}{'pg_string'})
2022-09-15 19:14:40 -04:00
});
$self->helper(authenticator => sub ($c) {
2023-06-15 11:04:47 -04:00
state $authenticator = Crypt::Passphrase->new(encoder => 'Argon2')
});
$self->helper(hr => sub ($c) {
2023-05-06 00:01:34 -04:00
state $hr = HTML::Restrict->new(
filter_text => 0,
2023-05-27 13:04:15 -04:00
strip_enclosed_content => [],
rules => {
br => [],
s => []
2023-05-27 13:09:41 -04:00
})
});
2022-10-12 23:58:46 -04:00
$self->helper(thread => sub ($c) {
state $thread = PostText::Model::Thread->new(
pg => $c->pg,
hr => $c->hr
)
});
2022-10-12 23:58:46 -04:00
$self->helper(remark => sub ($c) {
state $remark = PostText::Model::Remark->new(
pg => $c->pg,
hr => $c->hr
)
2022-09-15 19:14:40 -04:00
});
$self->helper(moderator => sub ($c) {
state $moderator = PostText::Model::Moderator->new(
pg => $c->pg,
authenticator => $c->authenticator
2023-10-27 16:57:21 -04:00
)
});
$self->helper(page => sub ($c) {
state $moderator = PostText::Model::Page->new(pg => $c->pg)
});
$self->helper(truncate_text => sub ($c, $input_text) {
my $truncated_text = 299 < length($input_text)
? substr($input_text, 0, 299) . '…' : $input_text;
return $truncated_text;
2022-09-15 19:14:40 -04:00
});
$self->helper(is_mod => sub ($c) {
if (my $mod_id = $c->session->{'mod_id'}) {
2023-01-08 23:53:30 -05:00
return 1 unless $c->moderator->lock_status($mod_id)
}
return undef;
});
2023-04-16 15:15:39 -04:00
$self->helper(is_admin => sub ($c) {
$c->session->{'is_admin'} || undef
});
2023-04-25 11:56:42 -04:00
$self->helper(markdown => sub ($c, $input_text) {
markdown($input_text, {empty_element_suffix => '>'})
2023-04-25 11:56:42 -04:00
});
2022-09-15 19:14:40 -04:00
# Finish configuring some things
$self->secrets($self->config->{'secrets'}) || die $@;
2023-10-27 16:57:21 -04:00
$self->pg->migrations->from_dir('migrations')->migrate(15);
2022-09-15 19:14:40 -04:00
if (my $threads_per_page = $self->config->{'threads_per_page'}) {
$self->thread->per_page($threads_per_page)
}
if (my $remarks_per_page = $self->config->{'remarks_per_page'}) {
$self->remark->per_page($remarks_per_page)
}
2023-10-27 16:57:21 -04:00
if (my $results_per_page = $self->config->{'results_per_page'}) {
$self->page->per_page($results_per_page)
}
2023-10-28 11:34:44 -04:00
if (my $max_thread_pages = $self->config->{'max_thread_pages'}) {
$self->thread->max_pages($max_thread_pages)
}
2022-10-18 21:28:18 -04:00
push @{$self->commands->namespaces}, 'PostText::Command';
2022-09-15 19:14:40 -04:00
# Begin routing
my $r = $self->routes->under(sub ($c) {
$c->session(expires => time + 31536000);
$c->session(expires => time + 3600) if $c->is_mod;
$c->session(expires => time + 1800) if $c->is_admin;
2022-09-15 19:14:40 -04:00
$c->session(author => 'Anonymous') unless $c->session('author');
2022-09-15 19:14:40 -04:00
1;
});
2023-08-05 03:14:09 -04:00
# For CAPTCHA protected routes
my $human = $r->under('/human', sub ($c) {
return 1 if $c->session('is_human');
return $c->redirect_to(
captcha_page => return_url =>
2023-11-01 13:23:23 -04:00
b64_encode gzip $c->url_with->to_abs->to_string
2023-08-05 03:14:09 -04:00
), undef;
});
2022-09-15 19:14:40 -04:00
# Root redirect
2022-10-01 18:00:23 -04:00
$r->get('/', sub ($c) { $c->redirect_to('threads_list') });
2022-09-15 19:14:40 -04:00
# Shortcut to new session cookie/identity
$r->get('/new', sub ($c) {
$c->session(expires => 1);
$c->redirect_to('threads_list');
});
2023-04-25 22:28:11 -04:00
# Static pages
$r->get('/about')->to('page#about')->name('about_page');
$r->get('/rules')->to('page#rules')->name('rules_page');
$r->get('/feeds')->to('page#feeds')->name('feeds_page');
2024-06-26 01:08:31 -04:00
$r->get('/javascript')->to('page#javascript')->name('javascript_page');
2023-10-27 23:03:32 -04:00
# Not-so-static but I mean they're all 'pages' c'mon
$human->get('/search')->to('page#search')->name('search_page');
2023-10-27 23:03:32 -04:00
$r->any([qw{GET POST}], '/captcha/*return_url')
2023-08-05 03:14:09 -04:00
->to('page#captcha')
->name('captcha_page');
2022-09-15 19:14:40 -04:00
# Thread
2023-08-05 03:14:09 -04:00
my $thread = $r ->any('/thread');
my $human_thread = $human->any('/thread');
2022-10-12 23:21:13 -04:00
2022-12-08 00:15:46 -05:00
$thread->get('/list/:list_page', [list_page => qr/\d+/], {list_page => 1})
2022-10-01 18:00:23 -04:00
->to('thread#by_page')
->name('threads_list');
2023-04-24 12:14:04 -04:00
$thread->any('/single/:thread_id', [thread_id => qr/\d+/])
2023-06-27 01:30:32 -04:00
->any('/:thread_page', [thread_page => qr/\d+/], {thread_page => 0})
->get('/', [format => [qw{html txt}]], {format => undef})
2022-10-01 18:00:23 -04:00
->to('thread#by_id')
->name('single_thread');
2022-09-15 19:14:40 -04:00
2023-08-05 03:14:09 -04:00
$thread->get('feed', [format => [qw{rss xml}]])
->to('thread#feed')
->name('threads_feed');
$human_thread->get('/bump/:thread_id', [thread_id => qr/\d+/])
2022-10-12 22:14:28 -04:00
->to('thread#bump')
->name('bump_thread');
2023-08-05 03:14:09 -04:00
$human_thread->get('/flag/:thread_id', [thread_id => qr/\d+/])
2022-10-13 23:57:58 -04:00
->to('thread#flag')
->name('flag_thread');
2024-08-14 16:30:55 -04:00
# Redirect for this old path to the new one
$thread->any([qw{GET POST}], '/post', sub ($c) {
$c->redirect_to('post_thread')
});
2023-11-10 13:12:25 -05:00
$human_thread->any([qw{GET POST}], '/post')
->to('thread#create')
->name('post_thread');
2022-09-15 19:14:40 -04:00
# Remark
2023-08-05 03:14:09 -04:00
my $remark = $r ->any('/remark');
my $human_remark = $human->any('/remark');
2022-10-12 23:06:26 -04:00
2023-06-27 01:30:32 -04:00
$remark->any('/single/:remark_id', [remark_id => qr/\d+/])
->get('/', [format => [qw{html txt}]], {format => undef})
2022-10-01 18:00:23 -04:00
->to('remark#by_id')
->name('single_remark');
2022-10-13 23:57:58 -04:00
2023-11-10 13:12:25 -05:00
$remark->get('feed', [format => [qw{rss xml}]])
->to('remark#feed')
->name('remarks_feed');
2023-08-05 15:22:02 -04:00
$human_remark->get('/flag/:remark_id', [remark_id => qr/\d+/])
2022-10-13 23:57:58 -04:00
->to('remark#flag')
->name('flag_remark');
2022-11-19 23:55:03 -05:00
2023-11-10 13:12:25 -05:00
$human_remark
->any([qw{GET POST}], '/post/:thread_id', [thread_id => qr/\d+/])
->any('/:remark_id', [remark_id => qr/\d+/], {remark_id => 0})
->to('remark#create')
->name('post_remark');
2023-09-25 23:00:12 -04:00
# Login/out
2022-11-19 23:55:03 -05:00
$r->any([qw{GET POST}], '/login')
->to('moderator#login')
->name('mod_login');
$r->get('/logout')
->to('moderator#logout')
->name('mod_logout');
2022-11-19 23:55:03 -05:00
# Moderator
2023-04-24 12:14:04 -04:00
my $moderator = $r->under('/moderator')->to('moderator#check');
2022-11-19 23:55:03 -05:00
$moderator->get('/flagged')
->to('moderator#flagged')
->name('flagged_list');
2022-12-08 19:50:21 -05:00
$moderator->get('/hidden')
->to('moderator#hidden')
->name('hidden_list');
2023-04-21 21:40:39 -04:00
$moderator->any([qw{GET POST}], '/reset')
->to('moderator#mod_reset')
->name('mod_reset');
$moderator->get('/list')
->to('moderator#list')
->name('mod_list');
2023-04-24 12:14:04 -04:00
my $mod_thread = $moderator->any('/thread');
$mod_thread->get('/unflag/:thread_id', [thread_id => qr/\d+/])
->to('moderator#unflag_thread')
->name('unflag_thread');
$mod_thread->get('/hide/:thread_id', [thread_id => qr/\d+/])
->to('moderator#hide_thread')
->name('hide_thread');
$mod_thread->get('/unhide/:thread_id', [thread_id => qr/\d+/])
->to('moderator#unhide_thread')
->name('unhide_thread');
$mod_thread->get('/single/:thread_id', [thread_id => qr/\d+/])
->to('moderator#thread_by_id')
->name('hidden_thread');
2023-04-24 12:14:04 -04:00
my $mod_remark = $moderator->any('/remark');
$mod_remark->get('/unflag/:remark_id', [remark_id => qr/\d+/])
->to('moderator#unflag_remark')
->name('unflag_remark');
$mod_remark->get('/hide/:remark_id', [remark_id => qr/\d+/])
->to('moderator#hide_remark')
->name('hide_remark');
$mod_remark->get('/unhide/:remark_id', [remark_id => qr/\d+/])
->to('moderator#unhide_remark')
->name('unhide_remark');
$mod_remark->get('/single/:remark_id', [remark_id => qr/\d+/])
->to('moderator#remark_by_id')
->name('hidden_remark');
2023-04-24 12:14:04 -04:00
# Admin
my $mod_admin = $moderator->under('/admin')->to('moderator#admin_check');
2023-04-21 17:32:16 -04:00
$mod_admin->any([qw{GET POST}], '/create')
->to('moderator#create')
->name('create_mod');
2023-04-21 21:40:39 -04:00
$mod_admin->any([qw{GET POST}], '/reset')
->to('moderator#admin_reset')
->name('admin_reset');
2023-04-21 22:25:05 -04:00
# lock() is a builtin so use _acct suffix
$mod_admin->any([qw{GET POST}], '/lock')
->to('moderator#lock_acct')
->name('lock_mod');
2023-04-21 22:25:05 -04:00
$mod_admin->any([qw{GET POST}], '/unlock')
->to('moderator#unlock_acct')
->name('unlock_mod');
2023-04-21 23:21:12 -04:00
$mod_admin->any([qw{GET POST}], '/promote')
->to('moderator#promote')
->name('promote_mod');
$mod_admin->any([qw{GET POST}], '/demote')
->to('moderator#demote')
->name('demote_admin');
2022-09-15 19:14:40 -04:00
}
1;