From 53a71d1d6b91e4d56af49a06f2e06bc4d11bf3eb Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 2 Dec 2014 21:31:06 +0100 Subject: [PATCH] Return an optional Sone from the current session. --- .../pterodactylus/sone/web/CreateAlbumPage.java | 2 +- .../net/pterodactylus/sone/web/CreatePostPage.java | 2 +- .../pterodactylus/sone/web/CreateReplyPage.java | 2 +- .../net/pterodactylus/sone/web/CreateSonePage.java | 2 +- .../sone/web/DeleteProfileFieldPage.java | 2 +- .../net/pterodactylus/sone/web/DeleteSonePage.java | 2 +- .../net/pterodactylus/sone/web/DistrustPage.java | 2 +- .../sone/web/EditProfileFieldPage.java | 2 +- .../pterodactylus/sone/web/EditProfilePage.java | 2 +- .../net/pterodactylus/sone/web/FollowSonePage.java | 2 +- .../pterodactylus/sone/web/ImageBrowserPage.java | 2 +- .../java/net/pterodactylus/sone/web/IndexPage.java | 2 +- .../net/pterodactylus/sone/web/KnownSonesPage.java | 11 ++++---- .../java/net/pterodactylus/sone/web/LikePage.java | 2 +- .../java/net/pterodactylus/sone/web/LoginPage.java | 4 +-- .../net/pterodactylus/sone/web/LogoutPage.java | 2 +- .../java/net/pterodactylus/sone/web/NewPage.java | 2 +- .../net/pterodactylus/sone/web/OptionsPage.java | 32 ++++++++++++---------- .../net/pterodactylus/sone/web/RescuePage.java | 2 +- .../pterodactylus/sone/web/SoneTemplatePage.java | 15 +++++----- .../java/net/pterodactylus/sone/web/TrustPage.java | 2 +- .../pterodactylus/sone/web/UnfollowSonePage.java | 2 +- .../net/pterodactylus/sone/web/UnlikePage.java | 2 +- .../net/pterodactylus/sone/web/UntrustPage.java | 2 +- .../pterodactylus/sone/web/UploadImagePage.java | 2 +- .../net/pterodactylus/sone/web/WebInterface.java | 14 +++++----- .../sone/web/ajax/CreatePostAjaxPage.java | 5 +--- .../sone/web/ajax/CreateReplyAjaxPage.java | 2 +- .../sone/web/ajax/DeleteProfileFieldAjaxPage.java | 2 +- .../sone/web/ajax/DistrustAjaxPage.java | 5 +--- .../sone/web/ajax/EditProfileFieldAjaxPage.java | 2 +- .../sone/web/ajax/FollowSoneAjaxPage.java | 5 +--- .../sone/web/ajax/GetNotificationsAjaxPage.java | 15 +++++----- .../sone/web/ajax/GetPostAjaxPage.java | 4 +-- .../sone/web/ajax/GetReplyAjaxPage.java | 4 +-- .../sone/web/ajax/GetStatusAjaxPage.java | 27 +++++++++--------- .../net/pterodactylus/sone/web/ajax/JsonPage.java | 7 +++-- .../pterodactylus/sone/web/ajax/LikeAjaxPage.java | 2 +- .../sone/web/ajax/MoveProfileFieldAjaxPage.java | 2 +- .../pterodactylus/sone/web/ajax/TrustAjaxPage.java | 5 +--- .../sone/web/ajax/UnfollowSoneAjaxPage.java | 2 +- .../sone/web/ajax/UnlikeAjaxPage.java | 5 +--- .../sone/web/ajax/UntrustAjaxPage.java | 5 +--- 43 files changed, 103 insertions(+), 114 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/web/CreateAlbumPage.java b/src/main/java/net/pterodactylus/sone/web/CreateAlbumPage.java index 1c599e4..a8ee678 100644 --- a/src/main/java/net/pterodactylus/sone/web/CreateAlbumPage.java +++ b/src/main/java/net/pterodactylus/sone/web/CreateAlbumPage.java @@ -62,7 +62,7 @@ public class CreateAlbumPage extends SoneTemplatePage { return; } String description = request.getHttpRequest().getPartAsStringFailsafe("description", 256).trim(); - Sone currentSone = getCurrentSone(request.getToadletContext()); + Sone currentSone = getCurrentSone(request.getToadletContext()).get(); String parentId = request.getHttpRequest().getPartAsStringFailsafe("parent", 36); Album parent = webInterface.getCore().getAlbum(parentId); if (parentId.equals("")) { diff --git a/src/main/java/net/pterodactylus/sone/web/CreatePostPage.java b/src/main/java/net/pterodactylus/sone/web/CreatePostPage.java index 6530a5b..0590f72 100644 --- a/src/main/java/net/pterodactylus/sone/web/CreatePostPage.java +++ b/src/main/java/net/pterodactylus/sone/web/CreatePostPage.java @@ -62,7 +62,7 @@ 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()); + Sone currentSone = getCurrentSone(request.getToadletContext()).get(); Sone sender = webInterface.getCore().getLocalSone(senderId); if (sender == null) { sender = currentSone; diff --git a/src/main/java/net/pterodactylus/sone/web/CreateReplyPage.java b/src/main/java/net/pterodactylus/sone/web/CreateReplyPage.java index 55903d8..4158313 100644 --- a/src/main/java/net/pterodactylus/sone/web/CreateReplyPage.java +++ b/src/main/java/net/pterodactylus/sone/web/CreateReplyPage.java @@ -68,7 +68,7 @@ public class CreateReplyPage extends SoneTemplatePage { String senderId = request.getHttpRequest().getPartAsStringFailsafe("sender", 43); Sone sender = webInterface.getCore().getLocalSone(senderId); if (sender == null) { - sender = getCurrentSone(request.getToadletContext()); + sender = getCurrentSone(request.getToadletContext()).get(); } text = TextFilter.filter(request.getHttpRequest().getHeader("host"), text); webInterface.getCore().createReply(sender, post.get(), text); diff --git a/src/main/java/net/pterodactylus/sone/web/CreateSonePage.java b/src/main/java/net/pterodactylus/sone/web/CreateSonePage.java index b2d4578..5c87ccb 100644 --- a/src/main/java/net/pterodactylus/sone/web/CreateSonePage.java +++ b/src/main/java/net/pterodactylus/sone/web/CreateSonePage.java @@ -137,7 +137,7 @@ public class CreateSonePage extends SoneTemplatePage { if (webInterface.getCore().getPreferences().isRequireFullAccess() && !toadletContext.isAllowedFullAccess()) { return false; } - return (getCurrentSone(toadletContext, false) == null) || (webInterface.getCore().getLocalSones().size() == 1); + return !getCurrentSone(toadletContext, false).isPresent() || (webInterface.getCore().getLocalSones().size() == 1); } } diff --git a/src/main/java/net/pterodactylus/sone/web/DeleteProfileFieldPage.java b/src/main/java/net/pterodactylus/sone/web/DeleteProfileFieldPage.java index 852cc91..d6ba0dd 100644 --- a/src/main/java/net/pterodactylus/sone/web/DeleteProfileFieldPage.java +++ b/src/main/java/net/pterodactylus/sone/web/DeleteProfileFieldPage.java @@ -54,7 +54,7 @@ public class DeleteProfileFieldPage extends SoneTemplatePage { @Override protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException { super.processTemplate(request, templateContext); - Sone currentSone = getCurrentSone(request.getToadletContext()); + Sone currentSone = getCurrentSone(request.getToadletContext()).get(); Profile profile = currentSone.getProfile(); /* get parameters from request. */ diff --git a/src/main/java/net/pterodactylus/sone/web/DeleteSonePage.java b/src/main/java/net/pterodactylus/sone/web/DeleteSonePage.java index 39d3fb3..aa824fb 100644 --- a/src/main/java/net/pterodactylus/sone/web/DeleteSonePage.java +++ b/src/main/java/net/pterodactylus/sone/web/DeleteSonePage.java @@ -56,7 +56,7 @@ public class DeleteSonePage extends SoneTemplatePage { super.processTemplate(request, templateContext); if (request.getMethod() == Method.POST) { if (request.getHttpRequest().isPartSet("deleteSone")) { - Sone currentSone = getCurrentSone(request.getToadletContext()); + Sone currentSone = getCurrentSone(request.getToadletContext()).get(); webInterface.getCore().deleteSone(currentSone); } throw new RedirectException("index.html"); diff --git a/src/main/java/net/pterodactylus/sone/web/DistrustPage.java b/src/main/java/net/pterodactylus/sone/web/DistrustPage.java index c8879a0..e0ec3fb 100644 --- a/src/main/java/net/pterodactylus/sone/web/DistrustPage.java +++ b/src/main/java/net/pterodactylus/sone/web/DistrustPage.java @@ -60,7 +60,7 @@ public class DistrustPage extends SoneTemplatePage { if (request.getMethod() == Method.POST) { String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 256); String identity = request.getHttpRequest().getPartAsStringFailsafe("sone", 44); - Sone currentSone = getCurrentSone(request.getToadletContext()); + Sone currentSone = getCurrentSone(request.getToadletContext()).get(); Optional sone = webInterface.getCore().getSone(identity); if (sone.isPresent()) { webInterface.getCore().distrustSone(currentSone, sone.get()); diff --git a/src/main/java/net/pterodactylus/sone/web/EditProfileFieldPage.java b/src/main/java/net/pterodactylus/sone/web/EditProfileFieldPage.java index fe9d78f..37ae6c8 100644 --- a/src/main/java/net/pterodactylus/sone/web/EditProfileFieldPage.java +++ b/src/main/java/net/pterodactylus/sone/web/EditProfileFieldPage.java @@ -54,7 +54,7 @@ public class EditProfileFieldPage extends SoneTemplatePage { @Override protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException { super.processTemplate(request, templateContext); - Sone currentSone = getCurrentSone(request.getToadletContext()); + Sone currentSone = getCurrentSone(request.getToadletContext()).get(); Profile profile = currentSone.getProfile(); /* get parameters from request. */ diff --git a/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java b/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java index 39c2561..28de87d 100644 --- a/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java +++ b/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java @@ -61,7 +61,7 @@ public class EditProfilePage extends SoneTemplatePage { protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException { super.processTemplate(request, templateContext); ToadletContext toadletContenxt = request.getToadletContext(); - Sone currentSone = getCurrentSone(toadletContenxt); + Sone currentSone = getCurrentSone(toadletContenxt).get(); Profile profile = currentSone.getProfile(); String firstName = profile.getFirstName(); String middleName = profile.getMiddleName(); diff --git a/src/main/java/net/pterodactylus/sone/web/FollowSonePage.java b/src/main/java/net/pterodactylus/sone/web/FollowSonePage.java index 11a680c..ca29fc9 100644 --- a/src/main/java/net/pterodactylus/sone/web/FollowSonePage.java +++ b/src/main/java/net/pterodactylus/sone/web/FollowSonePage.java @@ -54,7 +54,7 @@ public class FollowSonePage extends SoneTemplatePage { super.processTemplate(request, templateContext); if (request.getMethod() == Method.POST) { String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 256); - Sone currentSone = getCurrentSone(request.getToadletContext()); + Sone currentSone = getCurrentSone(request.getToadletContext()).get(); String soneIds = request.getHttpRequest().getPartAsStringFailsafe("sone", 1200); for (String soneId : soneIds.split("[ ,]+")) { Optional sone = webInterface.getCore().getSone(soneId); diff --git a/src/main/java/net/pterodactylus/sone/web/ImageBrowserPage.java b/src/main/java/net/pterodactylus/sone/web/ImageBrowserPage.java index 60b22d5..969160b 100644 --- a/src/main/java/net/pterodactylus/sone/web/ImageBrowserPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ImageBrowserPage.java @@ -102,7 +102,7 @@ public class ImageBrowserPage extends SoneTemplatePage { templateContext.set("albums", albumPagination.getItems()); return; } - Sone sone = getCurrentSone(request.getToadletContext(), false); + Sone sone = getCurrentSone(request.getToadletContext(), false).get(); templateContext.set("soneRequested", true); templateContext.set("sone", sone); } diff --git a/src/main/java/net/pterodactylus/sone/web/IndexPage.java b/src/main/java/net/pterodactylus/sone/web/IndexPage.java index e242f52..5e7664b 100644 --- a/src/main/java/net/pterodactylus/sone/web/IndexPage.java +++ b/src/main/java/net/pterodactylus/sone/web/IndexPage.java @@ -64,7 +64,7 @@ public class IndexPage extends SoneTemplatePage { @Override protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException { super.processTemplate(request, templateContext); - final Sone currentSone = getCurrentSone(request.getToadletContext()); + final Sone currentSone = getCurrentSone(request.getToadletContext()).get(); Collection allPosts = new ArrayList(); allPosts.addAll(currentSone.getPosts()); for (String friendSoneId : currentSone.getFriends()) { diff --git a/src/main/java/net/pterodactylus/sone/web/KnownSonesPage.java b/src/main/java/net/pterodactylus/sone/web/KnownSonesPage.java index d2e7abe..a741322 100644 --- a/src/main/java/net/pterodactylus/sone/web/KnownSonesPage.java +++ b/src/main/java/net/pterodactylus/sone/web/KnownSonesPage.java @@ -30,6 +30,7 @@ import net.pterodactylus.util.collection.Pagination; import net.pterodactylus.util.template.Template; import net.pterodactylus.util.template.TemplateContext; +import com.google.common.base.Optional; import com.google.common.base.Predicate; import com.google.common.base.Predicates; import com.google.common.collect.Collections2; @@ -73,22 +74,22 @@ public class KnownSonesPage extends SoneTemplatePage { templateContext.set("sort", sortField); templateContext.set("order", sortOrder); templateContext.set("filter", filter); - final Sone 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 != null) && "followed".equals(filter)) { + if (currentSone.isPresent() && "followed".equals(filter)) { knownSones = Collections2.filter(knownSones, new Predicate() { @Override public boolean apply(Sone sone) { - return currentSone.hasFriend(sone.getId()); + return currentSone.get().hasFriend(sone.getId()); } }); - } else if ((currentSone != null) && "not-followed".equals(filter)) { + } else if (currentSone.isPresent() && "not-followed".equals(filter)) { knownSones = Collections2.filter(knownSones, new Predicate() { @Override public boolean apply(Sone sone) { - return !currentSone.hasFriend(sone.getId()); + return !currentSone.get().hasFriend(sone.getId()); } }); } else if ("new".equals(filter)) { diff --git a/src/main/java/net/pterodactylus/sone/web/LikePage.java b/src/main/java/net/pterodactylus/sone/web/LikePage.java index 7f08e65..d6aca22 100644 --- a/src/main/java/net/pterodactylus/sone/web/LikePage.java +++ b/src/main/java/net/pterodactylus/sone/web/LikePage.java @@ -57,7 +57,7 @@ public class LikePage extends SoneTemplatePage { String type = request.getHttpRequest().getPartAsStringFailsafe("type", 16); String id = request.getHttpRequest().getPartAsStringFailsafe(type, 36); String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 256); - Sone currentSone = getCurrentSone(request.getToadletContext()); + Sone currentSone = getCurrentSone(request.getToadletContext()).get(); if ("post".equals(type)) { currentSone.addLikedPostId(id); } else if ("reply".equals(type)) { diff --git a/src/main/java/net/pterodactylus/sone/web/LoginPage.java b/src/main/java/net/pterodactylus/sone/web/LoginPage.java index 58e33a1..8d269dd 100644 --- a/src/main/java/net/pterodactylus/sone/web/LoginPage.java +++ b/src/main/java/net/pterodactylus/sone/web/LoginPage.java @@ -90,7 +90,7 @@ public class LoginPage extends SoneTemplatePage { */ @Override protected String getRedirectTarget(FreenetRequest request) { - if (getCurrentSone(request.getToadletContext(), false) != null) { + if (getCurrentSone(request.getToadletContext(), false).isPresent()) { return "index.html"; } return null; @@ -108,7 +108,7 @@ public class LoginPage extends SoneTemplatePage { if (webInterface.getCore().getPreferences().isRequireFullAccess() && !toadletContext.isAllowedFullAccess()) { return false; } - return getCurrentSone(toadletContext, false) == null; + return !getCurrentSone(toadletContext, false).isPresent(); } } diff --git a/src/main/java/net/pterodactylus/sone/web/LogoutPage.java b/src/main/java/net/pterodactylus/sone/web/LogoutPage.java index bc4c56d..e6d2980 100644 --- a/src/main/java/net/pterodactylus/sone/web/LogoutPage.java +++ b/src/main/java/net/pterodactylus/sone/web/LogoutPage.java @@ -61,7 +61,7 @@ public class LogoutPage extends SoneTemplatePage { if (webInterface.getCore().getPreferences().isRequireFullAccess() && !toadletContext.isAllowedFullAccess()) { return false; } - return (getCurrentSone(toadletContext, false) != null) && (webInterface.getCore().getLocalSones().size() != 1); + return getCurrentSone(toadletContext, false).isPresent() && (webInterface.getCore().getLocalSones().size() != 1); } } diff --git a/src/main/java/net/pterodactylus/sone/web/NewPage.java b/src/main/java/net/pterodactylus/sone/web/NewPage.java index 5977349..a79eefa 100644 --- a/src/main/java/net/pterodactylus/sone/web/NewPage.java +++ b/src/main/java/net/pterodactylus/sone/web/NewPage.java @@ -74,7 +74,7 @@ public class NewPage extends SoneTemplatePage { } /* filter and sort them. */ - List sortedPosts = ListNotificationFilters.filterPosts(new ArrayList(posts), webInterface.getCurrentSone(request.getToadletContext(), false)); + List sortedPosts = ListNotificationFilters.filterPosts(new ArrayList(posts), webInterface.getCurrentSone(request.getToadletContext(), false).orNull()); Collections.sort(sortedPosts, Post.TIME_COMPARATOR); /* paginate them. */ diff --git a/src/main/java/net/pterodactylus/sone/web/OptionsPage.java b/src/main/java/net/pterodactylus/sone/web/OptionsPage.java index c38f97b..cbc9686 100644 --- a/src/main/java/net/pterodactylus/sone/web/OptionsPage.java +++ b/src/main/java/net/pterodactylus/sone/web/OptionsPage.java @@ -31,6 +31,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 edit the options of the Sone plugin. * @@ -61,22 +63,22 @@ public class OptionsPage extends SoneTemplatePage { protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException { super.processTemplate(request, templateContext); Preferences preferences = webInterface.getCore().getPreferences(); - Sone currentSone = webInterface.getCurrentSone(request.getToadletContext(), false); + Optional currentSone = webInterface.getCurrentSone(request.getToadletContext(), false); if (request.getMethod() == Method.POST) { List fieldErrors = new ArrayList(); - if (currentSone != null) { + if (currentSone.isPresent()) { boolean autoFollow = request.getHttpRequest().isPartSet("auto-follow"); - currentSone.getOptions().setAutoFollow(autoFollow); + currentSone.get().getOptions().setAutoFollow(autoFollow); boolean enableSoneInsertNotifications = request.getHttpRequest().isPartSet("enable-sone-insert-notifications"); - currentSone.getOptions().setSoneInsertNotificationEnabled(enableSoneInsertNotifications); + currentSone.get().getOptions().setSoneInsertNotificationEnabled(enableSoneInsertNotifications); boolean showNotificationNewSones = request.getHttpRequest().isPartSet("show-notification-new-sones"); - currentSone.getOptions().setShowNewSoneNotifications(showNotificationNewSones); + currentSone.get().getOptions().setShowNewSoneNotifications(showNotificationNewSones); boolean showNotificationNewPosts = request.getHttpRequest().isPartSet("show-notification-new-posts"); - currentSone.getOptions().setShowNewPostNotifications(showNotificationNewPosts); + currentSone.get().getOptions().setShowNewPostNotifications(showNotificationNewPosts); boolean showNotificationNewReplies = request.getHttpRequest().isPartSet("show-notification-new-replies"); - currentSone.getOptions().setShowNewReplyNotifications(showNotificationNewReplies); + currentSone.get().getOptions().setShowNewReplyNotifications(showNotificationNewReplies); String showCustomAvatars = request.getHttpRequest().getPartAsStringFailsafe("show-custom-avatars", 32); - currentSone.getOptions().setShowCustomAvatars(ShowCustomAvatars.valueOf(showCustomAvatars)); + currentSone.get().getOptions().setShowCustomAvatars(ShowCustomAvatars.valueOf(showCustomAvatars)); webInterface.getCore().touchConfiguration(); } Integer insertionDelay = parseInt(request.getHttpRequest().getPartAsStringFailsafe("insertion-delay", 16), null); @@ -139,13 +141,13 @@ public class OptionsPage extends SoneTemplatePage { } templateContext.set("fieldErrors", fieldErrors); } - if (currentSone != null) { - templateContext.set("auto-follow", currentSone.getOptions().isAutoFollow()); - templateContext.set("enable-sone-insert-notifications", currentSone.getOptions().isSoneInsertNotificationEnabled()); - templateContext.set("show-notification-new-sones", currentSone.getOptions().isShowNewSoneNotifications()); - templateContext.set("show-notification-new-posts", currentSone.getOptions().isShowNewPostNotifications()); - templateContext.set("show-notification-new-replies", currentSone.getOptions().isShowNewReplyNotifications()); - templateContext.set("show-custom-avatars", currentSone.getOptions().getShowCustomAvatars().name()); + if (currentSone.isPresent()) { + templateContext.set("auto-follow", currentSone.get().getOptions().isAutoFollow()); + templateContext.set("enable-sone-insert-notifications", currentSone.get().getOptions().isSoneInsertNotificationEnabled()); + templateContext.set("show-notification-new-sones", currentSone.get().getOptions().isShowNewSoneNotifications()); + templateContext.set("show-notification-new-posts", currentSone.get().getOptions().isShowNewPostNotifications()); + templateContext.set("show-notification-new-replies", currentSone.get().getOptions().isShowNewReplyNotifications()); + templateContext.set("show-custom-avatars", currentSone.get().getOptions().getShowCustomAvatars().name()); } templateContext.set("insertion-delay", preferences.getInsertionDelay()); templateContext.set("posts-per-page", preferences.getPostsPerPage()); diff --git a/src/main/java/net/pterodactylus/sone/web/RescuePage.java b/src/main/java/net/pterodactylus/sone/web/RescuePage.java index 8c325d2..569aaaa 100644 --- a/src/main/java/net/pterodactylus/sone/web/RescuePage.java +++ b/src/main/java/net/pterodactylus/sone/web/RescuePage.java @@ -56,7 +56,7 @@ public class RescuePage extends SoneTemplatePage { @Override protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException { super.processTemplate(request, templateContext); - Sone currentSone = getCurrentSone(request.getToadletContext(), false); + Sone currentSone = getCurrentSone(request.getToadletContext(), false).get(); SoneRescuer soneRescuer = webInterface.getCore().getSoneRescuer(currentSone); if (request.getMethod() == Method.POST) { if ("true".equals(request.getHttpRequest().getPartAsStringFailsafe("fetch", 4))) { diff --git a/src/main/java/net/pterodactylus/sone/web/SoneTemplatePage.java b/src/main/java/net/pterodactylus/sone/web/SoneTemplatePage.java index e18e1a4..fa2798e 100644 --- a/src/main/java/net/pterodactylus/sone/web/SoneTemplatePage.java +++ b/src/main/java/net/pterodactylus/sone/web/SoneTemplatePage.java @@ -34,6 +34,7 @@ import net.pterodactylus.util.notify.Notification; import net.pterodactylus.util.template.Template; import net.pterodactylus.util.template.TemplateContext; +import com.google.common.base.Optional; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; @@ -137,7 +138,7 @@ public class SoneTemplatePage extends FreenetTemplatePage { * @return The currently logged in Sone, or {@code null} if no Sone is * currently logged in */ - protected Sone getCurrentSone(ToadletContext toadletContext) { + protected Optional getCurrentSone(ToadletContext toadletContext) { return webInterface.getCurrentSone(toadletContext); } @@ -152,7 +153,7 @@ public class SoneTemplatePage extends FreenetTemplatePage { * @return The currently logged in Sone, or {@code null} if no Sone is * currently logged in */ - protected Sone getCurrentSone(ToadletContext toadletContext, boolean create) { + protected Optional getCurrentSone(ToadletContext toadletContext, boolean create) { return webInterface.getCurrentSone(toadletContext, create); } @@ -223,9 +224,9 @@ public class SoneTemplatePage extends FreenetTemplatePage { @Override protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException { super.processTemplate(request, templateContext); - Sone currentSone = getCurrentSone(request.getToadletContext(), false); + Optional currentSone = getCurrentSone(request.getToadletContext(), false); templateContext.set("core", webInterface.getCore()); - templateContext.set("currentSone", currentSone); + templateContext.set("currentSone", currentSone.orNull()); templateContext.set("localSones", webInterface.getCore().getLocalSones()); templateContext.set("request", request); templateContext.set("currentVersion", SonePlugin.VERSION); @@ -233,7 +234,7 @@ public class SoneTemplatePage extends FreenetTemplatePage { templateContext.set("latestEdition", webInterface.getCore().getUpdateChecker().getLatestEdition()); templateContext.set("latestVersion", webInterface.getCore().getUpdateChecker().getLatestVersion()); templateContext.set("latestVersionTime", webInterface.getCore().getUpdateChecker().getLatestVersionDate()); - List notifications = ListNotificationFilters.filterNotifications(webInterface.getNotifications().getNotifications(), currentSone); + List notifications = ListNotificationFilters.filterNotifications(webInterface.getNotifications().getNotifications(), currentSone.orNull()); Collections.sort(notifications, Notification.CREATED_TIME_SORTER); templateContext.set("notifications", notifications); templateContext.set("notificationHash", notifications.hashCode()); @@ -244,7 +245,7 @@ public class SoneTemplatePage extends FreenetTemplatePage { */ @Override protected String getRedirectTarget(FreenetRequest request) { - if (requiresLogin() && (getCurrentSone(request.getToadletContext(), false) == null)) { + if (requiresLogin() && !getCurrentSone(request.getToadletContext(), false).isPresent()) { HTTPRequest httpRequest = request.getHttpRequest(); String originalUrl = httpRequest.getPath(); if (httpRequest.hasParameters()) { @@ -286,7 +287,7 @@ public class SoneTemplatePage extends FreenetTemplatePage { return false; } if (requiresLogin()) { - return getCurrentSone(toadletContext, false) != null; + return getCurrentSone(toadletContext, false).isPresent(); } return true; } diff --git a/src/main/java/net/pterodactylus/sone/web/TrustPage.java b/src/main/java/net/pterodactylus/sone/web/TrustPage.java index 39fd1de..807e771 100644 --- a/src/main/java/net/pterodactylus/sone/web/TrustPage.java +++ b/src/main/java/net/pterodactylus/sone/web/TrustPage.java @@ -60,7 +60,7 @@ public class TrustPage extends SoneTemplatePage { if (request.getMethod() == Method.POST) { String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 256); String identity = request.getHttpRequest().getPartAsStringFailsafe("sone", 44); - Sone currentSone = getCurrentSone(request.getToadletContext()); + Sone currentSone = getCurrentSone(request.getToadletContext()).get(); Optional sone = webInterface.getCore().getSone(identity); if (sone.isPresent()) { webInterface.getCore().trustSone(currentSone, sone.get()); diff --git a/src/main/java/net/pterodactylus/sone/web/UnfollowSonePage.java b/src/main/java/net/pterodactylus/sone/web/UnfollowSonePage.java index 4813239..bddfe23 100644 --- a/src/main/java/net/pterodactylus/sone/web/UnfollowSonePage.java +++ b/src/main/java/net/pterodactylus/sone/web/UnfollowSonePage.java @@ -52,7 +52,7 @@ public class UnfollowSonePage extends SoneTemplatePage { super.processTemplate(request, templateContext); if (request.getMethod() == Method.POST) { String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 256); - Sone currentSone = getCurrentSone(request.getToadletContext()); + Sone currentSone = getCurrentSone(request.getToadletContext()).get(); String soneIds = request.getHttpRequest().getPartAsStringFailsafe("sone", 2000); for (String soneId : soneIds.split("[ ,]+")) { webInterface.getCore().unfollowSone(currentSone, soneId); diff --git a/src/main/java/net/pterodactylus/sone/web/UnlikePage.java b/src/main/java/net/pterodactylus/sone/web/UnlikePage.java index 9254a42..3ba2e4a 100644 --- a/src/main/java/net/pterodactylus/sone/web/UnlikePage.java +++ b/src/main/java/net/pterodactylus/sone/web/UnlikePage.java @@ -57,7 +57,7 @@ public class UnlikePage extends SoneTemplatePage { String type = request.getHttpRequest().getPartAsStringFailsafe("type", 16); String id = request.getHttpRequest().getPartAsStringFailsafe(type, 36); String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 256); - Sone currentSone = getCurrentSone(request.getToadletContext()); + Sone currentSone = getCurrentSone(request.getToadletContext()).get(); if ("post".equals(type)) { currentSone.removeLikedPostId(id); } else if ("reply".equals(type)) { diff --git a/src/main/java/net/pterodactylus/sone/web/UntrustPage.java b/src/main/java/net/pterodactylus/sone/web/UntrustPage.java index d68d5ca..016359b 100644 --- a/src/main/java/net/pterodactylus/sone/web/UntrustPage.java +++ b/src/main/java/net/pterodactylus/sone/web/UntrustPage.java @@ -60,7 +60,7 @@ public class UntrustPage extends SoneTemplatePage { if (request.getMethod() == Method.POST) { String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 256); String identity = request.getHttpRequest().getPartAsStringFailsafe("sone", 44); - Sone currentSone = getCurrentSone(request.getToadletContext()); + Sone currentSone = getCurrentSone(request.getToadletContext()).get(); Optional sone = webInterface.getCore().getSone(identity); if (sone.isPresent()) { webInterface.getCore().untrustSone(currentSone, sone.get()); diff --git a/src/main/java/net/pterodactylus/sone/web/UploadImagePage.java b/src/main/java/net/pterodactylus/sone/web/UploadImagePage.java index 5e23a78..27e9cc8 100644 --- a/src/main/java/net/pterodactylus/sone/web/UploadImagePage.java +++ b/src/main/java/net/pterodactylus/sone/web/UploadImagePage.java @@ -81,7 +81,7 @@ public class UploadImagePage extends SoneTemplatePage { protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException { super.processTemplate(request, templateContext); if (request.getMethod() == Method.POST) { - Sone currentSone = getCurrentSone(request.getToadletContext()); + Sone currentSone = getCurrentSone(request.getToadletContext()).get(); String parentId = request.getHttpRequest().getPartAsStringFailsafe("parent", 36); Album parent = webInterface.getCore().getAlbum(parentId); if (parent == null) { diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index 02a5dd5..258669a 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -381,7 +381,7 @@ public class WebInterface { * @return The currently logged in Sone, or {@code null} if no Sone is * currently logged in */ - public Sone getCurrentSone(ToadletContext toadletContext) { + public Optional getCurrentSone(ToadletContext toadletContext) { return getCurrentSone(toadletContext, true); } @@ -396,10 +396,10 @@ public class WebInterface { * @return The currently logged in Sone, or {@code null} if no Sone is * currently logged in */ - public Sone getCurrentSone(ToadletContext toadletContext, boolean createSession) { + public Optional getCurrentSone(ToadletContext toadletContext, boolean createSession) { Collection localSones = getCore().getLocalSones(); if (localSones.size() == 1) { - return localSones.iterator().next(); + return Optional.of(localSones.iterator().next()); } return getCurrentSone(getCurrentSession(toadletContext, createSession)); } @@ -412,15 +412,15 @@ public class WebInterface { * @return The currently logged in Sone, or {@code null} if no Sone is * currently logged in */ - public Sone getCurrentSone(Optional session) { + public Optional getCurrentSone(Optional session) { if (!session.isPresent()) { - return null; + return Optional.absent(); } String soneId = (String) session.get().getAttribute("Sone.CurrentSone"); if (soneId == null) { - return null; + return Optional.absent(); } - return getCore().getLocalSone(soneId); + return Optional.fromNullable(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 5cb1047..71a76a6 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/CreatePostAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/CreatePostAjaxPage.java @@ -47,10 +47,7 @@ public class CreatePostAjaxPage extends JsonPage { */ @Override protected JsonReturnObject createJsonObject(FreenetRequest request) { - Sone sone = getCurrentSone(request.getToadletContext()); - if (sone == null) { - return createErrorJsonObject("auth-required"); - } + Sone sone = getCurrentSone(request.getToadletContext()).get(); String recipientId = request.getHttpRequest().getParam("recipient"); Optional recipient = webInterface.getCore().getSone(recipientId); String senderId = request.getHttpRequest().getParam("sender"); 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 3449dec..8c0387e 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/CreateReplyAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/CreateReplyAjaxPage.java @@ -57,7 +57,7 @@ public class CreateReplyAjaxPage extends JsonPage { String senderId = request.getHttpRequest().getParam("sender"); Sone sender = webInterface.getCore().getLocalSone(senderId); if (sender == null) { - sender = getCurrentSone(request.getToadletContext()); + sender = getCurrentSone(request.getToadletContext()).get(); } Optional post = webInterface.getCore().getPost(postId); if (!post.isPresent()) { diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/DeleteProfileFieldAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/DeleteProfileFieldAjaxPage.java index cb33fb9..97393f2 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/DeleteProfileFieldAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/DeleteProfileFieldAjaxPage.java @@ -51,7 +51,7 @@ public class DeleteProfileFieldAjaxPage extends JsonPage { @Override protected JsonReturnObject createJsonObject(FreenetRequest request) { String fieldId = request.getHttpRequest().getParam("field"); - Sone currentSone = getCurrentSone(request.getToadletContext()); + Sone currentSone = getCurrentSone(request.getToadletContext()).get(); Profile profile = currentSone.getProfile(); Field field = profile.getFieldById(fieldId); if (field == null) { diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/DistrustAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/DistrustAjaxPage.java index 4206e8f..8dfe085 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/DistrustAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/DistrustAjaxPage.java @@ -47,10 +47,7 @@ public class DistrustAjaxPage extends JsonPage { */ @Override protected JsonReturnObject createJsonObject(FreenetRequest request) { - Sone currentSone = getCurrentSone(request.getToadletContext(), false); - if (currentSone == null) { - return createErrorJsonObject("auth-required"); - } + Sone currentSone = getCurrentSone(request.getToadletContext(), false).get(); String soneId = request.getHttpRequest().getParam("sone"); Optional sone = webInterface.getCore().getSone(soneId); if (!sone.isPresent()) { diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/EditProfileFieldAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/EditProfileFieldAjaxPage.java index b4596ad..d32f973 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/EditProfileFieldAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/EditProfileFieldAjaxPage.java @@ -50,7 +50,7 @@ public class EditProfileFieldAjaxPage extends JsonPage { @Override protected JsonReturnObject createJsonObject(FreenetRequest request) { String fieldId = request.getHttpRequest().getParam("field"); - Sone currentSone = getCurrentSone(request.getToadletContext()); + Sone currentSone = getCurrentSone(request.getToadletContext()).get(); Profile profile = currentSone.getProfile(); Field field = profile.getFieldById(fieldId); if (field == null) { diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/FollowSoneAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/FollowSoneAjaxPage.java index 64c402a..3b51df2 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/FollowSoneAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/FollowSoneAjaxPage.java @@ -50,10 +50,7 @@ public class FollowSoneAjaxPage extends JsonPage { if (!sone.isPresent()) { return createErrorJsonObject("invalid-sone-id"); } - Sone currentSone = getCurrentSone(request.getToadletContext()); - if (currentSone == null) { - return createErrorJsonObject("auth-required"); - } + Sone currentSone = getCurrentSone(request.getToadletContext()).get(); webInterface.getCore().followSone(currentSone, soneId); webInterface.getCore().markSoneKnown(sone.get()); return createSuccessJsonObject(); 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 a25e665..d86e5a8 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPage.java @@ -37,6 +37,7 @@ import net.pterodactylus.util.template.TemplateContext; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; +import com.google.common.base.Optional; /** * AJAX handler to return all current notifications. @@ -80,9 +81,9 @@ public class GetNotificationsAjaxPage extends JsonPage { */ @Override protected JsonReturnObject createJsonObject(FreenetRequest request) { - Sone currentSone = getCurrentSone(request.getToadletContext(), false); + Optional currentSone = getCurrentSone(request.getToadletContext(), false); Collection notifications = webInterface.getNotifications().getNotifications(); - List filteredNotifications = ListNotificationFilters.filterNotifications(notifications, currentSone); + List filteredNotifications = ListNotificationFilters.filterNotifications(notifications, currentSone.orNull()); Collections.sort(filteredNotifications, Notification.CREATED_TIME_SORTER); ArrayNode jsonNotifications = new ArrayNode(instance); for (Notification notification : filteredNotifications) { @@ -144,12 +145,12 @@ public class GetNotificationsAjaxPage extends JsonPage { * The current Sone (may be {@code null}) * @return The current options */ - private static JsonNode createJsonOptions(Sone currentSone) { + private static JsonNode createJsonOptions(Optional currentSone) { ObjectNode options = new ObjectNode(instance); - if (currentSone != null) { - options.put("ShowNotification/NewSones", currentSone.getOptions().isShowNewSoneNotifications()); - options.put("ShowNotification/NewPosts", currentSone.getOptions().isShowNewPostNotifications()); - options.put("ShowNotification/NewReplies", currentSone.getOptions().isShowNewReplyNotifications()); + if (currentSone.isPresent()) { + options.put("ShowNotification/NewSones", currentSone.get().getOptions().isShowNewSoneNotifications()); + options.put("ShowNotification/NewPosts", currentSone.get().getOptions().isShowNewPostNotifications()); + options.put("ShowNotification/NewReplies", currentSone.get().getOptions().isShowNewReplyNotifications()); } return options; } 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 96002b3..51ab3ee 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/GetPostAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/GetPostAjaxPage.java @@ -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, Sone 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()); @@ -106,7 +106,7 @@ public class GetPostAjaxPage extends JsonPage { templateContext.set("core", webInterface.getCore()); templateContext.set("request", request); templateContext.set("post", post); - templateContext.set("currentSone", currentSone); + templateContext.set("currentSone", currentSone.orNull()); templateContext.set("localSones", webInterface.getCore().getLocalSones()); try { postTemplate.render(templateContext, stringWriter); 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 251c784..d0b79ac 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/GetReplyAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/GetReplyAjaxPage.java @@ -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, Sone 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()); @@ -108,7 +108,7 @@ public class GetReplyAjaxPage extends JsonPage { templateContext.set("core", webInterface.getCore()); templateContext.set("request", request); templateContext.set("reply", reply); - templateContext.set("currentSone", currentSone); + templateContext.set("currentSone", currentSone.orNull()); try { replyTemplate.render(templateContext, stringWriter); } catch (TemplateException te1) { 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 cc095a2..89a3858 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.java @@ -40,6 +40,7 @@ import net.pterodactylus.util.notify.Notification; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; +import com.google.common.base.Optional; import com.google.common.base.Predicate; import com.google.common.collect.Collections2; @@ -69,9 +70,9 @@ public class GetStatusAjaxPage extends JsonPage { */ @Override protected JsonReturnObject createJsonObject(FreenetRequest request) { - final Sone 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(Collections.singleton(getCurrentSone(request.getToadletContext(), false))); + Set sones = new HashSet(currentSone.asSet()); String loadSoneIds = request.getHttpRequest().getParam("soneIds"); if (loadSoneIds.length() > 0) { String[] soneIds = loadSoneIds.split(","); @@ -88,16 +89,16 @@ public class GetStatusAjaxPage extends JsonPage { jsonSones.add(createJsonSone(sone)); } /* load notifications. */ - List notifications = ListNotificationFilters.filterNotifications(webInterface.getNotifications().getNotifications(), currentSone); + List notifications = ListNotificationFilters.filterNotifications(webInterface.getNotifications().getNotifications(), currentSone.orNull()); Collections.sort(notifications, Notification.CREATED_TIME_SORTER); /* load new posts. */ Collection newPosts = webInterface.getNewPosts(); - if (currentSone != null) { + if (currentSone.isPresent()) { newPosts = Collections2.filter(newPosts, new Predicate() { @Override public boolean apply(Post post) { - return ListNotificationFilters.isPostVisible(currentSone, post); + return ListNotificationFilters.isPostVisible(currentSone.get(), post); } }); @@ -113,12 +114,12 @@ public class GetStatusAjaxPage extends JsonPage { } /* load new replies. */ Collection newReplies = webInterface.getNewReplies(); - if (currentSone != null) { + if (currentSone.isPresent()) { newReplies = Collections2.filter(newReplies, new Predicate() { @Override public boolean apply(PostReply reply) { - return ListNotificationFilters.isReplyVisible(currentSone, reply); + return ListNotificationFilters.isReplyVisible(currentSone.get(), reply); } }); @@ -134,7 +135,7 @@ public class GetStatusAjaxPage extends JsonPage { jsonReply.put("postSone", reply.getPost().get().getSone().getId()); jsonReplies.add(jsonReply); } - return createSuccessJsonObject().put("loggedIn", currentSone != null).put("options", createJsonOptions(currentSone)).put("sones", jsonSones).put("notificationHash", notifications.hashCode()).put("newPosts", jsonPosts).put("newReplies", jsonReplies); + return createSuccessJsonObject().put("loggedIn", currentSone.isPresent()).put("options", createJsonOptions(currentSone)).put("sones", jsonSones).put("notificationHash", notifications.hashCode()).put("newPosts", jsonPosts).put("newReplies", jsonReplies); } /** @@ -189,12 +190,12 @@ public class GetStatusAjaxPage extends JsonPage { * The current Sone (may be {@code null}) * @return The current options */ - private static JsonNode createJsonOptions(Sone currentSone) { + private static JsonNode createJsonOptions(Optional currentSone) { ObjectNode options = new ObjectNode(instance); - if (currentSone != null) { - options.put("ShowNotification/NewSones", currentSone.getOptions().isShowNewSoneNotifications()); - options.put("ShowNotification/NewPosts", currentSone.getOptions().isShowNewPostNotifications()); - options.put("ShowNotification/NewReplies", currentSone.getOptions().isShowNewReplyNotifications()); + if (currentSone.isPresent()) { + options.put("ShowNotification/NewSones", currentSone.get().getOptions().isShowNewSoneNotifications()); + options.put("ShowNotification/NewPosts", currentSone.get().getOptions().isShowNewPostNotifications()); + options.put("ShowNotification/NewReplies", currentSone.get().getOptions().isShowNewReplyNotifications()); } return options; } 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 d11cc0d..bb32362 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/JsonPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/JsonPage.java @@ -36,6 +36,7 @@ import net.pterodactylus.util.web.Page; import net.pterodactylus.util.web.Response; import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.base.Optional; import freenet.clients.http.ToadletContext; @@ -84,7 +85,7 @@ public abstract class JsonPage implements FreenetPage { * @return The currently logged in Sone, or {@code null} if no Sone is * currently logged in */ - protected Sone getCurrentSone(ToadletContext toadletContext) { + protected Optional getCurrentSone(ToadletContext toadletContext) { return webInterface.getCurrentSone(toadletContext); } @@ -99,7 +100,7 @@ public abstract class JsonPage implements FreenetPage { * @return The currently logged in Sone, or {@code null} if no Sone is * currently logged in */ - protected Sone getCurrentSone(ToadletContext toadletContext, boolean create) { + protected Optional getCurrentSone(ToadletContext toadletContext, boolean create) { return webInterface.getCurrentSone(toadletContext, create); } @@ -199,7 +200,7 @@ public abstract class JsonPage implements FreenetPage { } } if (requiresLogin()) { - if (getCurrentSone(request.getToadletContext(), false) == null) { + if (!getCurrentSone(request.getToadletContext(), false).isPresent()) { return response.setStatusCode(403).setStatusText("Forbidden").setContentType("application/json").write(objectMapper.writeValueAsString(new JsonErrorReturnObject("auth-required"))); } } diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/LikeAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/LikeAjaxPage.java index 55a947a..e7574a5 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/LikeAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/LikeAjaxPage.java @@ -49,7 +49,7 @@ public class LikeAjaxPage extends JsonPage { if ((id == null) || (id.length() == 0)) { return createErrorJsonObject("invalid-" + type + "-id"); } - Sone currentSone = getCurrentSone(request.getToadletContext()); + Sone currentSone = getCurrentSone(request.getToadletContext()).get(); if (currentSone == null) { return createErrorJsonObject("auth-required"); } diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/MoveProfileFieldAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/MoveProfileFieldAjaxPage.java index d8781ba..a577495 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/MoveProfileFieldAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/MoveProfileFieldAjaxPage.java @@ -51,7 +51,7 @@ public class MoveProfileFieldAjaxPage extends JsonPage { */ @Override protected JsonReturnObject createJsonObject(FreenetRequest request) { - Sone currentSone = getCurrentSone(request.getToadletContext()); + Sone currentSone = getCurrentSone(request.getToadletContext()).get(); Profile profile = currentSone.getProfile(); String fieldId = request.getHttpRequest().getParam("field"); Field field = profile.getFieldById(fieldId); diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/TrustAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/TrustAjaxPage.java index 3661cb1..1225de1 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/TrustAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/TrustAjaxPage.java @@ -47,10 +47,7 @@ public class TrustAjaxPage extends JsonPage { */ @Override protected JsonReturnObject createJsonObject(FreenetRequest request) { - Sone currentSone = getCurrentSone(request.getToadletContext(), false); - if (currentSone == null) { - return createErrorJsonObject("auth-required"); - } + Sone currentSone = getCurrentSone(request.getToadletContext(), false).get(); String soneId = request.getHttpRequest().getParam("sone"); Optional sone = webInterface.getCore().getSone(soneId); if (!sone.isPresent()) { diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/UnfollowSoneAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/UnfollowSoneAjaxPage.java index a285d20..1e87b15 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/UnfollowSoneAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/UnfollowSoneAjaxPage.java @@ -47,7 +47,7 @@ public class UnfollowSoneAjaxPage extends JsonPage { if (!webInterface.getCore().getSone(soneId).isPresent()) { return createErrorJsonObject("invalid-sone-id"); } - Sone currentSone = getCurrentSone(request.getToadletContext()); + Sone currentSone = getCurrentSone(request.getToadletContext()).get(); if (currentSone == null) { return createErrorJsonObject("auth-required"); } diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/UnlikeAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/UnlikeAjaxPage.java index 17be966..b1ab9e0 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/UnlikeAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/UnlikeAjaxPage.java @@ -49,10 +49,7 @@ public class UnlikeAjaxPage extends JsonPage { if ((id == null) || (id.length() == 0)) { return createErrorJsonObject("invalid-" + type + "-id"); } - Sone currentSone = getCurrentSone(request.getToadletContext()); - if (currentSone == null) { - return createErrorJsonObject("auth-required"); - } + Sone currentSone = getCurrentSone(request.getToadletContext()).get(); if ("post".equals(type)) { currentSone.removeLikedPostId(id); webInterface.getCore().touchConfiguration(); diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/UntrustAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/UntrustAjaxPage.java index 229f219..993bbe3 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/UntrustAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/UntrustAjaxPage.java @@ -47,10 +47,7 @@ public class UntrustAjaxPage extends JsonPage { */ @Override protected JsonReturnObject createJsonObject(FreenetRequest request) { - Sone currentSone = getCurrentSone(request.getToadletContext(), false); - if (currentSone == null) { - return createErrorJsonObject("auth-required"); - } + Sone currentSone = getCurrentSone(request.getToadletContext(), false).get(); String soneId = request.getHttpRequest().getParam("sone"); Optional sone = webInterface.getCore().getSone(soneId); if (!sone.isPresent()) { -- 2.7.4