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;
* @return All Sones
*/
public Set<Sone> getSones() {
- return new HashSet<Sone>(sones.values());
- }
-
- /**
- * Returns the Sone with the given ID, regardless whether it’s local or
- * remote.
- *
- * @param id
- * The ID of the Sone to get
- * @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);
+ synchronized (sones) {
+ return Collections.unmodifiableCollection(sones.values());
+ }
}
/**
*
* @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 Optional<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);
- }
- }
-
- /**
- * Checks whether the core knows a Sone with the given ID.
- *
- * @param id
- * The ID of the Sone
- * @return {@code true} if there is a Sone with the given ID, {@code false}
- * otherwise
- */
- public boolean hasSone(String id) {
- synchronized (sones) {
- return sones.containsKey(id);
+ return Optional.fromNullable(sones.get(id));
}
}
}
/**
- * 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 recipientId.equals(post.getRecipientId().orNull());
}
- }
+ });
}
- return directedPosts;
}
/**
sone.setClient(new Client("Sone", SonePlugin.VERSION.toString()));
sone.setKnown(true);
/* TODO - load posts ’n stuff */
+ trustedIdentities.put(ownIdentity, Collections.synchronizedSet(new HashSet<Identity>()));
sones.put(ownIdentity.getId(), sone);
final SoneInserter soneInserter = new SoneInserter(this, eventBus, freenetInterface, sone);
soneInserters.put(sone, soneInserter);
if (!soneFollowingTimes.containsKey(soneId)) {
long now = System.currentTimeMillis();
soneFollowingTimes.put(soneId, now);
- Sone followedSone = getSone(soneId);
- if (followedSone == null) {
+ Optional<Sone> followedSone = getSone(soneId);
+ if (!followedSone.isPresent()) {
return;
}
- for (Post post : followedSone.getPosts()) {
+ for (Post post : followedSone.get().getPosts()) {
if (post.getTime() < now) {
markPostKnown(post);
}
}
- for (PostReply reply : followedSone.getReplies()) {
+ for (PostReply reply : followedSone.get().getReplies()) {
if (reply.getTime() < now) {
markReplyKnown(reply);
}
* of the age of the given Sone
*/
public void updateSone(Sone sone, boolean soneRescueMode) {
- if (hasSone(sone.getId())) {
- Sone storedSone = getSone(sone.getId());
- if (!soneRescueMode && !(sone.getTime() > storedSone.getTime())) {
+ Optional<Sone> storedSone = getSone(sone.getId());
+ if (storedSone.isPresent()) {
+ if (!soneRescueMode && !(sone.getTime() > storedSone.get().getTime())) {
logger.log(Level.FINE, String.format("Downloaded Sone %s is not newer than stored Sone %s.", sone, storedSone));
return;
}
synchronized (posts) {
if (!soneRescueMode) {
- for (Post post : storedSone.getPosts()) {
+ for (Post post : storedSone.get().getPosts()) {
posts.remove(post.getId());
if (!sone.getPosts().contains(post)) {
eventBus.post(new PostRemovedEvent(post));
}
}
}
- List<Post> storedPosts = storedSone.getPosts();
+ List<Post> storedPosts = storedSone.get().getPosts();
synchronized (knownPosts) {
for (Post post : sone.getPosts()) {
post.setKnown(knownPosts.contains(post.getId()));
}
synchronized (replies) {
if (!soneRescueMode) {
- for (PostReply reply : storedSone.getReplies()) {
+ for (PostReply reply : storedSone.get().getReplies()) {
replies.remove(reply.getId());
if (!sone.getReplies().contains(reply)) {
eventBus.post(new PostReplyRemovedEvent(reply));
}
}
}
- Set<PostReply> storedReplies = storedSone.getReplies();
+ Set<PostReply> storedReplies = storedSone.get().getReplies();
synchronized (knownReplies) {
for (PostReply reply : sone.getReplies()) {
reply.setKnown(knownReplies.contains(reply.getId()));
}
synchronized (albums) {
synchronized (images) {
- for (Album album : storedSone.getAlbums()) {
+ for (Album album : storedSone.get().getAlbums()) {
albums.remove(album.getId());
for (Image image : album.getImages()) {
images.remove(image.getId());
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());
* The text of the post
* @return The created post
*/
- public Post createPost(Sone sone, Sone recipient, String text) {
+ public Post createPost(Sone sone, Optional<Sone> recipient, String text) {
return createPost(sone, recipient, System.currentTimeMillis(), text);
}
* The text of the post
* @return The created post
*/
- public Post createPost(Sone sone, Sone recipient, long time, String text) {
+ public Post createPost(Sone sone, Optional<Sone> recipient, long time, String text) {
checkNotNull(text, "text must not be null");
checkArgument(text.trim().length() > 0, "text must not be empty");
if (!sone.isLocal()) {
}
PostBuilder postBuilder = postBuilderFactory.newPostBuilder();
postBuilder.from(sone.getId()).randomId().withTime(time).withText(text.trim());
- if (recipient != null) {
- postBuilder.to(recipient.getId());
+ if (recipient.isPresent()) {
+ postBuilder.to(recipient.get().getId());
}
final Post post = postBuilder.build();
synchronized (posts) {
for (Post post : sone.getPosts()) {
String postPrefix = sonePrefix + "/Posts/" + postCounter++;
configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
- configuration.getStringValue(postPrefix + "/Recipient").setValue((post.getRecipient() != null) ? post.getRecipient().getId() : null);
+ configuration.getStringValue(postPrefix + "/Recipient").setValue(post.getRecipientId().orNull());
configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
}
/**
* Loads the configuration.
*/
- @SuppressWarnings("unchecked")
private void loadConfiguration() {
/* create options. */
options.addIntegerOption("InsertionDelay", new DefaultOption<Integer>(60, new IntegerRangePredicate(0, Integer.MAX_VALUE), new OptionWatcher<Integer>() {
break;
}
long time = configuration.getLongValue("SoneFollowingTimes/" + soneCounter + "/Time").getValue(Long.MAX_VALUE);
- Sone followedSone = getSone(soneId);
- if (followedSone == null) {
- logger.log(Level.WARNING, String.format("Ignoring Sone with invalid ID: %s", soneId));
- } else {
- synchronized (soneFollowingTimes) {
- soneFollowingTimes.put(soneId, time);
- }
+ synchronized (soneFollowingTimes) {
+ soneFollowingTimes.put(soneId, time);
}
++soneCounter;
}
OwnIdentity ownIdentity = ownIdentityAddedEvent.ownIdentity();
logger.log(Level.FINEST, String.format("Adding OwnIdentity: %s", ownIdentity));
if (ownIdentity.hasContext("Sone")) {
- trustedIdentities.put(ownIdentity, Collections.synchronizedSet(new HashSet<Identity>()));
addLocalSone(ownIdentity);
}
}
/* some local identity still trusts this identity, don’t remove. */
return;
}
- Sone sone = getSone(identity.getId(), false);
- if (sone == null) {
+ Optional<Sone> sone = getSone(identity.getId());
+ if (!sone.isPresent()) {
/* TODO - we don’t have the Sone anymore. should this happen? */
return;
}
synchronized (posts) {
synchronized (knownPosts) {
- for (Post post : sone.getPosts()) {
+ for (Post post : sone.get().getPosts()) {
posts.remove(post.getId());
eventBus.post(new PostRemovedEvent(post));
}
}
synchronized (replies) {
synchronized (knownReplies) {
- for (PostReply reply : sone.getReplies()) {
+ for (PostReply reply : sone.get().getReplies()) {
replies.remove(reply.getId());
eventBus.post(new PostReplyRemovedEvent(reply));
}
synchronized (sones) {
sones.remove(identity.getId());
}
- eventBus.post(new SoneRemovedEvent(sone));
+ eventBus.post(new SoneRemovedEvent(sone.get()));
}
/**