PostText/lib/PostText.pm

116 lines
3.1 KiB
Perl
Raw Normal View History

package PostText;
2022-09-15 19:14:40 -04:00
# Sep 22
use Mojo::Base 'Mojolicious', -signatures;
use Mojo::Pg;
use PostText::Model::Thread;
use PostText::Model::Remark;
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}]});
# 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-10-12 23:58:46 -04:00
$self->helper(thread => sub ($c) {
state $thread = PostText::Model::Thread->new(pg => $c->pg)
});
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
});
$self->helper(moderator => sub ($c) {
state $moderator = PostText::Model::Moderator->new(pg => $c->pg)
});
$self->helper(truncate_text => sub ($c, $input_text) {
my $truncated_text = 500 < length($input_text)
? substr($input_text, 0, 500) . '...' : $input_text;
return $truncated_text;
2022-09-15 19:14:40 -04:00
});
# Finish configuring some things
$self->secrets($self->config->{'secrets'}) || die $@;
2022-10-18 20:31:07 -04:00
$self->pg->migrations->from_dir('migrations')->migrate(8);
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)
}
$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);
$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');
$thread->under('/list')
2022-10-01 18:00:23 -04:00
->get('/:list_page', [list_page => qr/[0-9]+/], {list_page => 1})
->to('thread#by_page')
->name('threads_list');
2022-10-12 23:21:13 -04:00
$thread->any([qw{GET POST}], '/post')
->to('thread#create')
2022-10-01 18:00:23 -04:00
->name('post_thread');
2022-10-12 23:58:46 -04:00
$thread->under('/single/:thread_id', [thread_id => qr/[0-9]+/])
2022-10-01 18:00:23 -04:00
->get('/:thread_page', [thread_page => qr/[0-9]+/], {thread_page => 1})
->to('thread#by_id')
->name('single_thread');
2022-09-15 19:14:40 -04:00
2022-10-12 23:21:13 -04:00
$thread->under('/bump')
->get('/:thread_id', [thread_id => qr/[0-9]+/])
2022-10-12 22:14:28 -04:00
->to('thread#bump')
->name('bump_thread');
2022-10-13 23:57:58 -04:00
$thread->under('/flag')
->get('/:thread_id', [thread_id => qr/[0-9]+/])
->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');
$remark->under('/post')
2022-10-01 18:00:23 -04:00
->any([qw{GET POST}], '/:thread_id', [thread_id => qr/[0-9]+/])
->to('remark#create')
->name('post_remark');
2022-10-12 23:06:26 -04:00
$remark->under('/single')
->get('/:remark_id', [remark_id => qr/[0-9]+/])
2022-10-01 18:00:23 -04:00
->to('remark#by_id')
->name('single_remark');
2022-10-13 23:57:58 -04:00
$remark->under('/flag')
->get('/:remark_id', [remark_id => qr/[0-9]+/])
->to('remark#flag')
->name('flag_remark');
2022-09-15 19:14:40 -04:00
}
1;