Commits

Luke Plant committed 74f2d3e

AJAX moderation for comments - set 'hidden' state.

  • Participants
  • Parent commits 89c760c

Comments (0)

Files changed (6)

File src/Blog/Model.hs

                   , getPostsForCategory
                   , setPassword
                   , checkPassword
+                  , setCommentVisible
                   ) where
 
 import Data.Digest.Pure.SHA (showDigest, sha1)
 getPasswordForUsernameQuery = "SELECT password FROM users WHERE username = ?;"
 setPasswordForUsernameQuery = "UPDATE users SET password = ? WHERE username = ?;"
 
+setCommentHiddenQuery      = "UPDATE comments SET hidden = ? WHERE id = ?;"
+
 ---- Constructors ----
 
 makePost row =
                                                          , toSql username ]
                      )
   return ()
+
+
+-- comment moderation
+setCommentVisible :: (IConnection conn) =>
+               conn -> Int -> Bool -> IO ()
+setCommentVisible cn commentId visible = do
+    withTransaction cn (\cn ->
+                            run cn setCommentHiddenQuery [ toSql (if visible then 0 else 1 :: Int)
+                                                         , toSql commentId ]
+                       )
+    return ()

File src/Blog/Routes.hs

          , "admin/post/" <+/> empty                   //-> adminPosts             $ [adminRequired]
          , "admin/post/new/" <+/> empty               //-> adminNewPost           $ [adminRequired]
          , "admin/post/edit/" <+/> anyParam           //-> adminEditPost          $ [adminRequired]
+         , "admin/ajax/commentvisible/" <+/> empty    //-> adminCommentVisible    $ [adminRequired]
          , "debug/" <+/> anyParam                     //-> debug                  $ []
          ]

File src/Blog/Templates.hs

                                       , ("textFormatted", ToSElemD $ Cm.text_formatted cm)
                                       , ("email", ToSElemD $ Cm.email cm)
                                       , ("uid", ToSElemD $ Cm.uid cm)
+                                      , ("hidden", ToSElemD $ Cm.hidden cm)
                                       ]

File src/Blog/Views.hs

                                     ("showPreview", mode == "preview")
                                    )
 
+
+-- Admin AJAX
+
+-- TODO - proper JSON objects
+success = buildResponse [ addContent $ utf8 "success" ] utf8TextResponse
+failure = buildResponse [ addContent $ utf8 "failure" ] utf8TextResponse
+
+adminCommentVisible req = do
+  let commentId = getPOST req "id" `captureOrDefault` 0 :: Int
+  let visible   = getPOST req "visible" `captureOrDefault` False
+  if commentId <= 0
+    then  return $ Just $ failure
+    else do
+      cn <- connect
+      setCommentVisible cn commentId visible
+      return $ Just $ success
+
 createLoginCookies loginData timestamp =
   let username = fromJust $ Map.lookup "username" loginData
       password = fromJust $ Map.lookup "password" loginData

File src/templates/comment.st

 
-    <div class="comment$if(comment.isAuthor)$ author$endif$">
+    <div class="comment$if(comment.isAuthor)$ author$endif$$if(comment.hidden)$ hiddenComment$endif$" id="commentdiv$comment.uid$">
+      <div class="togglebar"></div>
       <div class="introline">
         <a id="comment$comment.uid$" href="#comment$comment.uid$" class="anchor">§</a> <span>On <span class="timestamp">$comment.date$</span>, <span class="commentBy">$comment.formattedName$</span> wrote:</span>
       </div>
       <div class="commenttext">
+      <span class="commentid">$comment.uid$</span>
          $comment.textFormatted:noescape()$
       </div>
+      <div class="commentControls">
+        <a id="hidecomment$comment.uid$" href="#" $if(comment.hidden)$style="display:none;"$endif$>Hide</a>
+        <a id="showcomment$comment.uid$" href="#" $if(!comment.hidden)$style="display:none;"$endif$>Show</a>
+        <a id="respondtocomment$comment.uid$" href="#">Respond</a>
+        <a id="deletecomment$comment.uid$" href="#">Delete</a>
+      </div>
     </div>
     <hr/>

File src/templates/pagestart.st

       \$(document).ready(function() {
       \$('#id_spamcheck').attr("name", "timestamp");
       });
+
+function readCookie(name) {
+    var nameEQ = name + "=";
+    var ca = document.cookie.split(';');
+    for(var i=0;i < ca.length; i++) {
+        var c = ca[i];
+	while (c.charAt(0)==' ') c = c.substring(1,c.length);
+	if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
+    }
+    return null;
+}
+
+function loggedIn() {
+    return (!(readCookie("username") == null));
+}
+
+
+function setCommentVisible(cid, visible) {
+    \$.post("/blog/admin/ajax/commentvisible/", { id: cid,
+                                                  visible: (visible ? 'True' : 'False') });
+
+}
+
+function hideComment(cid, event) {
+    setCommentVisible(cid, false);
+    \$("#commentdiv" + cid).addClass("hiddenComment").removeClass("manuallyShown");
+    \$("#showcomment" + cid).show();
+    \$("#hidecomment" + cid).hide();
+}
+
+function showComment(cid, event) {
+    setCommentVisible(cid, true);
+    \$("#commentdiv" + cid).removeClass("hiddenComment");
+    \$("#hidecomment" + cid).show();
+    \$("#showcomment" + cid).hide();
+}
+
+function respondToComment(cid, event) {
+
+}
+
+function deleteComment(cid, event) {
+
+}
+
+function addCommentModeration() {
+    \$(".comment").each(function() {
+                    var cid = \$(this).find(".commentid").text();
+                    \$(this).find("#hidecomment" + cid).click(function(ev) {
+                                                                 hideComment(cid, ev);
+                                                                 ev.preventDefault();
+                                                              });
+                    \$(this).find("#showcomment" + cid).click(function(ev) {
+                                                                 showComment(cid, ev);
+                                                                 ev.preventDefault();
+                                                              });
+                    \$(this).find("#respondtocomment" + cid).click(function(ev) {
+                                                                 respondToComment(cid, ev);
+                                                                 ev.preventDefault();
+                                                              });
+                    \$(this).find("#deletecomment" + cid).click(function(ev) {
+                                                                 deleteComment(cid, ev);
+                                                                 ev.preventDefault();
+                                                              });
+    });
+    \$(".commentControls").show();
+}
+
+\$(document).ready(function() {
+                    if (loggedIn()) {
+                        addCommentModeration();
+                    }
+                    \$(".togglebar").toggle(function(ev) {
+                                               \$(this.parentNode).addClass("manuallyShown");
+                                            },
+                                            function(ev) {
+                                               \$(this.parentNode).removeClass("manuallyShown");
+                                            });
+
+});
+
     </script>
   </head>
   <body>