Refactor notification filtering
[Sone.git] / src / main / java / net / pterodactylus / sone / notify / ListNotificationFilters.java
index 62a7d61..0a2a518 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Sone - ListNotificationFilters.java - Copyright © 2010 David Roden
+ * Sone - ListNotificationFilters.java - Copyright © 2010–2015 David Roden
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 
 package net.pterodactylus.sone.notify;
 
+import static com.google.common.collect.FluentIterable.from;
+
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
+import javax.annotation.Nonnull;
+import javax.inject.Inject;
+import javax.inject.Singleton;
 
 import net.pterodactylus.sone.data.Post;
-import net.pterodactylus.sone.data.Reply;
+import net.pterodactylus.sone.data.PostReply;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.util.notify.Notification;
 
+import com.google.common.base.Optional;
+
 /**
  * Filter for {@link ListNotification}s.
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
+@Singleton
 public class ListNotificationFilters {
 
+       private final PostVisibilityFilter postVisibilityFilter;
+       private final ReplyVisibilityFilter replyVisibilityFilter;
+
+       @Inject
+       public ListNotificationFilters(@Nonnull PostVisibilityFilter postVisibilityFilter, @Nonnull ReplyVisibilityFilter replyVisibilityFilter) {
+               this.postVisibilityFilter = postVisibilityFilter;
+               this.replyVisibilityFilter = replyVisibilityFilter;
+       }
+
        /**
         * Filters new-post and new-reply notifications in the given list of
-        * notifications. If {@code currentSone} is <code>null</code>, nothing is
-        * filtered and the given list is returned.
-        * If {@code currentSone} is not {@code null}, only posts that are posted by
-        * a friend Sone or the Sone itself, and replies that are replies to posts
-        * of friend Sones or the Sone itself will be retained
-        * in the notifications.
+        * notifications. If {@code currentSone} is <code>null</code>, new-post and
+        * new-reply notifications are removed completely. If {@code currentSone} is
+        * not {@code null}, only posts that are posted by a friend Sone or the Sone
+        * itself, and replies that are replies to posts of friend Sones or the Sone
+        * itself will be retained in the notifications.
         *
         * @param notifications
-        *            The notifications to filter
+        *              The notifications to filter
         * @param currentSone
-        *            The current Sone, or {@code null} if not logged in
+        *              The current Sone, or {@code null} if not logged in
         * @return The filtered notifications
         */
-       public static List<Notification> filterNotifications(List<Notification> notifications, Sone currentSone) {
-               if (currentSone == null) {
-                       return notifications;
-               }
-               ListNotification<Post> newPostNotification = getNotification(notifications, "new-post-notification", Post.class);
-               System.out.println("Found new-post-notification with " + ((newPostNotification != null) ? newPostNotification.getElements().size() : -1) + " posts.");
-               if (newPostNotification != null) {
-                       List<Post> newPosts = new ArrayList<Post>();
-                       for (Post post : newPostNotification.getElements()) {
-                               System.out.println("Checking Post: " + post);
-                               if (currentSone.hasFriend(post.getSone().getId()) || currentSone.equals(post.getSone())) {
-                                       System.out.println("  CS.hF: " + currentSone.hasFriend(post.getSone().getId()));
-                                       System.out.println("  CS.e:" + currentSone.equals(post.getSone()));
-                                       newPosts.add(post);
+       @SuppressWarnings("unchecked")
+       public List<Notification> filterNotifications(Collection<? extends Notification> notifications, Sone currentSone) {
+               List<Notification> filteredNotifications = new ArrayList<Notification>();
+               for (Notification notification : notifications) {
+                       if (notification.getId().equals("new-sone-notification")) {
+                               if ((currentSone != null) && !currentSone.getOptions().isShowNewSoneNotifications()) {
+                                       continue;
                                }
-                       }
-                       int notificationIndex = notifications.indexOf(newPostNotification);
-                       if (newPosts.isEmpty()) {
-                               System.out.println("Removing notification.");
-                               notifications.remove(notificationIndex);
-                       } else {
-                               System.out.println("Replacing Notification.");
-                               newPostNotification = new ListNotification<Post>(newPostNotification);
-                               newPostNotification.setElements(newPosts);
-                               notifications.set(notificationIndex, newPostNotification);
-                       }
-               }
-               ListNotification<Reply> newReplyNotification = getNotification(notifications, "new-replies-notification", Reply.class);
-               System.out.println("Found new-reply-notification with " + ((newReplyNotification != null) ? newReplyNotification.getElements().size() : -1) + " replies.");
-               if (newReplyNotification != null) {
-                       List<Reply> newReplies = new ArrayList<Reply>();
-                       for (Reply reply : newReplyNotification.getElements()) {
-                               System.out.println("Checking Reply: " + reply);
-                               if (currentSone.hasFriend(reply.getPost().getSone().getId()) || currentSone.equals(reply.getPost().getSone())) {
-                                       System.out.println("  CS.hF: " + currentSone.hasFriend(reply.getPost().getSone().getId()));
-                                       System.out.println("  CS.e: " + currentSone.equals(reply.getPost().getSone()));
-                                       newReplies.add(reply);
+                               filteredNotifications.add(notification);
+                       } else if (notification.getId().equals("new-post-notification")) {
+                               if (currentSone == null) {
+                                       continue;
+                               }
+                               if (!currentSone.getOptions().isShowNewPostNotifications()) {
+                                       continue;
+                               }
+                               Optional<ListNotification<Post>> filteredNotification = filterNewPostNotification((ListNotification<Post>) notification, currentSone);
+                               if (filteredNotification.isPresent()) {
+                                       filteredNotifications.add(filteredNotification.get());
+                               }
+                       } else if (notification.getId().equals("new-reply-notification")) {
+                               if (currentSone == null) {
+                                       continue;
+                               }
+                               if (!currentSone.getOptions().isShowNewReplyNotifications()) {
+                                       continue;
+                               }
+                               Optional<ListNotification<PostReply>> filteredNotification =
+                                               filterNewReplyNotification((ListNotification<PostReply>) notification, currentSone);
+                               if (filteredNotification.isPresent()) {
+                                       filteredNotifications.add(filteredNotification.get());
+                               }
+                       } else if (notification.getId().equals("mention-notification")) {
+                               Optional<ListNotification<Post>> filteredNotification = filterNewPostNotification((ListNotification<Post>) notification, null);
+                               if (filteredNotification.isPresent()) {
+                                       filteredNotifications.add(filteredNotification.get());
                                }
-                       }
-                       int notificationIndex = notifications.indexOf(newReplyNotification);
-                       if (newReplies.isEmpty()) {
-                               System.out.println("Removing Notification.");
-                               notifications.remove(notificationIndex);
                        } else {
-                               System.out.println("Replacing Notification.");
-                               newReplyNotification = new ListNotification<Reply>(newReplyNotification);
-                               newReplyNotification.setElements(newReplies);
-                               notifications.set(notificationIndex, newReplyNotification);
+                               filteredNotifications.add(notification);
                        }
                }
-               return notifications;
+               return filteredNotifications;
        }
 
        /**
-        * Finds the notification with the given ID in the list of notifications and
-        * returns it.
+        * Filters the new posts of the given notification. If {@code currentSone}
+        * is {@code null} and {@code soneRequired} is {@code true}, {@code null} is
+        * returned and the notification is subsequently removed. Otherwise only
+        * posts that are posted by friend Sones of the given Sone are retained; all
+        * other posts are removed.
         *
-        * @param <T>
-        *            The type of the item in the notification
-        * @param notifications
-        *            The notification to search
-        * @param notificationId
-        *            The ID of the requested notification
-        * @param notificationElementClass
-        *            The class of the notification item
-        * @return The requested notification, or {@code null} if no notification
-        *         with the given ID could be found
+        * @param newPostNotification
+        *              The new-post notification
+        * @param currentSone
+        *              The current Sone, or {@code null} if not logged in
+        * @return The filtered new-post notification, or {@code null} if the
+        * notification should be removed
         */
-       @SuppressWarnings("unchecked")
-       private static <T> ListNotification<T> getNotification(Collection<? extends Notification> notifications, String notificationId, Class<T> notificationElementClass) {
-               for (Notification notification : notifications) {
-                       if (!notificationId.equals(notification.getId())) {
-                               continue;
-                       }
-                       return (ListNotification<T>) notification;
+       @Nonnull
+       private Optional<ListNotification<Post>> filterNewPostNotification(@Nonnull ListNotification<Post> newPostNotification,
+                       @Nonnull Sone currentSone) {
+               List<Post> newPosts = from(newPostNotification.getElements()).filter(postVisibilityFilter.isVisible(currentSone)).toList();
+               if (newPosts.isEmpty()) {
+                       return Optional.absent();
+               }
+               if (newPosts.size() == newPostNotification.getElements().size()) {
+                       return Optional.of(newPostNotification);
+               }
+               ListNotification<Post> filteredNotification = new ListNotification<Post>(newPostNotification);
+               filteredNotification.setElements(newPosts);
+               filteredNotification.setLastUpdateTime(newPostNotification.getLastUpdatedTime());
+               return Optional.of(filteredNotification);
+       }
+
+       /**
+        * Filters the new replies of the given notification. If {@code currentSone}
+        * is {@code null}, {@code null} is returned and the notification is
+        * subsequently removed. Otherwise only replies that are replies to posts
+        * that are posted by friend Sones of the given Sone are retained; all other
+        * replies are removed.
+        *
+        * @param newReplyNotification
+        *              The new-reply notification
+        * @param currentSone
+        *              The current Sone, or {@code null} if not logged in
+        * @return The filtered new-reply notification, or {@code null} if the
+        * notification should be removed
+        */
+       private Optional<ListNotification<PostReply>> filterNewReplyNotification(ListNotification<PostReply> newReplyNotification,
+                       @Nonnull Sone currentSone) {
+               List<PostReply> newReplies = from(newReplyNotification.getElements()).filter(replyVisibilityFilter.isVisible(currentSone)).toList();
+               if (newReplies.isEmpty()) {
+                       return Optional.absent();
+               }
+               if (newReplies.size() == newReplyNotification.getElements().size()) {
+                       return Optional.of(newReplyNotification);
                }
-               return null;
+               ListNotification<PostReply> filteredNotification = new ListNotification<PostReply>(newReplyNotification);
+               filteredNotification.setElements(newReplies);
+               filteredNotification.setLastUpdateTime(newReplyNotification.getLastUpdatedTime());
+               return Optional.of(filteredNotification);
        }
 
 }