+ * Deletes the given post.
+ *
+ * @param post
+ * The post to delete
+ */
+ public void deletePost(Post post) {
+ if (!post.getSone().isLocal()) {
+ logger.log(Level.WARNING, String.format("Tried to delete post of non-local Sone: %s", post.getSone()));
+ return;
+ }
+ database.removePost(post);
+ eventBus.post(new PostRemovedEvent(post));
+ markPostKnown(post);
+ touchConfiguration();
+ }
+
+ /**
+ * Marks the given post as known, if it is currently not a known post
+ * (according to {@link Post#isKnown()}).
+ *
+ * @param post
+ * The post to mark as known
+ */
+ public void markPostKnown(Post post) {
+ post.setKnown(true);
+ eventBus.post(new MarkPostKnownEvent(post));
+ touchConfiguration();
+ for (PostReply reply : post.getReplies()) {
+ reply.modify().setKnown().update(postReplyUpdated());
+ }
+ }
+
+ /**
+ * Bookmarks the post with the given ID.
+ *
+ * @param id
+ * The ID of the post to bookmark
+ */
+ public void bookmarkPost(String id) {
+ synchronized (bookmarkedPosts) {
+ bookmarkedPosts.add(id);
+ }
+ }
+
+ /**
+ * Removes the given post from the bookmarks.
+ *
+ * @param post
+ * The post to unbookmark
+ */
+ public void unbookmark(Post post) {
+ unbookmarkPost(post.getId());
+ }
+
+ /**
+ * Removes the post with the given ID from the bookmarks.
+ *
+ * @param id
+ * The ID of the post to unbookmark
+ */
+ public void unbookmarkPost(String id) {
+ synchronized (bookmarkedPosts) {
+ bookmarkedPosts.remove(id);
+ }
+ }
+
+ /**
+ * Deletes the given reply.
+ *
+ * @param reply
+ * The reply to delete
+ */
+ public void deleteReply(PostReply reply) {
+ Sone sone = reply.getSone();
+ if (!sone.isLocal()) {
+ logger.log(Level.FINE, String.format("Tried to delete non-local reply: %s", reply));
+ return;
+ }
+ postReplyUpdated().get().replyUpdated(reply);
+ database.removePostReply(reply);
+ touchConfiguration();
+ }
+
+ /**
+ * Creates a new image.
+ *
+ * @param sone
+ * The Sone creating the image
+ * @param album
+ * The album the image will be inserted into
+ * @param temporaryImage
+ * The temporary image to create the image from
+ * @return The newly created image
+ */
+ public Image createImage(Sone sone, Album album, TemporaryImage temporaryImage) {
+ checkNotNull(sone, "sone must not be null");
+ checkNotNull(album, "album must not be null");
+ checkNotNull(temporaryImage, "temporaryImage must not be null");
+ checkArgument(sone.isLocal(), "sone must be a local Sone");
+ checkArgument(sone.equals(album.getSone()), "album must belong to the given Sone");
+ Image image = album.newImageBuilder().withId(temporaryImage.getId()).sized(temporaryImage.getWidth(), temporaryImage.getHeight()).build(imageCreated());
+ imageInserter.insertImage(temporaryImage, image);
+ return image;
+ }
+
+ /**
+ * Deletes the given image. This method will also delete a matching temporary
+ * image.
+ *
+ * @param image
+ * The image to delete
+ * @see #deleteTemporaryImage(String)
+ */
+ public void deleteImage(Image image) {
+ checkNotNull(image, "image must not be null");
+ checkArgument(image.getSone().isLocal(), "image must belong to a local Sone");
+ deleteTemporaryImage(image.getId());
+ image.remove();
+ touchConfiguration();
+ }
+
+ /**
+ * Creates a new temporary image.
+ *
+ * @param mimeType
+ * The MIME type of the temporary image
+ * @param imageData
+ * The encoded data of the image
+ * @return The temporary image
+ */
+ public TemporaryImage createTemporaryImage(String mimeType, byte[] imageData, int width, int height) {
+ TemporaryImage temporaryImage = new TemporaryImage(mimeType, imageData, width, height);
+ synchronized (temporaryImages) {
+ temporaryImages.put(temporaryImage.getId(), temporaryImage);
+ }
+ return temporaryImage;
+ }
+
+ /**
+ * Deletes the temporary image with the given ID.
+ *
+ * @param imageId
+ * The ID of the temporary image to delete
+ */
+ public void deleteTemporaryImage(String imageId) {
+ checkNotNull(imageId, "imageId must not be null");
+ synchronized (temporaryImages) {
+ temporaryImages.remove(imageId);
+ }
+ Optional<Image> image = getImage(imageId);
+ if (image.isPresent()) {
+ imageInserter.cancelImageInsert(image.get());
+ }
+ }
+
+ /**
+ * Notifies the core that the configuration, either of the core or of a single
+ * local Sone, has changed, and that the configuration should be saved.
+ */
+ public void touchConfiguration() {
+ lastConfigurationUpdate = System.currentTimeMillis();
+ }
+
+ //
+ // SERVICE METHODS
+ //
+
+ @Override
+ public void serviceStart() {
+ loadConfiguration();
+ updateChecker.start();
+ identityManager.start();
+ webOfTrustUpdater.init();
+ webOfTrustUpdater.start();
+ database.start();
+ }
+
+ @Override
+ public void serviceRun() {
+ long lastSaved = System.currentTimeMillis();
+ while (!shouldStop()) {
+ sleep(1000);
+ long now = System.currentTimeMillis();
+ if (shouldStop() || ((lastConfigurationUpdate > lastSaved) && ((now - lastConfigurationUpdate) > 5000))) {
+ for (Sone localSone : getLocalSones()) {
+ saveSone(localSone);
+ }
+ saveConfiguration();
+ lastSaved = now;
+ }
+ }
+ }
+
+ @Override
+ public void serviceStop() {
+ localElementTicker.shutdownNow();
+ synchronized (sones) {
+ for (Entry<Sone, SoneInserter> soneInserter : soneInserters.entrySet()) {
+ soneInserter.getValue().stop();
+ saveSone(soneInserter.getKey());
+ }
+ }
+ saveConfiguration();
+ database.stop();
+ webOfTrustUpdater.stop();
+ updateChecker.stop();
+ soneDownloader.stop();
+ soneDownloaders.shutdown();
+ identityManager.stop();
+ }
+
+ //
+ // PRIVATE METHODS
+ //
+
+ /**
+ * Saves the given Sone. This will persist all local settings for the given