2022-10-01 20:22:21 +00:00
|
|
|
package PostText;
|
2022-09-15 23:14:40 +00:00
|
|
|
|
2023-01-09 04:54:58 +00:00
|
|
|
# Sep 22 2022
|
2022-09-15 23:14:40 +00:00
|
|
|
|
|
|
|
use Mojo::Base 'Mojolicious', -signatures;
|
|
|
|
use Mojo::Pg;
|
2022-11-03 19:38:40 +00:00
|
|
|
use Crypt::Passphrase;
|
2023-04-28 16:51:22 +00:00
|
|
|
use Text::Markdown qw{markdown};
|
2023-05-04 01:05:57 +00:00
|
|
|
use HTML::Restrict;
|
2022-11-03 19:38:40 +00:00
|
|
|
|
|
|
|
# The local libs
|
2022-09-15 23:14:40 +00:00
|
|
|
use PostText::Model::Thread;
|
|
|
|
use PostText::Model::Remark;
|
2022-10-23 18:30:43 +00:00
|
|
|
use PostText::Model::Moderator;
|
2022-09-15 23:14:40 +00:00
|
|
|
|
|
|
|
sub startup($self) {
|
|
|
|
$self->plugin('Config');
|
|
|
|
$self->plugin('TagHelpers::Pagination');
|
2023-05-20 04:34:52 +00:00
|
|
|
$self->plugin(AssetPack => {pipes => [qw{Css Combine}]});
|
2022-09-15 23:14:40 +00:00
|
|
|
|
2022-09-27 18:37:55 +00:00
|
|
|
# Helpers
|
2022-10-13 03:58:46 +00:00
|
|
|
$self->helper(pg => sub ($c) {
|
|
|
|
state $pg = Mojo::Pg->new($c->config->{$self->mode}{'pg_string'})
|
2022-09-15 23:14:40 +00:00
|
|
|
});
|
2022-09-27 18:37:55 +00:00
|
|
|
|
2022-11-03 19:38:40 +00:00
|
|
|
$self->helper(authenticator => sub ($c) {
|
|
|
|
state $authenticator = Crypt::Passphrase->new(
|
|
|
|
encoder => 'Argon2',
|
|
|
|
validators => ['Bcrypt'], # For old passphrases
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
2023-05-04 01:05:57 +00:00
|
|
|
$self->helper(hr => sub ($c) {
|
2023-05-06 04:01:34 +00:00
|
|
|
state $hr = HTML::Restrict->new(
|
|
|
|
filter_text => 0,
|
2023-05-27 17:04:15 +00:00
|
|
|
strip_enclosed_content => [],
|
|
|
|
rules => {
|
|
|
|
br => [],
|
|
|
|
s => []
|
2023-05-27 17:09:41 +00:00
|
|
|
})
|
2023-05-04 01:05:57 +00:00
|
|
|
});
|
|
|
|
|
2022-10-13 03:58:46 +00:00
|
|
|
$self->helper(thread => sub ($c) {
|
2023-05-04 01:05:57 +00:00
|
|
|
state $thread = PostText::Model::Thread->new(
|
|
|
|
pg => $c->pg,
|
|
|
|
hr => $c->hr
|
|
|
|
)
|
2022-09-27 18:37:55 +00:00
|
|
|
});
|
|
|
|
|
2022-10-13 03:58:46 +00:00
|
|
|
$self->helper(remark => sub ($c) {
|
2023-05-04 01:05:57 +00:00
|
|
|
state $remark = PostText::Model::Remark->new(
|
|
|
|
pg => $c->pg,
|
|
|
|
hr => $c->hr
|
|
|
|
)
|
2022-09-15 23:14:40 +00:00
|
|
|
});
|
2022-09-27 18:37:55 +00:00
|
|
|
|
2022-10-23 18:30:43 +00:00
|
|
|
$self->helper(moderator => sub ($c) {
|
2022-11-03 19:38:40 +00:00
|
|
|
state $moderator = PostText::Model::Moderator->new(
|
|
|
|
pg => $c->pg,
|
|
|
|
authenticator => $c->authenticator
|
|
|
|
)
|
2022-10-23 18:30:43 +00:00
|
|
|
});
|
|
|
|
|
2022-09-27 18:37:55 +00:00
|
|
|
$self->helper(truncate_text => sub ($c, $input_text) {
|
|
|
|
my $truncated_text = 500 < length($input_text)
|
2022-11-22 04:02:25 +00:00
|
|
|
? substr($input_text, 0, 500) . '…' : $input_text;
|
2022-09-27 18:37:55 +00:00
|
|
|
|
|
|
|
return $truncated_text;
|
2022-09-15 23:14:40 +00:00
|
|
|
});
|
|
|
|
|
2022-11-29 04:55:11 +00:00
|
|
|
$self->helper(is_mod => sub ($c) {
|
|
|
|
if (my $mod_id = $c->session->{'mod_id'}) {
|
2023-01-09 04:53:30 +00:00
|
|
|
return 1 unless $c->moderator->lock_status($mod_id)
|
2022-11-29 04:55:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return undef;
|
|
|
|
});
|
|
|
|
|
2023-04-16 19:15:39 +00:00
|
|
|
$self->helper(is_admin => sub ($c) {
|
|
|
|
$c->session->{'is_admin'} || undef
|
|
|
|
});
|
|
|
|
|
2023-04-25 15:56:42 +00:00
|
|
|
$self->helper(markdown => sub ($c, $input_text) {
|
2023-04-28 16:51:22 +00:00
|
|
|
markdown($input_text, {empty_element_suffix => '>'})
|
2023-04-25 15:56:42 +00:00
|
|
|
});
|
|
|
|
|
2022-09-15 23:14:40 +00:00
|
|
|
# Finish configuring some things
|
|
|
|
$self->secrets($self->config->{'secrets'}) || die $@;
|
|
|
|
|
2023-05-04 01:05:57 +00:00
|
|
|
$self->pg->migrations->from_dir('migrations')->migrate(12);
|
2022-09-15 23:14:40 +00: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-05-19 22:49:08 +00:00
|
|
|
$self->asset->process;
|
2022-09-15 23:14:40 +00:00
|
|
|
|
2022-10-19 01:28:18 +00:00
|
|
|
push @{$self->commands->namespaces}, 'PostText::Command';
|
|
|
|
|
2022-09-15 23:14:40 +00:00
|
|
|
# Begin routing
|
|
|
|
my $r = $self->routes->under(sub ($c) {
|
|
|
|
$c->session(expires => time + 31536000);
|
|
|
|
|
2022-09-27 17:20:30 +00:00
|
|
|
$c->session(author => 'Anonymous') unless $c->session('author');
|
|
|
|
|
2022-09-15 23:14:40 +00:00
|
|
|
1;
|
|
|
|
});
|
|
|
|
|
|
|
|
# Root redirect
|
2022-10-01 22:00:23 +00:00
|
|
|
$r->get('/', sub ($c) { $c->redirect_to('threads_list') });
|
2022-09-15 23:14:40 +00:00
|
|
|
|
2023-05-27 02:56:22 +00:00
|
|
|
# Shortcut to new session cookie/identity
|
|
|
|
$r->get('/new', sub ($c) {
|
|
|
|
$c->session(expires => 1);
|
|
|
|
|
|
|
|
$c->redirect_to('threads_list');
|
|
|
|
});
|
|
|
|
|
2023-04-26 02:28:11 +00:00
|
|
|
# Static pages
|
|
|
|
$r->get('/about')->to('page#about')->name('about_page');
|
|
|
|
|
|
|
|
$r->get('/rules')->to('page#rules')->name('rules_page');
|
|
|
|
|
2022-09-15 23:14:40 +00:00
|
|
|
# Thread
|
2023-04-24 16:14:04 +00:00
|
|
|
my $thread = $r->any('/thread');
|
2022-10-13 03:21:13 +00:00
|
|
|
|
2022-12-08 05:15:46 +00:00
|
|
|
$thread->get('/list/:list_page', [list_page => qr/\d+/], {list_page => 1})
|
2022-10-01 22:00:23 +00:00
|
|
|
->to('thread#by_page')
|
|
|
|
->name('threads_list');
|
|
|
|
|
2022-10-13 03:21:13 +00:00
|
|
|
$thread->any([qw{GET POST}], '/post')
|
2022-10-07 02:46:38 +00:00
|
|
|
->to('thread#create')
|
2022-10-01 22:00:23 +00:00
|
|
|
->name('post_thread');
|
|
|
|
|
2023-04-24 16:14:04 +00:00
|
|
|
$thread->any('/single/:thread_id', [thread_id => qr/\d+/])
|
2022-11-22 02:59:43 +00:00
|
|
|
->get('/:thread_page', [thread_page => qr/\d+/], {thread_page => 1})
|
2022-10-01 22:00:23 +00:00
|
|
|
->to('thread#by_id')
|
|
|
|
->name('single_thread');
|
2022-09-15 23:14:40 +00:00
|
|
|
|
2022-12-08 05:15:46 +00:00
|
|
|
$thread->get('/bump/:thread_id', [thread_id => qr/\d+/])
|
2022-10-13 02:14:28 +00:00
|
|
|
->to('thread#bump')
|
|
|
|
->name('bump_thread');
|
|
|
|
|
2022-12-08 05:15:46 +00:00
|
|
|
$thread->get('/flag/:thread_id', [thread_id => qr/\d+/])
|
2022-10-14 03:57:58 +00:00
|
|
|
->to('thread#flag')
|
|
|
|
->name('flag_thread');
|
|
|
|
|
2023-04-24 18:25:32 +00:00
|
|
|
$thread->get('feed', [format => [qw{rss xml}]])
|
|
|
|
->to('thread#feed')
|
|
|
|
->name('threads_feed');
|
2023-04-24 17:26:29 +00:00
|
|
|
|
2022-09-15 23:14:40 +00:00
|
|
|
# Remark
|
2023-04-24 16:14:04 +00:00
|
|
|
my $remark = $r->any('/remark');
|
2022-10-13 03:06:26 +00:00
|
|
|
|
2022-12-08 05:15:46 +00:00
|
|
|
$remark->any([qw{GET POST}], '/post/:thread_id', [thread_id => qr/\d+/])
|
2022-10-01 22:00:23 +00:00
|
|
|
->to('remark#create')
|
|
|
|
->name('post_remark');
|
|
|
|
|
2022-12-08 05:15:46 +00:00
|
|
|
$remark->get('/single/:remark_id', [remark_id => qr/\d+/])
|
2022-10-01 22:00:23 +00:00
|
|
|
->to('remark#by_id')
|
|
|
|
->name('single_remark');
|
2022-10-14 03:57:58 +00:00
|
|
|
|
2022-12-08 05:15:46 +00:00
|
|
|
$remark->get('/flag/:remark_id', [remark_id => qr/\d+/])
|
2022-10-14 03:57:58 +00:00
|
|
|
->to('remark#flag')
|
|
|
|
->name('flag_remark');
|
2022-11-20 04:55:03 +00:00
|
|
|
|
2022-11-22 02:59:43 +00:00
|
|
|
# Login/out
|
2022-11-20 04:55:03 +00:00
|
|
|
$r->any([qw{GET POST}], '/login')
|
|
|
|
->to('moderator#login')
|
|
|
|
->name('mod_login');
|
|
|
|
|
2022-11-22 02:59:43 +00:00
|
|
|
$r->get('/logout')
|
|
|
|
->to('moderator#logout')
|
|
|
|
->name('mod_logout');
|
|
|
|
|
2022-11-20 04:55:03 +00:00
|
|
|
# Moderator
|
2023-04-24 16:14:04 +00:00
|
|
|
my $moderator = $r->under('/moderator')->to('moderator#check');
|
2022-11-20 04:55:03 +00:00
|
|
|
|
2022-12-03 04:22:55 +00:00
|
|
|
$moderator->get('/flagged')
|
|
|
|
->to('moderator#flagged')
|
|
|
|
->name('flagged_list');
|
2022-12-08 02:18:48 +00:00
|
|
|
|
2022-12-09 00:50:21 +00:00
|
|
|
$moderator->get('/hidden')
|
|
|
|
->to('moderator#hidden')
|
|
|
|
->name('hidden_list');
|
|
|
|
|
2023-04-22 01:40:39 +00:00
|
|
|
$moderator->any([qw{GET POST}], '/reset')
|
|
|
|
->to('moderator#mod_reset')
|
|
|
|
->name('mod_reset');
|
|
|
|
|
2023-04-24 16:14:04 +00:00
|
|
|
my $mod_thread = $moderator->any('/thread');
|
2022-12-08 06:13:59 +00:00
|
|
|
|
|
|
|
$mod_thread->get('/unflag/:thread_id', [thread_id => qr/\d+/])
|
|
|
|
->to('moderator#unflag_thread')
|
2022-12-08 02:18:48 +00:00
|
|
|
->name('unflag_thread');
|
|
|
|
|
2022-12-08 06:13:59 +00:00
|
|
|
$mod_thread->get('/hide/:thread_id', [thread_id => qr/\d+/])
|
|
|
|
->to('moderator#hide_thread')
|
2022-12-08 02:18:48 +00:00
|
|
|
->name('hide_thread');
|
|
|
|
|
2022-12-08 06:13:59 +00:00
|
|
|
$mod_thread->get('/unhide/:thread_id', [thread_id => qr/\d+/])
|
|
|
|
->to('moderator#unhide_thread')
|
2022-12-08 02:18:48 +00:00
|
|
|
->name('unhide_thread');
|
2022-12-08 18:41:23 +00:00
|
|
|
|
2023-04-24 16:14:04 +00:00
|
|
|
my $mod_remark = $moderator->any('/remark');
|
2022-12-08 18:41:23 +00:00
|
|
|
|
|
|
|
$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');
|
2022-12-18 19:00:54 +00:00
|
|
|
|
2023-04-24 16:14:04 +00:00
|
|
|
# Admin
|
|
|
|
my $mod_admin = $moderator->under('/admin')->to('moderator#admin_check');
|
2023-04-21 21:32:16 +00:00
|
|
|
|
|
|
|
$mod_admin->any([qw{GET POST}], '/create')
|
|
|
|
->to('moderator#create')
|
|
|
|
->name('create_mod');
|
2023-04-22 01:40:39 +00:00
|
|
|
|
|
|
|
$mod_admin->any([qw{GET POST}], '/reset')
|
|
|
|
->to('moderator#admin_reset')
|
|
|
|
->name('admin_reset');
|
2023-04-22 02:25:05 +00:00
|
|
|
|
|
|
|
# lock() is a builtin so use _acct suffix
|
|
|
|
$mod_admin->any([qw{GET POST}], '/lock')
|
|
|
|
->to('moderator#lock_acct')
|
2023-04-22 03:55:49 +00:00
|
|
|
->name('lock_mod');
|
2023-04-22 02:25:05 +00:00
|
|
|
|
|
|
|
$mod_admin->any([qw{GET POST}], '/unlock')
|
|
|
|
->to('moderator#unlock_acct')
|
2023-04-22 03:55:49 +00:00
|
|
|
->name('unlock_mod');
|
2023-04-22 03:21:12 +00: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 23:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|