X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FCore.java;h=91825a61ab256e28b3901994c85fe71041714b6e;hp=dc5e207125ea0f2e6d88359c2bf3da43714ebb5b;hb=7b55e0be6a3283e43a9bbab98f82aebdd948eb33;hpb=161c216be80f6f0438bacd2642b1aa0091ba1b1f diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index dc5e207..91825a6 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -1,5 +1,5 @@ /* - * 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 @@ -96,6 +96,7 @@ import net.pterodactylus.util.service.AbstractService; 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; @@ -115,7 +116,7 @@ import com.google.inject.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(); @@ -203,14 +204,14 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, * The database */ @Inject - public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, WebOfTrustUpdater webOfTrustUpdater, EventBus eventBus, Database database) { + public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, UpdateChecker updateChecker, 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.updateChecker = updateChecker; this.webOfTrustUpdater = webOfTrustUpdater; this.eventBus = eventBus; this.database = database; @@ -318,6 +319,11 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, return database.getSones(); } + @Override + public Function> soneLoader() { + return database.soneLoader(); + } + /** * Returns the Sone with the given ID, regardless whether it’s local or * remote. @@ -635,8 +641,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, } logger.info(String.format("Adding Sone from OwnIdentity: %s", ownIdentity)); Sone sone = database.newSoneBuilder().local().from(ownIdentity).build(); - sone.setLatestEdition(fromNullable(tryParse(ownIdentity.getProperty("Sone.LatestEdition"))).or(0L)); - sone.setClient(new Client("Sone", SonePlugin.VERSION.toString())); + String property = fromNullable(ownIdentity.getProperty("Sone.LatestEdition")).or("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, ownIdentity.getId()); eventBus.register(soneInserter); @@ -644,6 +651,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, soneInserters.put(sone, soneInserter); } loadSone(sone); + database.storeSone(sone); sone.setStatus(SoneStatus.idle); soneInserter.start(); return sone; @@ -680,8 +688,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, logger.log(Level.WARNING, "Given Identity is null!"); return null; } - final Long latestEdition = tryParse(fromNullable( - identity.getProperty("Sone.LatestEdition")).or("0")); + String property = fromNullable(identity.getProperty("Sone.LatestEdition")).or("0"); + long latestEdition = fromNullable(tryParse(property)).or(0L); Optional existingSone = getSone(identity.getId()); if (existingSone.isPresent() && existingSone.get().isLocal()) { return existingSone.get(); @@ -720,7 +728,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, public void followSone(Sone sone, String soneId) { checkNotNull(sone, "sone must not be null"); checkNotNull(soneId, "soneId must not be null"); - sone.addFriend(soneId); + database.addFriend(sone, soneId); synchronized (soneFollowingTimes) { if (!soneFollowingTimes.containsKey(soneId)) { long now = System.currentTimeMillis(); @@ -755,7 +763,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, public void unfollowSone(Sone sone, String soneId) { checkNotNull(sone, "sone must not be null"); checkNotNull(soneId, "soneId must not be null"); - sone.removeFriend(soneId); + database.removeFriend(sone, soneId); boolean unfollowedSoneStillFollowed = false; for (Sone localSone : getLocalSones()) { unfollowedSoneStillFollowed |= localSone.hasFriend(soneId); @@ -1021,9 +1029,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, Set likedReplyIds = configurationSoneParser.parseLikedPostReplyIds(); - /* load friends. */ - Set friends = configurationSoneParser.parseFriends(); - /* load albums. */ List topLevelAlbums; try { @@ -1060,11 +1065,11 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, } /* 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().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(ShowCustomAvatars.valueOf(configuration.getStringValue(sonePrefix + "/Options/ShowCustomAvatars").getValue(ShowCustomAvatars.NEVER.name()))); /* if we’re still here, Sone was loaded successfully. */ @@ -1075,25 +1080,16 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, sone.setReplies(replies); sone.setLikePostIds(likedPostIds); sone.setLikeReplyIds(likedReplyIds); - for (String friendId : friends) { - followSone(sone, friendId); - } for (Album album : sone.getRootAlbum().getAlbums()) { sone.getRootAlbum().removeAlbum(album); } for (Album album : topLevelAlbums) { sone.getRootAlbum().addAlbum(album); } - database.storeSone(sone); synchronized (soneInserters) { soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint); } } - synchronized (knownSones) { - for (String friend : friends) { - knownSones.add(friend); - } - } for (Post post : posts) { post.setKnown(true); } @@ -1415,6 +1411,11 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, saveSone(soneInserter.getKey()); } } + synchronized (soneRescuers) { + for (SoneRescuer soneRescuer : soneRescuers.values()) { + soneRescuer.stop(); + } + } saveConfiguration(); database.stop(); webOfTrustUpdater.stop(); @@ -1507,13 +1508,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, } configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter + "/ID").setValue(null); - /* save friends. */ - int friendCounter = 0; - for (String friendId : sone.getFriends()) { - configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter++ + "/ID").setValue(friendId); - } - configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(null); - /* save albums. first, collect in a flat structure, top-level first. */ List albums = FluentIterable.from(sone.getRootAlbum().getAlbums()).transformAndConcat(Album.FLATTENER).toList(); @@ -1524,7 +1518,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, 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); @@ -1736,8 +1729,14 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, /* TODO - we don’t have the Sone anymore. should this happen? */ return; } - database.removeSone(sone.get()); + for (PostReply postReply : sone.get().getReplies()) { + eventBus.post(new PostReplyRemovedEvent(postReply)); + } + for (Post post : sone.get().getPosts()) { + eventBus.post(new PostRemovedEvent(post)); + } eventBus.post(new SoneRemovedEvent(sone.get())); + database.removeSone(sone.get()); } /**