🔥 Remove setKnown() method from replies
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index 6525ac3..da1b2f8 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 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
@@ -24,6 +24,7 @@ import static com.google.common.primitives.Longs.tryParse;
 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.AlbumKt.getAllImages;
 
 import java.util.ArrayList;
 import java.util.Collection;
@@ -61,6 +62,7 @@ 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.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;
@@ -88,7 +90,7 @@ import net.pterodactylus.util.service.AbstractService;
 import net.pterodactylus.util.thread.NamedThreadFactory;
 
 import com.google.common.annotations.VisibleForTesting;
-import com.google.common.collect.FluentIterable;
+import com.google.common.base.Stopwatch;
 import com.google.common.collect.HashMultimap;
 import com.google.common.collect.Multimap;
 import com.google.common.collect.Multimaps;
@@ -176,25 +178,12 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        private volatile long lastConfigurationUpdate;
 
        private final MetricRegistry metricRegistry;
+       private final Histogram configurationSaveTimeHistogram;
+
+       private final SoneUriCreator soneUriCreator;
 
-       /**
-        * 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
-        */
        @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;
@@ -206,7 +195,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                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)));
        }
 
        //
@@ -622,7 +613,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                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) {
@@ -631,9 +622,10 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                loadSone(sone);
                database.storeSone(sone);
                sone.setStatus(SoneStatus.idle);
-               if (sone.getPosts().isEmpty() && sone.getReplies().isEmpty()) {
+               if (sone.getPosts().isEmpty() && sone.getReplies().isEmpty() && getAllImages(sone.getRootAlbum()).isEmpty()) {
                        // dirty hack
                        lockSone(sone);
+                       eventBus.post(new SoneLockedOnStartup(sone));
                }
                soneInserter.start();
                return sone;
@@ -746,75 +738,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        }
 
        /**
-        * Sets the trust value of the given origin Sone for the target Sone.
-        *
-        * @param origin
-        *            The origin Sone
-        * @param target
-        *            The target Sone
-        * @param trustValue
-        *            The trust value (from {@code -100} to {@code 100})
-        */
-       public void setTrust(Sone origin, Sone target, int trustValue) {
-               checkNotNull(origin, "origin must not be null");
-               checkArgument(origin.getIdentity() instanceof OwnIdentity, "origin must be a local Sone");
-               checkNotNull(target, "target must not be null");
-               checkArgument((trustValue >= -100) && (trustValue <= 100), "trustValue must be within [-100, 100]");
-               webOfTrustUpdater.setTrust((OwnIdentity) origin.getIdentity(), target.getIdentity(), trustValue, preferences.getTrustComment());
-       }
-
-       /**
-        * Removes any trust assignment for the given target Sone.
-        *
-        * @param origin
-        *            The trust origin
-        * @param target
-        *            The trust target
-        */
-       public void removeTrust(Sone origin, Sone target) {
-               checkNotNull(origin, "origin must not be null");
-               checkNotNull(target, "target must not be null");
-               checkArgument(origin.getIdentity() instanceof OwnIdentity, "origin must be a local Sone");
-               webOfTrustUpdater.setTrust((OwnIdentity) origin.getIdentity(), target.getIdentity(), null, null);
-       }
-
-       /**
-        * Assigns the configured positive trust value for the given target.
-        *
-        * @param origin
-        *            The trust origin
-        * @param target
-        *            The trust target
-        */
-       public void trustSone(Sone origin, Sone target) {
-               setTrust(origin, target, preferences.getPositiveTrust());
-       }
-
-       /**
-        * Assigns the configured negative trust value for the given target.
-        *
-        * @param origin
-        *            The trust origin
-        * @param target
-        *            The trust target
-        */
-       public void distrustSone(Sone origin, Sone target) {
-               setTrust(origin, target, preferences.getNegativeTrust());
-       }
-
-       /**
-        * Removes the trust assignment for the given target.
-        *
-        * @param origin
-        *            The trust origin
-        * @param target
-        *            The trust target
-        */
-       public void untrustSone(Sone origin, Sone target) {
-               removeTrust(origin, target);
-       }
-
-       /**
         * Updates the stored Sone with the given Sone.
         *
         * @param sone
@@ -874,9 +797,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                }
                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));
                        }
@@ -1053,7 +976,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        post.setKnown(true);
                }
                for (PostReply reply : replies) {
-                       reply.setKnown(true);
+                       database.setPostReplyKnown(reply);
                }
 
                logger.info(String.format("Sone loaded successfully: %s", sone));
@@ -1195,7 +1118,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         */
        public void markReplyKnown(PostReply reply) {
                boolean previouslyKnown = reply.isKnown();
-               reply.setKnown(true);
+               database.setPostReplyKnown(reply);
                eventBus.post(new MarkPostReplyKnownEvent(reply));
                if (!previouslyKnown) {
                        touchConfiguration();
@@ -1367,7 +1290,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                synchronized (soneInserters) {
                        for (Entry<Sone, SoneInserter> soneInserter : soneInserters.entrySet()) {
                                soneInserter.getValue().stop();
-                               saveSone(soneInserter.getKey());
+                               Sone latestSone = getLocalSone(soneInserter.getKey().getId());
+                               saveSone(latestSone);
                        }
                }
                synchronized (soneRescuers) {
@@ -1468,7 +1392,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        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) {
@@ -1546,7 +1470,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        database.save();
 
                        /* now save it. */
+                       Stopwatch stopwatch = Stopwatch.createStarted();
                        configuration.save();
+                       configurationSaveTimeHistogram.update(stopwatch.elapsed(TimeUnit.MICROSECONDS));
 
                } catch (ConfigurationException ce1) {
                        logger.log(Level.SEVERE, "Could not store configuration!", ce1);