PostText/PostText.pl

213 lines
5.2 KiB
Perl
Raw Normal View History

#!/usr/bin/env perl
# PostText v0.1
# Jul 22
use Mojolicious::Lite -signatures;
2022-07-28 21:13:26 -04:00
use Mojo::Pg;
use Data::Dumper; # For your debugging pleasure
2022-07-28 21:13:26 -04:00
# Load the local modules too
use lib 'lib';
use PostText::Model::Thread;
2022-08-22 15:50:10 -04:00
use PostText::Model::Remark;
2022-07-28 21:13:26 -04:00
# Load Mojo plugins
plugin 'Config';
plugin 'TagHelpers::Pagination';
2022-08-15 18:35:30 -04:00
plugin AssetPack => {pipes => [qw{Css Combine}]};
2022-07-28 21:13:26 -04:00
# Helpers
helper pg => sub {
state $pg = Mojo::Pg->new(app->config->{app->mode}{'pg_string'})
};
2022-08-03 23:44:34 -04:00
helper thread => sub {
state $thread = PostText::Model::Thread->new(pg => shift->pg)
};
2022-08-22 15:50:10 -04:00
helper remark => sub {
state $remark = PostText::Model::Remark->new(pg => shift->pg)
2022-08-19 22:42:30 -04:00
};
2022-07-28 21:13:26 -04:00
# Begin routing
under sub ($c) {
2022-09-02 21:15:31 -04:00
$c->session(expires => time + 31536000);
2022-07-28 21:13:26 -04:00
1;
};
# Root
2022-08-24 00:48:48 -04:00
get '/', sub ($c) { $c->redirect_to('list') };
2022-08-24 00:48:48 -04:00
# List
group {
2022-08-24 00:48:48 -04:00
under 'list';
2022-08-24 00:48:48 -04:00
get '/:list_page', [list_page => qr/[0-9]+/], {list_page => 1}, sub ($c) {
my $base_path = $c->match->path_for(list_page => undef)->{'path'};
my $this_page = $c->param('list_page');
2022-09-02 21:15:31 -04:00
my $last_page = $c->thread->last_page;
my $threads = $c->thread->by_page($this_page);
$c->stash(status => 404) unless $threads->[0];
$c->stash(
threads => $threads,
this_page => $this_page,
last_page => $last_page,
base_path => $base_path
);
2022-09-02 21:15:31 -04:00
$c->render;
};
};
# Post
group {
2022-08-24 00:35:35 -04:00
any [qw{GET POST}], '/post', sub ($c) {
my $v;
2022-09-02 21:15:31 -04:00
$v = $c->validation if $c->req->method eq 'POST';
if ($v && $v->has_data) {
2022-08-23 20:41:15 -04:00
my $thread_author = $c->param('name' );
my $thread_title = $c->param('title');
my $thread_body = $c->param('post' );
$v->required('name' )->size(1, 63 );
$v->required('title')->size(1, 127 );
$v->required('post' )->size(2, 4000);
if ($v->has_error) {
$c->stash(status => 400)
}
else {
2022-09-02 21:15:31 -04:00
$c->thread->create(
$thread_author,
$thread_title,
$thread_body
);
2022-08-24 00:48:48 -04:00
return $c->redirect_to('list');
}
}
2022-08-06 02:26:53 -04:00
2022-09-02 21:15:31 -04:00
return $c->render;
};
2022-08-06 02:26:53 -04:00
2022-08-24 00:35:35 -04:00
under '/post';
any [qw{GET POST}], '/:thread_id', [thread_id => qr/[0-9]+/], sub ($c) {
my ($thread_id, $v) = ($c->param('thread_id'), undef);
2022-09-02 21:15:31 -04:00
$v = $c->validation if $c->req->method eq 'POST';
if ($v && $v->has_data) {
my $remark_name = $c->param('name');
my $remark_body = $c->param('post');
$v->required('name' )->size(1, 63 );
$v->required('post' )->size(2, 4000);
if ($v->has_error) {
$c->stash(status => 400)
}
else {
$c->remark->create(
$thread_id,
$remark_name,
$remark_body
);
return $c->redirect_to(
2022-09-02 22:45:41 -04:00
'thread_page',
{thread_id => $thread_id}
);
}
2022-08-15 17:48:58 -04:00
}
2022-08-03 23:44:34 -04:00
2022-09-02 21:15:31 -04:00
my $thread = $c->thread->by_id($thread_id);
2022-09-02 21:17:01 -04:00
my $last_remark = $c->remark->last_for($thread_id);
2022-09-02 18:11:23 -04:00
$c->stash(
thread => $thread,
last_remark => $last_remark
);
2022-09-02 21:15:31 -04:00
return $c->render;
};
};
2022-08-19 23:11:20 -04:00
# Thread
group {
2022-08-24 00:21:18 -04:00
under '/thread/:thread_id', [thread_id => qr/[0-9]+/];
2022-08-19 23:11:20 -04:00
2022-09-02 22:45:41 -04:00
get '/:thread_page',
[thread_page => qr/[0-9]+/],
{thread_page => 1}, sub ($c) { # My editor is so confused by this lol
2022-08-19 23:11:20 -04:00
my $thread_id = $c->param('thread_id');
2022-09-02 21:15:31 -04:00
my $thread = $c->thread->by_id($thread_id);
2022-09-02 22:45:41 -04:00
my $base_path = $c->match->path_for(thread_page => undef)->{'path'};
my $this_page = $c->param('thread_page');
2022-09-02 21:15:31 -04:00
my $last_page = $c->remark->last_page_for($thread_id);
my $remarks = $c->remark->by_page_for($thread_id, $this_page);
2022-08-19 23:11:20 -04:00
2022-08-20 12:42:04 -04:00
if (my $thread_body = %$thread{'body'}) {
$c->stash(
2022-08-22 22:17:14 -04:00
thread => $thread,
base_path => $base_path,
this_page => $this_page,
last_page => $last_page,
remarks => $remarks
2022-08-20 12:42:04 -04:00
)
2022-08-19 23:11:20 -04:00
}
else {
$c->stash(
thread => [],
status => 404
)
}
2022-09-02 21:15:31 -04:00
# Check for remarks or remark page number
2022-08-24 16:19:37 -04:00
$c->stash(status => 404) unless $remarks->[0] || 1 >= $this_page;
2022-09-02 21:15:31 -04:00
$c->render;
2022-08-19 23:11:20 -04:00
};
};
2022-09-02 22:45:41 -04:00
# Remark
group {
under '/remark';
get '/:remark_id', [remark_id => qr/[0-9]+/], sub ($c) {
my $remark_id = $c->param('remark_id');
my $remark = $c->remark->by_id($remark_id);
$c->stash(status => 404) unless $remark->{'id'};
$c->stash(remark => $remark);
$c->render;
};
};
# Configure things
2022-07-28 21:13:26 -04:00
app->secrets(app->config->{'secrets'}) || die $@;
2022-08-22 15:50:10 -04:00
app->pg->migrations->from_dir('migrations')->migrate(5);
2022-07-29 22:15:14 -04:00
if (my $threads_per_page = app->config->{'threads_per_page'}) {
2022-09-02 21:15:31 -04:00
app->thread->per_page($threads_per_page)
}
2022-08-22 16:37:38 -04:00
if (my $remarks_per_page = app->config->{'remarks_per_page'}) {
2022-09-02 21:15:31 -04:00
app->remark->per_page($remarks_per_page)
2022-08-22 16:37:38 -04:00
}
2022-08-15 18:35:30 -04:00
app->asset->process('main.css', 'css/PostText.css');
# Send it
2022-09-02 21:15:31 -04:00
app->start;