Add like functionality to replies.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 23 Oct 2010 11:24:38 +0000 (13:24 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 23 Oct 2010 11:24:38 +0000 (13:24 +0200)
src/main/java/net/pterodactylus/sone/template/ReplyAccessor.java [new file with mode: 0644]
src/main/java/net/pterodactylus/sone/web/WebInterface.java
src/main/resources/templates/include/viewPost.html

diff --git a/src/main/java/net/pterodactylus/sone/template/ReplyAccessor.java b/src/main/java/net/pterodactylus/sone/template/ReplyAccessor.java
new file mode 100644 (file)
index 0000000..d5fd7e4
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * Sone - ReplyAccessor.java - Copyright © 2010 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.template;
+
+import net.pterodactylus.sone.core.Core;
+import net.pterodactylus.sone.data.Reply;
+import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.util.template.DataProvider;
+import net.pterodactylus.util.template.ReflectionAccessor;
+
+/**
+ * TODO
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class ReplyAccessor extends ReflectionAccessor {
+
+       private final Core core;
+
+       public ReplyAccessor(Core core) {
+               this.core = core;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public Object get(DataProvider dataProvider, Object object, String member) {
+               Reply reply = (Reply) object;
+               if ("likes".equals(member)) {
+                       return core.getLikes(reply);
+               } else if (member.equals("liked")) {
+                       Sone currentSone = (Sone) dataProvider.getData("currentSone");
+                       return (currentSone != null) && (currentSone.isLikedReplyId(reply.getId()));
+               }
+               return super.get(dataProvider, object, member);
+       }
+
+}
index d809580..78a03b5 100644 (file)
@@ -30,11 +30,13 @@ import java.util.logging.Logger;
 
 import net.pterodactylus.sone.core.Core;
 import net.pterodactylus.sone.data.Post;
+import net.pterodactylus.sone.data.Reply;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.freenet.L10nFilter;
 import net.pterodactylus.sone.main.SonePlugin;
 import net.pterodactylus.sone.template.GetPagePlugin;
 import net.pterodactylus.sone.template.PostAccessor;
+import net.pterodactylus.sone.template.ReplyAccessor;
 import net.pterodactylus.sone.template.RequestChangeFilter;
 import net.pterodactylus.sone.template.SoneAccessor;
 import net.pterodactylus.sone.template.SubstringFilter;
@@ -177,6 +179,7 @@ public class WebInterface extends AbstractService {
                templateFactory.addAccessor(Object.class, new ReflectionAccessor());
                templateFactory.addAccessor(Sone.class, new SoneAccessor(core()));
                templateFactory.addAccessor(Post.class, new PostAccessor(core()));
+               templateFactory.addAccessor(Reply.class, new ReplyAccessor(core()));
                templateFactory.addFilter("date", new DateFilter());
                templateFactory.addFilter("l10n", new L10nFilter(l10n()));
                templateFactory.addFilter("substring", new SubstringFilter());
index d212c39..8b4911d 100644 (file)
                                </div>
                                <div class="status-line">
                                        <div class="time"><% reply.time|date format="MMM d, yyyy, HH:mm:ss"></div>
+                                       <div class="likes<%if reply.likes.size|match value=0> hidden<%/if>"><span>⬆<span class="like-count"><% reply.likes.size></span></span></div>
+                                       <%ifnull ! currentSone>
+                                               <form class="like<%if reply.liked> hidden<%/if>" action="like.html" method="post">
+                                                       <input type="hidden" name="formPassword" value="<% formPassword|html>" />
+                                                       <input type="hidden" name="returnPage" value="<% request.uri|html>" />
+                                                       <input type="hidden" name="type" value="reply" />
+                                                       <input type="hidden" name="reply" value="<% reply.id|html>" />
+                                                       <button type="submit" value="1"><%= View.Post.LikeLink|l10n|html></button>
+                                               </form>
+                                               <form class="unlike<%if ! reply.liked> hidden<%/if>" action="unlike.html" method="post">
+                                                       <input type="hidden" name="formPassword" value="<% formPassword|html>" />
+                                                       <input type="hidden" name="returnPage" value="<% request.uri|html>" />
+                                                       <input type="hidden" name="type" value="reply" />
+                                                       <input type="hidden" name="reply" value="<% reply.id|html>" />
+                                                       <button type="submit" value="1"><%= View.Post.UnlikeLink|l10n|html></button>
+                                               </form>
+                                       <%/if>
                                        <%if reply.sone.current>
                                                <form class="delete" action="deleteReply.html" method="post">
                                                        <input type="hidden" name="formPassword" value="<% formPassword|html>" />