import net.pterodactylus.sone.data.Client;
import net.pterodactylus.sone.data.Image;
import net.pterodactylus.sone.data.Post;
-import net.pterodactylus.sone.data.PostBuilder;
-import net.pterodactylus.sone.data.PostBuilderFactory;
import net.pterodactylus.sone.data.PostReply;
-import net.pterodactylus.sone.data.PostReplyBuilder;
-import net.pterodactylus.sone.data.PostReplyBuilderFactory;
import net.pterodactylus.sone.data.Profile;
import net.pterodactylus.sone.data.Profile.Field;
import net.pterodactylus.sone.data.Reply;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.data.Sone.ShowCustomAvatars;
import net.pterodactylus.sone.data.Sone.SoneStatus;
+import net.pterodactylus.sone.database.PostBuilder;
+import net.pterodactylus.sone.database.PostBuilderFactory;
+import net.pterodactylus.sone.database.PostProvider;
+import net.pterodactylus.sone.database.PostReplyBuilder;
+import net.pterodactylus.sone.database.PostReplyBuilderFactory;
+import net.pterodactylus.sone.database.PostReplyProvider;
+import net.pterodactylus.sone.database.SoneProvider;
import net.pterodactylus.sone.data.TemporaryImage;
import net.pterodactylus.sone.fcp.FcpInterface;
import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired;
private volatile FcpInterface fcpInterface;
/** The times Sones were followed. */
- private final Map<Sone, Long> soneFollowingTimes = new HashMap<Sone, Long>();
+ private final Map<String, Long> soneFollowingTimes = new HashMap<String, Long>();
/** Locked local Sones. */
/* synchronize on itself. */
* @return The Sone with the given ID, or {@code null} if there is no such
* Sone
*/
- public Sone getSone(String id) {
- return getSone(id, true);
- }
-
- /**
- * Returns the Sone with the given ID, regardless whether it’s local or
- * remote.
- *
- * @param id
- * The ID of the Sone to get
- * @param create
- * {@code true} to create a new Sone if none exists,
- * {@code false} to return {@code null} if a Sone with the given
- * ID does not exist
- * @return The Sone with the given ID, or {@code null} if there is no such
- * Sone
- */
@Override
- public Sone getSone(String id, boolean create) {
+ public Sone getSone(String id) {
synchronized (sones) {
- if (!sones.containsKey(id) && create) {
- Sone sone = new Sone(id, false);
- sones.put(id, sone);
- }
return sones.get(id);
}
}
*/
public long getSoneFollowingTime(Sone sone) {
synchronized (soneFollowingTimes) {
- if (soneFollowingTimes.containsKey(sone)) {
- return soneFollowingTimes.get(sone);
- }
- return Long.MAX_VALUE;
+ return Optional.fromNullable(soneFollowingTimes.get(sone.getId())).or(Long.MAX_VALUE);
}
}
}
/**
- * Returns all posts that have the given Sone as recipient.
- *
- * @see Post#getRecipient()
- * @param recipient
- * The recipient of the posts
- * @return All posts that have the given Sone as recipient
+ * {@inheritDoc}
*/
- public Set<Post> getDirectedPosts(Sone recipient) {
- checkNotNull(recipient, "recipient must not be null");
- Set<Post> directedPosts = new HashSet<Post>();
+ @Override
+ public Collection<Post> getDirectedPosts(final String recipientId) {
+ checkNotNull(recipientId, "recipient must not be null");
synchronized (posts) {
- for (Post post : posts.values()) {
- if (recipient.equals(post.getRecipient())) {
- directedPosts.add(post);
+ return Collections2.filter(posts.values(), new Predicate<Post>() {
+
+ @Override
+ public boolean apply(Post post) {
+ return (post.getRecipient() != null) && (post.getRecipient().getId().equals(recipientId));
}
- }
+ });
}
- return directedPosts;
}
/**
sone.getOptions().addBooleanOption("ShowNotification/NewReplies", new DefaultOption<Boolean>(true));
sone.getOptions().addEnumOption("ShowCustomAvatars", new DefaultOption<ShowCustomAvatars>(ShowCustomAvatars.NEVER));
- followSone(sone, getSone("nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI"));
+ followSone(sone, "nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI");
touchConfiguration();
return sone;
}
eventBus.post(new NewSoneFoundEvent(sone));
for (Sone localSone : getLocalSones()) {
if (localSone.getOptions().getBooleanOption("AutoFollow").get()) {
- followSone(localSone, sone);
+ followSone(localSone, sone.getId());
}
}
}
public void followSone(Sone sone, String soneId) {
checkNotNull(sone, "sone must not be null");
checkNotNull(soneId, "soneId must not be null");
- Sone followedSone = getSone(soneId, true);
- if (followedSone == null) {
- logger.log(Level.INFO, String.format("Ignored Sone with invalid ID: %s", soneId));
- return;
- }
- followSone(sone, getSone(soneId));
- }
-
- /**
- * Lets the given local Sone follow the other given Sone. If the given Sone
- * was not followed by any local Sone before, this will mark all elements of
- * the followed Sone as read that have been created before the current
- * moment.
- *
- * @param sone
- * The local Sone that should follow the other Sone
- * @param followedSone
- * The Sone that should be followed
- */
- public void followSone(Sone sone, Sone followedSone) {
- checkNotNull(sone, "sone must not be null");
- checkNotNull(followedSone, "followedSone must not be null");
- sone.addFriend(followedSone.getId());
+ sone.addFriend(soneId);
synchronized (soneFollowingTimes) {
- if (!soneFollowingTimes.containsKey(followedSone)) {
+ if (!soneFollowingTimes.containsKey(soneId)) {
long now = System.currentTimeMillis();
- soneFollowingTimes.put(followedSone, now);
+ soneFollowingTimes.put(soneId, now);
+ Sone followedSone = getSone(soneId);
+ if (followedSone == null) {
+ return;
+ }
for (Post post : followedSone.getPosts()) {
if (post.getTime() < now) {
markPostKnown(post);
public void unfollowSone(Sone sone, String soneId) {
checkNotNull(sone, "sone must not be null");
checkNotNull(soneId, "soneId must not be null");
- unfollowSone(sone, getSone(soneId, false));
- }
-
- /**
- * Lets the given local Sone unfollow the other given Sone. If the given
- * local Sone is the last local Sone that followed the given Sone, its
- * following time will be removed.
- *
- * @param sone
- * The local Sone that should unfollow another Sone
- * @param unfollowedSone
- * The Sone being unfollowed
- */
- public void unfollowSone(Sone sone, Sone unfollowedSone) {
- checkNotNull(sone, "sone must not be null");
- checkNotNull(unfollowedSone, "unfollowedSone must not be null");
- sone.removeFriend(unfollowedSone.getId());
+ sone.removeFriend(soneId);
boolean unfollowedSoneStillFollowed = false;
for (Sone localSone : getLocalSones()) {
- unfollowedSoneStillFollowed |= localSone.hasFriend(unfollowedSone.getId());
+ unfollowedSoneStillFollowed |= localSone.hasFriend(soneId);
}
if (!unfollowedSoneStillFollowed) {
synchronized (soneFollowingTimes) {
- soneFollowingTimes.remove(unfollowedSone);
+ soneFollowingTimes.remove(soneId);
}
}
touchConfiguration();
knownSones.add(friend);
}
}
+ synchronized (this.posts) {
+ for (Post post : posts) {
+ this.posts.put(post.getId(), post);
+ }
+ }
synchronized (knownPosts) {
for (Post post : posts) {
knownPosts.add(post.getId());
}
}
+ synchronized (this.replies) {
+ for (PostReply postReply : replies) {
+ this.replies.put(postReply.getId(), postReply);
+ }
+ }
synchronized (knownReplies) {
for (PostReply reply : replies) {
knownReplies.add(reply.getId());
/* save Sone following times. */
soneCounter = 0;
synchronized (soneFollowingTimes) {
- for (Entry<Sone, Long> soneFollowingTime : soneFollowingTimes.entrySet()) {
- configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").setValue(soneFollowingTime.getKey().getId());
+ for (Entry<String, Long> soneFollowingTime : soneFollowingTimes.entrySet()) {
+ configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").setValue(soneFollowingTime.getKey());
configuration.getLongValue("SoneFollowingTimes/" + soneCounter + "/Time").setValue(soneFollowingTime.getValue());
++soneCounter;
}
logger.log(Level.WARNING, String.format("Ignoring Sone with invalid ID: %s", soneId));
} else {
synchronized (soneFollowingTimes) {
- soneFollowingTimes.put(getSone(soneId), time);
+ soneFollowingTimes.put(soneId, time);
}
}
++soneCounter;
/* some local identity still trusts this identity, don’t remove. */
return;
}
- Sone sone = getSone(identity.getId(), false);
+ Sone sone = getSone(identity.getId());
if (sone == null) {
/* TODO - we don’t have the Sone anymore. should this happen? */
return;