/*
- * Sone - Core.java - Copyright © 2010–2013 David Roden
+ * Sone - Core.java - Copyright © 2010–2016 David Roden
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
import java.util.logging.Level;
import java.util.logging.Logger;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidAlbumFound;
import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidImageFound;
import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidParentAlbumFound;
import net.pterodactylus.sone.core.SoneChangeDetector.PostProcessor;
import net.pterodactylus.sone.core.SoneChangeDetector.PostReplyProcessor;
import net.pterodactylus.sone.core.event.ImageInsertFinishedEvent;
+import net.pterodactylus.sone.core.event.InsertionDelayChangedEvent;
import net.pterodactylus.sone.core.event.MarkPostKnownEvent;
import net.pterodactylus.sone.core.event.MarkPostReplyKnownEvent;
import net.pterodactylus.sone.core.event.MarkSoneKnownEvent;
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.data.SoneOptions.LoadExternalContent;
import net.pterodactylus.sone.data.TemporaryImage;
import net.pterodactylus.sone.database.AlbumBuilder;
import net.pterodactylus.sone.database.Database;
import net.pterodactylus.util.thread.NamedThreadFactory;
import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.HashMultimap;
import com.google.common.eventbus.Subscribe;
import com.google.inject.Inject;
import com.google.inject.Singleton;
+import kotlin.jvm.functions.Function1;
/**
* The Sone core.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
@Singleton
public class Core extends AbstractService implements SoneProvider, PostProvider, PostReplyProvider {
/** The logger. */
- private static final Logger logger = getLogger("Sone.Core");
+ private static final Logger logger = getLogger(Core.class.getName());
/** The start time. */
private final long startupTime = System.currentTimeMillis();
/** The trust updater. */
private final WebOfTrustUpdater webOfTrustUpdater;
- /** The times Sones were followed. */
- private final Map<String, Long> soneFollowingTimes = new HashMap<String, Long>();
-
/** Locked local Sones. */
/* synchronize on itself. */
private final Set<Sone> lockedSones = new HashSet<Sone>();
* The database
*/
@Inject
- public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, WebOfTrustUpdater webOfTrustUpdater, EventBus eventBus, Database database) {
- super("Sone Core");
- this.configuration = configuration;
- this.freenetInterface = freenetInterface;
- this.identityManager = identityManager;
- this.soneDownloader = new SoneDownloaderImpl(this, freenetInterface);
- this.imageInserter = new ImageInserter(freenetInterface, freenetInterface.new InsertTokenSupplier());
- this.updateChecker = new UpdateChecker(eventBus, freenetInterface);
- this.webOfTrustUpdater = webOfTrustUpdater;
- this.eventBus = eventBus;
- this.database = database;
- preferences = new Preferences(eventBus);
- }
-
- @VisibleForTesting
- protected Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, SoneDownloader soneDownloader, ImageInserter imageInserter, UpdateChecker updateChecker, WebOfTrustUpdater webOfTrustUpdater, EventBus eventBus, Database database) {
+ public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, SoneDownloader soneDownloader, ImageInserter imageInserter, UpdateChecker updateChecker, WebOfTrustUpdater webOfTrustUpdater, EventBus eventBus, Database database) {
super("Sone Core");
this.configuration = configuration;
this.freenetInterface = freenetInterface;
/**
* {@inheritDocs}
*/
+ @Nonnull
@Override
public Collection<Sone> getSones() {
return database.getSones();
}
+ @Nonnull
@Override
- public Function<String, Optional<Sone>> soneLoader() {
- return database.soneLoader();
+ public Function1<String, Sone> getSoneLoader() {
+ return database.getSoneLoader();
}
/**
* Sone
*/
@Override
- public Optional<Sone> getSone(String id) {
+ @Nullable
+ public Sone getSone(@Nonnull String id) {
return database.getSone(id);
}
* @return The Sone with the given ID, or {@code null}
*/
public Sone getLocalSone(String id) {
- Optional<Sone> sone = database.getSone(id);
- if (sone.isPresent() && sone.get().isLocal()) {
- return sone.get();
+ Sone sone = database.getSone(id);
+ if ((sone != null) && sone.isLocal()) {
+ return sone;
}
return null;
}
* @return The Sone with the given ID
*/
public Sone getRemoteSone(String id) {
- return database.getSone(id).orNull();
+ return database.getSone(id);
}
/**
}
/**
- * Returns the time when the given was first followed by any local Sone.
- *
- * @param sone
- * The Sone to get the time for
- * @return The time (in milliseconds since Jan 1, 1970) the Sone has first
- * been followed, or {@link Long#MAX_VALUE}
- */
- public long getSoneFollowingTime(Sone sone) {
- synchronized (soneFollowingTimes) {
- return Optional.fromNullable(soneFollowingTimes.get(sone.getId())).or(Long.MAX_VALUE);
- }
- }
-
- /**
* Returns a post builder.
*
* @return A new post builder
return database.newPostBuilder();
}
- /**
- * {@inheritDoc}
- */
+ @Nullable
@Override
- public Optional<Post> getPost(String postId) {
+ public Post getPost(@Nonnull String postId) {
return database.getPost(postId);
}
/**
* {@inheritDoc}
*/
+ @Nullable
@Override
- public Optional<PostReply> getPostReply(String replyId) {
+ public PostReply getPostReply(String replyId) {
return database.getPostReply(replyId);
}
* @return The album with the given ID, or {@code null} if no album with the
* given ID exists
*/
- public Album getAlbum(String albumId) {
- return database.getAlbum(albumId).orNull();
+ @Nullable
+ public Album getAlbum(@Nonnull String albumId) {
+ return database.getAlbum(albumId);
}
public ImageBuilder imageBuilder() {
* The ID of the image
* @return The image with the given ID
*/
+ @Nullable
public Image getImage(String imageId) {
return getImage(imageId, true);
}
* @return The image with the given ID, or {@code null} if none exists and
* none was created
*/
+ @Nullable
public Image getImage(String imageId, boolean create) {
- Optional<Image> image = database.getImage(imageId);
- if (image.isPresent()) {
- return image.get();
+ Image image = database.getImage(imageId);
+ if (image != null) {
+ return image;
}
if (!create) {
return null;
Sone sone = database.newSoneBuilder().local().from(ownIdentity).build();
String property = fromNullable(ownIdentity.getProperty("Sone.LatestEdition")).or("0");
sone.setLatestEdition(fromNullable(tryParse(property)).or(0L));
- sone.setClient(new Client("Sone", SonePlugin.VERSION.toString()));
+ sone.setClient(new Client("Sone", SonePlugin.getPluginVersion()));
sone.setKnown(true);
- loadSone(sone);
- sone.setStatus(SoneStatus.idle);
- database.storeSone(sone);
SoneInserter soneInserter = new SoneInserter(this, eventBus, freenetInterface, ownIdentity.getId());
+ soneInserter.insertionDelayChanged(new InsertionDelayChangedEvent(preferences.getInsertionDelay()));
eventBus.register(soneInserter);
synchronized (soneInserters) {
soneInserters.put(sone, soneInserter);
}
+ loadSone(sone);
+ database.storeSone(sone);
+ sone.setStatus(SoneStatus.idle);
soneInserter.start();
return sone;
}
logger.log(Level.WARNING, "Given Identity is null!");
return null;
}
- final Long latestEdition = tryParse(fromNullable(
- identity.getProperty("Sone.LatestEdition")).or("0"));
- Optional<Sone> existingSone = getSone(identity.getId());
- if (existingSone.isPresent() && existingSone.get().isLocal()) {
- return existingSone.get();
+ String property = fromNullable(identity.getProperty("Sone.LatestEdition")).or("0");
+ long latestEdition = fromNullable(tryParse(property)).or(0L);
+ Sone existingSone = getSone(identity.getId());
+ if ((existingSone != null )&& existingSone.isLocal()) {
+ return existingSone;
}
- boolean newSone = !existingSone.isPresent();
- Sone sone = !newSone ? existingSone.get() : database.newSoneBuilder().from(identity).build();
+ boolean newSone = existingSone == null;
+ Sone sone = !newSone ? existingSone : database.newSoneBuilder().from(identity).build();
sone.setLatestEdition(latestEdition);
if (newSone) {
synchronized (knownSones) {
}
database.storeSone(sone);
soneDownloader.addSone(sone);
- soneDownloaders.execute(soneDownloader.fetchSoneWithUriAction(sone));
+ soneDownloaders.execute(soneDownloader.fetchSoneAsUskAction(sone));
return sone;
}
checkNotNull(sone, "sone must not be null");
checkNotNull(soneId, "soneId must not be null");
database.addFriend(sone, soneId);
- synchronized (soneFollowingTimes) {
- if (!soneFollowingTimes.containsKey(soneId)) {
- long now = System.currentTimeMillis();
- soneFollowingTimes.put(soneId, now);
- Optional<Sone> followedSone = getSone(soneId);
- if (!followedSone.isPresent()) {
- return;
- }
- for (Post post : followedSone.get().getPosts()) {
- if (post.getTime() < now) {
- markPostKnown(post);
- }
- }
- for (PostReply reply : followedSone.get().getReplies()) {
- if (reply.getTime() < now) {
- markReplyKnown(reply);
- }
- }
+ @SuppressWarnings("ConstantConditions") // we just followed, this can’t be null.
+ long now = database.getFollowingTime(soneId);
+ Sone followedSone = getSone(soneId);
+ if (followedSone == null) {
+ return;
+ }
+ for (Post post : followedSone.getPosts()) {
+ if (post.getTime() < now) {
+ markPostKnown(post);
+ }
+ }
+ for (PostReply reply : followedSone.getReplies()) {
+ if (reply.getTime() < now) {
+ markReplyKnown(reply);
}
}
touchConfiguration();
checkNotNull(sone, "sone must not be null");
checkNotNull(soneId, "soneId must not be null");
database.removeFriend(sone, soneId);
- boolean unfollowedSoneStillFollowed = false;
- for (Sone localSone : getLocalSones()) {
- unfollowedSoneStillFollowed |= localSone.hasFriend(soneId);
- }
- if (!unfollowedSoneStillFollowed) {
- synchronized (soneFollowingTimes) {
- soneFollowingTimes.remove(soneId);
- }
- }
touchConfiguration();
}
* of the age of the given Sone
*/
public void updateSone(final Sone sone, boolean soneRescueMode) {
- Optional<Sone> storedSone = getSone(sone.getId());
- if (storedSone.isPresent()) {
- if (!soneRescueMode && !(sone.getTime() > storedSone.get().getTime())) {
+ Sone storedSone = getSone(sone.getId());
+ if (storedSone != null) {
+ if (!soneRescueMode && !(sone.getTime() > storedSone.getTime())) {
logger.log(Level.FINE, String.format("Downloaded Sone %s is not newer than stored Sone %s.", sone, storedSone));
return;
}
List<Object> events =
- collectEventsForChangesInSone(storedSone.get(), sone);
+ collectEventsForChangesInSone(storedSone, sone);
database.storeSone(sone);
for (Object event : events) {
eventBus.post(event);
}
- sone.setOptions(storedSone.get().getOptions());
- sone.setKnown(storedSone.get().isKnown());
+ sone.setOptions(storedSone.getOptions());
+ sone.setKnown(storedSone.isKnown());
sone.setStatus((sone.getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle);
if (sone.isLocal()) {
touchConfiguration();
soneChangeDetector.onNewPosts(new PostProcessor() {
@Override
public void processPost(Post post) {
- if (post.getTime() < getSoneFollowingTime(newSone)) {
+ if (post.getTime() < database.getFollowingTime(newSone.getId())) {
post.setKnown(true);
} else if (!post.isKnown()) {
events.add(new NewPostFoundEvent(post));
soneChangeDetector.onNewPostReplies(new PostReplyProcessor() {
@Override
public void processPostReply(PostReply postReply) {
- if (postReply.getTime() < getSoneFollowingTime(newSone)) {
+ if (postReply.getTime() < database.getFollowingTime(newSone.getId())) {
postReply.setKnown(true);
} else if (!postReply.isKnown()) {
events.add(new NewPostReplyFoundEvent(postReply));
}
/* load options. */
- sone.getOptions().setAutoFollow(configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").getValue(null));
- sone.getOptions().setSoneInsertNotificationEnabled(configuration.getBooleanValue(sonePrefix + "/Options/EnableSoneInsertNotifications").getValue(null));
- sone.getOptions().setShowNewSoneNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewSones").getValue(null));
- sone.getOptions().setShowNewPostNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").getValue(null));
- sone.getOptions().setShowNewReplyNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").getValue(null));
- sone.getOptions().setShowCustomAvatars(ShowCustomAvatars.valueOf(configuration.getStringValue(sonePrefix + "/Options/ShowCustomAvatars").getValue(ShowCustomAvatars.NEVER.name())));
+ sone.getOptions().setAutoFollow(configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").getValue(false));
+ sone.getOptions().setSoneInsertNotificationEnabled(configuration.getBooleanValue(sonePrefix + "/Options/EnableSoneInsertNotifications").getValue(false));
+ sone.getOptions().setShowNewSoneNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewSones").getValue(true));
+ sone.getOptions().setShowNewPostNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").getValue(true));
+ sone.getOptions().setShowNewReplyNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").getValue(true));
+ sone.getOptions().setShowCustomAvatars(LoadExternalContent.valueOf(configuration.getStringValue(sonePrefix + "/Options/ShowCustomAvatars").getValue(LoadExternalContent.NEVER.name())));
+ sone.getOptions().setLoadLinkedImages(LoadExternalContent.valueOf(configuration.getStringValue(sonePrefix + "/Options/LoadLinkedImages").getValue(LoadExternalContent.NEVER.name())));
/* if we’re still here, Sone was loaded successfully. */
synchronized (sone) {
configuration.getStringValue(albumPrefix + "/Title").setValue(album.getTitle());
configuration.getStringValue(albumPrefix + "/Description").setValue(album.getDescription());
configuration.getStringValue(albumPrefix + "/Parent").setValue(album.getParent().equals(sone.getRootAlbum()) ? null : album.getParent().getId());
- configuration.getStringValue(albumPrefix + "/AlbumImage").setValue(album.getAlbumImage() == null ? null : album.getAlbumImage().getId());
}
configuration.getStringValue(sonePrefix + "/Albums/" + albumCounter + "/ID").setValue(null);
configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").setValue(sone.getOptions().isShowNewPostNotifications());
configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").setValue(sone.getOptions().isShowNewReplyNotifications());
configuration.getStringValue(sonePrefix + "/Options/ShowCustomAvatars").setValue(sone.getOptions().getShowCustomAvatars().name());
+ configuration.getStringValue(sonePrefix + "/Options/LoadLinkedImages").setValue(sone.getOptions().getLoadLinkedImages().name());
configuration.save();
configuration.getStringValue("KnownSone/" + soneCounter + "/ID").setValue(null);
}
- /* save Sone following times. */
- soneCounter = 0;
- synchronized (soneFollowingTimes) {
- for (Entry<String, Long> soneFollowingTime : soneFollowingTimes.entrySet()) {
- configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").setValue(soneFollowingTime.getKey());
- configuration.getLongValue("SoneFollowingTimes/" + soneCounter + "/Time").setValue(soneFollowingTime.getValue());
- ++soneCounter;
- }
- configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").setValue(null);
- }
-
/* save known posts. */
database.save();
knownSones.add(knownSoneId);
}
}
-
- /* load Sone following times. */
- soneCounter = 0;
- while (true) {
- String soneId = configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").getValue(null);
- if (soneId == null) {
- break;
- }
- long time = configuration.getLongValue("SoneFollowingTimes/" + soneCounter + "/Time").getValue(Long.MAX_VALUE);
- synchronized (soneFollowingTimes) {
- soneFollowingTimes.put(soneId, time);
- }
- ++soneCounter;
- }
}
/**
if (sone.isLocal()) {
return;
}
- sone.setLatestEdition(fromNullable(tryParse(identity.getProperty("Sone.LatestEdition"))).or(sone.getLatestEdition()));
+ String newLatestEdition = identity.getProperty("Sone.LatestEdition");
+ if (newLatestEdition != null) {
+ Long parsedNewLatestEdition = tryParse(newLatestEdition);
+ if (parsedNewLatestEdition != null) {
+ sone.setLatestEdition(parsedNewLatestEdition);
+ }
+ }
soneDownloader.addSone(sone);
- soneDownloaders.execute(soneDownloader.fetchSoneAction(sone));
+ soneDownloaders.execute(soneDownloader.fetchSoneAsSskAction(sone));
}
/**
return;
}
}
- Optional<Sone> sone = getSone(identity.getId());
- if (!sone.isPresent()) {
+ Sone sone = getSone(identity.getId());
+ if (sone == null) {
/* TODO - we don’t have the Sone anymore. should this happen? */
return;
}
- database.removeSone(sone.get());
- eventBus.post(new SoneRemovedEvent(sone.get()));
+ for (PostReply postReply : sone.getReplies()) {
+ eventBus.post(new PostReplyRemovedEvent(postReply));
+ }
+ for (Post post : sone.getPosts()) {
+ eventBus.post(new PostRemovedEvent(post));
+ }
+ eventBus.post(new SoneRemovedEvent(sone));
+ database.removeSone(sone);
}
/**