Return an optional Sone from the current session.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 2 Dec 2014 20:31:06 +0000 (21:31 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 2 Dec 2014 20:31:06 +0000 (21:31 +0100)
43 files changed:
src/main/java/net/pterodactylus/sone/web/CreateAlbumPage.java
src/main/java/net/pterodactylus/sone/web/CreatePostPage.java
src/main/java/net/pterodactylus/sone/web/CreateReplyPage.java
src/main/java/net/pterodactylus/sone/web/CreateSonePage.java
src/main/java/net/pterodactylus/sone/web/DeleteProfileFieldPage.java
src/main/java/net/pterodactylus/sone/web/DeleteSonePage.java
src/main/java/net/pterodactylus/sone/web/DistrustPage.java
src/main/java/net/pterodactylus/sone/web/EditProfileFieldPage.java
src/main/java/net/pterodactylus/sone/web/EditProfilePage.java
src/main/java/net/pterodactylus/sone/web/FollowSonePage.java
src/main/java/net/pterodactylus/sone/web/ImageBrowserPage.java
src/main/java/net/pterodactylus/sone/web/IndexPage.java
src/main/java/net/pterodactylus/sone/web/KnownSonesPage.java
src/main/java/net/pterodactylus/sone/web/LikePage.java
src/main/java/net/pterodactylus/sone/web/LoginPage.java
src/main/java/net/pterodactylus/sone/web/LogoutPage.java
src/main/java/net/pterodactylus/sone/web/NewPage.java
src/main/java/net/pterodactylus/sone/web/OptionsPage.java
src/main/java/net/pterodactylus/sone/web/RescuePage.java
src/main/java/net/pterodactylus/sone/web/SoneTemplatePage.java
src/main/java/net/pterodactylus/sone/web/TrustPage.java
src/main/java/net/pterodactylus/sone/web/UnfollowSonePage.java
src/main/java/net/pterodactylus/sone/web/UnlikePage.java
src/main/java/net/pterodactylus/sone/web/UntrustPage.java
src/main/java/net/pterodactylus/sone/web/UploadImagePage.java
src/main/java/net/pterodactylus/sone/web/WebInterface.java
src/main/java/net/pterodactylus/sone/web/ajax/CreatePostAjaxPage.java
src/main/java/net/pterodactylus/sone/web/ajax/CreateReplyAjaxPage.java
src/main/java/net/pterodactylus/sone/web/ajax/DeleteProfileFieldAjaxPage.java
src/main/java/net/pterodactylus/sone/web/ajax/DistrustAjaxPage.java
src/main/java/net/pterodactylus/sone/web/ajax/EditProfileFieldAjaxPage.java
src/main/java/net/pterodactylus/sone/web/ajax/FollowSoneAjaxPage.java
src/main/java/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPage.java
src/main/java/net/pterodactylus/sone/web/ajax/GetPostAjaxPage.java
src/main/java/net/pterodactylus/sone/web/ajax/GetReplyAjaxPage.java
src/main/java/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.java
src/main/java/net/pterodactylus/sone/web/ajax/JsonPage.java
src/main/java/net/pterodactylus/sone/web/ajax/LikeAjaxPage.java
src/main/java/net/pterodactylus/sone/web/ajax/MoveProfileFieldAjaxPage.java
src/main/java/net/pterodactylus/sone/web/ajax/TrustAjaxPage.java
src/main/java/net/pterodactylus/sone/web/ajax/UnfollowSoneAjaxPage.java
src/main/java/net/pterodactylus/sone/web/ajax/UnlikeAjaxPage.java
src/main/java/net/pterodactylus/sone/web/ajax/UntrustAjaxPage.java

index 1c599e4..a8ee678 100644 (file)
@@ -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("")) {
index 6530a5b..0590f72 100644 (file)
@@ -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;
index 55903d8..4158313 100644 (file)
@@ -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);
index b2d4578..5c87ccb 100644 (file)
@@ -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);
        }
 
 }
index 852cc91..d6ba0dd 100644 (file)
@@ -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. */
index 39d3fb3..aa824fb 100644 (file)
@@ -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");
index c8879a0..e0ec3fb 100644 (file)
@@ -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> sone = webInterface.getCore().getSone(identity);
                        if (sone.isPresent()) {
                                webInterface.getCore().distrustSone(currentSone, sone.get());
index fe9d78f..37ae6c8 100644 (file)
@@ -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. */
index 39c2561..28de87d 100644 (file)
@@ -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();
index 11a680c..ca29fc9 100644 (file)
@@ -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> sone = webInterface.getCore().getSone(soneId);
index 60b22d5..969160b 100644 (file)
@@ -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);
        }
index e242f52..5e7664b 100644 (file)
@@ -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<Post> allPosts = new ArrayList<Post>();
                allPosts.addAll(currentSone.getPosts());
                for (String friendSoneId : currentSone.getFriends()) {
index d2e7abe..a741322 100644 (file)
@@ -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<Sone> currentSone = getCurrentSone(request.getToadletContext(), false);
                Collection<Sone> 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<Sone>() {
 
                                @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<Sone>() {
 
                                @Override
                                public boolean apply(Sone sone) {
-                                       return !currentSone.hasFriend(sone.getId());
+                                       return !currentSone.get().hasFriend(sone.getId());
                                }
                        });
                } else if ("new".equals(filter)) {
index 7f08e65..d6aca22 100644 (file)
@@ -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)) {
index 58e33a1..8d269dd 100644 (file)
@@ -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();
        }
 
 }
index bc4c56d..e6d2980 100644 (file)
@@ -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);
        }
 
 }
index 5977349..a79eefa 100644 (file)
@@ -74,7 +74,7 @@ public class NewPage extends SoneTemplatePage {
                }
 
                /* filter and sort them. */
-               List<Post> sortedPosts = ListNotificationFilters.filterPosts(new ArrayList<Post>(posts), webInterface.getCurrentSone(request.getToadletContext(), false));
+               List<Post> sortedPosts = ListNotificationFilters.filterPosts(new ArrayList<Post>(posts), webInterface.getCurrentSone(request.getToadletContext(), false).orNull());
                Collections.sort(sortedPosts, Post.TIME_COMPARATOR);
 
                /* paginate them. */
index c38f97b..cbc9686 100644 (file)
@@ -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<Sone> currentSone = webInterface.getCurrentSone(request.getToadletContext(), false);
                if (request.getMethod() == Method.POST) {
                        List<String> fieldErrors = new ArrayList<String>();
-                       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());
index 8c325d2..569aaaa 100644 (file)
@@ -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))) {
index e18e1a4..fa2798e 100644 (file)
@@ -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<Sone> 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<Sone> 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<Sone> 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<Notification> notifications = ListNotificationFilters.filterNotifications(webInterface.getNotifications().getNotifications(), currentSone);
+               List<Notification> 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;
        }
index 39fd1de..807e771 100644 (file)
@@ -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> sone = webInterface.getCore().getSone(identity);
                        if (sone.isPresent()) {
                                webInterface.getCore().trustSone(currentSone, sone.get());
index 4813239..bddfe23 100644 (file)
@@ -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);
index 9254a42..3ba2e4a 100644 (file)
@@ -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)) {
index d68d5ca..016359b 100644 (file)
@@ -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> sone = webInterface.getCore().getSone(identity);
                        if (sone.isPresent()) {
                                webInterface.getCore().untrustSone(currentSone, sone.get());
index 5e23a78..27e9cc8 100644 (file)
@@ -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) {
index 02a5dd5..258669a 100644 (file)
@@ -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<Sone> 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<Sone> getCurrentSone(ToadletContext toadletContext, boolean createSession) {
                Collection<Sone> 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> session) {
+       public Optional<Sone> getCurrentSone(Optional<Session> 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));
        }
 
        /**
index 5cb1047..71a76a6 100644 (file)
@@ -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<Sone> recipient = webInterface.getCore().getSone(recipientId);
                String senderId = request.getHttpRequest().getParam("sender");
index 3449dec..8c0387e 100644 (file)
@@ -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> post = webInterface.getCore().getPost(postId);
                if (!post.isPresent()) {
index cb33fb9..97393f2 100644 (file)
@@ -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) {
index 4206e8f..8dfe085 100644 (file)
@@ -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> sone = webInterface.getCore().getSone(soneId);
                if (!sone.isPresent()) {
index b4596ad..d32f973 100644 (file)
@@ -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) {
index 64c402a..3b51df2 100644 (file)
@@ -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();
index a25e665..d86e5a8 100644 (file)
@@ -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<Sone> currentSone = getCurrentSone(request.getToadletContext(), false);
                Collection<Notification> notifications = webInterface.getNotifications().getNotifications();
-               List<Notification> filteredNotifications = ListNotificationFilters.filterNotifications(notifications, currentSone);
+               List<Notification> 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<Sone> 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;
        }
index 96002b3..51ab3ee 100644 (file)
@@ -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<Sone> 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);
index 251c784..d0b79ac 100644 (file)
@@ -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<Sone> 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) {
index cc095a2..89a3858 100644 (file)
@@ -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<Sone> currentSone = getCurrentSone(request.getToadletContext(), false);
                /* load Sones. always return the status of the current Sone. */
-               Set<Sone> sones = new HashSet<Sone>(Collections.singleton(getCurrentSone(request.getToadletContext(), false)));
+               Set<Sone> sones = new HashSet<Sone>(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<Notification> notifications = ListNotificationFilters.filterNotifications(webInterface.getNotifications().getNotifications(), currentSone);
+               List<Notification> notifications = ListNotificationFilters.filterNotifications(webInterface.getNotifications().getNotifications(), currentSone.orNull());
                Collections.sort(notifications, Notification.CREATED_TIME_SORTER);
                /* load new posts. */
                Collection<Post> newPosts = webInterface.getNewPosts();
-               if (currentSone != null) {
+               if (currentSone.isPresent()) {
                        newPosts = Collections2.filter(newPosts, new Predicate<Post>() {
 
                                @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<PostReply> newReplies = webInterface.getNewReplies();
-               if (currentSone != null) {
+               if (currentSone.isPresent()) {
                        newReplies = Collections2.filter(newReplies, new Predicate<PostReply>() {
 
                                @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<Sone> 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;
        }
index d11cc0d..bb32362 100644 (file)
@@ -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<Sone> 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<Sone> 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")));
                        }
                }
index 55a947a..e7574a5 100644 (file)
@@ -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");
                }
index d8781ba..a577495 100644 (file)
@@ -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);
index 3661cb1..1225de1 100644 (file)
@@ -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> sone = webInterface.getCore().getSone(soneId);
                if (!sone.isPresent()) {
index a285d20..1e87b15 100644 (file)
@@ -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");
                }
index 17be966..b1ab9e0 100644 (file)
@@ -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();
index 229f219..993bbe3 100644 (file)
@@ -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> sone = webInterface.getCore().getSone(soneId);
                if (!sone.isPresent()) {