2 * Sone - Core.java - Copyright © 2010–2019 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.core;
20 import static com.google.common.base.Optional.fromNullable;
21 import static com.google.common.base.Preconditions.checkArgument;
22 import static com.google.common.base.Preconditions.checkNotNull;
23 import static com.google.common.primitives.Longs.tryParse;
24 import static java.lang.String.format;
25 import static java.util.logging.Level.WARNING;
26 import static java.util.logging.Logger.getLogger;
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.HashMap;
31 import java.util.HashSet;
32 import java.util.List;
34 import java.util.Map.Entry;
36 import java.util.concurrent.ExecutorService;
37 import java.util.concurrent.Executors;
38 import java.util.concurrent.ScheduledExecutorService;
39 import java.util.concurrent.TimeUnit;
40 import java.util.logging.Level;
41 import java.util.logging.Logger;
43 import javax.annotation.Nonnull;
44 import javax.annotation.Nullable;
46 import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidAlbumFound;
47 import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidImageFound;
48 import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidParentAlbumFound;
49 import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidPostFound;
50 import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidPostReplyFound;
51 import net.pterodactylus.sone.core.event.ImageInsertFinishedEvent;
52 import net.pterodactylus.sone.core.event.InsertionDelayChangedEvent;
53 import net.pterodactylus.sone.core.event.MarkPostKnownEvent;
54 import net.pterodactylus.sone.core.event.MarkPostReplyKnownEvent;
55 import net.pterodactylus.sone.core.event.MarkSoneKnownEvent;
56 import net.pterodactylus.sone.core.event.NewPostFoundEvent;
57 import net.pterodactylus.sone.core.event.NewPostReplyFoundEvent;
58 import net.pterodactylus.sone.core.event.NewSoneFoundEvent;
59 import net.pterodactylus.sone.core.event.PostRemovedEvent;
60 import net.pterodactylus.sone.core.event.PostReplyRemovedEvent;
61 import net.pterodactylus.sone.core.event.SoneLockedEvent;
62 import net.pterodactylus.sone.core.event.SoneRemovedEvent;
63 import net.pterodactylus.sone.core.event.SoneUnlockedEvent;
64 import net.pterodactylus.sone.data.Album;
65 import net.pterodactylus.sone.data.Client;
66 import net.pterodactylus.sone.data.Image;
67 import net.pterodactylus.sone.data.Post;
68 import net.pterodactylus.sone.data.PostReply;
69 import net.pterodactylus.sone.data.Profile;
70 import net.pterodactylus.sone.data.Profile.Field;
71 import net.pterodactylus.sone.data.Reply;
72 import net.pterodactylus.sone.data.Sone;
73 import net.pterodactylus.sone.data.Sone.SoneStatus;
74 import net.pterodactylus.sone.data.SoneOptions.LoadExternalContent;
75 import net.pterodactylus.sone.data.TemporaryImage;
76 import net.pterodactylus.sone.database.AlbumBuilder;
77 import net.pterodactylus.sone.database.Database;
78 import net.pterodactylus.sone.database.DatabaseException;
79 import net.pterodactylus.sone.database.ImageBuilder;
80 import net.pterodactylus.sone.database.PostBuilder;
81 import net.pterodactylus.sone.database.PostProvider;
82 import net.pterodactylus.sone.database.PostReplyBuilder;
83 import net.pterodactylus.sone.database.PostReplyProvider;
84 import net.pterodactylus.sone.database.SoneBuilder;
85 import net.pterodactylus.sone.database.SoneProvider;
86 import net.pterodactylus.sone.freenet.wot.Identity;
87 import net.pterodactylus.sone.freenet.wot.IdentityManager;
88 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
89 import net.pterodactylus.sone.freenet.wot.event.IdentityAddedEvent;
90 import net.pterodactylus.sone.freenet.wot.event.IdentityRemovedEvent;
91 import net.pterodactylus.sone.freenet.wot.event.IdentityUpdatedEvent;
92 import net.pterodactylus.sone.freenet.wot.event.OwnIdentityAddedEvent;
93 import net.pterodactylus.sone.freenet.wot.event.OwnIdentityRemovedEvent;
94 import net.pterodactylus.sone.main.SonePlugin;
95 import net.pterodactylus.util.config.Configuration;
96 import net.pterodactylus.util.config.ConfigurationException;
97 import net.pterodactylus.util.service.AbstractService;
98 import net.pterodactylus.util.thread.NamedThreadFactory;
100 import com.google.common.annotations.VisibleForTesting;
101 import com.google.common.base.Optional;
102 import com.google.common.collect.FluentIterable;
103 import com.google.common.collect.HashMultimap;
104 import com.google.common.collect.Multimap;
105 import com.google.common.collect.Multimaps;
106 import com.google.common.eventbus.EventBus;
107 import com.google.common.eventbus.Subscribe;
108 import com.google.inject.Inject;
109 import com.google.inject.Singleton;
110 import kotlin.jvm.functions.Function1;
116 public class Core extends AbstractService implements SoneProvider, PostProvider, PostReplyProvider {
119 private static final Logger logger = getLogger(Core.class.getName());
121 /** The start time. */
122 private final long startupTime = System.currentTimeMillis();
124 /** The preferences. */
125 private final Preferences preferences;
127 /** The event bus. */
128 private final EventBus eventBus;
130 /** The configuration. */
131 private final Configuration configuration;
133 /** Whether we’re currently saving the configuration. */
134 private boolean storingConfiguration = false;
136 /** The identity manager. */
137 private final IdentityManager identityManager;
139 /** Interface to freenet. */
140 private final FreenetInterface freenetInterface;
142 /** The Sone downloader. */
143 private final SoneDownloader soneDownloader;
145 /** The image inserter. */
146 private final ImageInserter imageInserter;
148 /** Sone downloader thread-pool. */
149 private final ExecutorService soneDownloaders = Executors.newFixedThreadPool(10, new NamedThreadFactory("Sone Downloader %2$d"));
151 /** The update checker. */
152 private final UpdateChecker updateChecker;
154 /** The trust updater. */
155 private final WebOfTrustUpdater webOfTrustUpdater;
157 /** Locked local Sones. */
158 /* synchronize on itself. */
159 private final Set<Sone> lockedSones = new HashSet<>();
161 /** Sone inserters. */
162 /* synchronize access on this on sones. */
163 private final Map<Sone, SoneInserter> soneInserters = new HashMap<>();
165 /** Sone rescuers. */
166 /* synchronize access on this on sones. */
167 private final Map<Sone, SoneRescuer> soneRescuers = new HashMap<>();
169 /** All known Sones. */
170 private final Set<String> knownSones = new HashSet<>();
172 /** The post database. */
173 private final Database database;
175 /** Trusted identities, sorted by own identities. */
176 private final Multimap<OwnIdentity, Identity> trustedIdentities = Multimaps.synchronizedSetMultimap(HashMultimap.<OwnIdentity, Identity>create());
178 /** All temporary images. */
179 private final Map<String, TemporaryImage> temporaryImages = new HashMap<>();
181 /** Ticker for threads that mark own elements as known. */
182 private final ScheduledExecutorService localElementTicker = Executors.newScheduledThreadPool(1);
184 /** The time the configuration was last touched. */
185 private volatile long lastConfigurationUpdate;
188 * Creates a new core.
190 * @param configuration
191 * The configuration of the core
192 * @param freenetInterface
193 * The freenet interface
194 * @param identityManager
195 * The identity manager
196 * @param webOfTrustUpdater
197 * The WebOfTrust updater
204 public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, SoneDownloader soneDownloader, ImageInserter imageInserter, UpdateChecker updateChecker, WebOfTrustUpdater webOfTrustUpdater, EventBus eventBus, Database database) {
206 this.configuration = configuration;
207 this.freenetInterface = freenetInterface;
208 this.identityManager = identityManager;
209 this.soneDownloader = soneDownloader;
210 this.imageInserter = imageInserter;
211 this.updateChecker = updateChecker;
212 this.webOfTrustUpdater = webOfTrustUpdater;
213 this.eventBus = eventBus;
214 this.database = database;
215 preferences = new Preferences(eventBus);
223 * Returns the time Sone was started.
225 * @return The startup time (in milliseconds since Jan 1, 1970 UTC)
227 public long getStartupTime() {
232 * Returns the options used by the core.
234 * @return The options of the core
236 public Preferences getPreferences() {
241 * Returns the identity manager used by the core.
243 * @return The identity manager
245 public IdentityManager getIdentityManager() {
246 return identityManager;
250 * Returns the update checker.
252 * @return The update checker
254 public UpdateChecker getUpdateChecker() {
255 return updateChecker;
259 * Returns the Sone rescuer for the given local Sone.
262 * The local Sone to get the rescuer for
263 * @return The Sone rescuer for the given Sone
265 public SoneRescuer getSoneRescuer(Sone sone) {
266 checkNotNull(sone, "sone must not be null");
267 checkArgument(sone.isLocal(), "sone must be local");
268 synchronized (soneRescuers) {
269 SoneRescuer soneRescuer = soneRescuers.get(sone);
270 if (soneRescuer == null) {
271 soneRescuer = new SoneRescuer(this, soneDownloader, sone);
272 soneRescuers.put(sone, soneRescuer);
280 * Returns whether the given Sone is currently locked.
284 * @return {@code true} if the Sone is locked, {@code false} if it is not
286 public boolean isLocked(Sone sone) {
287 synchronized (lockedSones) {
288 return lockedSones.contains(sone);
292 public SoneBuilder soneBuilder() {
293 return database.newSoneBuilder();
301 public Collection<Sone> getSones() {
302 return database.getSones();
307 public Function1<String, Sone> getSoneLoader() {
308 return database.getSoneLoader();
312 * Returns the Sone with the given ID, regardless whether it’s local or
316 * The ID of the Sone to get
317 * @return The Sone with the given ID, or {@code null} if there is no such
322 public Sone getSone(@Nonnull String id) {
323 return database.getSone(id);
330 public Collection<Sone> getLocalSones() {
331 return database.getLocalSones();
335 * Returns the local Sone with the given ID, optionally creating a new Sone.
339 * @return The Sone with the given ID, or {@code null}
341 public Sone getLocalSone(String id) {
342 Sone sone = database.getSone(id);
343 if ((sone != null) && sone.isLocal()) {
353 public Collection<Sone> getRemoteSones() {
354 return database.getRemoteSones();
358 * Returns the remote Sone with the given ID.
362 * The ID of the remote Sone to get
363 * @return The Sone with the given ID
365 public Sone getRemoteSone(String id) {
366 return database.getSone(id);
370 * Returns whether the given Sone has been modified.
373 * The Sone to check for modifications
374 * @return {@code true} if a modification has been detected in the Sone,
375 * {@code false} otherwise
377 public boolean isModifiedSone(Sone sone) {
378 return soneInserters.containsKey(sone) && soneInserters.get(sone).isModified();
382 * Returns a post builder.
384 * @return A new post builder
386 public PostBuilder postBuilder() {
387 return database.newPostBuilder();
392 public Post getPost(@Nonnull String postId) {
393 return database.getPost(postId);
400 public Collection<Post> getPosts(String soneId) {
401 return database.getPosts(soneId);
408 public Collection<Post> getDirectedPosts(final String recipientId) {
409 checkNotNull(recipientId, "recipient must not be null");
410 return database.getDirectedPosts(recipientId);
414 * Returns a post reply builder.
416 * @return A new post reply builder
418 public PostReplyBuilder postReplyBuilder() {
419 return database.newPostReplyBuilder();
427 public PostReply getPostReply(String replyId) {
428 return database.getPostReply(replyId);
435 public List<PostReply> getReplies(final String postId) {
436 return database.getReplies(postId);
440 * Returns all Sones that have liked the given post.
443 * The post to get the liking Sones for
444 * @return The Sones that like the given post
446 public Set<Sone> getLikes(Post post) {
447 Set<Sone> sones = new HashSet<>();
448 for (Sone sone : getSones()) {
449 if (sone.getLikedPostIds().contains(post.getId())) {
457 * Returns all Sones that have liked the given reply.
460 * The reply to get the liking Sones for
461 * @return The Sones that like the given reply
463 public Set<Sone> getLikes(PostReply reply) {
464 Set<Sone> sones = new HashSet<>();
465 for (Sone sone : getSones()) {
466 if (sone.getLikedReplyIds().contains(reply.getId())) {
474 * Returns whether the given post is bookmarked.
478 * @return {@code true} if the given post is bookmarked, {@code false}
481 public boolean isBookmarked(Post post) {
482 return database.isPostBookmarked(post);
486 * Returns all currently known bookmarked posts.
488 * @return All bookmarked posts
490 public Set<Post> getBookmarkedPosts() {
491 return database.getBookmarkedPosts();
494 public AlbumBuilder albumBuilder() {
495 return database.newAlbumBuilder();
499 * Returns the album with the given ID, optionally creating a new album if
500 * an album with the given ID can not be found.
503 * The ID of the album
504 * @return The album with the given ID, or {@code null} if no album with the
508 public Album getAlbum(@Nonnull String albumId) {
509 return database.getAlbum(albumId);
512 public ImageBuilder imageBuilder() {
513 return database.newImageBuilder();
517 * Returns the image with the given ID, creating it if necessary.
520 * The ID of the image
521 * @return The image with the given ID
524 public Image getImage(String imageId) {
525 return getImage(imageId, true);
529 * Returns the image with the given ID, optionally creating it if it does
533 * The ID of the image
535 * {@code true} to create an image if none exists with the given
537 * @return The image with the given ID, or {@code null} if none exists and
541 public Image getImage(String imageId, boolean create) {
542 Image image = database.getImage(imageId);
549 Image newImage = database.newImageBuilder().withId(imageId).build();
550 database.storeImage(newImage);
555 * Returns the temporary image with the given ID.
558 * The ID of the temporary image
559 * @return The temporary image, or {@code null} if there is no temporary
560 * image with the given ID
562 public TemporaryImage getTemporaryImage(String imageId) {
563 synchronized (temporaryImages) {
564 return temporaryImages.get(imageId);
573 * Locks the given Sone. A locked Sone will not be inserted by
574 * {@link SoneInserter} until it is {@link #unlockSone(Sone) unlocked}
580 public void lockSone(Sone sone) {
581 synchronized (lockedSones) {
582 if (lockedSones.add(sone)) {
583 eventBus.post(new SoneLockedEvent(sone));
589 * Unlocks the given Sone.
591 * @see #lockSone(Sone)
595 public void unlockSone(Sone sone) {
596 synchronized (lockedSones) {
597 if (lockedSones.remove(sone)) {
598 eventBus.post(new SoneUnlockedEvent(sone));
604 * Adds a local Sone from the given own identity.
607 * The own identity to create a Sone from
608 * @return The added (or already existing) Sone
610 public Sone addLocalSone(OwnIdentity ownIdentity) {
611 if (ownIdentity == null) {
612 logger.log(Level.WARNING, "Given OwnIdentity is null!");
615 logger.info(String.format("Adding Sone from OwnIdentity: %s", ownIdentity));
616 Sone sone = database.newSoneBuilder().local().from(ownIdentity).build();
617 String property = fromNullable(ownIdentity.getProperty("Sone.LatestEdition")).or("0");
618 sone.setLatestEdition(fromNullable(tryParse(property)).or(0L));
619 sone.setClient(new Client("Sone", SonePlugin.getPluginVersion()));
621 SoneInserter soneInserter = new SoneInserter(this, eventBus, freenetInterface, ownIdentity.getId());
622 soneInserter.insertionDelayChanged(new InsertionDelayChangedEvent(preferences.getInsertionDelay()));
623 eventBus.register(soneInserter);
624 synchronized (soneInserters) {
625 soneInserters.put(sone, soneInserter);
628 database.storeSone(sone);
629 sone.setStatus(SoneStatus.idle);
630 soneInserter.start();
635 * Creates a new Sone for the given own identity.
638 * The own identity to create a Sone for
639 * @return The created Sone
641 public Sone createSone(OwnIdentity ownIdentity) {
642 if (!webOfTrustUpdater.addContextWait(ownIdentity, "Sone")) {
643 logger.log(Level.SEVERE, String.format("Could not add “Sone” context to own identity: %s", ownIdentity));
646 Sone sone = addLocalSone(ownIdentity);
648 followSone(sone, "nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI");
649 touchConfiguration();
654 * Adds the Sone of the given identity.
657 * The identity whose Sone to add
658 * @return The added or already existing Sone
660 public Sone addRemoteSone(Identity identity) {
661 if (identity == null) {
662 logger.log(Level.WARNING, "Given Identity is null!");
665 String property = fromNullable(identity.getProperty("Sone.LatestEdition")).or("0");
666 long latestEdition = fromNullable(tryParse(property)).or(0L);
667 Sone existingSone = getSone(identity.getId());
668 if ((existingSone != null )&& existingSone.isLocal()) {
671 boolean newSone = existingSone == null;
672 Sone sone = !newSone ? existingSone : database.newSoneBuilder().from(identity).build();
673 sone.setLatestEdition(latestEdition);
675 synchronized (knownSones) {
676 newSone = !knownSones.contains(sone.getId());
678 sone.setKnown(!newSone);
680 eventBus.post(new NewSoneFoundEvent(sone));
681 for (Sone localSone : getLocalSones()) {
682 if (localSone.getOptions().isAutoFollow()) {
683 followSone(localSone, sone.getId());
688 database.storeSone(sone);
689 soneDownloader.addSone(sone);
690 soneDownloaders.execute(soneDownloader.fetchSoneAsUskAction(sone));
695 * Lets the given local Sone follow the Sone with the given ID.
698 * The local Sone that should follow another Sone
700 * The ID of the Sone to follow
702 public void followSone(Sone sone, String soneId) {
703 checkNotNull(sone, "sone must not be null");
704 checkNotNull(soneId, "soneId must not be null");
705 database.addFriend(sone, soneId);
706 @SuppressWarnings("ConstantConditions") // we just followed, this can’t be null.
707 long now = database.getFollowingTime(soneId);
708 Sone followedSone = getSone(soneId);
709 if (followedSone == null) {
712 for (Post post : followedSone.getPosts()) {
713 if (post.getTime() < now) {
717 for (PostReply reply : followedSone.getReplies()) {
718 if (reply.getTime() < now) {
719 markReplyKnown(reply);
722 touchConfiguration();
726 * Lets the given local Sone unfollow the Sone with the given ID.
729 * The local Sone that should unfollow another Sone
731 * The ID of the Sone being unfollowed
733 public void unfollowSone(Sone sone, String soneId) {
734 checkNotNull(sone, "sone must not be null");
735 checkNotNull(soneId, "soneId must not be null");
736 database.removeFriend(sone, soneId);
737 touchConfiguration();
741 * Sets the trust value of the given origin Sone for the target Sone.
748 * The trust value (from {@code -100} to {@code 100})
750 public void setTrust(Sone origin, Sone target, int trustValue) {
751 checkNotNull(origin, "origin must not be null");
752 checkArgument(origin.getIdentity() instanceof OwnIdentity, "origin must be a local Sone");
753 checkNotNull(target, "target must not be null");
754 checkArgument((trustValue >= -100) && (trustValue <= 100), "trustValue must be within [-100, 100]");
755 webOfTrustUpdater.setTrust((OwnIdentity) origin.getIdentity(), target.getIdentity(), trustValue, preferences.getTrustComment());
759 * Removes any trust assignment for the given target Sone.
766 public void removeTrust(Sone origin, Sone target) {
767 checkNotNull(origin, "origin must not be null");
768 checkNotNull(target, "target must not be null");
769 checkArgument(origin.getIdentity() instanceof OwnIdentity, "origin must be a local Sone");
770 webOfTrustUpdater.setTrust((OwnIdentity) origin.getIdentity(), target.getIdentity(), null, null);
774 * Assigns the configured positive trust value for the given target.
781 public void trustSone(Sone origin, Sone target) {
782 setTrust(origin, target, preferences.getPositiveTrust());
786 * Assigns the configured negative trust value for the given target.
793 public void distrustSone(Sone origin, Sone target) {
794 setTrust(origin, target, preferences.getNegativeTrust());
798 * Removes the trust assignment for the given target.
805 public void untrustSone(Sone origin, Sone target) {
806 removeTrust(origin, target);
810 * Updates the stored Sone with the given Sone.
815 public void updateSone(Sone sone) {
816 updateSone(sone, false);
820 * Updates the stored Sone with the given Sone. If {@code soneRescueMode} is
821 * {@code true}, an older Sone than the current Sone can be given to restore
826 * @param soneRescueMode
827 * {@code true} if the stored Sone should be updated regardless
828 * of the age of the given Sone
830 public void updateSone(final Sone sone, boolean soneRescueMode) {
831 Sone storedSone = getSone(sone.getId());
832 if (storedSone != null) {
833 if (!soneRescueMode && !(sone.getTime() > storedSone.getTime())) {
834 logger.log(Level.FINE, String.format("Downloaded Sone %s is not newer than stored Sone %s.", sone, storedSone));
837 List<Object> events =
838 collectEventsForChangesInSone(storedSone, sone);
839 database.storeSone(sone);
840 for (Object event : events) {
841 eventBus.post(event);
843 sone.setOptions(storedSone.getOptions());
844 sone.setKnown(storedSone.isKnown());
845 sone.setStatus((sone.getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle);
846 if (sone.isLocal()) {
847 touchConfiguration();
852 private List<Object> collectEventsForChangesInSone(Sone oldSone, Sone newSone) {
853 List<Object> events = new ArrayList<>();
854 SoneComparison soneComparison = new SoneComparison(oldSone, newSone);
855 for (Post newPost : soneComparison.getNewPosts()) {
856 if (newPost.getSone().equals(newSone)) {
857 newPost.setKnown(true);
858 } else if (newPost.getTime() < database.getFollowingTime(newSone.getId())) {
859 newPost.setKnown(true);
860 } else if (!newPost.isKnown()) {
861 events.add(new NewPostFoundEvent(newPost));
864 for (Post post : soneComparison.getRemovedPosts()) {
865 events.add(new PostRemovedEvent(post));
867 for (PostReply postReply : soneComparison.getNewPostReplies()) {
868 if (postReply.getSone().equals(newSone)) {
869 postReply.setKnown(true);
870 } else if (postReply.getTime() < database.getFollowingTime(newSone.getId())) {
871 postReply.setKnown(true);
872 } else if (!postReply.isKnown()) {
873 events.add(new NewPostReplyFoundEvent(postReply));
876 for (PostReply postReply : soneComparison.getRemovedPostReplies()) {
877 events.add(new PostReplyRemovedEvent(postReply));
883 * Deletes the given Sone. This will remove the Sone from the
884 * {@link #getLocalSones() local Sones}, stop its {@link SoneInserter} and
885 * remove the context from its identity.
890 public void deleteSone(Sone sone) {
891 if (!(sone.getIdentity() instanceof OwnIdentity)) {
892 logger.log(Level.WARNING, String.format("Tried to delete Sone of non-own identity: %s", sone));
895 if (!getLocalSones().contains(sone)) {
896 logger.log(Level.WARNING, String.format("Tried to delete non-local Sone: %s", sone));
899 SoneInserter soneInserter = soneInserters.remove(sone);
901 database.removeSone(sone);
902 webOfTrustUpdater.removeContext((OwnIdentity) sone.getIdentity(), "Sone");
903 webOfTrustUpdater.removeProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition");
905 configuration.getLongValue("Sone/" + sone.getId() + "/Time").setValue(null);
906 } catch (ConfigurationException ce1) {
907 logger.log(Level.WARNING, "Could not remove Sone from configuration!", ce1);
912 * Marks the given Sone as known. If the Sone was not {@link Post#isKnown()
913 * known} before, a {@link MarkSoneKnownEvent} is fired.
916 * The Sone to mark as known
918 public void markSoneKnown(Sone sone) {
919 if (!sone.isKnown()) {
921 synchronized (knownSones) {
922 knownSones.add(sone.getId());
924 eventBus.post(new MarkSoneKnownEvent(sone));
925 touchConfiguration();
930 * Loads and updates the given Sone from the configuration. If any error is
931 * encountered, loading is aborted and the given Sone is not changed.
934 * The Sone to load and update
936 public void loadSone(Sone sone) {
937 if (!sone.isLocal()) {
938 logger.log(Level.FINE, String.format("Tried to load non-local Sone: %s", sone));
941 logger.info(String.format("Loading local Sone: %s", sone));
944 String sonePrefix = "Sone/" + sone.getId();
945 Long soneTime = configuration.getLongValue(sonePrefix + "/Time").getValue(null);
946 if (soneTime == null) {
947 logger.log(Level.INFO, "Could not load Sone because no Sone has been saved.");
950 String lastInsertFingerprint = configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").getValue("");
953 ConfigurationSoneParser configurationSoneParser = new ConfigurationSoneParser(configuration, sone);
954 Profile profile = configurationSoneParser.parseProfile();
957 Collection<Post> posts;
959 posts = configurationSoneParser.parsePosts(database);
960 } catch (InvalidPostFound ipf) {
961 logger.log(Level.WARNING, "Invalid post found, aborting load!");
966 Collection<PostReply> replies;
968 replies = configurationSoneParser.parsePostReplies(database);
969 } catch (InvalidPostReplyFound iprf) {
970 logger.log(Level.WARNING, "Invalid reply found, aborting load!");
974 /* load post likes. */
975 Set<String> likedPostIds =
976 configurationSoneParser.parseLikedPostIds();
978 /* load reply likes. */
979 Set<String> likedReplyIds =
980 configurationSoneParser.parseLikedPostReplyIds();
983 List<Album> topLevelAlbums;
986 configurationSoneParser.parseTopLevelAlbums(database);
987 } catch (InvalidAlbumFound iaf) {
988 logger.log(Level.WARNING, "Invalid album found, aborting load!");
990 } catch (InvalidParentAlbumFound ipaf) {
991 logger.log(Level.WARNING, format("Invalid parent album ID: %s",
992 ipaf.getAlbumParentId()));
998 configurationSoneParser.parseImages(database);
999 } catch (InvalidImageFound iif) {
1000 logger.log(WARNING, "Invalid image found, aborting load!");
1002 } catch (InvalidParentAlbumFound ipaf) {
1003 logger.log(Level.WARNING,
1004 format("Invalid album image (%s) encountered, aborting load!",
1005 ipaf.getAlbumParentId()));
1010 String avatarId = configuration.getStringValue(sonePrefix + "/Profile/Avatar").getValue(null);
1011 if (avatarId != null) {
1012 final Map<String, Image> images =
1013 configurationSoneParser.getImages();
1014 profile.setAvatar(images.get(avatarId));
1018 sone.getOptions().setAutoFollow(configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").getValue(false));
1019 sone.getOptions().setSoneInsertNotificationEnabled(configuration.getBooleanValue(sonePrefix + "/Options/EnableSoneInsertNotifications").getValue(false));
1020 sone.getOptions().setShowNewSoneNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewSones").getValue(true));
1021 sone.getOptions().setShowNewPostNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").getValue(true));
1022 sone.getOptions().setShowNewReplyNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").getValue(true));
1023 sone.getOptions().setShowCustomAvatars(LoadExternalContent.valueOf(configuration.getStringValue(sonePrefix + "/Options/ShowCustomAvatars").getValue(LoadExternalContent.NEVER.name())));
1024 sone.getOptions().setLoadLinkedImages(LoadExternalContent.valueOf(configuration.getStringValue(sonePrefix + "/Options/LoadLinkedImages").getValue(LoadExternalContent.NEVER.name())));
1026 /* if we’re still here, Sone was loaded successfully. */
1027 synchronized (sone) {
1028 sone.setTime(soneTime);
1029 sone.setProfile(profile);
1030 sone.setPosts(posts);
1031 sone.setReplies(replies);
1032 sone.setLikePostIds(likedPostIds);
1033 sone.setLikeReplyIds(likedReplyIds);
1034 for (Album album : sone.getRootAlbum().getAlbums()) {
1035 sone.getRootAlbum().removeAlbum(album);
1037 for (Album album : topLevelAlbums) {
1038 sone.getRootAlbum().addAlbum(album);
1040 synchronized (soneInserters) {
1041 soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint);
1044 for (Post post : posts) {
1045 post.setKnown(true);
1047 for (PostReply reply : replies) {
1048 reply.setKnown(true);
1051 logger.info(String.format("Sone loaded successfully: %s", sone));
1055 * Creates a new post.
1058 * The Sone that creates the post
1060 * The recipient Sone, or {@code null} if this post does not have
1063 * The text of the post
1064 * @return The created post
1066 public Post createPost(Sone sone, Optional<Sone> recipient, String text) {
1067 checkNotNull(text, "text must not be null");
1068 checkArgument(text.trim().length() > 0, "text must not be empty");
1069 if (!sone.isLocal()) {
1070 logger.log(Level.FINE, String.format("Tried to create post for non-local Sone: %s", sone));
1073 PostBuilder postBuilder = database.newPostBuilder();
1074 postBuilder.from(sone.getId()).randomId().currentTime().withText(text.trim());
1075 if (recipient.isPresent()) {
1076 postBuilder.to(recipient.get().getId());
1078 final Post post = postBuilder.build();
1079 database.storePost(post);
1080 eventBus.post(new NewPostFoundEvent(post));
1082 touchConfiguration();
1083 localElementTicker.schedule(new MarkPostKnown(post), 10, TimeUnit.SECONDS);
1088 * Deletes the given post.
1091 * The post to delete
1093 public void deletePost(Post post) {
1094 if (!post.getSone().isLocal()) {
1095 logger.log(Level.WARNING, String.format("Tried to delete post of non-local Sone: %s", post.getSone()));
1098 database.removePost(post);
1099 eventBus.post(new PostRemovedEvent(post));
1100 markPostKnown(post);
1101 touchConfiguration();
1105 * Marks the given post as known, if it is currently not a known post
1106 * (according to {@link Post#isKnown()}).
1109 * The post to mark as known
1111 public void markPostKnown(Post post) {
1112 post.setKnown(true);
1113 eventBus.post(new MarkPostKnownEvent(post));
1114 touchConfiguration();
1115 for (PostReply reply : getReplies(post.getId())) {
1116 markReplyKnown(reply);
1120 public void bookmarkPost(Post post) {
1121 database.bookmarkPost(post);
1125 * Removes the given post from the bookmarks.
1128 * The post to unbookmark
1130 public void unbookmarkPost(Post post) {
1131 database.unbookmarkPost(post);
1135 * Creates a new reply.
1138 * The Sone that creates the reply
1140 * The post that this reply refers to
1142 * The text of the reply
1143 * @return The created reply
1145 public PostReply createReply(Sone sone, Post post, String text) {
1146 checkNotNull(text, "text must not be null");
1147 checkArgument(text.trim().length() > 0, "text must not be empty");
1148 if (!sone.isLocal()) {
1149 logger.log(Level.FINE, String.format("Tried to create reply for non-local Sone: %s", sone));
1152 PostReplyBuilder postReplyBuilder = postReplyBuilder();
1153 postReplyBuilder.randomId().from(sone.getId()).to(post.getId()).currentTime().withText(text.trim());
1154 final PostReply reply = postReplyBuilder.build();
1155 database.storePostReply(reply);
1156 eventBus.post(new NewPostReplyFoundEvent(reply));
1157 sone.addReply(reply);
1158 touchConfiguration();
1159 localElementTicker.schedule(new MarkReplyKnown(reply), 10, TimeUnit.SECONDS);
1164 * Deletes the given reply.
1167 * The reply to delete
1169 public void deleteReply(PostReply reply) {
1170 Sone sone = reply.getSone();
1171 if (!sone.isLocal()) {
1172 logger.log(Level.FINE, String.format("Tried to delete non-local reply: %s", reply));
1175 database.removePostReply(reply);
1176 markReplyKnown(reply);
1177 sone.removeReply(reply);
1178 touchConfiguration();
1182 * Marks the given reply as known, if it is currently not a known reply
1183 * (according to {@link Reply#isKnown()}).
1186 * The reply to mark as known
1188 public void markReplyKnown(PostReply reply) {
1189 boolean previouslyKnown = reply.isKnown();
1190 reply.setKnown(true);
1191 eventBus.post(new MarkPostReplyKnownEvent(reply));
1192 if (!previouslyKnown) {
1193 touchConfiguration();
1198 * Creates a new album for the given Sone.
1201 * The Sone to create the album for
1203 * The parent of the album (may be {@code null} to create a
1205 * @return The new album
1207 public Album createAlbum(Sone sone, Album parent) {
1208 Album album = database.newAlbumBuilder().randomId().by(sone).build();
1209 database.storeAlbum(album);
1210 parent.addAlbum(album);
1215 * Deletes the given album. The owner of the album has to be a local Sone,
1216 * and the album has to be {@link Album#isEmpty() empty} to be deleted.
1219 * The album to remove
1221 public void deleteAlbum(Album album) {
1222 checkNotNull(album, "album must not be null");
1223 checkArgument(album.getSone().isLocal(), "album’s Sone must be a local Sone");
1224 if (!album.isEmpty()) {
1227 album.getParent().removeAlbum(album);
1228 database.removeAlbum(album);
1229 touchConfiguration();
1233 * Creates a new image.
1236 * The Sone creating the image
1238 * The album the image will be inserted into
1239 * @param temporaryImage
1240 * The temporary image to create the image from
1241 * @return The newly created image
1243 public Image createImage(Sone sone, Album album, TemporaryImage temporaryImage) {
1244 checkNotNull(sone, "sone must not be null");
1245 checkNotNull(album, "album must not be null");
1246 checkNotNull(temporaryImage, "temporaryImage must not be null");
1247 checkArgument(sone.isLocal(), "sone must be a local Sone");
1248 checkArgument(sone.equals(album.getSone()), "album must belong to the given Sone");
1249 Image image = database.newImageBuilder().withId(temporaryImage.getId()).build().modify().setSone(sone).setCreationTime(System.currentTimeMillis()).update();
1250 album.addImage(image);
1251 database.storeImage(image);
1252 imageInserter.insertImage(temporaryImage, image);
1257 * Deletes the given image. This method will also delete a matching
1260 * @see #deleteTemporaryImage(String)
1262 * The image to delete
1264 public void deleteImage(Image image) {
1265 checkNotNull(image, "image must not be null");
1266 checkArgument(image.getSone().isLocal(), "image must belong to a local Sone");
1267 deleteTemporaryImage(image.getId());
1268 image.getAlbum().removeImage(image);
1269 database.removeImage(image);
1270 touchConfiguration();
1274 * Creates a new temporary image.
1277 * The MIME type of the temporary image
1279 * The encoded data of the image
1280 * @return The temporary image
1282 public TemporaryImage createTemporaryImage(String mimeType, byte[] imageData) {
1283 TemporaryImage temporaryImage = new TemporaryImage();
1284 temporaryImage.setMimeType(mimeType).setImageData(imageData);
1285 synchronized (temporaryImages) {
1286 temporaryImages.put(temporaryImage.getId(), temporaryImage);
1288 return temporaryImage;
1292 * Deletes the temporary image with the given ID.
1295 * The ID of the temporary image to delete
1297 public void deleteTemporaryImage(String imageId) {
1298 checkNotNull(imageId, "imageId must not be null");
1299 synchronized (temporaryImages) {
1300 temporaryImages.remove(imageId);
1302 Image image = getImage(imageId, false);
1303 if (image != null) {
1304 imageInserter.cancelImageInsert(image);
1309 * Notifies the core that the configuration, either of the core or of a
1310 * single local Sone, has changed, and that the configuration should be
1313 public void touchConfiguration() {
1314 lastConfigurationUpdate = System.currentTimeMillis();
1325 public void serviceStart() {
1326 loadConfiguration();
1327 updateChecker.start();
1328 identityManager.start();
1329 webOfTrustUpdater.init();
1330 webOfTrustUpdater.start();
1331 database.startAsync();
1338 public void serviceRun() {
1339 long lastSaved = System.currentTimeMillis();
1340 while (!shouldStop()) {
1342 long now = System.currentTimeMillis();
1343 if (shouldStop() || ((lastConfigurationUpdate > lastSaved) && ((now - lastConfigurationUpdate) > 5000))) {
1344 for (Sone localSone : getLocalSones()) {
1345 saveSone(localSone);
1347 saveConfiguration();
1357 public void serviceStop() {
1358 localElementTicker.shutdownNow();
1359 synchronized (soneInserters) {
1360 for (Entry<Sone, SoneInserter> soneInserter : soneInserters.entrySet()) {
1361 soneInserter.getValue().stop();
1362 saveSone(soneInserter.getKey());
1365 synchronized (soneRescuers) {
1366 for (SoneRescuer soneRescuer : soneRescuers.values()) {
1370 saveConfiguration();
1371 database.stopAsync();
1372 webOfTrustUpdater.stop();
1373 updateChecker.stop();
1374 soneDownloader.stop();
1375 soneDownloaders.shutdown();
1376 identityManager.stop();
1384 * Saves the given Sone. This will persist all local settings for the given
1385 * Sone, such as the friends list and similar, private options.
1390 private synchronized void saveSone(Sone sone) {
1391 if (!sone.isLocal()) {
1392 logger.log(Level.FINE, String.format("Tried to save non-local Sone: %s", sone));
1395 if (!(sone.getIdentity() instanceof OwnIdentity)) {
1396 logger.log(Level.WARNING, String.format("Local Sone without OwnIdentity found, refusing to save: %s", sone));
1400 logger.log(Level.INFO, String.format("Saving Sone: %s", sone));
1402 /* save Sone into configuration. */
1403 String sonePrefix = "Sone/" + sone.getId();
1404 configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime());
1405 configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").setValue(soneInserters.get(sone).getLastInsertFingerprint());
1408 Profile profile = sone.getProfile();
1409 configuration.getStringValue(sonePrefix + "/Profile/FirstName").setValue(profile.getFirstName());
1410 configuration.getStringValue(sonePrefix + "/Profile/MiddleName").setValue(profile.getMiddleName());
1411 configuration.getStringValue(sonePrefix + "/Profile/LastName").setValue(profile.getLastName());
1412 configuration.getIntValue(sonePrefix + "/Profile/BirthDay").setValue(profile.getBirthDay());
1413 configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").setValue(profile.getBirthMonth());
1414 configuration.getIntValue(sonePrefix + "/Profile/BirthYear").setValue(profile.getBirthYear());
1415 configuration.getStringValue(sonePrefix + "/Profile/Avatar").setValue(profile.getAvatar());
1417 /* save profile fields. */
1418 int fieldCounter = 0;
1419 for (Field profileField : profile.getFields()) {
1420 String fieldPrefix = sonePrefix + "/Profile/Fields/" + fieldCounter++;
1421 configuration.getStringValue(fieldPrefix + "/Name").setValue(profileField.getName());
1422 configuration.getStringValue(fieldPrefix + "/Value").setValue(profileField.getValue());
1424 configuration.getStringValue(sonePrefix + "/Profile/Fields/" + fieldCounter + "/Name").setValue(null);
1427 int postCounter = 0;
1428 for (Post post : sone.getPosts()) {
1429 String postPrefix = sonePrefix + "/Posts/" + postCounter++;
1430 configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
1431 configuration.getStringValue(postPrefix + "/Recipient").setValue(post.getRecipientId().orNull());
1432 configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
1433 configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
1435 configuration.getStringValue(sonePrefix + "/Posts/" + postCounter + "/ID").setValue(null);
1438 int replyCounter = 0;
1439 for (PostReply reply : sone.getReplies()) {
1440 String replyPrefix = sonePrefix + "/Replies/" + replyCounter++;
1441 configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
1442 configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPostId());
1443 configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
1444 configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
1446 configuration.getStringValue(sonePrefix + "/Replies/" + replyCounter + "/ID").setValue(null);
1448 /* save post likes. */
1449 int postLikeCounter = 0;
1450 for (String postId : sone.getLikedPostIds()) {
1451 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter++ + "/ID").setValue(postId);
1453 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter + "/ID").setValue(null);
1455 /* save reply likes. */
1456 int replyLikeCounter = 0;
1457 for (String replyId : sone.getLikedReplyIds()) {
1458 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter++ + "/ID").setValue(replyId);
1460 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter + "/ID").setValue(null);
1462 /* save albums. first, collect in a flat structure, top-level first. */
1463 List<Album> albums = FluentIterable.from(sone.getRootAlbum().getAlbums()).transformAndConcat(Album.FLATTENER).toList();
1465 int albumCounter = 0;
1466 for (Album album : albums) {
1467 String albumPrefix = sonePrefix + "/Albums/" + albumCounter++;
1468 configuration.getStringValue(albumPrefix + "/ID").setValue(album.getId());
1469 configuration.getStringValue(albumPrefix + "/Title").setValue(album.getTitle());
1470 configuration.getStringValue(albumPrefix + "/Description").setValue(album.getDescription());
1471 configuration.getStringValue(albumPrefix + "/Parent").setValue(album.getParent().equals(sone.getRootAlbum()) ? null : album.getParent().getId());
1473 configuration.getStringValue(sonePrefix + "/Albums/" + albumCounter + "/ID").setValue(null);
1476 int imageCounter = 0;
1477 for (Album album : albums) {
1478 for (Image image : album.getImages()) {
1479 if (!image.isInserted()) {
1482 String imagePrefix = sonePrefix + "/Images/" + imageCounter++;
1483 configuration.getStringValue(imagePrefix + "/ID").setValue(image.getId());
1484 configuration.getStringValue(imagePrefix + "/Album").setValue(album.getId());
1485 configuration.getStringValue(imagePrefix + "/Key").setValue(image.getKey());
1486 configuration.getStringValue(imagePrefix + "/Title").setValue(image.getTitle());
1487 configuration.getStringValue(imagePrefix + "/Description").setValue(image.getDescription());
1488 configuration.getLongValue(imagePrefix + "/CreationTime").setValue(image.getCreationTime());
1489 configuration.getIntValue(imagePrefix + "/Width").setValue(image.getWidth());
1490 configuration.getIntValue(imagePrefix + "/Height").setValue(image.getHeight());
1493 configuration.getStringValue(sonePrefix + "/Images/" + imageCounter + "/ID").setValue(null);
1496 configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").setValue(sone.getOptions().isAutoFollow());
1497 configuration.getBooleanValue(sonePrefix + "/Options/EnableSoneInsertNotifications").setValue(sone.getOptions().isSoneInsertNotificationEnabled());
1498 configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewSones").setValue(sone.getOptions().isShowNewSoneNotifications());
1499 configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").setValue(sone.getOptions().isShowNewPostNotifications());
1500 configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").setValue(sone.getOptions().isShowNewReplyNotifications());
1501 configuration.getStringValue(sonePrefix + "/Options/ShowCustomAvatars").setValue(sone.getOptions().getShowCustomAvatars().name());
1502 configuration.getStringValue(sonePrefix + "/Options/LoadLinkedImages").setValue(sone.getOptions().getLoadLinkedImages().name());
1504 configuration.save();
1506 webOfTrustUpdater.setProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition", String.valueOf(sone.getLatestEdition()));
1508 logger.log(Level.INFO, String.format("Sone %s saved.", sone));
1509 } catch (ConfigurationException ce1) {
1510 logger.log(Level.WARNING, String.format("Could not save Sone: %s", sone), ce1);
1515 * Saves the current options.
1517 private void saveConfiguration() {
1518 synchronized (configuration) {
1519 if (storingConfiguration) {
1520 logger.log(Level.FINE, "Already storing configuration…");
1523 storingConfiguration = true;
1526 /* store the options first. */
1528 preferences.saveTo(configuration);
1530 /* save known Sones. */
1531 int soneCounter = 0;
1532 synchronized (knownSones) {
1533 for (String knownSoneId : knownSones) {
1534 configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").setValue(knownSoneId);
1536 configuration.getStringValue("KnownSone/" + soneCounter + "/ID").setValue(null);
1539 /* save known posts. */
1543 configuration.save();
1545 } catch (ConfigurationException ce1) {
1546 logger.log(Level.SEVERE, "Could not store configuration!", ce1);
1547 } catch (DatabaseException de1) {
1548 logger.log(Level.SEVERE, "Could not save database!", de1);
1550 synchronized (configuration) {
1551 storingConfiguration = false;
1557 * Loads the configuration.
1559 private void loadConfiguration() {
1560 new PreferencesLoader(preferences).loadFrom(configuration);
1562 /* load known Sones. */
1563 int soneCounter = 0;
1565 String knownSoneId = configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").getValue(null);
1566 if (knownSoneId == null) {
1569 synchronized (knownSones) {
1570 knownSones.add(knownSoneId);
1576 * Notifies the core that a new {@link OwnIdentity} was added.
1578 * @param ownIdentityAddedEvent
1582 public void ownIdentityAdded(OwnIdentityAddedEvent ownIdentityAddedEvent) {
1583 OwnIdentity ownIdentity = ownIdentityAddedEvent.ownIdentity();
1584 logger.log(Level.FINEST, String.format("Adding OwnIdentity: %s", ownIdentity));
1585 if (ownIdentity.hasContext("Sone")) {
1586 addLocalSone(ownIdentity);
1591 * Notifies the core that an {@link OwnIdentity} was removed.
1593 * @param ownIdentityRemovedEvent
1597 public void ownIdentityRemoved(OwnIdentityRemovedEvent ownIdentityRemovedEvent) {
1598 OwnIdentity ownIdentity = ownIdentityRemovedEvent.ownIdentity();
1599 logger.log(Level.FINEST, String.format("Removing OwnIdentity: %s", ownIdentity));
1600 trustedIdentities.removeAll(ownIdentity);
1604 * Notifies the core that a new {@link Identity} was added.
1606 * @param identityAddedEvent
1610 public void identityAdded(IdentityAddedEvent identityAddedEvent) {
1611 Identity identity = identityAddedEvent.identity();
1612 logger.log(Level.FINEST, String.format("Adding Identity: %s", identity));
1613 trustedIdentities.put(identityAddedEvent.ownIdentity(), identity);
1614 addRemoteSone(identity);
1618 * Notifies the core that an {@link Identity} was updated.
1620 * @param identityUpdatedEvent
1624 public void identityUpdated(IdentityUpdatedEvent identityUpdatedEvent) {
1625 Identity identity = identityUpdatedEvent.identity();
1626 final Sone sone = getRemoteSone(identity.getId());
1627 if (sone.isLocal()) {
1630 String newLatestEdition = identity.getProperty("Sone.LatestEdition");
1631 if (newLatestEdition != null) {
1632 Long parsedNewLatestEdition = tryParse(newLatestEdition);
1633 if (parsedNewLatestEdition != null) {
1634 sone.setLatestEdition(parsedNewLatestEdition);
1637 soneDownloader.addSone(sone);
1638 soneDownloaders.execute(soneDownloader.fetchSoneAsSskAction(sone));
1642 * Notifies the core that an {@link Identity} was removed.
1644 * @param identityRemovedEvent
1648 public void identityRemoved(IdentityRemovedEvent identityRemovedEvent) {
1649 OwnIdentity ownIdentity = identityRemovedEvent.ownIdentity();
1650 Identity identity = identityRemovedEvent.identity();
1651 trustedIdentities.remove(ownIdentity, identity);
1652 for (Entry<OwnIdentity, Collection<Identity>> trustedIdentity : trustedIdentities.asMap().entrySet()) {
1653 if (trustedIdentity.getKey().equals(ownIdentity)) {
1656 if (trustedIdentity.getValue().contains(identity)) {
1660 Sone sone = getSone(identity.getId());
1662 /* TODO - we don’t have the Sone anymore. should this happen? */
1665 for (PostReply postReply : sone.getReplies()) {
1666 eventBus.post(new PostReplyRemovedEvent(postReply));
1668 for (Post post : sone.getPosts()) {
1669 eventBus.post(new PostRemovedEvent(post));
1671 eventBus.post(new SoneRemovedEvent(sone));
1672 database.removeSone(sone);
1676 * Deletes the temporary image.
1678 * @param imageInsertFinishedEvent
1682 public void imageInsertFinished(ImageInsertFinishedEvent imageInsertFinishedEvent) {
1683 logger.log(Level.WARNING, String.format("Image insert finished for %s: %s", imageInsertFinishedEvent.image(), imageInsertFinishedEvent.resultingUri()));
1684 imageInsertFinishedEvent.image().modify().setKey(imageInsertFinishedEvent.resultingUri().toString()).update();
1685 deleteTemporaryImage(imageInsertFinishedEvent.image().getId());
1686 touchConfiguration();
1690 class MarkPostKnown implements Runnable {
1692 private final Post post;
1694 public MarkPostKnown(Post post) {
1700 markPostKnown(post);
1706 class MarkReplyKnown implements Runnable {
1708 private final PostReply postReply;
1710 public MarkReplyKnown(PostReply postReply) {
1711 this.postReply = postReply;
1716 markReplyKnown(postReply);