From fd88107b013522d7620f5297386472206f320e10 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 2 Dec 2014 22:01:05 +0100 Subject: [PATCH] Return local Sones from core and web interface. --- src/main/java/net/pterodactylus/sone/core/Core.java | 21 ++++----------------- .../pterodactylus/sone/database/SoneProvider.java | 3 ++- .../sone/database/memory/MemoryDatabase.java | 10 ++++++++-- .../net/pterodactylus/sone/web/CreatePostPage.java | 12 ++++++------ .../net/pterodactylus/sone/web/CreateReplyPage.java | 10 +++++----- .../net/pterodactylus/sone/web/KnownSonesPage.java | 3 ++- .../net/pterodactylus/sone/web/LockSonePage.java | 9 ++++++--- .../java/net/pterodactylus/sone/web/LoginPage.java | 9 ++++++--- .../net/pterodactylus/sone/web/OptionsPage.java | 4 ++-- .../pterodactylus/sone/web/SoneTemplatePage.java | 7 ++++--- .../net/pterodactylus/sone/web/UnlockSonePage.java | 9 ++++++--- .../net/pterodactylus/sone/web/WebInterface.java | 11 ++++++----- .../sone/web/ajax/CreatePostAjaxPage.java | 13 +++++++------ .../sone/web/ajax/CreateReplyAjaxPage.java | 12 ++++++------ .../sone/web/ajax/GetNotificationsAjaxPage.java | 6 +++--- .../sone/web/ajax/GetPostAjaxPage.java | 4 ++-- .../sone/web/ajax/GetReplyAjaxPage.java | 4 ++-- .../sone/web/ajax/GetStatusAjaxPage.java | 5 +++-- .../net/pterodactylus/sone/web/ajax/JsonPage.java | 6 +++--- .../sone/web/ajax/LockSoneAjaxPage.java | 9 ++++++--- .../sone/web/ajax/UnlockSoneAjaxPage.java | 9 ++++++--- .../pterodactylus/sone/fcp/LockSoneCommandTest.java | 7 ++++--- .../sone/fcp/UnlockSoneCommandTest.java | 7 ++++--- .../pterodactylus/sone/text/SoneTextParserTest.java | 3 ++- 24 files changed, 105 insertions(+), 88 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 71dd349..b485445 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -55,6 +55,7 @@ import net.pterodactylus.sone.core.event.SoneRemovedEvent; import net.pterodactylus.sone.core.event.SoneUnlockedEvent; import net.pterodactylus.sone.data.Album; import net.pterodactylus.sone.data.Image; +import net.pterodactylus.sone.data.LocalSone; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.PostReply; import net.pterodactylus.sone.data.Reply; @@ -326,27 +327,13 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, return database.getSone(id); } - /** - * {@inheritDocs} - */ @Override - public Collection getLocalSones() { + public Collection getLocalSones() { return database.getLocalSones(); } - /** - * Returns the local Sone with the given ID, optionally creating a new Sone. - * - * @param id - * The ID of the Sone - * @return The Sone with the given ID, or {@code null} - */ - public Sone getLocalSone(String id) { - Optional sone = database.getSone(id); - if (sone.isPresent() && sone.get().isLocal()) { - return sone.get(); - } - return null; + public Optional getLocalSone(String id) { + return database.getLocalSone(id); } /** diff --git a/src/main/java/net/pterodactylus/sone/database/SoneProvider.java b/src/main/java/net/pterodactylus/sone/database/SoneProvider.java index 73467a2..fd35d7b 100644 --- a/src/main/java/net/pterodactylus/sone/database/SoneProvider.java +++ b/src/main/java/net/pterodactylus/sone/database/SoneProvider.java @@ -20,6 +20,7 @@ package net.pterodactylus.sone.database; import java.util.Collection; import net.pterodactylus.sone.core.Core; +import net.pterodactylus.sone.data.LocalSone; import net.pterodactylus.sone.data.Sone; import com.google.common.base.Function; @@ -58,7 +59,7 @@ public interface SoneProvider { * * @return All local Sones */ - public Collection getLocalSones(); + public Collection getLocalSones(); /** * Returns all remote Sones. diff --git a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java index 12ef439..128a4bd 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java @@ -583,10 +583,16 @@ public class MemoryDatabase extends AbstractService implements Database { } @Override - public Collection getLocalSones() { + public Collection getLocalSones() { lock.readLock().lock(); try { - return from(allSones.values()).filter(LOCAL_SONE_FILTER).toSet(); + return from(allSones.values()).filter(LOCAL_SONE_FILTER).transform(new Function() { + @Override + public LocalSone apply(Sone sone) { + // FIXME – Sones will not always implement LocalSone + return (LocalSone) sone; + } + }).toSet(); } finally { lock.readLock().unlock(); } diff --git a/src/main/java/net/pterodactylus/sone/web/CreatePostPage.java b/src/main/java/net/pterodactylus/sone/web/CreatePostPage.java index 0590f72..d66690e 100644 --- a/src/main/java/net/pterodactylus/sone/web/CreatePostPage.java +++ b/src/main/java/net/pterodactylus/sone/web/CreatePostPage.java @@ -19,6 +19,7 @@ package net.pterodactylus.sone.web; import com.google.common.base.Optional; +import net.pterodactylus.sone.data.LocalSone; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.text.TextFilter; @@ -62,14 +63,13 @@ public class CreatePostPage extends SoneTemplatePage { if (text.length() != 0) { String senderId = request.getHttpRequest().getPartAsStringFailsafe("sender", 43); String recipientId = request.getHttpRequest().getPartAsStringFailsafe("recipient", 43); - Sone currentSone = getCurrentSone(request.getToadletContext()).get(); - Sone sender = webInterface.getCore().getLocalSone(senderId); - if (sender == null) { - sender = currentSone; - } Optional recipient = webInterface.getCore().getSone(recipientId); + Optional sender = webInterface.getCore().getLocalSone(senderId); + if (!sender.isPresent()) { + sender = getCurrentSone(request.getToadletContext()); + } text = TextFilter.filter(request.getHttpRequest().getHeader("host"), text); - webInterface.getCore().createPost(sender, recipient, text); + webInterface.getCore().createPost(sender.get(), recipient, text); throw new RedirectException(returnPage); } templateContext.set("errorTextEmpty", true); diff --git a/src/main/java/net/pterodactylus/sone/web/CreateReplyPage.java b/src/main/java/net/pterodactylus/sone/web/CreateReplyPage.java index 4158313..3ec13f6 100644 --- a/src/main/java/net/pterodactylus/sone/web/CreateReplyPage.java +++ b/src/main/java/net/pterodactylus/sone/web/CreateReplyPage.java @@ -19,8 +19,8 @@ package net.pterodactylus.sone.web; import com.google.common.base.Optional; +import net.pterodactylus.sone.data.LocalSone; import net.pterodactylus.sone.data.Post; -import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.text.TextFilter; import net.pterodactylus.sone.web.page.FreenetRequest; import net.pterodactylus.util.template.Template; @@ -66,12 +66,12 @@ public class CreateReplyPage extends SoneTemplatePage { } if (text.length() > 0) { String senderId = request.getHttpRequest().getPartAsStringFailsafe("sender", 43); - Sone sender = webInterface.getCore().getLocalSone(senderId); - if (sender == null) { - sender = getCurrentSone(request.getToadletContext()).get(); + Optional sender = webInterface.getCore().getLocalSone(senderId); + if (!sender.isPresent()) { + sender = getCurrentSone(request.getToadletContext()); } text = TextFilter.filter(request.getHttpRequest().getHeader("host"), text); - webInterface.getCore().createReply(sender, post.get(), text); + webInterface.getCore().createReply(sender.get(), post.get(), text); throw new RedirectException(returnPage); } templateContext.set("errorTextEmpty", true); diff --git a/src/main/java/net/pterodactylus/sone/web/KnownSonesPage.java b/src/main/java/net/pterodactylus/sone/web/KnownSonesPage.java index a741322..6a3c537 100644 --- a/src/main/java/net/pterodactylus/sone/web/KnownSonesPage.java +++ b/src/main/java/net/pterodactylus/sone/web/KnownSonesPage.java @@ -24,6 +24,7 @@ import java.util.Collection; import java.util.Collections; import java.util.List; +import net.pterodactylus.sone.data.LocalSone; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.web.page.FreenetRequest; import net.pterodactylus.util.collection.Pagination; @@ -74,7 +75,7 @@ public class KnownSonesPage extends SoneTemplatePage { templateContext.set("sort", sortField); templateContext.set("order", sortOrder); templateContext.set("filter", filter); - final Optional currentSone = getCurrentSone(request.getToadletContext(), false); + final Optional currentSone = getCurrentSone(request.getToadletContext(), false); Collection knownSones = Collections2.filter(webInterface.getCore().getSones(), Sone.EMPTY_SONE_FILTER); if (currentSone.isPresent() && "followed".equals(filter)) { knownSones = Collections2.filter(knownSones, new Predicate() { diff --git a/src/main/java/net/pterodactylus/sone/web/LockSonePage.java b/src/main/java/net/pterodactylus/sone/web/LockSonePage.java index f72252f..cf33d05 100644 --- a/src/main/java/net/pterodactylus/sone/web/LockSonePage.java +++ b/src/main/java/net/pterodactylus/sone/web/LockSonePage.java @@ -17,11 +17,14 @@ package net.pterodactylus.sone.web; +import net.pterodactylus.sone.data.LocalSone; import net.pterodactylus.sone.data.Sone; 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; + /** * This page lets the user lock a {@link Sone} to prevent it from being * inserted. @@ -53,9 +56,9 @@ public class LockSonePage extends SoneTemplatePage { protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException { super.processTemplate(request, templateContext); String soneId = request.getHttpRequest().getPartAsStringFailsafe("sone", 44); - Sone sone = webInterface.getCore().getLocalSone(soneId); - if (sone != null) { - webInterface.getCore().lockSone(sone); + Optional sone = webInterface.getCore().getLocalSone(soneId); + if (sone.isPresent()) { + webInterface.getCore().lockSone(sone.get()); } String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 256); throw new RedirectException(returnPage); diff --git a/src/main/java/net/pterodactylus/sone/web/LoginPage.java b/src/main/java/net/pterodactylus/sone/web/LoginPage.java index 8d269dd..5934497 100644 --- a/src/main/java/net/pterodactylus/sone/web/LoginPage.java +++ b/src/main/java/net/pterodactylus/sone/web/LoginPage.java @@ -24,6 +24,7 @@ import java.util.Collections; import java.util.List; import java.util.logging.Logger; +import net.pterodactylus.sone.data.LocalSone; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.freenet.wot.OwnIdentity; import net.pterodactylus.sone.web.page.FreenetRequest; @@ -32,6 +33,8 @@ import net.pterodactylus.util.template.TemplateContext; import net.pterodactylus.util.web.Method; import freenet.clients.http.ToadletContext; +import com.google.common.base.Optional; + /** * The login page manages logging the user in. * @@ -71,9 +74,9 @@ public class LoginPage extends SoneTemplatePage { templateContext.set("sones", localSones); if (request.getMethod() == Method.POST) { String soneId = request.getHttpRequest().getPartAsStringFailsafe("sone-id", 100); - Sone selectedSone = webInterface.getCore().getLocalSone(soneId); - if (selectedSone != null) { - setCurrentSone(request.getToadletContext(), selectedSone); + Optional selectedSone = webInterface.getCore().getLocalSone(soneId); + if (selectedSone.isPresent()) { + setCurrentSone(request.getToadletContext(), selectedSone.get()); String target = request.getHttpRequest().getParam("target"); if ((target == null) || (target.length() == 0)) { target = "index.html"; diff --git a/src/main/java/net/pterodactylus/sone/web/OptionsPage.java b/src/main/java/net/pterodactylus/sone/web/OptionsPage.java index cbc9686..4b8ef00 100644 --- a/src/main/java/net/pterodactylus/sone/web/OptionsPage.java +++ b/src/main/java/net/pterodactylus/sone/web/OptionsPage.java @@ -23,7 +23,7 @@ import java.util.ArrayList; import java.util.List; import net.pterodactylus.sone.core.Preferences; -import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.data.LocalSone; import net.pterodactylus.sone.data.Sone.ShowCustomAvatars; import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired; import net.pterodactylus.sone.web.page.FreenetRequest; @@ -63,7 +63,7 @@ public class OptionsPage extends SoneTemplatePage { protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException { super.processTemplate(request, templateContext); Preferences preferences = webInterface.getCore().getPreferences(); - Optional currentSone = webInterface.getCurrentSone(request.getToadletContext(), false); + Optional currentSone = webInterface.getCurrentSone(request.getToadletContext(), false); if (request.getMethod() == Method.POST) { List fieldErrors = new ArrayList(); if (currentSone.isPresent()) { diff --git a/src/main/java/net/pterodactylus/sone/web/SoneTemplatePage.java b/src/main/java/net/pterodactylus/sone/web/SoneTemplatePage.java index fa2798e..506fc15 100644 --- a/src/main/java/net/pterodactylus/sone/web/SoneTemplatePage.java +++ b/src/main/java/net/pterodactylus/sone/web/SoneTemplatePage.java @@ -25,6 +25,7 @@ import java.util.Collections; import java.util.List; import java.util.Map; +import net.pterodactylus.sone.data.LocalSone; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.main.SonePlugin; import net.pterodactylus.sone.notify.ListNotificationFilters; @@ -138,7 +139,7 @@ public class SoneTemplatePage extends FreenetTemplatePage { * @return The currently logged in Sone, or {@code null} if no Sone is * currently logged in */ - protected Optional getCurrentSone(ToadletContext toadletContext) { + protected Optional getCurrentSone(ToadletContext toadletContext) { return webInterface.getCurrentSone(toadletContext); } @@ -153,7 +154,7 @@ public class SoneTemplatePage extends FreenetTemplatePage { * @return The currently logged in Sone, or {@code null} if no Sone is * currently logged in */ - protected Optional getCurrentSone(ToadletContext toadletContext, boolean create) { + protected Optional getCurrentSone(ToadletContext toadletContext, boolean create) { return webInterface.getCurrentSone(toadletContext, create); } @@ -224,7 +225,7 @@ public class SoneTemplatePage extends FreenetTemplatePage { @Override protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException { super.processTemplate(request, templateContext); - Optional currentSone = getCurrentSone(request.getToadletContext(), false); + Optional currentSone = getCurrentSone(request.getToadletContext(), false); templateContext.set("core", webInterface.getCore()); templateContext.set("currentSone", currentSone.orNull()); templateContext.set("localSones", webInterface.getCore().getLocalSones()); diff --git a/src/main/java/net/pterodactylus/sone/web/UnlockSonePage.java b/src/main/java/net/pterodactylus/sone/web/UnlockSonePage.java index 8491163..56ee2b7 100644 --- a/src/main/java/net/pterodactylus/sone/web/UnlockSonePage.java +++ b/src/main/java/net/pterodactylus/sone/web/UnlockSonePage.java @@ -17,11 +17,14 @@ package net.pterodactylus.sone.web; +import net.pterodactylus.sone.data.LocalSone; import net.pterodactylus.sone.data.Sone; 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; + /** * This page lets the user unlock a {@link Sone} to allow its insertion. * @@ -52,9 +55,9 @@ public class UnlockSonePage extends SoneTemplatePage { protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException { super.processTemplate(request, templateContext); String soneId = request.getHttpRequest().getPartAsStringFailsafe("sone", 44); - Sone sone = webInterface.getCore().getLocalSone(soneId); - if (sone != null) { - webInterface.getCore().unlockSone(sone); + Optional localSone = webInterface.getCore().getLocalSone(soneId); + if (localSone.isPresent()) { + webInterface.getCore().unlockSone(localSone.get()); } String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 256); throw new RedirectException(returnPage); diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index 258669a..e245b6a 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -64,6 +64,7 @@ import net.pterodactylus.sone.core.event.SoneUnlockedEvent; import net.pterodactylus.sone.core.event.UpdateFoundEvent; import net.pterodactylus.sone.data.Album; import net.pterodactylus.sone.data.Image; +import net.pterodactylus.sone.data.LocalSone; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.PostReply; import net.pterodactylus.sone.data.Profile; @@ -381,7 +382,7 @@ public class WebInterface { * @return The currently logged in Sone, or {@code null} if no Sone is * currently logged in */ - public Optional getCurrentSone(ToadletContext toadletContext) { + public Optional getCurrentSone(ToadletContext toadletContext) { return getCurrentSone(toadletContext, true); } @@ -396,8 +397,8 @@ public class WebInterface { * @return The currently logged in Sone, or {@code null} if no Sone is * currently logged in */ - public Optional getCurrentSone(ToadletContext toadletContext, boolean createSession) { - Collection localSones = getCore().getLocalSones(); + public Optional getCurrentSone(ToadletContext toadletContext, boolean createSession) { + Collection localSones = getCore().getLocalSones(); if (localSones.size() == 1) { return Optional.of(localSones.iterator().next()); } @@ -412,7 +413,7 @@ public class WebInterface { * @return The currently logged in Sone, or {@code null} if no Sone is * currently logged in */ - public Optional getCurrentSone(Optional session) { + public Optional getCurrentSone(Optional session) { if (!session.isPresent()) { return Optional.absent(); } @@ -420,7 +421,7 @@ public class WebInterface { if (soneId == null) { return Optional.absent(); } - return Optional.fromNullable(getCore().getLocalSone(soneId)); + return getCore().getLocalSone(soneId); } /** diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/CreatePostAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/CreatePostAjaxPage.java index 71a76a6..50bf221 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/CreatePostAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/CreatePostAjaxPage.java @@ -17,6 +17,7 @@ package net.pterodactylus.sone.web.ajax; +import net.pterodactylus.sone.data.LocalSone; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.text.TextFilter; @@ -47,21 +48,21 @@ public class CreatePostAjaxPage extends JsonPage { */ @Override protected JsonReturnObject createJsonObject(FreenetRequest request) { - Sone sone = getCurrentSone(request.getToadletContext()).get(); + LocalSone sone = getCurrentSone(request.getToadletContext()).get(); String recipientId = request.getHttpRequest().getParam("recipient"); Optional recipient = webInterface.getCore().getSone(recipientId); String senderId = request.getHttpRequest().getParam("sender"); - Sone sender = webInterface.getCore().getLocalSone(senderId); - if (sender == null) { - sender = sone; + Optional sender = webInterface.getCore().getLocalSone(senderId); + if (!sender.isPresent()) { + sender = Optional.of(sone); } String text = request.getHttpRequest().getParam("text"); if ((text == null) || (text.trim().length() == 0)) { return createErrorJsonObject("text-required"); } text = TextFilter.filter(request.getHttpRequest().getHeader("host"), text); - Post newPost = webInterface.getCore().createPost(sender, recipient, text); - return createSuccessJsonObject().put("postId", newPost.getId()).put("sone", sender.getId()).put("recipient", newPost.getRecipientId().orNull()); + Post newPost = webInterface.getCore().createPost(sender.get(), recipient, text); + return createSuccessJsonObject().put("postId", newPost.getId()).put("sone", sender.get().getId()).put("recipient", newPost.getRecipientId().orNull()); } } diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/CreateReplyAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/CreateReplyAjaxPage.java index 8c0387e..0056156 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/CreateReplyAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/CreateReplyAjaxPage.java @@ -19,9 +19,9 @@ package net.pterodactylus.sone.web.ajax; import com.google.common.base.Optional; +import net.pterodactylus.sone.data.LocalSone; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.PostReply; -import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.text.TextFilter; import net.pterodactylus.sone.web.WebInterface; import net.pterodactylus.sone.web.page.FreenetRequest; @@ -55,17 +55,17 @@ public class CreateReplyAjaxPage extends JsonPage { String postId = request.getHttpRequest().getParam("post"); String text = request.getHttpRequest().getParam("text").trim(); String senderId = request.getHttpRequest().getParam("sender"); - Sone sender = webInterface.getCore().getLocalSone(senderId); - if (sender == null) { - sender = getCurrentSone(request.getToadletContext()).get(); + Optional sender = webInterface.getCore().getLocalSone(senderId); + if (!sender.isPresent()) { + sender = getCurrentSone(request.getToadletContext()); } Optional post = webInterface.getCore().getPost(postId); if (!post.isPresent()) { return createErrorJsonObject("invalid-post-id"); } text = TextFilter.filter(request.getHttpRequest().getHeader("host"), text); - PostReply reply = webInterface.getCore().createReply(sender, post.get(), text); - return createSuccessJsonObject().put("reply", reply.getId()).put("sone", sender.getId()); + PostReply reply = webInterface.getCore().createReply(sender.get(), post.get(), text); + return createSuccessJsonObject().put("reply", reply.getId()).put("sone", sender.get().getId()); } } diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPage.java index d86e5a8..e6a6a9f 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPage.java @@ -25,7 +25,7 @@ import java.util.Collection; import java.util.Collections; import java.util.List; -import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.data.LocalSone; import net.pterodactylus.sone.main.SonePlugin; import net.pterodactylus.sone.notify.ListNotificationFilters; import net.pterodactylus.sone.web.WebInterface; @@ -81,7 +81,7 @@ public class GetNotificationsAjaxPage extends JsonPage { */ @Override protected JsonReturnObject createJsonObject(FreenetRequest request) { - Optional currentSone = getCurrentSone(request.getToadletContext(), false); + Optional currentSone = getCurrentSone(request.getToadletContext(), false); Collection notifications = webInterface.getNotifications().getNotifications(); List filteredNotifications = ListNotificationFilters.filterNotifications(notifications, currentSone.orNull()); Collections.sort(filteredNotifications, Notification.CREATED_TIME_SORTER); @@ -145,7 +145,7 @@ public class GetNotificationsAjaxPage extends JsonPage { * The current Sone (may be {@code null}) * @return The current options */ - private static JsonNode createJsonOptions(Optional currentSone) { + private static JsonNode createJsonOptions(Optional currentSone) { ObjectNode options = new ObjectNode(instance); if (currentSone.isPresent()) { options.put("ShowNotification/NewSones", currentSone.get().getOptions().isShowNewSoneNotifications()); diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/GetPostAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/GetPostAjaxPage.java index 51ab3ee..bb1e416 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/GetPostAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/GetPostAjaxPage.java @@ -22,8 +22,8 @@ import static com.fasterxml.jackson.databind.node.JsonNodeFactory.instance; import com.google.common.base.Optional; +import net.pterodactylus.sone.data.LocalSone; import net.pterodactylus.sone.data.Post; -import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.web.WebInterface; import net.pterodactylus.sone.web.page.FreenetRequest; import net.pterodactylus.util.io.Closer; @@ -95,7 +95,7 @@ public class GetPostAjaxPage extends JsonPage { * The currently logged in Sone (to store in the template) * @return The JSON representation of the post */ - private JsonNode createJsonPost(FreenetRequest request, Post post, Optional currentSone) { + private JsonNode createJsonPost(FreenetRequest request, Post post, Optional currentSone) { ObjectNode jsonPost = new ObjectNode(instance); jsonPost.put("id", post.getId()); jsonPost.put("sone", post.getSone().getId()); 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 d0b79ac..14d96ee 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/GetReplyAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/GetReplyAjaxPage.java @@ -22,8 +22,8 @@ import static com.fasterxml.jackson.databind.node.JsonNodeFactory.instance; import com.google.common.base.Optional; +import net.pterodactylus.sone.data.LocalSone; import net.pterodactylus.sone.data.PostReply; -import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.web.WebInterface; import net.pterodactylus.sone.web.page.FreenetRequest; import net.pterodactylus.util.io.Closer; @@ -97,7 +97,7 @@ public class GetReplyAjaxPage extends JsonPage { * The currently logged in Sone (to store in the template) * @return The JSON representation of the reply */ - private JsonNode createJsonReply(FreenetRequest request, PostReply reply, Optional currentSone) { + private JsonNode createJsonReply(FreenetRequest request, PostReply reply, Optional currentSone) { ObjectNode jsonReply = new ObjectNode(instance); jsonReply.put("id", reply.getId()); jsonReply.put("postId", reply.getPostId()); diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.java index bb59a8d..84c96dd 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.java @@ -29,6 +29,7 @@ import java.util.HashSet; import java.util.List; import java.util.Set; +import net.pterodactylus.sone.data.LocalSone; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.PostReply; import net.pterodactylus.sone.data.Sone; @@ -73,7 +74,7 @@ public class GetStatusAjaxPage extends JsonPage { */ @Override protected JsonReturnObject createJsonObject(FreenetRequest request) { - final Optional currentSone = getCurrentSone(request.getToadletContext(), false); + final Optional currentSone = getCurrentSone(request.getToadletContext(), false); /* load Sones. always return the status of the current Sone. */ Set sones = new HashSet(currentSone.asSet()); String loadSoneIds = request.getHttpRequest().getParam("soneIds"); @@ -191,7 +192,7 @@ public class GetStatusAjaxPage extends JsonPage { * The current Sone (may be {@code null}) * @return The current options */ - private static JsonNode createJsonOptions(Optional currentSone) { + private static JsonNode createJsonOptions(Optional currentSone) { ObjectNode options = new ObjectNode(instance); if (currentSone.isPresent()) { options.put("ShowNotification/NewSones", currentSone.get().getOptions().isShowNewSoneNotifications()); diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/JsonPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/JsonPage.java index ada501a..e181a4e 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/JsonPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/JsonPage.java @@ -27,7 +27,7 @@ import java.net.URI; import java.util.logging.Level; import java.util.logging.Logger; -import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.data.LocalSone; import net.pterodactylus.sone.web.WebInterface; import net.pterodactylus.sone.web.page.FreenetPage; import net.pterodactylus.sone.web.page.FreenetRequest; @@ -77,11 +77,11 @@ public abstract class JsonPage implements FreenetPage { // ACCESSORS // - protected Optional getCurrentSone(ToadletContext toadletContext) { + protected Optional getCurrentSone(ToadletContext toadletContext) { return webInterface.getCurrentSone(toadletContext); } - protected Optional getCurrentSone(ToadletContext toadletContext, boolean createSession) { + protected Optional getCurrentSone(ToadletContext toadletContext, boolean createSession) { return webInterface.getCurrentSone(toadletContext, createSession); } diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/LockSoneAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/LockSoneAjaxPage.java index cc04338..516db2d 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/LockSoneAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/LockSoneAjaxPage.java @@ -18,10 +18,13 @@ package net.pterodactylus.sone.web.ajax; import net.pterodactylus.sone.core.Core; +import net.pterodactylus.sone.data.LocalSone; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.web.WebInterface; import net.pterodactylus.sone.web.page.FreenetRequest; +import com.google.common.base.Optional; + /** * Lets the user {@link Core#lockSone(Sone) lock} a {@link Sone}. * @@ -45,11 +48,11 @@ public class LockSoneAjaxPage extends JsonPage { @Override protected JsonReturnObject createJsonObject(FreenetRequest request) { String soneId = request.getHttpRequest().getParam("sone"); - Sone sone = webInterface.getCore().getLocalSone(soneId); - if (sone == null) { + Optional sone = webInterface.getCore().getLocalSone(soneId); + if (!sone.isPresent()) { return createErrorJsonObject("invalid-sone-id"); } - webInterface.getCore().lockSone(sone); + webInterface.getCore().lockSone(sone.get()); return createSuccessJsonObject(); } diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/UnlockSoneAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/UnlockSoneAjaxPage.java index 3a91f81..8384e85 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/UnlockSoneAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/UnlockSoneAjaxPage.java @@ -18,10 +18,13 @@ package net.pterodactylus.sone.web.ajax; import net.pterodactylus.sone.core.Core; +import net.pterodactylus.sone.data.LocalSone; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.web.WebInterface; import net.pterodactylus.sone.web.page.FreenetRequest; +import com.google.common.base.Optional; + /** * Lets the user {@link Core#unlockSone(Sone) unlock} a {@link Sone}. * @@ -45,11 +48,11 @@ public class UnlockSoneAjaxPage extends JsonPage { @Override protected JsonReturnObject createJsonObject(FreenetRequest request) { String soneId = request.getHttpRequest().getParam("sone"); - Sone sone = webInterface.getCore().getLocalSone(soneId); - if (sone == null) { + Optional localSone = webInterface.getCore().getLocalSone(soneId); + if (!localSone.isPresent()) { return createErrorJsonObject("invalid-sone-id"); } - webInterface.getCore().unlockSone(sone); + webInterface.getCore().unlockSone(localSone.get()); return createSuccessJsonObject(); } diff --git a/src/test/java/net/pterodactylus/sone/fcp/LockSoneCommandTest.java b/src/test/java/net/pterodactylus/sone/fcp/LockSoneCommandTest.java index 2306931..383b52a 100644 --- a/src/test/java/net/pterodactylus/sone/fcp/LockSoneCommandTest.java +++ b/src/test/java/net/pterodactylus/sone/fcp/LockSoneCommandTest.java @@ -26,6 +26,7 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import net.pterodactylus.sone.core.Core; +import net.pterodactylus.sone.data.LocalSone; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder; import net.pterodactylus.sone.freenet.fcp.Command.Response; @@ -45,12 +46,12 @@ public class LockSoneCommandTest { @Test public void testLockingALocalSone() throws FcpException { - Sone localSone = mock(Sone.class); + LocalSone localSone = mock(LocalSone.class); when(localSone.getId()).thenReturn("LocalSone"); when(localSone.isLocal()).thenReturn(true); Core core = mock(Core.class); - when(core.getSone(eq("LocalSone"))).thenReturn(Optional.of(localSone)); - when(core.getLocalSone(eq("LocalSone"))).thenReturn(localSone); + when(core.getSone(eq("LocalSone"))).thenReturn(Optional.of(localSone)); + when(core.getLocalSone(eq("LocalSone"))).thenReturn(Optional.of(localSone)); SimpleFieldSet fields = new SimpleFieldSetBuilder().put("Sone", "LocalSone").get(); LockSoneCommand lockSoneCommand = new LockSoneCommand(core); diff --git a/src/test/java/net/pterodactylus/sone/fcp/UnlockSoneCommandTest.java b/src/test/java/net/pterodactylus/sone/fcp/UnlockSoneCommandTest.java index ca615ee..48f5aa1 100644 --- a/src/test/java/net/pterodactylus/sone/fcp/UnlockSoneCommandTest.java +++ b/src/test/java/net/pterodactylus/sone/fcp/UnlockSoneCommandTest.java @@ -26,6 +26,7 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import net.pterodactylus.sone.core.Core; +import net.pterodactylus.sone.data.LocalSone; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder; import net.pterodactylus.sone.freenet.fcp.Command.Response; @@ -45,12 +46,12 @@ public class UnlockSoneCommandTest { @Test public void testUnlockingALocalSone() throws FcpException { - Sone localSone = mock(Sone.class); + LocalSone localSone = mock(LocalSone.class); when(localSone.getId()).thenReturn("LocalSone"); when(localSone.isLocal()).thenReturn(true); Core core = mock(Core.class); - when(core.getSone(eq("LocalSone"))).thenReturn(Optional.of(localSone)); - when(core.getLocalSone(eq("LocalSone"))).thenReturn(localSone); + when(core.getSone(eq("LocalSone"))).thenReturn(Optional.of(localSone)); + when(core.getLocalSone(eq("LocalSone"))).thenReturn(Optional.of(localSone)); SimpleFieldSet fields = new SimpleFieldSetBuilder().put("Sone", "LocalSone").get(); UnlockSoneCommand unlockSoneCommand = new UnlockSoneCommand(core); diff --git a/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java b/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java index 2ac6db7..ff4ec15 100644 --- a/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java +++ b/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java @@ -22,6 +22,7 @@ import java.io.StringReader; import java.util.Arrays; import java.util.Collection; +import net.pterodactylus.sone.data.LocalSone; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.data.impl.IdOnlySone; import net.pterodactylus.sone.database.SoneProvider; @@ -212,7 +213,7 @@ public class SoneTextParserTest extends TestCase { * {@inheritDocs} */ @Override - public Collection getLocalSones() { + public Collection getLocalSones() { return null; } -- 2.7.4