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("")) {
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;
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);
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);
}
}
@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. */
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");
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());
@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. */
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();
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);
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);
}
@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()) {
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;
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)) {
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)) {
*/
@Override
protected String getRedirectTarget(FreenetRequest request) {
- if (getCurrentSone(request.getToadletContext(), false) != null) {
+ if (getCurrentSone(request.getToadletContext(), false).isPresent()) {
return "index.html";
}
return null;
if (webInterface.getCore().getPreferences().isRequireFullAccess() && !toadletContext.isAllowedFullAccess()) {
return false;
}
- return getCurrentSone(toadletContext, false) == null;
+ return !getCurrentSone(toadletContext, false).isPresent();
}
}
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);
}
}
}
/* 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. */
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.
*
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);
}
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());
@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))) {
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;
* @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);
}
* @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);
}
@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);
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());
*/
@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()) {
return false;
}
if (requiresLogin()) {
- return getCurrentSone(toadletContext, false) != null;
+ return getCurrentSone(toadletContext, false).isPresent();
}
return true;
}
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());
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);
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)) {
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());
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) {
* @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);
}
* @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));
}
* @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));
}
/**
*/
@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");
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()) {
@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) {
*/
@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()) {
@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) {
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();
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.
*/
@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) {
* 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;
}
* 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());
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);
* 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());
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) {
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;
*/
@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(",");
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);
}
});
}
/* 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);
}
});
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);
}
/**
* 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;
}
import net.pterodactylus.util.web.Response;
import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.base.Optional;
import freenet.clients.http.ToadletContext;
* @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);
}
* @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);
}
}
}
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")));
}
}
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");
}
*/
@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);
*/
@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()) {
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");
}
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();
*/
@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()) {