summaryrefslogtreecommitdiff
path: root/lib/VNWeb/Discussions
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2020-01-05 12:47:04 +0100
committerYorhel <git@yorhel.nl>2020-01-05 12:52:12 +0100
commitd1d981efa5b0910260cbd1c226e3ab683c8304ae (patch)
tree8704fd96dc8e5820dae8017e5bfaf882ed80980f /lib/VNWeb/Discussions
parent0efea750ac26629f48fb50a9cdc9d71bd08257b7 (diff)
refactor: Combine json_api() & elm_form() + generate Elm function to msg API endpoint
The new elm_api() function now creates an API endpoint (like json_api()) and generates a corresponding Elm module to interact with that API (like elm_form()). The API endpoint URL is now derived from the name of the Elm module, so there's no need to think of a separate URL and less prone to making typos when using that URL from Elm. Reduces the boilerplace a bit as well.
Diffstat (limited to 'lib/VNWeb/Discussions')
-rw-r--r--lib/VNWeb/Discussions/Edit.pm107
-rw-r--r--lib/VNWeb/Discussions/Elm.pm (renamed from lib/VNWeb/Discussions/JS.pm)4
-rw-r--r--lib/VNWeb/Discussions/Thread.pm60
3 files changed, 82 insertions, 89 deletions
diff --git a/lib/VNWeb/Discussions/Edit.pm b/lib/VNWeb/Discussions/Edit.pm
index f552d5e4..550be76c 100644
--- a/lib/VNWeb/Discussions/Edit.pm
+++ b/lib/VNWeb/Discussions/Edit.pm
@@ -34,61 +34,8 @@ my $FORM = {
my $FORM_OUT = form_compile out => $FORM;
my $FORM_IN = form_compile in => $FORM;
-elm_form DiscussionsEdit => $FORM_OUT, $FORM_IN;
-
-TUWF::get qr{(?:/t/(?<board>$BOARD_RE)/new|/$RE{postid}/edit)}, sub {
- my($board_type, $board_id) = (tuwf->capture('board')||'') =~ /^([^0-9]+)([0-9]*)$/;
- my($tid, $num) = (tuwf->capture('id'), tuwf->capture('num'));
-
- $board_type = 'ge' if $board_type && $board_type eq 'an' && !auth->permBoardmod;
-
- my $t = !$tid ? {} : tuwf->dbRowi('
- SELECT t.id, tp.tid, tp.num, t.title, t.locked, t.private, t.poll_question, t.poll_max_options, tp.hidden, tp.msg, tp.uid AS user_id,', sql_totime('tp.date'), 'AS date
- FROM threads t
- JOIN threads_posts tp ON tp.tid = t.id AND tp.num =', \$num,
- 'WHERE t.id =', \$tid,
- 'AND', sql_visible_threads());
- return tuwf->resNotFound if $tid && !$t->{id};
- return tuwf->resDenied if !can_edit t => $t;
-
- $t->{poll}{options} = $t->{poll_question} && [ map $_->{option}, tuwf->dbAlli('SELECT option FROM threads_poll_options WHERE tid =', \$t->{id}, 'ORDER BY id')->@* ];
- $t->{poll}{question} = delete $t->{poll_question};
- $t->{poll}{max_options} = delete $t->{poll_max_options};
- $t->{poll} = undef if !$t->{poll}{question};
-
- if($tid) {
- enrich_boards undef, $t;
- } else {
- $t->{boards} = [ {
- btype => $board_type,
- iid => $board_id||0,
- title => !$board_id ? undef :
- tuwf->dbVali('SELECT title FROM', sql_boards(), 'x WHERE btype =', \$board_type, 'AND iid =', \$board_id)
- } ];
- return tuwf->resNotFound if $board_id && !length $t->{boards}[0]{title};
- push $t->{boards}->@*, { btype => 'u', iid => auth->uid, title => auth->user->{user_name} }
- if $board_type eq 'u' && $board_id != auth->uid;
- }
-
- $t->{can_mod} = auth->permBoardmod;
- $t->{can_private} = auth->permBoardmod || auth->permDbmod || auth->permUsermod;
-
- $t->{msg} //= '';
- $t->{title} //= tuwf->reqGet('title');
- $t->{tid} //= undef;
- $t->{num} //= undef;
- $t->{private} //= 0;
- $t->{hidden} //= 0;
- $t->{locked} //= 0;
-
- framework_ title => $tid ? 'Edit post' : 'Create new thread', sub {
- elm_ 'Discussions.Edit' => $FORM_OUT, $t;
- };
-};
-
-
-json_api qr{/t/edit\.json}, $FORM_IN, sub {
+elm_api DiscussionsEdit => $FORM_OUT, $FORM_IN, sub {
my($data) = @_;
my $tid = $data->{tid};
my $num = $data->{num} || 1;
@@ -160,4 +107,56 @@ json_api qr{/t/edit\.json}, $FORM_IN, sub {
elm_Redirect post_url $tid, $num, $num;
};
+
+TUWF::get qr{(?:/t/(?<board>$BOARD_RE)/new|/$RE{postid}/edit)}, sub {
+ my($board_type, $board_id) = (tuwf->capture('board')||'') =~ /^([^0-9]+)([0-9]*)$/;
+ my($tid, $num) = (tuwf->capture('id'), tuwf->capture('num'));
+
+ $board_type = 'ge' if $board_type && $board_type eq 'an' && !auth->permBoardmod;
+
+ my $t = !$tid ? {} : tuwf->dbRowi('
+ SELECT t.id, tp.tid, tp.num, t.title, t.locked, t.private, t.poll_question, t.poll_max_options, tp.hidden, tp.msg, tp.uid AS user_id,', sql_totime('tp.date'), 'AS date
+ FROM threads t
+ JOIN threads_posts tp ON tp.tid = t.id AND tp.num =', \$num,
+ 'WHERE t.id =', \$tid,
+ 'AND', sql_visible_threads());
+ return tuwf->resNotFound if $tid && !$t->{id};
+ return tuwf->resDenied if !can_edit t => $t;
+
+ $t->{poll}{options} = $t->{poll_question} && [ map $_->{option}, tuwf->dbAlli('SELECT option FROM threads_poll_options WHERE tid =', \$t->{id}, 'ORDER BY id')->@* ];
+ $t->{poll}{question} = delete $t->{poll_question};
+ $t->{poll}{max_options} = delete $t->{poll_max_options};
+ $t->{poll} = undef if !$t->{poll}{question};
+
+ if($tid) {
+ enrich_boards undef, $t;
+ } else {
+ $t->{boards} = [ {
+ btype => $board_type,
+ iid => $board_id||0,
+ title => !$board_id ? undef :
+ tuwf->dbVali('SELECT title FROM', sql_boards(), 'x WHERE btype =', \$board_type, 'AND iid =', \$board_id)
+ } ];
+ return tuwf->resNotFound if $board_id && !length $t->{boards}[0]{title};
+ push $t->{boards}->@*, { btype => 'u', iid => auth->uid, title => auth->user->{user_name} }
+ if $board_type eq 'u' && $board_id != auth->uid;
+ }
+
+ $t->{can_mod} = auth->permBoardmod;
+ $t->{can_private} = auth->permBoardmod || auth->permDbmod || auth->permUsermod;
+
+ $t->{msg} //= '';
+ $t->{title} //= tuwf->reqGet('title');
+ $t->{tid} //= undef;
+ $t->{num} //= undef;
+ $t->{private} //= 0;
+ $t->{hidden} //= 0;
+ $t->{locked} //= 0;
+
+ framework_ title => $tid ? 'Edit post' : 'Create new thread', sub {
+ elm_ 'Discussions.Edit' => $FORM_OUT, $t;
+ };
+};
+
+
1;
diff --git a/lib/VNWeb/Discussions/JS.pm b/lib/VNWeb/Discussions/Elm.pm
index 4c097830..77944926 100644
--- a/lib/VNWeb/Discussions/JS.pm
+++ b/lib/VNWeb/Discussions/Elm.pm
@@ -1,10 +1,10 @@
-package VNWeb::Discussions::JS;
+package VNWeb::Discussions::Elm;
use VNWeb::Prelude;
use VNWeb::Discussions::Lib;
# Autocompletion search results for boards
-json_api qr{/t/boards.json}, {
+elm_api Boards => undef, {
search => {},
}, sub {
return elm_Unauth if !auth->permBoard;
diff --git a/lib/VNWeb/Discussions/Thread.pm b/lib/VNWeb/Discussions/Thread.pm
index 7132f099..e410c920 100644
--- a/lib/VNWeb/Discussions/Thread.pm
+++ b/lib/VNWeb/Discussions/Thread.pm
@@ -19,14 +19,26 @@ my $POLL_OUT = form_compile any => {
} },
};
-
my $POLL_IN = form_compile any => {
tid => { id => 1 },
options => { type => 'array', values => { id => 1 } },
};
+elm_api DiscussionsPoll => $POLL_OUT, $POLL_IN, sub {
+ my($data) = @_;
+ return elm_Unauth if !auth;
+
+ my $t = tuwf->dbRowi('SELECT poll_question, poll_max_options FROM threads t WHERE id =', \$data->{tid}, 'AND', sql_visible_threads());
+ return tuwf->resNotFound if !$t->{poll_question};
+
+ die 'Too many options' if $data->{options}->@* > $t->{poll_max_options};
+ validate_dbid sql('SELECT id FROM threads_poll_options WHERE tid =', \$data->{tid}, 'AND id IN'), $data->{options}->@*;
+
+ tuwf->dbExeci('DELETE FROM threads_poll_votes WHERE tid =', \$data->{tid}, 'AND uid =', \auth->uid);
+ tuwf->dbExeci('INSERT INTO threads_poll_votes', { tid => $data->{tid}, uid => auth->uid, optid => $_ }) for $data->{options}->@*;
+ elm_Success
+};
-elm_form 'DiscussionsPoll' => $POLL_OUT, $POLL_IN;
@@ -39,7 +51,19 @@ my $REPLY = {
my $REPLY_IN = form_compile in => $REPLY;
my $REPLY_OUT = form_compile out => $REPLY;
-elm_form 'DiscussionsReply' => $REPLY_OUT, $REPLY_IN;
+elm_api DiscussionsReply => $REPLY_OUT, $REPLY_IN, sub {
+ my($data) = @_;
+ my $t = tuwf->dbRowi('SELECT id, locked, count FROM threads t WHERE id =', \$data->{tid}, 'AND', sql_visible_threads());
+ return tuwf->resNotFound if !$t->{id};
+ return elm_Unauth if !can_edit t => $t;
+
+ my $num = $t->{count}+1;
+ my $msg = bb_subst_links $data->{msg};
+ tuwf->dbExeci('INSERT INTO threads_posts', { tid => $t->{id}, num => $num, uid => auth->uid, msg => $msg });
+ tuwf->dbExeci('UPDATE threads SET count =', \$num, 'WHERE id =', \$t->{id});
+ elm_Redirect post_url $t->{id}, $num, 'last';
+};
+
@@ -178,34 +202,4 @@ TUWF::get qr{/$RE{postid}}, sub {
tuwf->resRedirect(post_url($id, $num, $num), 'perm')
};
-
-json_api qr{/t/pollvote\.json}, $POLL_IN, sub {
- my($data) = @_;
- return elm_Unauth if !auth;
-
- my $t = tuwf->dbRowi('SELECT poll_question, poll_max_options FROM threads t WHERE id =', \$data->{tid}, 'AND', sql_visible_threads());
- return tuwf->resNotFound if !$t->{poll_question};
-
- die 'Too many options' if $data->{options}->@* > $t->{poll_max_options};
- validate_dbid sql('SELECT id FROM threads_poll_options WHERE tid =', \$data->{tid}, 'AND id IN'), $data->{options}->@*;
-
- tuwf->dbExeci('DELETE FROM threads_poll_votes WHERE tid =', \$data->{tid}, 'AND uid =', \auth->uid);
- tuwf->dbExeci('INSERT INTO threads_poll_votes', { tid => $data->{tid}, uid => auth->uid, optid => $_ }) for $data->{options}->@*;
- elm_Success
-};
-
-
-json_api qr{/t/reply\.json}, $REPLY_IN, sub {
- my($data) = @_;
- my $t = tuwf->dbRowi('SELECT id, locked, count FROM threads t WHERE id =', \$data->{tid}, 'AND', sql_visible_threads());
- return tuwf->resNotFound if !$t->{id};
- return elm_Unauth if !can_edit t => $t;
-
- my $num = $t->{count}+1;
- my $msg = bb_subst_links $data->{msg};
- tuwf->dbExeci('INSERT INTO threads_posts', { tid => $t->{id}, num => $num, uid => auth->uid, msg => $msg });
- tuwf->dbExeci('UPDATE threads SET count =', \$num, 'WHERE id =', \$t->{id});
- elm_Redirect post_url $t->{id}, $num, 'last';
-};
-
1;