import net.pterodactylus.util.service.AbstractService;
import net.pterodactylus.util.thread.NamedThreadFactory;
+import com.google.common.base.Function;
+import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Collections2;
+import com.google.common.collect.FluentIterable;
+import com.google.common.collect.Ordering;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import com.google.inject.Inject;
* {@inheritDoc}
*/
@Override
- public Post getPost(String postId) {
+ public Optional<Post> getPost(String postId) {
synchronized (posts) {
- return posts.get(postId);
+ return Optional.fromNullable(posts.get(postId));
}
}
* {@inheritDoc}
*/
@Override
- public PostReply getPostReply(String replyId) {
+ public Optional<PostReply> getPostReply(String replyId) {
synchronized (replies) {
- return replies.get(replyId);
+ return Optional.fromNullable(replies.get(replyId));
}
}
* {@inheritDoc}
*/
@Override
- public List<PostReply> getReplies(Post post) {
- Set<Sone> sones = getSones();
- List<PostReply> replies = new ArrayList<PostReply>();
- for (Sone sone : sones) {
- for (PostReply reply : sone.getReplies()) {
- if (reply.getPost().equals(post)) {
- replies.add(reply);
- }
+ public List<PostReply> getReplies(final Post post) {
+ return Ordering.from(Reply.TIME_COMPARATOR).sortedCopy(FluentIterable.from(getSones()).transformAndConcat(new Function<Sone, Iterable<PostReply>>() {
+
+ @Override
+ public Iterable<PostReply> apply(Sone sone) {
+ return sone.getReplies();
}
- }
- Collections.sort(replies, Reply.TIME_COMPARATOR);
- return replies;
+ }).filter(new Predicate<PostReply>() {
+
+ @Override
+ public boolean apply(PostReply reply) {
+ return post.getId().equals(reply.getPostId());
+ }
+ }));
}
/**
Set<Post> posts = new HashSet<Post>();
synchronized (bookmarkedPosts) {
for (String bookmarkedPostId : bookmarkedPosts) {
- Post post = getPost(bookmarkedPostId);
- if (post != null) {
- posts.add(post);
+ Optional<Post> post = getPost(bookmarkedPostId);
+ if (!post.isPresent()) {
+ posts.add(post.get());
}
}
}
List<Post> storedPosts = storedSone.getPosts();
synchronized (knownPosts) {
for (Post post : sone.getPosts()) {
- PostBuilder postBuilder = postBuilderFactory.newPostBuilder();
- postBuilder.copyPost(post).from(storedSone.getId());
- Post newPost = postBuilder.build().setKnown(knownPosts.contains(post.getId()));
- if (!storedPosts.contains(newPost)) {
- if (newPost.getTime() < getSoneFollowingTime(sone)) {
- knownPosts.add(newPost.getId());
- newPost.setKnown(true);
- } else if (!knownPosts.contains(newPost.getId())) {
- eventBus.post(new NewPostFoundEvent(newPost));
+ post.setKnown(knownPosts.contains(post.getId()));
+ if (!storedPosts.contains(post)) {
+ if (post.getTime() < getSoneFollowingTime(sone)) {
+ knownPosts.add(post.getId());
+ post.setKnown(true);
+ } else if (!knownPosts.contains(post.getId())) {
+ eventBus.post(new NewPostFoundEvent(post));
}
}
- posts.put(newPost.getId(), newPost);
+ posts.put(post.getId(), post);
}
}
}
}
}
}
- synchronized (storedSone) {
- if (!soneRescueMode || (sone.getTime() > storedSone.getTime())) {
- storedSone.setTime(sone.getTime());
- }
- storedSone.setClient(sone.getClient());
- storedSone.setProfile(sone.getProfile());
- if (soneRescueMode) {
- for (Post post : sone.getPosts()) {
- storedSone.addPost(post);
- }
- for (PostReply reply : sone.getReplies()) {
- storedSone.addReply(reply);
- }
- for (String likedPostId : sone.getLikedPostIds()) {
- storedSone.addLikedPostId(likedPostId);
- }
- for (String likedReplyId : sone.getLikedReplyIds()) {
- storedSone.addLikedReplyId(likedReplyId);
- }
- for (Album album : sone.getAlbums()) {
- storedSone.addAlbum(album);
- }
- } else {
- storedSone.setPosts(sone.getPosts());
- storedSone.setReplies(sone.getReplies());
- storedSone.setLikePostIds(sone.getLikedPostIds());
- storedSone.setLikeReplyIds(sone.getLikedReplyIds());
- storedSone.setAlbums(sone.getAlbums());
- }
- storedSone.setLatestEdition(sone.getLatestEdition());
+ synchronized (sones) {
+ sones.put(sone.getId(), sone);
}
}
}
logger.log(Level.WARNING, "Invalid post found, aborting load!");
return;
}
- PostBuilder postBuilder = postBuilderFactory.newPostBuilder().withId(postId).from(sone.getId()).withTime(postTime).withText(postText);
+ PostBuilder postBuilder = postBuilder().withId(postId).from(sone.getId()).withTime(postTime).withText(postText);
if ((postRecipientId != null) && (postRecipientId.length() == 43)) {
postBuilder.to(postRecipientId);
}
webOfTrustUpdater.stop();
updateChecker.stop();
soneDownloader.stop();
+ soneDownloaders.shutdown();
identityManager.stop();
}
for (PostReply reply : sone.getReplies()) {
String replyPrefix = sonePrefix + "/Replies/" + replyCounter++;
configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
- configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPost().getId());
+ configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPostId());
configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
}
import net.pterodactylus.sone.data.Post;
+import com.google.common.base.Optional;
+
/**
* Interface for objects that can provide {@link Post}s by their ID.
*
* The ID of the post to return
* @return The post with the given ID, or {@code null}
*/
- public Post getPost(String postId);
+ public Optional<Post> getPost(String postId);
}
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.PostReply;
+import com.google.common.base.Optional;
+
/**
* Interface for objects that can provide {@link PostReply}s.
*
* The ID of the reply to get
* @return The reply, or {@code null} if there is no such reply
*/
- public PostReply getPostReply(String id);
+ public Optional<PostReply> getPostReply(String id);
/**
* Returns all replies for the given post, order ascending by time.
package net.pterodactylus.sone.data;
+import com.google.common.base.Optional;
+import com.google.common.base.Predicate;
+
/**
* A reply is like a {@link Post} but can never be posted on its own, it always
* refers to another {@link Post}.
public interface PostReply extends Reply<PostReply> {
/**
+ * Filter that selects {@link PostReply}s that have a
+ * {@link Optional#isPresent() present} {@link #getPost() post}.
+ */
+ public static final Predicate<PostReply> HAS_POST_FILTER = new Predicate<PostReply>() {
+
+ @Override
+ public boolean apply(PostReply postReply) {
+ return postReply.getPost().isPresent();
+ }
+ };
+
+ /**
+ * Returns the ID of the post this reply refers to.
+ *
+ * @return The ID of the post this reply refers to
+ */
+ public String getPostId();
+
+ /**
* Returns the post this reply refers to.
*
* @return The post this reply refers to
*/
- public Post getPost();
+ public Optional<Post> getPost();
}
package net.pterodactylus.sone.data.impl;
+import com.google.inject.Inject;
+
import net.pterodactylus.sone.core.SoneProvider;
import net.pterodactylus.sone.data.PostBuilder;
import net.pterodactylus.sone.data.PostBuilderFactory;
* @param soneProvider
* The Sone provider
*/
+ @Inject
public DefaultPostBuilderFactory(SoneProvider soneProvider) {
this.soneProvider = soneProvider;
}
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.PostReply;
+import com.google.common.base.Optional;
+
/**
* Simple {@link PostReply} implementation.
*
//
/**
+ * {@inheritDocs}
+ */
+ @Override
+ public String getPostId() {
+ return postId;
+ }
+
+ /**
* {@inheritDoc}
*/
@Override
- public Post getPost() {
+ public Optional<Post> getPost() {
return postProvider.getPost(postId);
}
import net.pterodactylus.sone.freenet.fcp.FcpException;
import net.pterodactylus.sone.template.SoneAccessor;
+import com.google.common.base.Optional;
import com.google.common.collect.Collections2;
import freenet.node.FSParseException;
protected Post getPost(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
try {
String postId = simpleFieldSet.getString(parameterName);
- Post post = core.getPost(postId);
- if (post == null) {
+ Optional<Post> post = core.getPost(postId);
+ if (!post.isPresent()) {
throw new FcpException("Could not load post from “" + postId + "”.");
}
- return post;
+ return post.get();
} catch (FSParseException fspe1) {
throw new FcpException("Could not post ID from “" + parameterName + "”.", fspe1);
}
protected PostReply getReply(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
try {
String replyId = simpleFieldSet.getString(parameterName);
- PostReply reply = core.getPostReply(replyId);
- if (reply == null) {
+ Optional<PostReply> reply = core.getPostReply(replyId);
+ if (!reply.isPresent()) {
throw new FcpException("Could not load reply from “" + replyId + "”.");
}
- return reply;
+ return reply.get();
} catch (FSParseException fspe1) {
throw new FcpException("Could not reply ID from “" + parameterName + "”.", fspe1);
}
@Override
protected void configure() {
+ bind(Core.class).in(Singleton.class);
bind(EventBus.class).toInstance(eventBus);
bind(Configuration.class).toInstance(startConfiguration);
bind(FreenetInterface.class).in(Singleton.class);
import net.pterodactylus.sone.freenet.wot.Trust;
import net.pterodactylus.util.notify.Notification;
+import com.google.common.base.Optional;
+
/**
* Filter for {@link ListNotification}s.
*
*/
public static boolean isReplyVisible(Sone sone, PostReply reply) {
checkNotNull(reply, "reply must not be null");
- Post post = reply.getPost();
- if (post == null) {
+ Optional<Post> post = reply.getPost();
+ if (!post.isPresent()) {
return false;
}
- if (!isPostVisible(sone, post)) {
+ if (!isPostVisible(sone, post.get())) {
return false;
}
if (reply.getTime() > System.currentTimeMillis()) {
import net.pterodactylus.util.template.Filter;
import net.pterodactylus.util.template.TemplateContext;
+import com.google.common.base.Optional;
+
/**
* {@link Filter} implementation that groups replies by the post the are in
* reply to, returning a map with the post as key and the list of replies as
Map<Post, Set<Sone>> postSones = new HashMap<Post, Set<Sone>>();
Map<Post, Set<PostReply>> postReplies = new HashMap<Post, Set<PostReply>>();
for (PostReply reply : allReplies) {
- Post post = reply.getPost();
- Set<Sone> sones = postSones.get(post);
+ /*
+ * All replies from a new-reply notification have posts,
+ * ListNotificationFilters takes care of that.
+ */
+ Optional<Post> post = reply.getPost();
+ Set<Sone> sones = postSones.get(post.get());
if (sones == null) {
sones = new HashSet<Sone>();
- postSones.put(post, sones);
+ postSones.put(post.get(), sones);
}
sones.add(reply.getSone());
- Set<PostReply> replies = postReplies.get(post);
+ Set<PostReply> replies = postReplies.get(post.get());
if (replies == null) {
replies = new HashSet<PostReply>();
- postReplies.put(post, replies);
+ postReplies.put(post.get(), replies);
}
replies.add(reply);
}
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import com.google.common.base.Optional;
+
import net.pterodactylus.sone.core.PostProvider;
import net.pterodactylus.sone.core.SoneProvider;
import net.pterodactylus.sone.data.Post;
if (linkType == LinkType.POST) {
if (line.length() >= (7 + 36)) {
String postId = line.substring(7, 43);
- Post post = postProvider.getPost(postId);
- if ((post != null) && (post.getSone() != null)) {
- parts.add(new PostPart(post));
+ Optional<Post> post = postProvider.getPost(postId);
+ if (post.isPresent()) {
+ parts.add(new PostPart(post.get()));
} else {
parts.add(new PlainTextPart(line.substring(0, 43)));
}
package net.pterodactylus.sone.web;
+import com.google.common.base.Optional;
+
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.text.TextFilter;
String text = request.getHttpRequest().getPartAsStringFailsafe("text", 65536).trim();
String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 256);
if (request.getMethod() == Method.POST) {
- Post post = webInterface.getCore().getPost(postId);
+ Optional<Post> post = webInterface.getCore().getPost(postId);
+ if (!post.isPresent()) {
+ throw new RedirectException("noPermission.html");
+ }
if (text.length() > 0) {
String senderId = request.getHttpRequest().getPartAsStringFailsafe("sender", 43);
Sone sender = webInterface.getCore().getLocalSone(senderId, false);
sender = getCurrentSone(request.getToadletContext());
}
text = TextFilter.filter(request.getHttpRequest().getHeader("host"), text);
- webInterface.getCore().createReply(sender, post, text);
+ webInterface.getCore().createReply(sender, post.get(), text);
throw new RedirectException(returnPage);
}
templateContext.set("errorTextEmpty", true);
package net.pterodactylus.sone.web;
+import com.google.common.base.Optional;
+
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.web.page.FreenetRequest;
import net.pterodactylus.util.template.Template;
if (request.getMethod() == Method.GET) {
String postId = request.getHttpRequest().getParam("post");
String returnPage = request.getHttpRequest().getParam("returnPage");
- Post post = webInterface.getCore().getPost(postId);
- templateContext.set("post", post);
+ Optional<Post> post = webInterface.getCore().getPost(postId);
+ if (!post.isPresent()) {
+ throw new RedirectException("noPermission.html");
+ }
+ templateContext.set("post", post.get());
templateContext.set("returnPage", returnPage);
return;
} else if (request.getMethod() == Method.POST) {
String postId = request.getHttpRequest().getPartAsStringFailsafe("post", 36);
String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 256);
- Post post = webInterface.getCore().getPost(postId);
- if (!post.getSone().isLocal()) {
+ Optional<Post> post = webInterface.getCore().getPost(postId);
+ if (!post.isPresent() || !post.get().getSone().isLocal()) {
throw new RedirectException("noPermission.html");
}
if (request.getHttpRequest().isPartSet("confirmDelete")) {
- webInterface.getCore().deletePost(post);
+ webInterface.getCore().deletePost(post.get());
throw new RedirectException(returnPage);
} else if (request.getHttpRequest().isPartSet("abortDelete")) {
throw new RedirectException(returnPage);
import net.pterodactylus.util.template.TemplateContext;
import net.pterodactylus.util.web.Method;
+import com.google.common.base.Optional;
+
/**
* This page lets the user delete a reply.
*
protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
super.processTemplate(request, templateContext);
String replyId = request.getHttpRequest().getPartAsStringFailsafe("reply", 36);
- PostReply reply = webInterface.getCore().getPostReply(replyId);
+ Optional<PostReply> reply = webInterface.getCore().getPostReply(replyId);
String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 256);
if (request.getMethod() == Method.POST) {
- if (!reply.getSone().isLocal()) {
+ if (!reply.get().getSone().isLocal()) {
throw new RedirectException("noPermission.html");
}
if (request.getHttpRequest().isPartSet("confirmDelete")) {
- webInterface.getCore().deleteReply(reply);
+ webInterface.getCore().deleteReply(reply.get());
throw new RedirectException(returnPage);
} else if (request.getHttpRequest().isPartSet("abortDelete")) {
throw new RedirectException(returnPage);
import net.pterodactylus.util.template.Template;
import net.pterodactylus.util.template.TemplateContext;
+import com.google.common.base.Optional;
+
/**
* Page that lets the user mark a number of {@link Sone}s, {@link Post}s, or
* {@link Reply Replie}s as known.
for (StringTokenizer idTokenizer = new StringTokenizer(ids); idTokenizer.hasMoreTokens();) {
String id = idTokenizer.nextToken();
if (type.equals("post")) {
- Post post = webInterface.getCore().getPost(id);
- if (post == null) {
+ Optional<Post> post = webInterface.getCore().getPost(id);
+ if (!post.isPresent()) {
continue;
}
- webInterface.getCore().markPostKnown(post);
+ webInterface.getCore().markPostKnown(post.get());
} else if (type.equals("reply")) {
- PostReply reply = webInterface.getCore().getPostReply(id);
- if (reply == null) {
+ Optional<PostReply> reply = webInterface.getCore().getPostReply(id);
+ if (!reply.isPresent()) {
continue;
}
- webInterface.getCore().markReplyKnown(reply);
+ webInterface.getCore().markReplyKnown(reply.get());
} else if (type.equals("sone")) {
Sone sone = webInterface.getCore().getSone(id, false);
if (sone == null) {
import java.util.List;
import java.util.Set;
+import com.google.common.collect.Collections2;
+
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.PostReply;
import net.pterodactylus.sone.notify.ListNotificationFilters;
/* collect new elements from notifications. */
Set<Post> posts = new HashSet<Post>(webInterface.getNewPosts());
- for (PostReply reply : webInterface.getNewReplies()) {
- posts.add(reply.getPost());
+ for (PostReply reply : Collections2.filter(webInterface.getNewReplies(), PostReply.HAS_POST_FILTER)) {
+ posts.add(reply.getPost().get());
}
/* filter and sort them. */
import net.pterodactylus.util.text.TextException;
import com.google.common.base.Function;
+import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
*/
private String getReplyPostId(String phrase) {
String replyId = phrase.startsWith("reply://") ? phrase.substring(8) : phrase;
- return (webInterface.getCore().getPostReply(replyId) != null) ? webInterface.getCore().getPostReply(replyId).getPost().getId() : null;
+ Optional<PostReply> postReply = webInterface.getCore().getPostReply(replyId);
+ if (!postReply.isPresent()) {
+ return null;
+ }
+ return postReply.get().getPostId();
}
/**
import java.net.URI;
+import com.google.common.base.Optional;
+
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.template.SoneAccessor;
import net.pterodactylus.sone.web.page.FreenetRequest;
@Override
protected String getPageTitle(FreenetRequest request) {
String postId = request.getHttpRequest().getParam("post");
- Post post = webInterface.getCore().getPost(postId);
+ Optional<Post> post = webInterface.getCore().getPost(postId);
String title = "";
- if ((post != null) && (post.getSone() != null)) {
- title = post.getText().substring(0, Math.min(20, post.getText().length())) + "…";
- title += " - " + SoneAccessor.getNiceName(post.getSone()) + " - ";
+ if (post.isPresent()) {
+ title = post.get().getText().substring(0, Math.min(20, post.get().getText().length())) + "…";
+ title += " - " + SoneAccessor.getNiceName(post.get().getSone()) + " - ";
}
title += webInterface.getL10n().getString("Page.ViewPost.Title");
return title;
super.processTemplate(request, templateContext);
String postId = request.getHttpRequest().getParam("post");
boolean raw = request.getHttpRequest().getParam("raw").equals("true");
- Post post = webInterface.getCore().getPost(postId);
- templateContext.set("post", post);
+ Optional<Post> post = webInterface.getCore().getPost(postId);
+ templateContext.set("post", post.get());
templateContext.set("raw", raw);
}
import net.pterodactylus.util.template.Template;
import net.pterodactylus.util.template.TemplateContext;
+import com.google.common.base.Optional;
+
/**
* Lets the user browser another Sone.
*
Set<PostReply> replies = sone.getReplies();
final Map<Post, List<PostReply>> repliedPosts = new HashMap<Post, List<PostReply>>();
for (PostReply reply : replies) {
- Post post = reply.getPost();
- if (repliedPosts.containsKey(post) || sone.equals(post.getSone()) || (sone.equals(post.getRecipient()))) {
+ Optional<Post> post = reply.getPost();
+ if (!post.isPresent() || repliedPosts.containsKey(post.get()) || sone.equals(post.get().getSone()) || (sone.equals(post.get().getRecipient()))) {
continue;
}
- repliedPosts.put(post, webInterface.getCore().getReplies(post));
+ repliedPosts.put(post.get(), webInterface.getCore().getReplies(post.get()));
}
List<Post> posts = new ArrayList<Post>(repliedPosts.keySet());
Collections.sort(posts, new Comparator<Post>() {
}
if (!hasFirstStartNotification()) {
notificationManager.addNotification(isLocal ? localReplyNotification : newReplyNotification);
- if (!getMentionedSones(reply.getText()).isEmpty() && !isLocal && (reply.getPost().getSone() != null) && (reply.getTime() <= System.currentTimeMillis())) {
- mentionNotification.add(reply.getPost());
+ if (!getMentionedSones(reply.getText()).isEmpty() && !isLocal && reply.getPost().isPresent() && (reply.getTime() <= System.currentTimeMillis())) {
+ mentionNotification.add(reply.getPost().get());
notificationManager.addNotification(mentionNotification);
}
} else {
public void markReplyKnown(MarkPostReplyKnownEvent markPostReplyKnownEvent) {
newReplyNotification.remove(markPostReplyKnownEvent.postReply());
localReplyNotification.remove(markPostReplyKnownEvent.postReply());
- mentionNotification.remove(markPostReplyKnownEvent.postReply().getPost());
+ mentionNotification.remove(markPostReplyKnownEvent.postReply().getPost().get());
}
/**
localReplyNotification.remove(reply);
if (!getMentionedSones(reply.getText()).isEmpty()) {
boolean isMentioned = false;
- for (PostReply existingReply : getCore().getReplies(reply.getPost())) {
+ for (PostReply existingReply : getCore().getReplies(reply.getPost().get())) {
isMentioned |= !reply.isKnown() && !getMentionedSones(existingReply.getText()).isEmpty();
}
if (!isMentioned) {
- mentionNotification.remove(reply.getPost());
+ mentionNotification.remove(reply.getPost().get());
}
}
}
@SuppressWarnings("synthetic-access")
public void run() {
lockedSonesNotification.add(sone);
- lockedSonesTickerObjects.remove(sone);
notificationManager.addNotification(lockedSonesNotification);
}
}, 5, TimeUnit.MINUTES);
package net.pterodactylus.sone.web.ajax;
+import com.google.common.base.Optional;
+
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.PostReply;
import net.pterodactylus.sone.data.Sone;
if (sender == null) {
sender = getCurrentSone(request.getToadletContext());
}
- Post post = webInterface.getCore().getPost(postId);
- if ((post == null) || (post.getSone() == null)) {
+ Optional<Post> post = webInterface.getCore().getPost(postId);
+ if (!post.isPresent()) {
return createErrorJsonObject("invalid-post-id");
}
text = TextFilter.filter(request.getHttpRequest().getHeader("host"), text);
- PostReply reply = webInterface.getCore().createReply(sender, post, text);
+ PostReply reply = webInterface.getCore().createReply(sender, post.get(), text);
return createSuccessJsonObject().put("reply", reply.getId()).put("sone", sender.getId());
}
package net.pterodactylus.sone.web.ajax;
+import com.google.common.base.Optional;
+
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.web.WebInterface;
import net.pterodactylus.sone.web.page.FreenetRequest;
@Override
protected JsonObject createJsonObject(FreenetRequest request) {
String postId = request.getHttpRequest().getParam("post");
- Post post = webInterface.getCore().getPost(postId);
- if ((post == null) || (post.getSone() == null)) {
+ Optional<Post> post = webInterface.getCore().getPost(postId);
+ if (!post.isPresent()) {
return createErrorJsonObject("invalid-post-id");
}
- if (!post.getSone().isLocal()) {
+ if (!post.get().getSone().isLocal()) {
return createErrorJsonObject("not-authorized");
}
- webInterface.getCore().deletePost(post);
+ webInterface.getCore().deletePost(post.get());
return createSuccessJsonObject();
}
package net.pterodactylus.sone.web.ajax;
+import com.google.common.base.Optional;
+
import net.pterodactylus.sone.data.PostReply;
import net.pterodactylus.sone.web.WebInterface;
import net.pterodactylus.sone.web.page.FreenetRequest;
@Override
protected JsonObject createJsonObject(FreenetRequest request) {
String replyId = request.getHttpRequest().getParam("reply");
- PostReply reply = webInterface.getCore().getPostReply(replyId);
- if (reply == null) {
+ Optional<PostReply> reply = webInterface.getCore().getPostReply(replyId);
+ if (!reply.isPresent()) {
return createErrorJsonObject("invalid-reply-id");
}
- if (!reply.getSone().isLocal()) {
+ if (!reply.get().getSone().isLocal()) {
return createErrorJsonObject("not-authorized");
}
- webInterface.getCore().deleteReply(reply);
+ webInterface.getCore().deleteReply(reply.get());
return createSuccessJsonObject();
}
import net.pterodactylus.util.json.JsonArray;
import net.pterodactylus.util.json.JsonObject;
+import com.google.common.base.Optional;
+
/**
* AJAX page that retrieves the number of “likes” a {@link Post} has.
*
return createErrorJsonObject("invalid-" + type + "-id");
}
if ("post".equals(type)) {
- Post post = webInterface.getCore().getPost(id);
- Set<Sone> sones = webInterface.getCore().getLikes(post);
+ Optional<Post> post = webInterface.getCore().getPost(id);
+ if (!post.isPresent()) {
+ return createErrorJsonObject("invalid-post-id");
+ }
+ Set<Sone> sones = webInterface.getCore().getLikes(post.get());
return createSuccessJsonObject().put("likes", sones.size()).put("sones", getSones(sones));
} else if ("reply".equals(type)) {
- PostReply reply = webInterface.getCore().getPostReply(id);
- Set<Sone> sones = webInterface.getCore().getLikes(reply);
+ Optional<PostReply> reply = webInterface.getCore().getPostReply(id);
+ if (!reply.isPresent()) {
+ return createErrorJsonObject("invalid-reply-id");
+ }
+ Set<Sone> sones = webInterface.getCore().getLikes(reply.get());
return createSuccessJsonObject().put("likes", sones.size()).put("sones", getSones(sones));
}
return createErrorJsonObject("invalid-type");
import java.io.StringWriter;
+import com.google.common.base.Optional;
+
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.web.WebInterface;
@Override
protected JsonObject createJsonObject(FreenetRequest request) {
String postId = request.getHttpRequest().getParam("post");
- Post post = webInterface.getCore().getPost(postId);
- if (post == null) {
+ Optional<Post> post = webInterface.getCore().getPost(postId);
+ if (!post.isPresent()) {
return createErrorJsonObject("invalid-post-id");
}
- return createSuccessJsonObject().put("post", createJsonPost(request, post, getCurrentSone(request.getToadletContext())));
+ return createSuccessJsonObject().put("post", createJsonPost(request, post.get(), getCurrentSone(request.getToadletContext())));
}
/**
import java.io.StringWriter;
+import com.google.common.base.Optional;
+
import net.pterodactylus.sone.data.PostReply;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.web.WebInterface;
@Override
protected JsonObject createJsonObject(FreenetRequest request) {
String replyId = request.getHttpRequest().getParam("reply");
- PostReply reply = webInterface.getCore().getPostReply(replyId);
- if ((reply == null) || (reply.getSone() == null)) {
+ Optional<PostReply> reply = webInterface.getCore().getPostReply(replyId);
+ if (!reply.isPresent()) {
return createErrorJsonObject("invalid-reply-id");
}
- return createSuccessJsonObject().put("reply", createJsonReply(request, reply, getCurrentSone(request.getToadletContext())));
+ return createSuccessJsonObject().put("reply", createJsonReply(request, reply.get(), getCurrentSone(request.getToadletContext())));
}
/**
private JsonObject createJsonReply(FreenetRequest request, PostReply reply, Sone currentSone) {
JsonObject jsonReply = new JsonObject();
jsonReply.put("id", reply.getId());
- jsonReply.put("postId", reply.getPost().getId());
+ jsonReply.put("postId", reply.getPostId());
jsonReply.put("soneId", reply.getSone().getId());
jsonReply.put("time", reply.getTime());
StringWriter stringWriter = new StringWriter();
});
}
/* remove replies to unknown posts. */
- newReplies = Collections2.filter(newReplies, new Predicate<PostReply>() {
-
- @Override
- public boolean apply(PostReply reply) {
- return (reply.getPost() != null) && (reply.getPost().getSone() != null);
- }
- });
+ newReplies = Collections2.filter(newReplies, PostReply.HAS_POST_FILTER);
JsonArray jsonReplies = new JsonArray();
for (PostReply reply : newReplies) {
JsonObject jsonReply = new JsonObject();
jsonReply.put("id", reply.getId());
jsonReply.put("sone", reply.getSone().getId());
- jsonReply.put("post", reply.getPost().getId());
- jsonReply.put("postSone", reply.getPost().getSone().getId());
+ jsonReply.put("post", reply.getPostId());
+ 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);
import net.pterodactylus.sone.web.page.FreenetRequest;
import net.pterodactylus.util.json.JsonObject;
+import com.google.common.base.Optional;
+
/**
* Ajax page that returns a formatted, relative timestamp for replies or posts.
*
if (allIds.length() > 0) {
String[] ids = allIds.split(",");
for (String id : ids) {
- Post post = webInterface.getCore().getPost(id);
- if (post == null) {
+ Optional<Post> post = webInterface.getCore().getPost(id);
+ if (!post.isPresent()) {
continue;
}
JsonObject postTime = new JsonObject();
- Time time = getTime(post.getTime());
+ Time time = getTime(post.get().getTime());
postTime.put("timeText", time.getText());
postTime.put("refreshTime", TimeUnit.MILLISECONDS.toSeconds(time.getRefresh()));
synchronized (dateFormat) {
- postTime.put("tooltip", dateFormat.format(new Date(post.getTime())));
+ postTime.put("tooltip", dateFormat.format(new Date(post.get().getTime())));
}
postTimes.put(id, postTime);
}
if (allIds.length() > 0) {
String[] ids = allIds.split(",");
for (String id : ids) {
- PostReply reply = webInterface.getCore().getPostReply(id);
- if (reply == null) {
+ Optional<PostReply> reply = webInterface.getCore().getPostReply(id);
+ if (!reply.isPresent()) {
continue;
}
JsonObject replyTime = new JsonObject();
- Time time = getTime(reply.getTime());
+ Time time = getTime(reply.get().getTime());
replyTime.put("timeText", time.getText());
replyTime.put("refreshTime", TimeUnit.MILLISECONDS.toSeconds(time.getRefresh()));
synchronized (dateFormat) {
- replyTime.put("tooltip", dateFormat.format(new Date(reply.getTime())));
+ replyTime.put("tooltip", dateFormat.format(new Date(reply.get().getTime())));
}
replyTimes.put(id, replyTime);
}
import net.pterodactylus.sone.web.page.FreenetRequest;
import net.pterodactylus.util.json.JsonObject;
+import com.google.common.base.Optional;
+
/**
* AJAX page that lets the user mark a number of {@link Sone}s, {@link Post}s,
* or {@link Reply}s as known.
Core core = webInterface.getCore();
for (String id : ids) {
if (type.equals("post")) {
- Post post = core.getPost(id);
- if (post == null) {
+ Optional<Post> post = core.getPost(id);
+ if (!post.isPresent()) {
continue;
}
- core.markPostKnown(post);
+ core.markPostKnown(post.get());
} else if (type.equals("reply")) {
- PostReply reply = core.getPostReply(id);
- if (reply == null) {
+ Optional<PostReply> reply = core.getPostReply(id);
+ if (!reply.isPresent()) {
continue;
}
- core.markReplyKnown(reply);
+ core.markReplyKnown(reply.get());
} else if (type.equals("sone")) {
Sone sone = core.getSone(id, false);
if (sone == null) {