From 93fdf9e218d808f65f618abbddce183191d8c15b Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 24 Jan 2013 06:22:59 +0100 Subject: [PATCH] Make post reply returned by provider optional. --- src/main/java/net/pterodactylus/sone/core/Core.java | 5 +++-- .../java/net/pterodactylus/sone/core/PostReplyProvider.java | 4 +++- .../java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java | 7 ++++--- src/main/java/net/pterodactylus/sone/web/DeleteReplyPage.java | 8 +++++--- src/main/java/net/pterodactylus/sone/web/MarkAsKnownPage.java | 8 +++++--- src/main/java/net/pterodactylus/sone/web/SearchPage.java | 7 ++++++- .../net/pterodactylus/sone/web/ajax/DeleteReplyAjaxPage.java | 10 ++++++---- .../java/net/pterodactylus/sone/web/ajax/GetLikesAjaxPage.java | 9 +++++++-- .../java/net/pterodactylus/sone/web/ajax/GetReplyAjaxPage.java | 8 +++++--- .../java/net/pterodactylus/sone/web/ajax/GetTimesAjaxPage.java | 10 ++++++---- .../net/pterodactylus/sone/web/ajax/MarkAsKnownAjaxPage.java | 8 +++++--- 11 files changed, 55 insertions(+), 29 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index f056c0e..70d9ea2 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -88,6 +88,7 @@ import net.pterodactylus.util.service.AbstractService; import net.pterodactylus.util.thread.NamedThreadFactory; import com.google.common.base.Function; +import com.google.common.base.Optional; import com.google.common.base.Predicate; import com.google.common.base.Predicates; import com.google.common.collect.Collections2; @@ -580,9 +581,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, * {@inheritDoc} */ @Override - public PostReply getPostReply(String replyId) { + public Optional getPostReply(String replyId) { synchronized (replies) { - return replies.get(replyId); + return Optional.fromNullable(replies.get(replyId)); } } diff --git a/src/main/java/net/pterodactylus/sone/core/PostReplyProvider.java b/src/main/java/net/pterodactylus/sone/core/PostReplyProvider.java index 9854b6c..5decdc7 100644 --- a/src/main/java/net/pterodactylus/sone/core/PostReplyProvider.java +++ b/src/main/java/net/pterodactylus/sone/core/PostReplyProvider.java @@ -22,6 +22,8 @@ import java.util.List; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.PostReply; +import com.google.common.base.Optional; + /** * Interface for objects that can provide {@link PostReply}s. * @@ -36,7 +38,7 @@ public interface PostReplyProvider { * The ID of the reply to get * @return The reply, or {@code null} if there is no such reply */ - public PostReply getPostReply(String id); + public Optional getPostReply(String id); /** * Returns all replies for the given post, order ascending by time. diff --git a/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java b/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java index 80802ad..1617523 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java @@ -33,6 +33,7 @@ import net.pterodactylus.sone.freenet.fcp.Command; import net.pterodactylus.sone.freenet.fcp.FcpException; import net.pterodactylus.sone.template.SoneAccessor; +import com.google.common.base.Optional; import com.google.common.collect.Collections2; import freenet.node.FSParseException; @@ -211,11 +212,11 @@ public abstract class AbstractSoneCommand extends AbstractCommand { protected PostReply getReply(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException { try { String replyId = simpleFieldSet.getString(parameterName); - PostReply reply = core.getPostReply(replyId); - if (reply == null) { + Optional reply = core.getPostReply(replyId); + if (!reply.isPresent()) { throw new FcpException("Could not load reply from “" + replyId + "”."); } - return reply; + return reply.get(); } catch (FSParseException fspe1) { throw new FcpException("Could not reply ID from “" + parameterName + "”.", fspe1); } diff --git a/src/main/java/net/pterodactylus/sone/web/DeleteReplyPage.java b/src/main/java/net/pterodactylus/sone/web/DeleteReplyPage.java index 25a9e0b..f7ca132 100644 --- a/src/main/java/net/pterodactylus/sone/web/DeleteReplyPage.java +++ b/src/main/java/net/pterodactylus/sone/web/DeleteReplyPage.java @@ -23,6 +23,8 @@ import net.pterodactylus.util.template.Template; import net.pterodactylus.util.template.TemplateContext; import net.pterodactylus.util.web.Method; +import com.google.common.base.Optional; + /** * This page lets the user delete a reply. * @@ -53,14 +55,14 @@ public class DeleteReplyPage extends SoneTemplatePage { protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException { super.processTemplate(request, templateContext); String replyId = request.getHttpRequest().getPartAsStringFailsafe("reply", 36); - PostReply reply = webInterface.getCore().getPostReply(replyId); + Optional reply = webInterface.getCore().getPostReply(replyId); String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 256); if (request.getMethod() == Method.POST) { - if (!reply.getSone().isLocal()) { + if (!reply.get().getSone().isLocal()) { throw new RedirectException("noPermission.html"); } if (request.getHttpRequest().isPartSet("confirmDelete")) { - webInterface.getCore().deleteReply(reply); + webInterface.getCore().deleteReply(reply.get()); throw new RedirectException(returnPage); } else if (request.getHttpRequest().isPartSet("abortDelete")) { throw new RedirectException(returnPage); diff --git a/src/main/java/net/pterodactylus/sone/web/MarkAsKnownPage.java b/src/main/java/net/pterodactylus/sone/web/MarkAsKnownPage.java index 0bf5285..04ad304 100644 --- a/src/main/java/net/pterodactylus/sone/web/MarkAsKnownPage.java +++ b/src/main/java/net/pterodactylus/sone/web/MarkAsKnownPage.java @@ -27,6 +27,8 @@ import net.pterodactylus.sone.web.page.FreenetRequest; import net.pterodactylus.util.template.Template; import net.pterodactylus.util.template.TemplateContext; +import com.google.common.base.Optional; + /** * Page that lets the user mark a number of {@link Sone}s, {@link Post}s, or * {@link Reply Replie}s as known. @@ -71,11 +73,11 @@ public class MarkAsKnownPage extends SoneTemplatePage { } webInterface.getCore().markPostKnown(post); } else if (type.equals("reply")) { - PostReply reply = webInterface.getCore().getPostReply(id); - if (reply == null) { + Optional reply = webInterface.getCore().getPostReply(id); + if (!reply.isPresent()) { continue; } - webInterface.getCore().markReplyKnown(reply); + webInterface.getCore().markReplyKnown(reply.get()); } else if (type.equals("sone")) { Sone sone = webInterface.getCore().getSone(id, false); if (sone == null) { diff --git a/src/main/java/net/pterodactylus/sone/web/SearchPage.java b/src/main/java/net/pterodactylus/sone/web/SearchPage.java index 337918d..b937b2f 100644 --- a/src/main/java/net/pterodactylus/sone/web/SearchPage.java +++ b/src/main/java/net/pterodactylus/sone/web/SearchPage.java @@ -44,6 +44,7 @@ import net.pterodactylus.util.text.StringEscaper; import net.pterodactylus.util.text.TextException; import com.google.common.base.Function; +import com.google.common.base.Optional; import com.google.common.base.Predicate; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; @@ -336,7 +337,11 @@ public class SearchPage extends SoneTemplatePage { */ private String getReplyPostId(String phrase) { String replyId = phrase.startsWith("reply://") ? phrase.substring(8) : phrase; - return (webInterface.getCore().getPostReply(replyId) != null) ? webInterface.getCore().getPostReply(replyId).getPost().getId() : null; + Optional postReply = webInterface.getCore().getPostReply(replyId); + if (!postReply.isPresent()) { + return null; + } + return postReply.get().getPost().getId(); } /** diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/DeleteReplyAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/DeleteReplyAjaxPage.java index b1a3317..2aa217d 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/DeleteReplyAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/DeleteReplyAjaxPage.java @@ -17,6 +17,8 @@ package net.pterodactylus.sone.web.ajax; +import com.google.common.base.Optional; + import net.pterodactylus.sone.data.PostReply; import net.pterodactylus.sone.web.WebInterface; import net.pterodactylus.sone.web.page.FreenetRequest; @@ -49,14 +51,14 @@ public class DeleteReplyAjaxPage extends JsonPage { @Override protected JsonObject createJsonObject(FreenetRequest request) { String replyId = request.getHttpRequest().getParam("reply"); - PostReply reply = webInterface.getCore().getPostReply(replyId); - if (reply == null) { + Optional reply = webInterface.getCore().getPostReply(replyId); + if (!reply.isPresent()) { return createErrorJsonObject("invalid-reply-id"); } - if (!reply.getSone().isLocal()) { + if (!reply.get().getSone().isLocal()) { return createErrorJsonObject("not-authorized"); } - webInterface.getCore().deleteReply(reply); + webInterface.getCore().deleteReply(reply.get()); return createSuccessJsonObject(); } diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/GetLikesAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/GetLikesAjaxPage.java index 04191fa..f4a20fe 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/GetLikesAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/GetLikesAjaxPage.java @@ -31,6 +31,8 @@ import net.pterodactylus.sone.web.page.FreenetRequest; import net.pterodactylus.util.json.JsonArray; import net.pterodactylus.util.json.JsonObject; +import com.google.common.base.Optional; + /** * AJAX page that retrieves the number of “likes” a {@link Post} has. * @@ -67,8 +69,11 @@ public class GetLikesAjaxPage extends JsonPage { Set sones = webInterface.getCore().getLikes(post); return createSuccessJsonObject().put("likes", sones.size()).put("sones", getSones(sones)); } else if ("reply".equals(type)) { - PostReply reply = webInterface.getCore().getPostReply(id); - Set sones = webInterface.getCore().getLikes(reply); + Optional reply = webInterface.getCore().getPostReply(id); + if (!reply.isPresent()) { + return createErrorJsonObject("invalid-reply-id"); + } + Set sones = webInterface.getCore().getLikes(reply.get()); return createSuccessJsonObject().put("likes", sones.size()).put("sones", getSones(sones)); } return createErrorJsonObject("invalid-type"); diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/GetReplyAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/GetReplyAjaxPage.java index 6139776..a2ee122 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/GetReplyAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/GetReplyAjaxPage.java @@ -19,6 +19,8 @@ package net.pterodactylus.sone.web.ajax; import java.io.StringWriter; +import com.google.common.base.Optional; + import net.pterodactylus.sone.data.PostReply; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.web.WebInterface; @@ -62,11 +64,11 @@ public class GetReplyAjaxPage extends JsonPage { @Override protected JsonObject createJsonObject(FreenetRequest request) { String replyId = request.getHttpRequest().getParam("reply"); - PostReply reply = webInterface.getCore().getPostReply(replyId); - if ((reply == null) || (reply.getSone() == null)) { + Optional reply = webInterface.getCore().getPostReply(replyId); + if (!reply.isPresent()) { return createErrorJsonObject("invalid-reply-id"); } - return createSuccessJsonObject().put("reply", createJsonReply(request, reply, getCurrentSone(request.getToadletContext()))); + return createSuccessJsonObject().put("reply", createJsonReply(request, reply.get(), getCurrentSone(request.getToadletContext()))); } /** diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/GetTimesAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/GetTimesAjaxPage.java index 13cf424..7aa6779 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/GetTimesAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/GetTimesAjaxPage.java @@ -28,6 +28,8 @@ import net.pterodactylus.sone.web.WebInterface; import net.pterodactylus.sone.web.page.FreenetRequest; import net.pterodactylus.util.json.JsonObject; +import com.google.common.base.Optional; + /** * Ajax page that returns a formatted, relative timestamp for replies or posts. * @@ -77,16 +79,16 @@ public class GetTimesAjaxPage extends JsonPage { if (allIds.length() > 0) { String[] ids = allIds.split(","); for (String id : ids) { - PostReply reply = webInterface.getCore().getPostReply(id); - if (reply == null) { + Optional reply = webInterface.getCore().getPostReply(id); + if (!reply.isPresent()) { continue; } JsonObject replyTime = new JsonObject(); - Time time = getTime(reply.getTime()); + Time time = getTime(reply.get().getTime()); replyTime.put("timeText", time.getText()); replyTime.put("refreshTime", TimeUnit.MILLISECONDS.toSeconds(time.getRefresh())); synchronized (dateFormat) { - replyTime.put("tooltip", dateFormat.format(new Date(reply.getTime()))); + replyTime.put("tooltip", dateFormat.format(new Date(reply.get().getTime()))); } replyTimes.put(id, replyTime); } diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/MarkAsKnownAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/MarkAsKnownAjaxPage.java index e9c582a..fec5800 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/MarkAsKnownAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/MarkAsKnownAjaxPage.java @@ -26,6 +26,8 @@ import net.pterodactylus.sone.web.WebInterface; import net.pterodactylus.sone.web.page.FreenetRequest; import net.pterodactylus.util.json.JsonObject; +import com.google.common.base.Optional; + /** * AJAX page that lets the user mark a number of {@link Sone}s, {@link Post}s, * or {@link Reply}s as known. @@ -63,11 +65,11 @@ public class MarkAsKnownAjaxPage extends JsonPage { } core.markPostKnown(post); } else if (type.equals("reply")) { - PostReply reply = core.getPostReply(id); - if (reply == null) { + Optional reply = core.getPostReply(id); + if (!reply.isPresent()) { continue; } - core.markReplyKnown(reply); + core.markReplyKnown(reply.get()); } else if (type.equals("sone")) { Sone sone = core.getSone(id, false); if (sone == null) { -- 2.7.4