import static java.util.logging.Logger.getLogger;
import java.util.Collection;
-import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
+import javax.inject.Named;
import net.pterodactylus.sone.core.Core;
import net.pterodactylus.sone.core.ElementLoader;
import net.pterodactylus.sone.core.event.*;
-import net.pterodactylus.sone.data.Image;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.PostReply;
import net.pterodactylus.sone.data.Sone;
private final Loaders loaders;
/** The notification manager. */
- private final NotificationManager notificationManager = new NotificationManager();
+ private final NotificationManager notificationManager;
/** The Sone plugin. */
private final SonePlugin sonePlugin;
private final MetricRegistry metricRegistry;
private final Translation translation;
- /** The “new Sone” notification. */
- private final ListNotification<Sone> newSoneNotification;
-
/** The “new post” notification. */
private final ListNotification<Post> newPostNotification;
/** Notifications for sone inserts. */
private final Map<Sone, TemplateNotification> soneInsertNotifications = new HashMap<>();
- /** Sone locked notification ticker objects. */
- private final Map<Sone, ScheduledFuture<?>> lockedSonesTickerObjects = Collections.synchronizedMap(new HashMap<Sone, ScheduledFuture<?>>());
-
- /** The “Sone locked” notification. */
- private final ListNotification<Sone> lockedSonesNotification;
-
- /** The “new version” notification. */
- private final TemplateNotification newVersionNotification;
-
- /** The “inserting images” notification. */
- private final ListNotification<Image> insertingImagesNotification;
-
- /** The “inserted images” notification. */
- private final ListNotification<Image> insertedImagesNotification;
-
- /** The “image insert failed” notification. */
- private final ListNotification<Image> imageInsertFailedNotification;
-
/** Scheduled executor for time-based notifications. */
private final ScheduledExecutorService ticker = Executors.newScheduledThreadPool(1);
ParserFilter parserFilter, ShortenFilter shortenFilter,
RenderFilter renderFilter,
LinkedElementRenderFilter linkedElementRenderFilter,
- PageToadletRegistry pageToadletRegistry, MetricRegistry metricRegistry, Translation translation, L10nFilter l10nFilter) {
+ PageToadletRegistry pageToadletRegistry, MetricRegistry metricRegistry, Translation translation, L10nFilter l10nFilter,
+ NotificationManager notificationManager, @Named("newRemotePost") ListNotification<Post> newPostNotification,
+ @Named("localPost") ListNotification<Post> localPostNotification) {
this.sonePlugin = sonePlugin;
this.loaders = loaders;
this.listNotificationFilter = listNotificationFilter;
this.metricRegistry = metricRegistry;
this.l10nFilter = l10nFilter;
this.translation = translation;
+ this.notificationManager = notificationManager;
+ this.newPostNotification = newPostNotification;
+ this.localPostNotification = localPostNotification;
formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
soneTextParser = new SoneTextParser(getCore(), getCore());
templateContextFactory.addTemplateObject("formPassword", formPassword);
/* create notifications. */
- Template newSoneNotificationTemplate = loaders.loadTemplate("/templates/notify/newSoneNotification.html");
- newSoneNotification = new ListNotification<>("new-sone-notification", "sones", newSoneNotificationTemplate, false);
-
- Template newPostNotificationTemplate = loaders.loadTemplate("/templates/notify/newPostNotification.html");
- newPostNotification = new ListNotification<>("new-post-notification", "posts", newPostNotificationTemplate, false);
-
- Template localPostNotificationTemplate = loaders.loadTemplate("/templates/notify/newPostNotification.html");
- localPostNotification = new ListNotification<>("local-post-notification", "posts", localPostNotificationTemplate, false);
-
Template newReplyNotificationTemplate = loaders.loadTemplate("/templates/notify/newReplyNotification.html");
newReplyNotification = new ListNotification<>("new-reply-notification", "replies", newReplyNotificationTemplate, false);
Template mentionNotificationTemplate = loaders.loadTemplate("/templates/notify/mentionNotification.html");
mentionNotification = new ListNotification<>("mention-notification", "posts", mentionNotificationTemplate, false);
-
- Template lockedSonesTemplate = loaders.loadTemplate("/templates/notify/lockedSonesNotification.html");
- lockedSonesNotification = new ListNotification<>("sones-locked-notification", "sones", lockedSonesTemplate);
-
- Template newVersionTemplate = loaders.loadTemplate("/templates/notify/newVersionNotification.html");
- newVersionNotification = new TemplateNotification("new-version-notification", newVersionTemplate);
-
- Template insertingImagesTemplate = loaders.loadTemplate("/templates/notify/inserting-images-notification.html");
- insertingImagesNotification = new ListNotification<>("inserting-images-notification", "images", insertingImagesTemplate);
-
- Template insertedImagesTemplate = loaders.loadTemplate("/templates/notify/inserted-images-notification.html");
- insertedImagesNotification = new ListNotification<>("inserted-images-notification", "images", insertedImagesTemplate);
-
- Template imageInsertFailedTemplate = loaders.loadTemplate("/templates/notify/image-insert-failed-notification.html");
- imageInsertFailedNotification = new ListNotification<>("image-insert-failed-notification", "images", imageInsertFailedTemplate);
}
//
return formPassword;
}
- /**
- * Returns the posts that have been announced as new in the
- * {@link #newPostNotification}.
- *
- * @return The new posts
- */
- public Set<Post> getNewPosts() {
- return ImmutableSet.<Post> builder().addAll(newPostNotification.getElements()).addAll(localPostNotification.getElements()).build();
- }
-
@Nonnull
public Collection<Post> getNewPosts(@Nullable Sone currentSone) {
Set<Post> allNewPosts = ImmutableSet.<Post> builder()
//
/**
- * Notifies the web interface that a new {@link Sone} was found.
- *
- * @param newSoneFoundEvent
- * The event
- */
- @Subscribe
- public void newSoneFound(NewSoneFoundEvent newSoneFoundEvent) {
- newSoneNotification.add(newSoneFoundEvent.getSone());
- if (!hasFirstStartNotification()) {
- notificationManager.addNotification(newSoneNotification);
- }
- }
-
- /**
* Notifies the web interface that a new {@link Post} was found.
*
* @param newPostFoundEvent
public void newPostFound(NewPostFoundEvent newPostFoundEvent) {
Post post = newPostFoundEvent.getPost();
boolean isLocal = post.getSone().isLocal();
- if (isLocal) {
- localPostNotification.add(post);
- } else {
- newPostNotification.add(post);
- }
if (!hasFirstStartNotification()) {
- notificationManager.addNotification(isLocal ? localPostNotification : newPostNotification);
if (!getMentionedSones(post.getText()).isEmpty() && !isLocal) {
mentionNotification.add(post);
notificationManager.addNotification(mentionNotification);
}
- } else {
- getCore().markPostKnown(post);
}
}
}
}
- /**
- * Notifies the web interface that a {@link Sone} was marked as known.
- *
- * @param markSoneKnownEvent
- * The event
- */
- @Subscribe
- public void markSoneKnown(MarkSoneKnownEvent markSoneKnownEvent) {
- newSoneNotification.remove(markSoneKnownEvent.getSone());
- }
-
@Subscribe
public void markPostKnown(MarkPostKnownEvent markPostKnownEvent) {
removePost(markPostKnownEvent.getPost());
}
@Subscribe
- public void soneRemoved(SoneRemovedEvent soneRemovedEvent) {
- newSoneNotification.remove(soneRemovedEvent.getSone());
- }
-
- @Subscribe
public void postRemoved(PostRemovedEvent postRemovedEvent) {
removePost(postRemovedEvent.getPost());
}
private void removePost(Post post) {
newPostNotification.remove(post);
- localPostNotification.remove(post);
if (!localSoneMentionedInNewPostOrReply(post)) {
mentionNotification.remove(post);
}
}
/**
- * Notifies the web interface that a Sone was locked.
- *
- * @param soneLockedEvent
- * The event
- */
- @Subscribe
- public void soneLocked(SoneLockedEvent soneLockedEvent) {
- final Sone sone = soneLockedEvent.getSone();
- ScheduledFuture<?> tickerObject = ticker.schedule(new Runnable() {
-
- @Override
- @SuppressWarnings("synthetic-access")
- public void run() {
- lockedSonesNotification.add(sone);
- notificationManager.addNotification(lockedSonesNotification);
- }
- }, 5, TimeUnit.MINUTES);
- lockedSonesTickerObjects.put(sone, tickerObject);
- }
-
- /**
- * Notifies the web interface that a Sone was unlocked.
- *
- * @param soneUnlockedEvent
- * The event
- */
- @Subscribe
- public void soneUnlocked(SoneUnlockedEvent soneUnlockedEvent) {
- lockedSonesNotification.remove(soneUnlockedEvent.getSone());
- lockedSonesTickerObjects.remove(soneUnlockedEvent.getSone()).cancel(false);
- }
-
- /**
* Notifies the web interface that a {@link Sone} is being inserted.
*
* @param soneInsertingEvent
}
}
- /**
- * Notifies the web interface that a new Sone version was found.
- *
- * @param updateFoundEvent
- * The event
- */
- @Subscribe
- public void updateFound(UpdateFoundEvent updateFoundEvent) {
- newVersionNotification.set("latestVersion", updateFoundEvent.getVersion());
- newVersionNotification.set("latestEdition", updateFoundEvent.getLatestEdition());
- newVersionNotification.set("releaseTime", updateFoundEvent.getReleaseTime());
- newVersionNotification.set("disruptive", updateFoundEvent.isDisruptive());
- notificationManager.addNotification(newVersionNotification);
- }
-
- /**
- * Notifies the web interface that an image insert was started
- *
- * @param imageInsertStartedEvent
- * The event
- */
- @Subscribe
- public void imageInsertStarted(ImageInsertStartedEvent imageInsertStartedEvent) {
- insertingImagesNotification.add(imageInsertStartedEvent.getImage());
- notificationManager.addNotification(insertingImagesNotification);
- }
-
- /**
- * Notifies the web interface that an {@link Image} insert was aborted.
- *
- * @param imageInsertAbortedEvent
- * The event
- */
- @Subscribe
- public void imageInsertAborted(ImageInsertAbortedEvent imageInsertAbortedEvent) {
- insertingImagesNotification.remove(imageInsertAbortedEvent.getImage());
- }
-
- /**
- * Notifies the web interface that an {@link Image} insert is finished.
- *
- * @param imageInsertFinishedEvent
- * The event
- */
- @Subscribe
- public void imageInsertFinished(ImageInsertFinishedEvent imageInsertFinishedEvent) {
- insertingImagesNotification.remove(imageInsertFinishedEvent.getImage());
- insertedImagesNotification.add(imageInsertFinishedEvent.getImage());
- notificationManager.addNotification(insertedImagesNotification);
- }
-
- /**
- * Notifies the web interface that an {@link Image} insert has failed.
- *
- * @param imageInsertFailedEvent
- * The event
- */
- @Subscribe
- public void imageInsertFailed(ImageInsertFailedEvent imageInsertFailedEvent) {
- insertingImagesNotification.remove(imageInsertFailedEvent.getImage());
- imageInsertFailedNotification.add(imageInsertFailedEvent.getImage());
- notificationManager.addNotification(imageInsertFailedNotification);
- }
-
@Subscribe
public void debugActivated(@Nonnull DebugActivatedEvent debugActivatedEvent) {
pageToadletRegistry.activateDebugMode();