add like support
authorChristian Weiske <cweiske@cweiske.de>
Mon, 8 Aug 2016 04:50:01 +0000 (06:50 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Mon, 8 Aug 2016 04:50:01 +0000 (06:50 +0200)
data/templates/post-like.htm [new file with mode: 0644]
data/templates/post-reply.htm [moved from data/templates/comment.htm with 100% similarity]
src/anoweco/Storage.php
www/comment.php
www/micropub.php

diff --git a/data/templates/post-like.htm b/data/templates/post-like.htm
new file mode 100644 (file)
index 0000000..f02d1fc
--- /dev/null
@@ -0,0 +1,19 @@
+<html xmlns="http://www.w3.org/1999/xhtml">
+ <head>
+  <meta charset="utf-8"/>
+  <title>Like of {{attribute(json, 'like-of').0}}</title>
+ </head>
+ <body class="h-entry">
+  <h1>Like #{{crow.comment_id}}</h1>
+  <p>
+   {% spaceless %}
+   <a class="p-author h-card" href="{{author.url}}">
+    <img class="u-photo" src="{{author.imageurl}}" alt="" width="32" height="32" />
+    {{author.name}}</a>
+   {% endspaceless %}
+   likes
+   <a rel="u-like-of"
+      href="{{attribute(json, 'like-of').0}}">{{attribute(json, 'like-of').0}}</a>.
+  </p>
+ </body>
+</html>
index f9a0f4f57e1a08d157c27b7de00f39e427994bc8..f3b18ae3a1da275864dcd9cdf29f3d0f9b931230 100644 (file)
@@ -31,14 +31,25 @@ class Storage
         );
 
         $ofUrl = '';
+        $type  = null;
         if (isset($json->properties->{'in-reply-to'})) {
             $ofUrl = reset($json->properties->{'in-reply-to'});
+            $type = 'reply';
+        } else if (isset($json->properties->{'like-of'})) {
+            $ofUrl = reset($json->properties->{'like-of'});
+            $type  = 'like';
+        } else {
+            throw new \Exception(
+                'Invalid post type, only reply and like allowed',
+                400
+            );
         }
+
         $stmt->execute(
             array(
                 ':userId' => $userId,
                 ':ofUrl'  => $ofUrl,
-                ':type'   => reset($json->type),
+                ':type'   => $type,
                 ':json'   => json_encode($json),
             )
         );
index 6dc1b0c9308f14c3bb8c154d4c7c15be204548ce..b1fe2d762e601b40df33ab5a0c50a3eab53591ff 100644 (file)
@@ -26,29 +26,35 @@ if ($comment === null) {
     exit(1);
 }
 
-if (isset($comment->properties->content['html'])) {
-    $htmlContent = $comment->properties->content['html'];
-} else {
-    $htmlContent = nl2br($comment->properties->content[0]);
-}
-
 $rowComment = $comment->Xrow;
 $rowUser    = $comment->user;
-render(
-    'comment',
-    array(
-        'json' => $comment->properties,
-        'crow' => $rowComment,
-        'comment' => $comment,
-        'author'  => array(
-            'name' => $rowUser->user_name,
-            'url'  => Urls::full(Urls::user($rowUser->user_id)),
-            'imageurl' => Urls::userImg($rowUser),
-        ),
-        'htmlContent' => $htmlContent,
-        'replyUrl' => Urls::full(
-            '/reply.php?url=' . urlencode(Urls::full($rowComment->comment_id))
-        ),
-    )
+
+$vars = array(
+    'json' => $comment->properties,
+    'crow' => $rowComment,
+    'comment' => $comment,
+    'author'  => array(
+        'name' => $rowUser->user_name,
+        'url'  => Urls::full(Urls::user($rowUser->user_id)),
+        'imageurl' => Urls::userImg($rowUser),
+    ),
+    'replyUrl' => Urls::full(
+        '/reply.php?url=' . urlencode(Urls::full($rowComment->comment_id))
+    ),
 );
+
+if ($rowComment->comment_type == 'like') {
+    $template = 'post-like';
+} else {
+    //reply
+    $template = 'post-reply';
+    if (isset($comment->properties->content['html'])) {
+        $htmlContent = $comment->properties->content['html'];
+    } else {
+        $htmlContent = nl2br($comment->properties->content[0]);
+    }
+    $vars['htmlContent'] = $htmlContent;
+}
+
+render($template, $vars);
 ?>
index 9d91272a7ecf4feb0d6dee4f899e2bf49094ff1f..0dd476a17f877b41bc9975471addeb8d1f633f50 100644 (file)
@@ -82,14 +82,6 @@ function handleCreate($json, $token)
         );
     }
 
-    if (!isset($json->properties->{'in-reply-to'})) {
-        mpError(
-            'HTTP/1.0 400 Bad Request',
-            'invalid_request',
-            'Only replies accepted'
-        );
-    }
-
     $storage = new Storage();
     try {
         $id = $storage->addComment($json, $userId);
@@ -98,8 +90,19 @@ function handleCreate($json, $token)
         header('Location: ' . Urls::full(Urls::comment($id)));
         exit();
     } catch (\Exception $e) {
-        //FIXME: return correct status code
-        header('HTTP/1.0 500 Internal Server Error');
+        if ($e->getCode() == 400) {
+            mpError(
+                'HTTP/1.0 400 Bad Request',
+                'invalid_request',
+                $e->getMessage()
+            );
+        }
+
+        mpError(
+            'HTTP/1.0 500 Internal Server Error',
+            'this_violates_the_spec',
+            $e->getMessage()
+        );
         exit();
     }
 }