my $message = "";
$params->{title} = $util->escape_html({}, $data->{title});
- if (!length($params->{title}) > 0) {
- $message = "no title!";
- }
+ $message = "no title!" if !$params->{title};
$params->{time} = $data->{createtime};
}
}
+sub comment {
+ my $c = shift;
+ my $params = {};
+ my $extra_params = {};
+ my $opts = {};
+
+ my $users = $c->model('users');
+ my $comments = $c->model('comments');
+ my $util = $c->model('util');
+ my $user = $c->stash('user');
+
+ my $allowed_tags = <<'EOT';
+{
+ "a": ["href"],
+ "blockquote": [],
+ "i": [],
+ "strong": []
+}
+EOT
+
+ my $allowed = decode_json($allowed_tags);
+ my $data = $c->req->json;
+ my $message = "";
+
+ $params->{title} = $util->escape_html({}, $data->{title});
+ $params->{comment} = $util->clean_html($allowed, $data->{comment});
+ $params->{sid} = $data->{discussion_id};
+ $params->{stoid} = $data->{stoid};
+ $params->{pid} = $data->{pid};
+ $params->{action} = $data->{action} || 'preview';
+
+ $message = "no title!" if !$params->{title};
+ $message = "no comment!" if !$params->{comment};
+ if (length($message) > 0) {
+ $c->render(json => { err => 1, message => $message });
+ $c->rendered(400);
+ return;
+ }
+
+ if ($data->{action} eq 'preview') {
+ $data->{title} = $params->{title};
+ $data->{comment} = $params->{comment};
+ $c->render(json => $data);
+ return;
+ }
+ elsif ($params->{action} eq 'post') {
+ my $cid = $comments->create($params, $user, $extra_params, $opts);
+ if ($cid) {
+ $c->render(json => {type => "comment", id => $cid});
+ }
+ else {
+ if ($comments->last_errorno && $comments->last_errorno == 1062) { #ER_DUP_ENTRY
+ $c->rendered(409);
+ return;
+ }
+ $c->render(json => { err => 1, message => $comments->last_error });
+ warn $comments->last_error;
+ $c->rendered(500);
+ }
+ }
+}
+
1;
SKIP: {
skip "mode is not 'test'", 1 if ($t->app->mode ne 'test');
+ # commentn test
+ my $test_data = {
+ title => "テストコメント",
+ comment => "コメントテスト本文\nてすとてすと" . localtime,
+ discussion_id => 1,
+ stoid => 1,
+ action => 'post',
+ };
+ $t->post_ok('/api/v1/comment' => {Accept => '*/*'} => json => $test_data)
+ ->status_is(200)
+ ->json_has('/id')
+ ->json_is('/type' => "comment");
+
+}
+
+SKIP: {
+ skip "mode is not 'test'", 1 if ($t->app->mode ne 'test');
# story test
my $createtime = strftime('%FT%T', gmtime);
my $test_data = {
#print STDERR Dumper
};
+subtest 'comment preview' => sub {
+ my $test_data = {
+ title => "テストコメント",
+ comment => "コメントテスト本文\nてすとてすと" . localtime,
+ stoid => 1,
+ action => 'preview',
+ };
+ $t->post_ok('/api/v1/comment' => {Accept => '*/*'} => json => $test_data)
+ ->status_is(200)
+ ->content_type_is('application/json')
+ ->json_has('/title')
+ ->json_has('/comment');
+};
+
done_testing();