OSDN Git Service

Web: implement /api/v1/comment
authorhylom <hylom@users.sourceforge.jp>
Fri, 28 Oct 2016 19:05:46 +0000 (04:05 +0900)
committerhylom <hylom@users.sourceforge.jp>
Fri, 28 Oct 2016 19:05:46 +0000 (04:05 +0900)
src/newslash_web/lib/Newslash/Web.pm
src/newslash_web/lib/Newslash/Web/Controller/API/Post.pm
src/newslash_web/t/api.t

index d16937d..278499b 100644 (file)
@@ -83,6 +83,7 @@ sub startup {
     my $api = $r->under('/api/v1');
     $api->post('/submission')->to('API::Post#submission');
     $api->post('/story')->to('API::Post#story');
+    $api->post('/comment')->to('API::Post#comment');
 }
 
 1;
index ff7ad39..cb19489 100644 (file)
@@ -82,9 +82,7 @@ EOT
     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};
 
@@ -140,4 +138,66 @@ EOT
     }
 }
 
+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;
index 3a3c7f2..efc2671 100644 (file)
@@ -31,6 +31,23 @@ SKIP: {
 
 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 = {
@@ -71,4 +88,18 @@ subtest 'submission preview' => sub {
     #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();