/*
- * Sone - Core.java - Copyright © 2010–2019 David Roden
+ * Sone - Core.java - Copyright © 2010–2020 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 static java.lang.String.format;
import static java.util.logging.Level.WARNING;
import static java.util.logging.Logger.getLogger;
-import static net.pterodactylus.sone.data.AlbumsKt.getAllImages;
+import static net.pterodactylus.sone.data.AlbumKt.getAllImages;
import java.util.ArrayList;
import java.util.Collection;
import net.pterodactylus.sone.data.Reply;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.data.Sone.SoneStatus;
+import net.pterodactylus.sone.data.SoneKt;
import net.pterodactylus.sone.data.SoneOptions.LoadExternalContent;
import net.pterodactylus.sone.data.TemporaryImage;
import net.pterodactylus.sone.database.AlbumBuilder;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Stopwatch;
-import com.google.common.collect.FluentIterable;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
private final MetricRegistry metricRegistry;
private final Histogram configurationSaveTimeHistogram;
- /**
- * Creates a new core.
- *
- * @param configuration
- * The configuration of the core
- * @param freenetInterface
- * The freenet interface
- * @param identityManager
- * The identity manager
- * @param webOfTrustUpdater
- * The WebOfTrust updater
- * @param eventBus
- * The event bus
- * @param database
- * The database
- */
+ private final SoneUriCreator soneUriCreator;
+
@Inject
- public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, SoneDownloader soneDownloader, ImageInserter imageInserter, UpdateChecker updateChecker, WebOfTrustUpdater webOfTrustUpdater, EventBus eventBus, Database database, MetricRegistry metricRegistry) {
+ public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, SoneDownloader soneDownloader, ImageInserter imageInserter, UpdateChecker updateChecker, WebOfTrustUpdater webOfTrustUpdater, EventBus eventBus, Database database, MetricRegistry metricRegistry, SoneUriCreator soneUriCreator) {
super("Sone Core");
this.configuration = configuration;
this.freenetInterface = freenetInterface;
this.eventBus = eventBus;
this.database = database;
this.metricRegistry = metricRegistry;
+ this.soneUriCreator = soneUriCreator;
preferences = new Preferences(eventBus);
this.configurationSaveTimeHistogram = metricRegistry.histogram("configuration.save.duration", () -> new Histogram(new ExponentiallyDecayingReservoir(3000, 0)));
}
sone.setLatestEdition(fromNullable(tryParse(property)).or(0L));
sone.setClient(new Client("Sone", SonePlugin.getPluginVersion()));
sone.setKnown(true);
- SoneInserter soneInserter = new SoneInserter(this, eventBus, freenetInterface, metricRegistry, ownIdentity.getId());
+ SoneInserter soneInserter = new SoneInserter(this, eventBus, freenetInterface, metricRegistry, soneUriCreator, ownIdentity.getId());
soneInserter.insertionDelayChanged(new InsertionDelayChangedEvent(preferences.getInsertionDelay()));
eventBus.register(soneInserter);
synchronized (soneInserters) {
if (sone.getPosts().isEmpty() && sone.getReplies().isEmpty() && getAllImages(sone.getRootAlbum()).isEmpty()) {
// dirty hack
lockSone(sone);
+ eventBus.post(new SoneLockedOnStartup(sone));
}
soneInserter.start();
return sone;
}
for (PostReply postReply : soneComparison.getNewPostReplies()) {
if (postReply.getSone().equals(newSone)) {
- postReply.setKnown(true);
+ database.setPostReplyKnown(postReply);
} else if (postReply.getTime() < database.getFollowingTime(newSone.getId())) {
- postReply.setKnown(true);
+ database.setPostReplyKnown(postReply);
} else if (!postReply.isKnown()) {
events.add(new NewPostReplyFoundEvent(postReply));
}
post.setKnown(true);
}
for (PostReply reply : replies) {
- reply.setKnown(true);
+ database.setPostReplyKnown(reply);
}
logger.info(String.format("Sone loaded successfully: %s", sone));
*/
public void markReplyKnown(PostReply reply) {
boolean previouslyKnown = reply.isKnown();
- reply.setKnown(true);
+ database.setPostReplyKnown(reply);
eventBus.post(new MarkPostReplyKnownEvent(reply));
if (!previouslyKnown) {
touchConfiguration();
configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter + "/ID").setValue(null);
/* save albums. first, collect in a flat structure, top-level first. */
- List<Album> albums = FluentIterable.from(sone.getRootAlbum().getAlbums()).transformAndConcat(Album.FLATTENER).toList();
+ List<Album> albums = SoneKt.getAllAlbums(sone);
int albumCounter = 0;
for (Album album : albums) {
OwnIdentity ownIdentity = identityRemovedEvent.getOwnIdentity();
Identity identity = identityRemovedEvent.getIdentity();
trustedIdentities.remove(ownIdentity, identity);
- for (Entry<OwnIdentity, Collection<Identity>> trustedIdentity : trustedIdentities.asMap().entrySet()) {
- if (trustedIdentity.getKey().equals(ownIdentity)) {
- continue;
- }
- if (trustedIdentity.getValue().contains(identity)) {
- return;
- }
+ if (otherOwnIdentityStillKnowsIdentity(ownIdentity, identity)) {
+ return;
}
Sone sone = getSone(identity.getId());
if (sone == null) {
database.removeSone(sone);
}
+ private boolean otherOwnIdentityStillKnowsIdentity(OwnIdentity ownIdentity, Identity identity) {
+ return trustedIdentities.entries().stream().anyMatch(e -> !e.getKey().equals(ownIdentity) && e.getValue().equals(identity));
+ }
+
/**
* Deletes the temporary image.
*