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.collect.FluentIterable;
102 import com.google.common.collect.HashMultimap;
103 import com.google.common.collect.Multimap;
104 import com.google.common.collect.Multimaps;
105 import com.google.common.eventbus.EventBus;
106 import com.google.common.eventbus.Subscribe;
107 import com.google.inject.Inject;
108 import com.google.inject.Singleton;
109 import kotlin.jvm.functions.Function1;
115 public class Core extends AbstractService implements SoneProvider, PostProvider, PostReplyProvider {
118 private static final Logger logger = getLogger(Core.class.getName());
120 /** The start time. */
121 private final long startupTime = System.currentTimeMillis();
123 private final DebugInformation debugInformation = new DebugInformation();
125 /** The preferences. */
126 private final Preferences preferences;
128 /** The event bus. */
129 private final EventBus eventBus;
131 /** The configuration. */
132 private final Configuration configuration;
134 /** Whether we’re currently saving the configuration. */
135 private boolean storingConfiguration = false;
137 /** The identity manager. */
138 private final IdentityManager identityManager;
140 /** Interface to freenet. */
141 private final FreenetInterface freenetInterface;
143 /** The Sone downloader. */
144 private final SoneDownloader soneDownloader;
146 /** The image inserter. */
147 private final ImageInserter imageInserter;
149 /** Sone downloader thread-pool. */
150 private final ExecutorService soneDownloaders = Executors.newFixedThreadPool(10, new NamedThreadFactory("Sone Downloader %2$d"));
152 /** The update checker. */
153 private final UpdateChecker updateChecker;
155 /** The trust updater. */
156 private final WebOfTrustUpdater webOfTrustUpdater;
158 /** Locked local Sones. */
159 /* synchronize on itself. */
160 private final Set<Sone> lockedSones = new HashSet<>();
162 /** Sone inserters. */
163 /* synchronize access on this on sones. */
164 private final Map<Sone, SoneInserter> soneInserters = new HashMap<>();
166 /** Sone rescuers. */
167 /* synchronize access on this on sones. */
168 private final Map<Sone, SoneRescuer> soneRescuers = new HashMap<>();
170 /** All known Sones. */
171 private final Set<String> knownSones = new HashSet<>();
173 /** The post database. */
174 private final Database database;
176 /** Trusted identities, sorted by own identities. */
177 private final Multimap<OwnIdentity, Identity> trustedIdentities = Multimaps.synchronizedSetMultimap(HashMultimap.<OwnIdentity, Identity>create());
179 /** All temporary images. */
180 private final Map<String, TemporaryImage> temporaryImages = new HashMap<>();
182 /** Ticker for threads that mark own elements as known. */
183 private final ScheduledExecutorService localElementTicker = Executors.newScheduledThreadPool(1);
185 /** The time the configuration was last touched. */
186 private volatile long lastConfigurationUpdate;
189 * Creates a new core.
191 * @param configuration
192 * The configuration of the core
193 * @param freenetInterface
194 * The freenet interface
195 * @param identityManager
196 * The identity manager
197 * @param webOfTrustUpdater
198 * The WebOfTrust updater
205 public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, SoneDownloader soneDownloader, ImageInserter imageInserter, UpdateChecker updateChecker, WebOfTrustUpdater webOfTrustUpdater, EventBus eventBus, Database database) {
207 this.configuration = configuration;
208 this.freenetInterface = freenetInterface;
209 this.identityManager = identityManager;
210 this.soneDownloader = soneDownloader;
211 this.imageInserter = imageInserter;
212 this.updateChecker = updateChecker;
213 this.webOfTrustUpdater = webOfTrustUpdater;
214 this.eventBus = eventBus;
215 this.database = database;
216 preferences = new Preferences(eventBus);
224 * Returns the time Sone was started.
226 * @return The startup time (in milliseconds since Jan 1, 1970 UTC)
228 public long getStartupTime() {
233 public DebugInformation getDebugInformation() {
234 return debugInformation;
238 * Returns the options used by the core.
240 * @return The options of the core
242 public Preferences getPreferences() {
247 * Returns the identity manager used by the core.
249 * @return The identity manager
251 public IdentityManager getIdentityManager() {
252 return identityManager;
256 * Returns the update checker.
258 * @return The update checker
260 public UpdateChecker getUpdateChecker() {
261 return updateChecker;
265 * Returns the Sone rescuer for the given local Sone.
268 * The local Sone to get the rescuer for
269 * @return The Sone rescuer for the given Sone
271 public SoneRescuer getSoneRescuer(Sone sone) {
272 checkNotNull(sone, "sone must not be null");
273 checkArgument(sone.isLocal(), "sone must be local");
274 synchronized (soneRescuers) {
275 SoneRescuer soneRescuer = soneRescuers.get(sone);
276 if (soneRescuer == null) {
277 soneRescuer = new SoneRescuer(this, soneDownloader, sone);
278 soneRescuers.put(sone, soneRescuer);
286 * Returns whether the given Sone is currently locked.
290 * @return {@code true} if the Sone is locked, {@code false} if it is not
292 public boolean isLocked(Sone sone) {
293 synchronized (lockedSones) {
294 return lockedSones.contains(sone);
298 public SoneBuilder soneBuilder() {
299 return database.newSoneBuilder();
307 public Collection<Sone> getSones() {
308 return database.getSones();
313 public Function1<String, Sone> getSoneLoader() {
314 return database.getSoneLoader();
318 * Returns the Sone with the given ID, regardless whether it’s local or
322 * The ID of the Sone to get
323 * @return The Sone with the given ID, or {@code null} if there is no such
328 public Sone getSone(@Nonnull String id) {
329 return database.getSone(id);
336 public Collection<Sone> getLocalSones() {
337 return database.getLocalSones();
341 * Returns the local Sone with the given ID, optionally creating a new Sone.
345 * @return The Sone with the given ID, or {@code null}
347 public Sone getLocalSone(String id) {
348 Sone sone = database.getSone(id);
349 if ((sone != null) && sone.isLocal()) {
359 public Collection<Sone> getRemoteSones() {
360 return database.getRemoteSones();
364 * Returns the remote Sone with the given ID.
368 * The ID of the remote Sone to get
369 * @return The Sone with the given ID
371 public Sone getRemoteSone(String id) {
372 return database.getSone(id);
376 * Returns whether the given Sone has been modified.
379 * The Sone to check for modifications
380 * @return {@code true} if a modification has been detected in the Sone,
381 * {@code false} otherwise
383 public boolean isModifiedSone(Sone sone) {
384 return soneInserters.containsKey(sone) && soneInserters.get(sone).isModified();
388 * Returns a post builder.
390 * @return A new post builder
392 public PostBuilder postBuilder() {
393 return database.newPostBuilder();
398 public Post getPost(@Nonnull String postId) {
399 return database.getPost(postId);
406 public Collection<Post> getPosts(String soneId) {
407 return database.getPosts(soneId);
414 public Collection<Post> getDirectedPosts(final String recipientId) {
415 checkNotNull(recipientId, "recipient must not be null");
416 return database.getDirectedPosts(recipientId);
420 * Returns a post reply builder.
422 * @return A new post reply builder
424 public PostReplyBuilder postReplyBuilder() {
425 return database.newPostReplyBuilder();
433 public PostReply getPostReply(String replyId) {
434 return database.getPostReply(replyId);
441 public List<PostReply> getReplies(final String postId) {
442 return database.getReplies(postId);
446 * Returns all Sones that have liked the given post.
449 * The post to get the liking Sones for
450 * @return The Sones that like the given post
452 public Set<Sone> getLikes(Post post) {
453 Set<Sone> sones = new HashSet<>();
454 for (Sone sone : getSones()) {
455 if (sone.getLikedPostIds().contains(post.getId())) {
463 * Returns all Sones that have liked the given reply.
466 * The reply to get the liking Sones for
467 * @return The Sones that like the given reply
469 public Set<Sone> getLikes(PostReply reply) {
470 Set<Sone> sones = new HashSet<>();
471 for (Sone sone : getSones()) {
472 if (sone.getLikedReplyIds().contains(reply.getId())) {
480 * Returns whether the given post is bookmarked.
484 * @return {@code true} if the given post is bookmarked, {@code false}
487 public boolean isBookmarked(Post post) {
488 return database.isPostBookmarked(post);
492 * Returns all currently known bookmarked posts.
494 * @return All bookmarked posts
496 public Set<Post> getBookmarkedPosts() {
497 return database.getBookmarkedPosts();
500 public AlbumBuilder albumBuilder() {
501 return database.newAlbumBuilder();
505 * Returns the album with the given ID, optionally creating a new album if
506 * an album with the given ID can not be found.
509 * The ID of the album
510 * @return The album with the given ID, or {@code null} if no album with the
514 public Album getAlbum(@Nonnull String albumId) {
515 return database.getAlbum(albumId);
518 public ImageBuilder imageBuilder() {
519 return database.newImageBuilder();
523 * Returns the image with the given ID, creating it if necessary.
526 * The ID of the image
527 * @return The image with the given ID
530 public Image getImage(String imageId) {
531 return getImage(imageId, true);
535 * Returns the image with the given ID, optionally creating it if it does
539 * The ID of the image
541 * {@code true} to create an image if none exists with the given
543 * @return The image with the given ID, or {@code null} if none exists and
547 public Image getImage(String imageId, boolean create) {
548 Image image = database.getImage(imageId);
555 Image newImage = database.newImageBuilder().withId(imageId).build();
556 database.storeImage(newImage);
561 * Returns the temporary image with the given ID.
564 * The ID of the temporary image
565 * @return The temporary image, or {@code null} if there is no temporary
566 * image with the given ID
568 public TemporaryImage getTemporaryImage(String imageId) {
569 synchronized (temporaryImages) {
570 return temporaryImages.get(imageId);
579 * Locks the given Sone. A locked Sone will not be inserted by
580 * {@link SoneInserter} until it is {@link #unlockSone(Sone) unlocked}
586 public void lockSone(Sone sone) {
587 synchronized (lockedSones) {
588 if (lockedSones.add(sone)) {
589 eventBus.post(new SoneLockedEvent(sone));
595 * Unlocks the given Sone.
597 * @see #lockSone(Sone)
601 public void unlockSone(Sone sone) {
602 synchronized (lockedSones) {
603 if (lockedSones.remove(sone)) {
604 eventBus.post(new SoneUnlockedEvent(sone));
610 * Adds a local Sone from the given own identity.
613 * The own identity to create a Sone from
614 * @return The added (or already existing) Sone
616 public Sone addLocalSone(OwnIdentity ownIdentity) {
617 if (ownIdentity == null) {
618 logger.log(Level.WARNING, "Given OwnIdentity is null!");
621 logger.info(String.format("Adding Sone from OwnIdentity: %s", ownIdentity));
622 Sone sone = database.newSoneBuilder().local().from(ownIdentity).build();
623 String property = fromNullable(ownIdentity.getProperty("Sone.LatestEdition")).or("0");
624 sone.setLatestEdition(fromNullable(tryParse(property)).or(0L));
625 sone.setClient(new Client("Sone", SonePlugin.getPluginVersion()));
627 SoneInserter soneInserter = new SoneInserter(this, eventBus, freenetInterface, ownIdentity.getId());
628 soneInserter.insertionDelayChanged(new InsertionDelayChangedEvent(preferences.getInsertionDelay()));
629 eventBus.register(soneInserter);
630 synchronized (soneInserters) {
631 soneInserters.put(sone, soneInserter);
634 database.storeSone(sone);
635 sone.setStatus(SoneStatus.idle);
636 soneInserter.start();
641 * Creates a new Sone for the given own identity.
644 * The own identity to create a Sone for
645 * @return The created Sone
647 public Sone createSone(OwnIdentity ownIdentity) {
648 if (!webOfTrustUpdater.addContextWait(ownIdentity, "Sone")) {
649 logger.log(Level.SEVERE, String.format("Could not add “Sone” context to own identity: %s", ownIdentity));
652 Sone sone = addLocalSone(ownIdentity);
654 followSone(sone, "nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI");
655 touchConfiguration();
660 * Adds the Sone of the given identity.
663 * The identity whose Sone to add
664 * @return The added or already existing Sone
666 public Sone addRemoteSone(Identity identity) {
667 if (identity == null) {
668 logger.log(Level.WARNING, "Given Identity is null!");
671 String property = fromNullable(identity.getProperty("Sone.LatestEdition")).or("0");
672 long latestEdition = fromNullable(tryParse(property)).or(0L);
673 Sone existingSone = getSone(identity.getId());
674 if ((existingSone != null )&& existingSone.isLocal()) {
677 boolean newSone = existingSone == null;
678 Sone sone = !newSone ? existingSone : database.newSoneBuilder().from(identity).build();
679 sone.setLatestEdition(latestEdition);
681 synchronized (knownSones) {
682 newSone = !knownSones.contains(sone.getId());
684 sone.setKnown(!newSone);
686 eventBus.post(new NewSoneFoundEvent(sone));
687 for (Sone localSone : getLocalSones()) {
688 if (localSone.getOptions().isAutoFollow()) {
689 followSone(localSone, sone.getId());
694 database.storeSone(sone);
695 soneDownloader.addSone(sone);
696 soneDownloaders.execute(soneDownloader.fetchSoneAsUskAction(sone));
701 * Lets the given local Sone follow the Sone with the given ID.
704 * The local Sone that should follow another Sone
706 * The ID of the Sone to follow
708 public void followSone(Sone sone, String soneId) {
709 checkNotNull(sone, "sone must not be null");
710 checkNotNull(soneId, "soneId must not be null");
711 database.addFriend(sone, soneId);
712 @SuppressWarnings("ConstantConditions") // we just followed, this can’t be null.
713 long now = database.getFollowingTime(soneId);
714 Sone followedSone = getSone(soneId);
715 if (followedSone == null) {
718 for (Post post : followedSone.getPosts()) {
719 if (post.getTime() < now) {
723 for (PostReply reply : followedSone.getReplies()) {
724 if (reply.getTime() < now) {
725 markReplyKnown(reply);
728 touchConfiguration();
732 * Lets the given local Sone unfollow the Sone with the given ID.
735 * The local Sone that should unfollow another Sone
737 * The ID of the Sone being unfollowed
739 public void unfollowSone(Sone sone, String soneId) {
740 checkNotNull(sone, "sone must not be null");
741 checkNotNull(soneId, "soneId must not be null");
742 database.removeFriend(sone, soneId);
743 touchConfiguration();
747 * Sets the trust value of the given origin Sone for the target Sone.
754 * The trust value (from {@code -100} to {@code 100})
756 public void setTrust(Sone origin, Sone target, int trustValue) {
757 checkNotNull(origin, "origin must not be null");
758 checkArgument(origin.getIdentity() instanceof OwnIdentity, "origin must be a local Sone");
759 checkNotNull(target, "target must not be null");
760 checkArgument((trustValue >= -100) && (trustValue <= 100), "trustValue must be within [-100, 100]");
761 webOfTrustUpdater.setTrust((OwnIdentity) origin.getIdentity(), target.getIdentity(), trustValue, preferences.getTrustComment());
765 * Removes any trust assignment for the given target Sone.
772 public void removeTrust(Sone origin, Sone target) {
773 checkNotNull(origin, "origin must not be null");
774 checkNotNull(target, "target must not be null");
775 checkArgument(origin.getIdentity() instanceof OwnIdentity, "origin must be a local Sone");
776 webOfTrustUpdater.setTrust((OwnIdentity) origin.getIdentity(), target.getIdentity(), null, null);
780 * Assigns the configured positive trust value for the given target.
787 public void trustSone(Sone origin, Sone target) {
788 setTrust(origin, target, preferences.getPositiveTrust());
792 * Assigns the configured negative trust value for the given target.
799 public void distrustSone(Sone origin, Sone target) {
800 setTrust(origin, target, preferences.getNegativeTrust());
804 * Removes the trust assignment for the given target.
811 public void untrustSone(Sone origin, Sone target) {
812 removeTrust(origin, target);
816 * Updates the stored Sone with the given Sone.
821 public void updateSone(Sone sone) {
822 updateSone(sone, false);
826 * Updates the stored Sone with the given Sone. If {@code soneRescueMode} is
827 * {@code true}, an older Sone than the current Sone can be given to restore
832 * @param soneRescueMode
833 * {@code true} if the stored Sone should be updated regardless
834 * of the age of the given Sone
836 public void updateSone(final Sone sone, boolean soneRescueMode) {
837 Sone storedSone = getSone(sone.getId());
838 if (storedSone != null) {
839 if (!soneRescueMode && !(sone.getTime() > storedSone.getTime())) {
840 logger.log(Level.FINE, String.format("Downloaded Sone %s is not newer than stored Sone %s.", sone, storedSone));
843 List<Object> events =
844 collectEventsForChangesInSone(storedSone, sone);
845 database.storeSone(sone);
846 for (Object event : events) {
847 eventBus.post(event);
849 sone.setOptions(storedSone.getOptions());
850 sone.setKnown(storedSone.isKnown());
851 sone.setStatus((sone.getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle);
852 if (sone.isLocal()) {
853 touchConfiguration();
858 private List<Object> collectEventsForChangesInSone(Sone oldSone, Sone newSone) {
859 List<Object> events = new ArrayList<>();
860 SoneComparison soneComparison = new SoneComparison(oldSone, newSone);
861 for (Post newPost : soneComparison.getNewPosts()) {
862 if (newPost.getSone().equals(newSone)) {
863 newPost.setKnown(true);
864 } else if (newPost.getTime() < database.getFollowingTime(newSone.getId())) {
865 newPost.setKnown(true);
866 } else if (!newPost.isKnown()) {
867 events.add(new NewPostFoundEvent(newPost));
870 for (Post post : soneComparison.getRemovedPosts()) {
871 events.add(new PostRemovedEvent(post));
873 for (PostReply postReply : soneComparison.getNewPostReplies()) {
874 if (postReply.getSone().equals(newSone)) {
875 postReply.setKnown(true);
876 } else if (postReply.getTime() < database.getFollowingTime(newSone.getId())) {
877 postReply.setKnown(true);
878 } else if (!postReply.isKnown()) {
879 events.add(new NewPostReplyFoundEvent(postReply));
882 for (PostReply postReply : soneComparison.getRemovedPostReplies()) {
883 events.add(new PostReplyRemovedEvent(postReply));
889 * Deletes the given Sone. This will remove the Sone from the
890 * {@link #getLocalSones() local Sones}, stop its {@link SoneInserter} and
891 * remove the context from its identity.
896 public void deleteSone(Sone sone) {
897 if (!(sone.getIdentity() instanceof OwnIdentity)) {
898 logger.log(Level.WARNING, String.format("Tried to delete Sone of non-own identity: %s", sone));
901 if (!getLocalSones().contains(sone)) {
902 logger.log(Level.WARNING, String.format("Tried to delete non-local Sone: %s", sone));
905 SoneInserter soneInserter = soneInserters.remove(sone);
907 database.removeSone(sone);
908 webOfTrustUpdater.removeContext((OwnIdentity) sone.getIdentity(), "Sone");
909 webOfTrustUpdater.removeProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition");
911 configuration.getLongValue("Sone/" + sone.getId() + "/Time").setValue(null);
912 } catch (ConfigurationException ce1) {
913 logger.log(Level.WARNING, "Could not remove Sone from configuration!", ce1);
918 * Marks the given Sone as known. If the Sone was not {@link Post#isKnown()
919 * known} before, a {@link MarkSoneKnownEvent} is fired.
922 * The Sone to mark as known
924 public void markSoneKnown(Sone sone) {
925 if (!sone.isKnown()) {
927 synchronized (knownSones) {
928 knownSones.add(sone.getId());
930 eventBus.post(new MarkSoneKnownEvent(sone));
931 touchConfiguration();
936 * Loads and updates the given Sone from the configuration. If any error is
937 * encountered, loading is aborted and the given Sone is not changed.
940 * The Sone to load and update
942 public void loadSone(Sone sone) {
943 if (!sone.isLocal()) {
944 logger.log(Level.FINE, String.format("Tried to load non-local Sone: %s", sone));
947 logger.info(String.format("Loading local Sone: %s", sone));
950 String sonePrefix = "Sone/" + sone.getId();
951 Long soneTime = configuration.getLongValue(sonePrefix + "/Time").getValue(null);
952 if (soneTime == null) {
953 logger.log(Level.INFO, "Could not load Sone because no Sone has been saved.");
956 String lastInsertFingerprint = configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").getValue("");
959 ConfigurationSoneParser configurationSoneParser = new ConfigurationSoneParser(configuration, sone);
960 Profile profile = configurationSoneParser.parseProfile();
963 Collection<Post> posts;
965 posts = configurationSoneParser.parsePosts(database);
966 } catch (InvalidPostFound ipf) {
967 logger.log(Level.WARNING, "Invalid post found, aborting load!");
972 Collection<PostReply> replies;
974 replies = configurationSoneParser.parsePostReplies(database);
975 } catch (InvalidPostReplyFound iprf) {
976 logger.log(Level.WARNING, "Invalid reply found, aborting load!");
980 /* load post likes. */
981 Set<String> likedPostIds =
982 configurationSoneParser.parseLikedPostIds();
984 /* load reply likes. */
985 Set<String> likedReplyIds =
986 configurationSoneParser.parseLikedPostReplyIds();
989 List<Album> topLevelAlbums;
992 configurationSoneParser.parseTopLevelAlbums(database);
993 } catch (InvalidAlbumFound iaf) {
994 logger.log(Level.WARNING, "Invalid album found, aborting load!");
996 } catch (InvalidParentAlbumFound ipaf) {
997 logger.log(Level.WARNING, format("Invalid parent album ID: %s",
998 ipaf.getAlbumParentId()));
1004 configurationSoneParser.parseImages(database);
1005 } catch (InvalidImageFound iif) {
1006 logger.log(WARNING, "Invalid image found, aborting load!");
1008 } catch (InvalidParentAlbumFound ipaf) {
1009 logger.log(Level.WARNING,
1010 format("Invalid album image (%s) encountered, aborting load!",
1011 ipaf.getAlbumParentId()));
1016 String avatarId = configuration.getStringValue(sonePrefix + "/Profile/Avatar").getValue(null);
1017 if (avatarId != null) {
1018 final Map<String, Image> images =
1019 configurationSoneParser.getImages();
1020 profile.setAvatar(images.get(avatarId));
1024 sone.getOptions().setAutoFollow(configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").getValue(false));
1025 sone.getOptions().setSoneInsertNotificationEnabled(configuration.getBooleanValue(sonePrefix + "/Options/EnableSoneInsertNotifications").getValue(false));
1026 sone.getOptions().setShowNewSoneNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewSones").getValue(true));
1027 sone.getOptions().setShowNewPostNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").getValue(true));
1028 sone.getOptions().setShowNewReplyNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").getValue(true));
1029 sone.getOptions().setShowCustomAvatars(LoadExternalContent.valueOf(configuration.getStringValue(sonePrefix + "/Options/ShowCustomAvatars").getValue(LoadExternalContent.NEVER.name())));
1030 sone.getOptions().setLoadLinkedImages(LoadExternalContent.valueOf(configuration.getStringValue(sonePrefix + "/Options/LoadLinkedImages").getValue(LoadExternalContent.NEVER.name())));
1032 /* if we’re still here, Sone was loaded successfully. */
1033 synchronized (sone) {
1034 sone.setTime(soneTime);
1035 sone.setProfile(profile);
1036 sone.setPosts(posts);
1037 sone.setReplies(replies);
1038 sone.setLikePostIds(likedPostIds);
1039 sone.setLikeReplyIds(likedReplyIds);
1040 for (Album album : sone.getRootAlbum().getAlbums()) {
1041 sone.getRootAlbum().removeAlbum(album);
1043 for (Album album : topLevelAlbums) {
1044 sone.getRootAlbum().addAlbum(album);
1046 synchronized (soneInserters) {
1047 soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint);
1050 for (Post post : posts) {
1051 post.setKnown(true);
1053 for (PostReply reply : replies) {
1054 reply.setKnown(true);
1057 logger.info(String.format("Sone loaded successfully: %s", sone));
1061 * Creates a new post.
1064 * The Sone that creates the post
1066 * The recipient Sone, or {@code null} if this post does not have
1069 * The text of the post
1070 * @return The created post
1072 public Post createPost(Sone sone, @Nullable Sone recipient, String text) {
1073 checkNotNull(text, "text must not be null");
1074 checkArgument(text.trim().length() > 0, "text must not be empty");
1075 if (!sone.isLocal()) {
1076 logger.log(Level.FINE, String.format("Tried to create post for non-local Sone: %s", sone));
1079 PostBuilder postBuilder = database.newPostBuilder();
1080 postBuilder.from(sone.getId()).randomId().currentTime().withText(text.trim());
1081 if (recipient != null) {
1082 postBuilder.to(recipient.getId());
1084 final Post post = postBuilder.build();
1085 database.storePost(post);
1086 eventBus.post(new NewPostFoundEvent(post));
1088 touchConfiguration();
1089 localElementTicker.schedule(new MarkPostKnown(post), 10, TimeUnit.SECONDS);
1094 * Deletes the given post.
1097 * The post to delete
1099 public void deletePost(Post post) {
1100 if (!post.getSone().isLocal()) {
1101 logger.log(Level.WARNING, String.format("Tried to delete post of non-local Sone: %s", post.getSone()));
1104 database.removePost(post);
1105 eventBus.post(new PostRemovedEvent(post));
1106 markPostKnown(post);
1107 touchConfiguration();
1111 * Marks the given post as known, if it is currently not a known post
1112 * (according to {@link Post#isKnown()}).
1115 * The post to mark as known
1117 public void markPostKnown(Post post) {
1118 post.setKnown(true);
1119 eventBus.post(new MarkPostKnownEvent(post));
1120 touchConfiguration();
1121 for (PostReply reply : getReplies(post.getId())) {
1122 markReplyKnown(reply);
1126 public void bookmarkPost(Post post) {
1127 database.bookmarkPost(post);
1131 * Removes the given post from the bookmarks.
1134 * The post to unbookmark
1136 public void unbookmarkPost(Post post) {
1137 database.unbookmarkPost(post);
1141 * Creates a new reply.
1144 * The Sone that creates the reply
1146 * The post that this reply refers to
1148 * The text of the reply
1149 * @return The created reply
1151 public PostReply createReply(Sone sone, Post post, String text) {
1152 checkNotNull(text, "text must not be null");
1153 checkArgument(text.trim().length() > 0, "text must not be empty");
1154 if (!sone.isLocal()) {
1155 logger.log(Level.FINE, String.format("Tried to create reply for non-local Sone: %s", sone));
1158 PostReplyBuilder postReplyBuilder = postReplyBuilder();
1159 postReplyBuilder.randomId().from(sone.getId()).to(post.getId()).currentTime().withText(text.trim());
1160 final PostReply reply = postReplyBuilder.build();
1161 database.storePostReply(reply);
1162 eventBus.post(new NewPostReplyFoundEvent(reply));
1163 sone.addReply(reply);
1164 touchConfiguration();
1165 localElementTicker.schedule(new MarkReplyKnown(reply), 10, TimeUnit.SECONDS);
1170 * Deletes the given reply.
1173 * The reply to delete
1175 public void deleteReply(PostReply reply) {
1176 Sone sone = reply.getSone();
1177 if (!sone.isLocal()) {
1178 logger.log(Level.FINE, String.format("Tried to delete non-local reply: %s", reply));
1181 database.removePostReply(reply);
1182 markReplyKnown(reply);
1183 sone.removeReply(reply);
1184 touchConfiguration();
1188 * Marks the given reply as known, if it is currently not a known reply
1189 * (according to {@link Reply#isKnown()}).
1192 * The reply to mark as known
1194 public void markReplyKnown(PostReply reply) {
1195 boolean previouslyKnown = reply.isKnown();
1196 reply.setKnown(true);
1197 eventBus.post(new MarkPostReplyKnownEvent(reply));
1198 if (!previouslyKnown) {
1199 touchConfiguration();
1204 * Creates a new album for the given Sone.
1207 * The Sone to create the album for
1209 * The parent of the album (may be {@code null} to create a
1211 * @return The new album
1213 public Album createAlbum(Sone sone, Album parent) {
1214 Album album = database.newAlbumBuilder().randomId().by(sone).build();
1215 database.storeAlbum(album);
1216 parent.addAlbum(album);
1221 * Deletes the given album. The owner of the album has to be a local Sone,
1222 * and the album has to be {@link Album#isEmpty() empty} to be deleted.
1225 * The album to remove
1227 public void deleteAlbum(Album album) {
1228 checkNotNull(album, "album must not be null");
1229 checkArgument(album.getSone().isLocal(), "album’s Sone must be a local Sone");
1230 if (!album.isEmpty()) {
1233 album.getParent().removeAlbum(album);
1234 database.removeAlbum(album);
1235 touchConfiguration();
1239 * Creates a new image.
1242 * The Sone creating the image
1244 * The album the image will be inserted into
1245 * @param temporaryImage
1246 * The temporary image to create the image from
1247 * @return The newly created image
1249 public Image createImage(Sone sone, Album album, TemporaryImage temporaryImage) {
1250 checkNotNull(sone, "sone must not be null");
1251 checkNotNull(album, "album must not be null");
1252 checkNotNull(temporaryImage, "temporaryImage must not be null");
1253 checkArgument(sone.isLocal(), "sone must be a local Sone");
1254 checkArgument(sone.equals(album.getSone()), "album must belong to the given Sone");
1255 Image image = database.newImageBuilder().withId(temporaryImage.getId()).build().modify().setSone(sone).setCreationTime(System.currentTimeMillis()).update();
1256 album.addImage(image);
1257 database.storeImage(image);
1258 imageInserter.insertImage(temporaryImage, image);
1263 * Deletes the given image. This method will also delete a matching
1266 * @see #deleteTemporaryImage(String)
1268 * The image to delete
1270 public void deleteImage(Image image) {
1271 checkNotNull(image, "image must not be null");
1272 checkArgument(image.getSone().isLocal(), "image must belong to a local Sone");
1273 deleteTemporaryImage(image.getId());
1274 image.getAlbum().removeImage(image);
1275 database.removeImage(image);
1276 touchConfiguration();
1280 * Creates a new temporary image.
1283 * The MIME type of the temporary image
1285 * The encoded data of the image
1286 * @return The temporary image
1288 public TemporaryImage createTemporaryImage(String mimeType, byte[] imageData) {
1289 TemporaryImage temporaryImage = new TemporaryImage();
1290 temporaryImage.setMimeType(mimeType).setImageData(imageData);
1291 synchronized (temporaryImages) {
1292 temporaryImages.put(temporaryImage.getId(), temporaryImage);
1294 return temporaryImage;
1298 * Deletes the temporary image with the given ID.
1301 * The ID of the temporary image to delete
1303 public void deleteTemporaryImage(String imageId) {
1304 checkNotNull(imageId, "imageId must not be null");
1305 synchronized (temporaryImages) {
1306 temporaryImages.remove(imageId);
1308 Image image = getImage(imageId, false);
1309 if (image != null) {
1310 imageInserter.cancelImageInsert(image);
1315 * Notifies the core that the configuration, either of the core or of a
1316 * single local Sone, has changed, and that the configuration should be
1319 public void touchConfiguration() {
1320 lastConfigurationUpdate = System.currentTimeMillis();
1331 public void serviceStart() {
1332 loadConfiguration();
1333 updateChecker.start();
1334 identityManager.start();
1335 webOfTrustUpdater.init();
1336 webOfTrustUpdater.start();
1337 database.startAsync();
1344 public void serviceRun() {
1345 long lastSaved = System.currentTimeMillis();
1346 while (!shouldStop()) {
1348 long now = System.currentTimeMillis();
1349 if (shouldStop() || ((lastConfigurationUpdate > lastSaved) && ((now - lastConfigurationUpdate) > 5000))) {
1350 for (Sone localSone : getLocalSones()) {
1351 saveSone(localSone);
1353 saveConfiguration();
1363 public void serviceStop() {
1364 localElementTicker.shutdownNow();
1365 synchronized (soneInserters) {
1366 for (Entry<Sone, SoneInserter> soneInserter : soneInserters.entrySet()) {
1367 soneInserter.getValue().stop();
1368 saveSone(soneInserter.getKey());
1371 synchronized (soneRescuers) {
1372 for (SoneRescuer soneRescuer : soneRescuers.values()) {
1376 saveConfiguration();
1377 database.stopAsync();
1378 webOfTrustUpdater.stop();
1379 updateChecker.stop();
1380 soneDownloader.stop();
1381 soneDownloaders.shutdown();
1382 identityManager.stop();
1390 * Saves the given Sone. This will persist all local settings for the given
1391 * Sone, such as the friends list and similar, private options.
1396 private synchronized void saveSone(Sone sone) {
1397 if (!sone.isLocal()) {
1398 logger.log(Level.FINE, String.format("Tried to save non-local Sone: %s", sone));
1401 if (!(sone.getIdentity() instanceof OwnIdentity)) {
1402 logger.log(Level.WARNING, String.format("Local Sone without OwnIdentity found, refusing to save: %s", sone));
1406 logger.log(Level.INFO, String.format("Saving Sone: %s", sone));
1408 /* save Sone into configuration. */
1409 String sonePrefix = "Sone/" + sone.getId();
1410 configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime());
1411 configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").setValue(soneInserters.get(sone).getLastInsertFingerprint());
1414 Profile profile = sone.getProfile();
1415 configuration.getStringValue(sonePrefix + "/Profile/FirstName").setValue(profile.getFirstName());
1416 configuration.getStringValue(sonePrefix + "/Profile/MiddleName").setValue(profile.getMiddleName());
1417 configuration.getStringValue(sonePrefix + "/Profile/LastName").setValue(profile.getLastName());
1418 configuration.getIntValue(sonePrefix + "/Profile/BirthDay").setValue(profile.getBirthDay());
1419 configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").setValue(profile.getBirthMonth());
1420 configuration.getIntValue(sonePrefix + "/Profile/BirthYear").setValue(profile.getBirthYear());
1421 configuration.getStringValue(sonePrefix + "/Profile/Avatar").setValue(profile.getAvatar());
1423 /* save profile fields. */
1424 int fieldCounter = 0;
1425 for (Field profileField : profile.getFields()) {
1426 String fieldPrefix = sonePrefix + "/Profile/Fields/" + fieldCounter++;
1427 configuration.getStringValue(fieldPrefix + "/Name").setValue(profileField.getName());
1428 configuration.getStringValue(fieldPrefix + "/Value").setValue(profileField.getValue());
1430 configuration.getStringValue(sonePrefix + "/Profile/Fields/" + fieldCounter + "/Name").setValue(null);
1433 int postCounter = 0;
1434 for (Post post : sone.getPosts()) {
1435 String postPrefix = sonePrefix + "/Posts/" + postCounter++;
1436 configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
1437 configuration.getStringValue(postPrefix + "/Recipient").setValue(post.getRecipientId().orNull());
1438 configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
1439 configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
1441 configuration.getStringValue(sonePrefix + "/Posts/" + postCounter + "/ID").setValue(null);
1444 int replyCounter = 0;
1445 for (PostReply reply : sone.getReplies()) {
1446 String replyPrefix = sonePrefix + "/Replies/" + replyCounter++;
1447 configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
1448 configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPostId());
1449 configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
1450 configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
1452 configuration.getStringValue(sonePrefix + "/Replies/" + replyCounter + "/ID").setValue(null);
1454 /* save post likes. */
1455 int postLikeCounter = 0;
1456 for (String postId : sone.getLikedPostIds()) {
1457 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter++ + "/ID").setValue(postId);
1459 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter + "/ID").setValue(null);
1461 /* save reply likes. */
1462 int replyLikeCounter = 0;
1463 for (String replyId : sone.getLikedReplyIds()) {
1464 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter++ + "/ID").setValue(replyId);
1466 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter + "/ID").setValue(null);
1468 /* save albums. first, collect in a flat structure, top-level first. */
1469 List<Album> albums = FluentIterable.from(sone.getRootAlbum().getAlbums()).transformAndConcat(Album.FLATTENER).toList();
1471 int albumCounter = 0;
1472 for (Album album : albums) {
1473 String albumPrefix = sonePrefix + "/Albums/" + albumCounter++;
1474 configuration.getStringValue(albumPrefix + "/ID").setValue(album.getId());
1475 configuration.getStringValue(albumPrefix + "/Title").setValue(album.getTitle());
1476 configuration.getStringValue(albumPrefix + "/Description").setValue(album.getDescription());
1477 configuration.getStringValue(albumPrefix + "/Parent").setValue(album.getParent().equals(sone.getRootAlbum()) ? null : album.getParent().getId());
1479 configuration.getStringValue(sonePrefix + "/Albums/" + albumCounter + "/ID").setValue(null);
1482 int imageCounter = 0;
1483 for (Album album : albums) {
1484 for (Image image : album.getImages()) {
1485 if (!image.isInserted()) {
1488 String imagePrefix = sonePrefix + "/Images/" + imageCounter++;
1489 configuration.getStringValue(imagePrefix + "/ID").setValue(image.getId());
1490 configuration.getStringValue(imagePrefix + "/Album").setValue(album.getId());
1491 configuration.getStringValue(imagePrefix + "/Key").setValue(image.getKey());
1492 configuration.getStringValue(imagePrefix + "/Title").setValue(image.getTitle());
1493 configuration.getStringValue(imagePrefix + "/Description").setValue(image.getDescription());
1494 configuration.getLongValue(imagePrefix + "/CreationTime").setValue(image.getCreationTime());
1495 configuration.getIntValue(imagePrefix + "/Width").setValue(image.getWidth());
1496 configuration.getIntValue(imagePrefix + "/Height").setValue(image.getHeight());
1499 configuration.getStringValue(sonePrefix + "/Images/" + imageCounter + "/ID").setValue(null);
1502 configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").setValue(sone.getOptions().isAutoFollow());
1503 configuration.getBooleanValue(sonePrefix + "/Options/EnableSoneInsertNotifications").setValue(sone.getOptions().isSoneInsertNotificationEnabled());
1504 configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewSones").setValue(sone.getOptions().isShowNewSoneNotifications());
1505 configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").setValue(sone.getOptions().isShowNewPostNotifications());
1506 configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").setValue(sone.getOptions().isShowNewReplyNotifications());
1507 configuration.getStringValue(sonePrefix + "/Options/ShowCustomAvatars").setValue(sone.getOptions().getShowCustomAvatars().name());
1508 configuration.getStringValue(sonePrefix + "/Options/LoadLinkedImages").setValue(sone.getOptions().getLoadLinkedImages().name());
1510 configuration.save();
1512 webOfTrustUpdater.setProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition", String.valueOf(sone.getLatestEdition()));
1514 logger.log(Level.INFO, String.format("Sone %s saved.", sone));
1515 } catch (ConfigurationException ce1) {
1516 logger.log(Level.WARNING, String.format("Could not save Sone: %s", sone), ce1);
1521 * Saves the current options.
1523 private void saveConfiguration() {
1524 synchronized (configuration) {
1525 if (storingConfiguration) {
1526 logger.log(Level.FINE, "Already storing configuration…");
1529 storingConfiguration = true;
1532 /* store the options first. */
1534 preferences.saveTo(configuration);
1536 /* save known Sones. */
1537 int soneCounter = 0;
1538 synchronized (knownSones) {
1539 for (String knownSoneId : knownSones) {
1540 configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").setValue(knownSoneId);
1542 configuration.getStringValue("KnownSone/" + soneCounter + "/ID").setValue(null);
1545 /* save known posts. */
1549 configuration.save();
1551 } catch (ConfigurationException ce1) {
1552 logger.log(Level.SEVERE, "Could not store configuration!", ce1);
1553 } catch (DatabaseException de1) {
1554 logger.log(Level.SEVERE, "Could not save database!", de1);
1556 synchronized (configuration) {
1557 storingConfiguration = false;
1563 * Loads the configuration.
1565 private void loadConfiguration() {
1566 new PreferencesLoader(preferences).loadFrom(configuration);
1568 /* load known Sones. */
1569 int soneCounter = 0;
1571 String knownSoneId = configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").getValue(null);
1572 if (knownSoneId == null) {
1575 synchronized (knownSones) {
1576 knownSones.add(knownSoneId);
1582 * Notifies the core that a new {@link OwnIdentity} was added.
1584 * @param ownIdentityAddedEvent
1588 public void ownIdentityAdded(OwnIdentityAddedEvent ownIdentityAddedEvent) {
1589 OwnIdentity ownIdentity = ownIdentityAddedEvent.ownIdentity();
1590 logger.log(Level.FINEST, String.format("Adding OwnIdentity: %s", ownIdentity));
1591 if (ownIdentity.hasContext("Sone")) {
1592 addLocalSone(ownIdentity);
1597 * Notifies the core that an {@link OwnIdentity} was removed.
1599 * @param ownIdentityRemovedEvent
1603 public void ownIdentityRemoved(OwnIdentityRemovedEvent ownIdentityRemovedEvent) {
1604 OwnIdentity ownIdentity = ownIdentityRemovedEvent.ownIdentity();
1605 logger.log(Level.FINEST, String.format("Removing OwnIdentity: %s", ownIdentity));
1606 trustedIdentities.removeAll(ownIdentity);
1610 * Notifies the core that a new {@link Identity} was added.
1612 * @param identityAddedEvent
1616 public void identityAdded(IdentityAddedEvent identityAddedEvent) {
1617 Identity identity = identityAddedEvent.identity();
1618 logger.log(Level.FINEST, String.format("Adding Identity: %s", identity));
1619 trustedIdentities.put(identityAddedEvent.ownIdentity(), identity);
1620 addRemoteSone(identity);
1624 * Notifies the core that an {@link Identity} was updated.
1626 * @param identityUpdatedEvent
1630 public void identityUpdated(IdentityUpdatedEvent identityUpdatedEvent) {
1631 Identity identity = identityUpdatedEvent.identity();
1632 final Sone sone = getRemoteSone(identity.getId());
1633 if (sone.isLocal()) {
1636 String newLatestEdition = identity.getProperty("Sone.LatestEdition");
1637 if (newLatestEdition != null) {
1638 Long parsedNewLatestEdition = tryParse(newLatestEdition);
1639 if (parsedNewLatestEdition != null) {
1640 sone.setLatestEdition(parsedNewLatestEdition);
1643 soneDownloader.addSone(sone);
1644 soneDownloaders.execute(soneDownloader.fetchSoneAsSskAction(sone));
1648 * Notifies the core that an {@link Identity} was removed.
1650 * @param identityRemovedEvent
1654 public void identityRemoved(IdentityRemovedEvent identityRemovedEvent) {
1655 OwnIdentity ownIdentity = identityRemovedEvent.ownIdentity();
1656 Identity identity = identityRemovedEvent.identity();
1657 trustedIdentities.remove(ownIdentity, identity);
1658 for (Entry<OwnIdentity, Collection<Identity>> trustedIdentity : trustedIdentities.asMap().entrySet()) {
1659 if (trustedIdentity.getKey().equals(ownIdentity)) {
1662 if (trustedIdentity.getValue().contains(identity)) {
1666 Sone sone = getSone(identity.getId());
1668 /* TODO - we don’t have the Sone anymore. should this happen? */
1671 for (PostReply postReply : sone.getReplies()) {
1672 eventBus.post(new PostReplyRemovedEvent(postReply));
1674 for (Post post : sone.getPosts()) {
1675 eventBus.post(new PostRemovedEvent(post));
1677 eventBus.post(new SoneRemovedEvent(sone));
1678 database.removeSone(sone);
1682 * Deletes the temporary image.
1684 * @param imageInsertFinishedEvent
1688 public void imageInsertFinished(ImageInsertFinishedEvent imageInsertFinishedEvent) {
1689 logger.log(Level.WARNING, String.format("Image insert finished for %s: %s", imageInsertFinishedEvent.getImage(), imageInsertFinishedEvent.getResultingUri()));
1690 imageInsertFinishedEvent.getImage().modify().setKey(imageInsertFinishedEvent.getResultingUri().toString()).update();
1691 deleteTemporaryImage(imageInsertFinishedEvent.getImage().getId());
1692 touchConfiguration();
1696 class MarkPostKnown implements Runnable {
1698 private final Post post;
1700 public MarkPostKnown(Post post) {
1706 markPostKnown(post);
1712 class MarkReplyKnown implements Runnable {
1714 private final PostReply postReply;
1716 public MarkReplyKnown(PostReply postReply) {
1717 this.postReply = postReply;
1722 markReplyKnown(postReply);