Remove Sone instance from SoneUpdater interface.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / SoneDownloaderImpl.java
index ca47132..d59f1f5 100644 (file)
@@ -99,22 +99,23 @@ public class SoneDownloaderImpl extends AbstractService implements SoneDownloade
         *              The Sone to add
         */
        @Override
-       public void addSone(Sone sone) {
+       public void addSone(final Sone sone) {
                if (!sones.add(sone)) {
                        freenetInterface.unregisterUsk(sone);
                }
-               freenetInterface.registerUsk(sone, this);
+               freenetInterface.registerUsk(sone, new SoneUpdater() {
+                       @Override
+                       public void updateSone(long edition) {
+                               if (edition > sone.getLatestEdition()) {
+                                       sone.setLatestEdition(edition);
+                                       new Thread(fetchSoneAction(sone),
+                                                       "Sone Downloader").start();
+                               }
+                       }
+               });
        }
 
-       /**
-        * Fetches the updated Sone. This method is a callback method for
-        * {@link FreenetInterface#registerUsk(Sone, SoneDownloader)}.
-        *
-        * @param sone
-        *              The Sone to fetch
-        */
-       @Override
-       public void fetchSone(Sone sone) {
+       private void fetchSone(Sone sone) {
                fetchSone(sone, sone.getRequestUri().sskForUSK());
        }
 
@@ -449,13 +450,20 @@ public class SoneDownloaderImpl extends AbstractService implements SoneDownloade
                                }
                                Album parent = null;
                                if (parentId != null) {
-                                       parent = core.getAlbum(parentId, false);
+                                       parent = core.getAlbum(parentId);
                                        if (parent == null) {
                                                logger.log(Level.WARNING, String.format("Downloaded Sone %s has album with invalid parent!", sone));
                                                return null;
                                        }
                                }
-                               Album album = core.getOrCreateAlbum(id).setSone(sone).modify().setTitle(title).setDescription(description).update();
+                               Album album = core.albumBuilder()
+                                               .withId(id)
+                                               .by(sone)
+                                               .build()
+                                               .modify()
+                                               .setTitle(title)
+                                               .setDescription(description)
+                                               .update();
                                if (parent != null) {
                                        parent.addAlbum(album);
                                } else {