2022-10-01 16:22:21 -04:00
|
|
|
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;
|
|
|
|
use Mojo::Pg;
|
2022-11-03 15:38:40 -04:00
|
|
|
use Crypt::Passphrase;
|
|
|
|
|
|
|
|
# The local libs
|
2022-09-15 19:14:40 -04:00
|
|
|
use PostText::Model::Thread;
|
|
|
|
use PostText::Model::Remark;
|
2022-10-23 14:30:43 -04:00
|
|
|
use PostText::Model::Moderator;
|
2022-09-15 19:14:40 -04:00
|
|
|
|
|
|
|
sub startup($self) {
|
|
|
|
$self->plugin('Config');
|
|
|
|
$self->plugin('TagHelpers::Pagination');
|
|
|
|
$self->plugin(AssetPack => {pipes => [qw{Css Combine}]});
|
|
|
|
|
2022-09-27 14:37:55 -04:00
|
|
|
# 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
|
|
|
});
|
2022-09-27 14:37:55 -04:00
|
|
|
|
2022-11-03 15:38:40 -04:00
|
|
|
$self->helper(authenticator => sub ($c) {
|
|
|
|
state $authenticator = Crypt::Passphrase->new(
|
|
|
|
encoder => 'Argon2',
|
|
|
|
validators => ['Bcrypt'], # For old passphrases
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
2022-10-12 23:58:46 -04:00
|
|
|
$self->helper(thread => sub ($c) {
|
|
|
|
state $thread = PostText::Model::Thread->new(pg => $c->pg)
|
2022-09-27 14:37:55 -04:00
|
|
|
});
|
|
|
|
|
2022-10-12 23:58:46 -04:00
|
|
|
$self->helper(remark => sub ($c) {
|
|
|
|
state $remark = PostText::Model::Remark->new(pg => $c->pg)
|
2022-09-15 19:14:40 -04:00
|
|
|
});
|
2022-09-27 14:37:55 -04:00
|
|
|
|
2022-10-23 14:30:43 -04:00
|
|
|
$self->helper(moderator => sub ($c) {
|
2022-11-03 15:38:40 -04:00
|
|
|
state $moderator = PostText::Model::Moderator->new(
|
|
|
|
pg => $c->pg,
|
|
|
|
authenticator => $c->authenticator
|
|
|
|
)
|
2022-10-23 14:30:43 -04:00
|
|
|
});
|
|
|
|
|
2022-09-27 14:37:55 -04:00
|
|
|
$self->helper(truncate_text => sub ($c, $input_text) {
|
|
|
|
my $truncated_text = 500 < length($input_text)
|
2022-11-21 23:02:25 -05:00
|
|
|
? substr($input_text, 0, 500) . '…' : $input_text;
|
2022-09-27 14:37:55 -04:00
|
|
|
|
|
|
|
return $truncated_text;
|
2022-09-15 19:14:40 -04:00
|
|
|
});
|
|
|
|
|
2022-11-28 23:55:11 -05: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)
|
2022-11-28 23:55:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return undef;
|
|
|
|
});
|
|
|
|
|
2022-09-15 19:14:40 -04:00
|
|
|
# Finish configuring some things
|
|
|
|
$self->secrets($self->config->{'secrets'}) || die $@;
|
|
|
|
|
2022-12-17 21:46:29 -05:00
|
|
|
$self->pg->migrations->from_dir('migrations')->migrate(11);
|
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)
|
|
|
|
}
|
|
|
|
|
2022-12-08 13:41:23 -05:00
|
|
|
if (my $date_format = $self->config->{'date_format'}) {
|
|
|
|
$self->thread->date_format($date_format);
|
|
|
|
$self->remark->date_format($date_format);
|
|
|
|
}
|
|
|
|
|
2022-09-15 19:14:40 -04:00
|
|
|
$self->asset->process('main.css', 'css/PostText.css');
|
|
|
|
|
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);
|
|
|
|
|
2022-09-27 13:20:30 -04:00
|
|
|
$c->session(author => 'Anonymous') unless $c->session('author');
|
|
|
|
|
2022-09-15 19:14:40 -04:00
|
|
|
1;
|
|
|
|
});
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
# Thread
|
2022-10-12 23:21:13 -04:00
|
|
|
my $thread = $r->under('/thread');
|
|
|
|
|
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');
|
|
|
|
|
2022-10-12 23:21:13 -04:00
|
|
|
$thread->any([qw{GET POST}], '/post')
|
2022-10-06 22:46:38 -04:00
|
|
|
->to('thread#create')
|
2022-10-01 18:00:23 -04:00
|
|
|
->name('post_thread');
|
|
|
|
|
2022-11-21 21:59:43 -05:00
|
|
|
$thread->under('/single/:thread_id', [thread_id => qr/\d+/])
|
|
|
|
->get('/:thread_page', [thread_page => qr/\d+/], {thread_page => 1})
|
2022-10-01 18:00:23 -04:00
|
|
|
->to('thread#by_id')
|
|
|
|
->name('single_thread');
|
2022-09-15 19:14:40 -04:00
|
|
|
|
2022-12-08 00:15:46 -05:00
|
|
|
$thread->get('/bump/:thread_id', [thread_id => qr/\d+/])
|
2022-10-12 22:14:28 -04:00
|
|
|
->to('thread#bump')
|
|
|
|
->name('bump_thread');
|
|
|
|
|
2022-12-08 00:15:46 -05:00
|
|
|
$thread->get('/flag/:thread_id', [thread_id => qr/\d+/])
|
2022-10-13 23:57:58 -04:00
|
|
|
->to('thread#flag')
|
|
|
|
->name('flag_thread');
|
|
|
|
|
2022-09-15 19:14:40 -04:00
|
|
|
# Remark
|
2022-10-12 23:06:26 -04:00
|
|
|
my $remark = $r->under('/remark');
|
|
|
|
|
2022-12-08 00:15:46 -05:00
|
|
|
$remark->any([qw{GET POST}], '/post/:thread_id', [thread_id => qr/\d+/])
|
2022-10-01 18:00:23 -04:00
|
|
|
->to('remark#create')
|
|
|
|
->name('post_remark');
|
|
|
|
|
2022-12-08 00:15:46 -05:00
|
|
|
$remark->get('/single/:remark_id', [remark_id => qr/\d+/])
|
2022-10-01 18:00:23 -04:00
|
|
|
->to('remark#by_id')
|
|
|
|
->name('single_remark');
|
2022-10-13 23:57:58 -04:00
|
|
|
|
2022-12-08 00:15:46 -05:00
|
|
|
$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
|
|
|
|
2022-11-21 21:59:43 -05:00
|
|
|
# Login/out
|
2022-11-19 23:55:03 -05:00
|
|
|
$r->any([qw{GET POST}], '/login')
|
|
|
|
->to('moderator#login')
|
|
|
|
->name('mod_login');
|
|
|
|
|
2022-11-21 21:59:43 -05:00
|
|
|
$r->get('/logout')
|
|
|
|
->to('moderator#logout')
|
|
|
|
->name('mod_logout');
|
|
|
|
|
2022-11-19 23:55:03 -05:00
|
|
|
# Moderator
|
|
|
|
my $moderator = $r->under('/moderator', sub ($c) {
|
2022-11-28 23:55:11 -05:00
|
|
|
return 1 if $c->is_mod;
|
2022-11-19 23:55:03 -05:00
|
|
|
|
2022-12-18 14:00:54 -05:00
|
|
|
# Return undef otherwise a body is rendered with the redirect...
|
2022-12-07 21:18:48 -05:00
|
|
|
return $c->redirect_to('mod_login'), undef;
|
2022-11-19 23:55:03 -05:00
|
|
|
});
|
|
|
|
|
2022-12-02 23:22:55 -05:00
|
|
|
$moderator->get('/flagged')
|
|
|
|
->to('moderator#flagged')
|
|
|
|
->name('flagged_list');
|
2022-12-07 21:18:48 -05:00
|
|
|
|
2022-12-08 19:50:21 -05:00
|
|
|
$moderator->get('/hidden')
|
|
|
|
->to('moderator#hidden')
|
|
|
|
->name('hidden_list');
|
|
|
|
|
2022-12-08 01:13:59 -05:00
|
|
|
my $mod_thread = $moderator->under('/thread');
|
|
|
|
|
|
|
|
$mod_thread->get('/unflag/:thread_id', [thread_id => qr/\d+/])
|
|
|
|
->to('moderator#unflag_thread')
|
2022-12-07 21:18:48 -05:00
|
|
|
->name('unflag_thread');
|
|
|
|
|
2022-12-08 01:13:59 -05:00
|
|
|
$mod_thread->get('/hide/:thread_id', [thread_id => qr/\d+/])
|
|
|
|
->to('moderator#hide_thread')
|
2022-12-07 21:18:48 -05:00
|
|
|
->name('hide_thread');
|
|
|
|
|
2022-12-08 01:13:59 -05:00
|
|
|
$mod_thread->get('/unhide/:thread_id', [thread_id => qr/\d+/])
|
|
|
|
->to('moderator#unhide_thread')
|
2022-12-07 21:18:48 -05:00
|
|
|
->name('unhide_thread');
|
2022-12-08 13:41:23 -05:00
|
|
|
|
|
|
|
my $mod_remark = $moderator->under('/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');
|
2022-12-18 14:00:54 -05:00
|
|
|
|
2022-12-24 23:37:50 -05:00
|
|
|
# under() for admins
|
2022-12-18 14:00:54 -05:00
|
|
|
|
|
|
|
# Actions to create mods...
|
2022-09-15 19:14:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|