🎨 Replace list notification filter with Kotlin version
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 28 Jun 2020 22:10:43 +0000 (00:10 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 28 Jun 2020 22:14:04 +0000 (00:14 +0200)
src/main/java/net/pterodactylus/sone/notify/ListNotificationFilter.java [deleted file]
src/main/kotlin/net/pterodactylus/sone/notify/ListNotificationFilter.kt [new file with mode: 0644]
src/main/kotlin/net/pterodactylus/sone/notify/Notifications.kt

diff --git a/src/main/java/net/pterodactylus/sone/notify/ListNotificationFilter.java b/src/main/java/net/pterodactylus/sone/notify/ListNotificationFilter.java
deleted file mode 100644 (file)
index 76bfbff..0000000
+++ /dev/null
@@ -1,164 +0,0 @@
-/*
- * Sone - ListNotificationFilter.java - Copyright Â© 2010–2020 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
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-package net.pterodactylus.sone.notify;
-
-import static java.util.stream.Collectors.toList;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
-import javax.annotation.Nonnull;
-import javax.annotation.Nullable;
-import javax.inject.Inject;
-import javax.inject.Singleton;
-
-import net.pterodactylus.sone.data.Post;
-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.
- */
-@Singleton
-public class ListNotificationFilter {
-
-       private final PostVisibilityFilter postVisibilityFilter;
-       private final ReplyVisibilityFilter replyVisibilityFilter;
-
-       @Inject
-       public ListNotificationFilter(@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>, 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
-        * @param currentSone
-        *              The current Sone, or {@code null} if not logged in
-        * @return The filtered notifications
-        */
-       @SuppressWarnings("unchecked")
-       public List<Notification> filterNotifications(Collection<? extends Notification> notifications, Sone currentSone) {
-               List<Notification> filteredNotifications = new ArrayList<>();
-               for (Notification notification : notifications) {
-                       if (notification.getId().equals("new-sone-notification")) {
-                               if ((currentSone != null) && !currentSone.getOptions().isShowNewSoneNotifications()) {
-                                       continue;
-                               }
-                               filteredNotifications.add(notification);
-                       } else if (notification.getId().equals("new-post-notification")) {
-                               if (currentSone == null) {
-                                       continue;
-                               }
-                               if (!currentSone.getOptions().isShowNewPostNotifications()) {
-                                       continue;
-                               }
-                               Optional<ListNotification<Post>> filteredNotification = filterPostNotification((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 = filterPostNotification((ListNotification<Post>) notification, null);
-                               if (filteredNotification.isPresent()) {
-                                       filteredNotifications.add(filteredNotification.get());
-                               }
-                       } else {
-                               filteredNotifications.add(notification);
-                       }
-               }
-               return filteredNotifications;
-       }
-
-       /**
-        * Filters the posts of the given notification.
-        *
-        * @param postNotification
-        *              The post notification
-        * @param currentSone
-        *              The current Sone, or {@code null} if not logged in
-        * @return The filtered post notification, or {@link Optional#absent()} if the notification should be removed
-        */
-       @Nonnull
-       private Optional<ListNotification<Post>> filterPostNotification(@Nonnull ListNotification<Post> postNotification,
-                       @Nullable Sone currentSone) {
-               List<Post> newPosts = postNotification.getElements().stream().filter(postVisibilityFilter.isVisible(currentSone)).collect(toList());
-               if (newPosts.isEmpty()) {
-                       return Optional.absent();
-               }
-               if (newPosts.size() == postNotification.getElements().size()) {
-                       return Optional.of(postNotification);
-               }
-               ListNotification<Post> filteredNotification = new ListNotification<>(postNotification);
-               filteredNotification.setElements(newPosts);
-               filteredNotification.setLastUpdateTime(postNotification.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 = newReplyNotification.getElements().stream().filter(replyVisibilityFilter.isVisible(currentSone)).collect(toList());
-               if (newReplies.isEmpty()) {
-                       return Optional.absent();
-               }
-               if (newReplies.size() == newReplyNotification.getElements().size()) {
-                       return Optional.of(newReplyNotification);
-               }
-               ListNotification<PostReply> filteredNotification = new ListNotification<>(newReplyNotification);
-               filteredNotification.setElements(newReplies);
-               filteredNotification.setLastUpdateTime(newReplyNotification.getLastUpdatedTime());
-               return Optional.of(filteredNotification);
-       }
-
-}
diff --git a/src/main/kotlin/net/pterodactylus/sone/notify/ListNotificationFilter.kt b/src/main/kotlin/net/pterodactylus/sone/notify/ListNotificationFilter.kt
new file mode 100644 (file)
index 0000000..aedb180
--- /dev/null
@@ -0,0 +1,90 @@
+/*
+ * Sone - ListNotificationFilter.kt - Copyright Â© 2010–2020 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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package net.pterodactylus.sone.notify
+
+import net.pterodactylus.sone.data.Post
+import net.pterodactylus.sone.data.PostReply
+import net.pterodactylus.sone.data.Sone
+import net.pterodactylus.sone.utils.ifTrue
+import net.pterodactylus.util.notify.Notification
+import javax.inject.Inject
+import javax.inject.Singleton
+
+/**
+ * Filter for [ListNotification]s.
+ */
+@Singleton
+class ListNotificationFilter @Inject constructor(private val postVisibilityFilter: PostVisibilityFilter, private val replyVisibilityFilter: ReplyVisibilityFilter) {
+
+       /**
+        * Filters new-post and new-reply notifications in the given list of
+        * notifications. If `currentSone` is `null`, new-post and
+        * new-reply notifications are removed completely. If `currentSone` is
+        * not `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
+        * @param currentSone
+        * The current Sone, or `null` if not logged in
+        * @return The filtered notifications
+        */
+       fun filterNotifications(notifications: Collection<Notification>, currentSone: Sone?) =
+                       notifications.mapNotNull { it.filtered(currentSone) }
+
+       @Suppress("UNCHECKED_CAST")
+       private fun Notification.filtered(currentSone: Sone?): Notification? = when {
+               isNewSoneNotification -> {
+                       takeIf { currentSone == null || currentSone.options.isShowNewSoneNotifications }
+               }
+               isNewPostNotification -> {
+                       (currentSone != null && currentSone.options.isShowNewPostNotifications).ifTrue {
+                               (this as ListNotification<Post>).filterNotification { postVisibilityFilter.isPostVisible(currentSone, it) }
+                       }
+               }
+               isNewReplyNotification -> {
+                       (currentSone != null && currentSone.options.isShowNewReplyNotifications).ifTrue {
+                               (this as ListNotification<PostReply>).filterNotification { replyVisibilityFilter.isReplyVisible(currentSone, it) }
+                       }
+               }
+               isMentionNotification -> {
+                       (this as ListNotification<Post>).filterNotification { postVisibilityFilter.isPostVisible(currentSone, it) }
+               }
+               else -> this
+       }
+
+}
+
+/**
+ * Filters the elements of this list notification.
+ *
+ * @param filter The filter for the notification’s elements
+ * @return A list notification containing the filtered elements, or `null`
+ * if the notification does not have any elements after filtering
+ */
+private fun <T> ListNotification<T>.filterNotification(filter: (T) -> Boolean) =
+               elements.filter(filter).let { filteredElements ->
+                       when (filteredElements.size) {
+                               0 -> null
+                               elements.size -> this
+                               else -> ListNotification(this).apply {
+                                       setElements(filteredElements)
+                                       setLastUpdateTime(lastUpdatedTime)
+                               }
+                       }
+               }
index 5df9484..86d1316 100644 (file)
@@ -30,3 +30,28 @@ operator fun NotificationManager.contains(id: String) =
  */
 fun NotificationManager.hasFirstStartNotification() =
                "first-start-notification" in this
+
+
+/**
+ * Returns whether this notification is for new Sones.
+ */
+val Notification.isNewSoneNotification: Boolean
+       get() = id == "new-sone-notification"
+
+/**
+ * Returns whether this notification is for new posts.
+ */
+val Notification.isNewPostNotification: Boolean
+       get() = id == "new-post-notification"
+
+/**
+ * Returns whether this notification is for new replies.
+ */
+val Notification.isNewReplyNotification: Boolean
+       get() = id == "new-reply-notification"
+
+/**
+ * Returns whether this notification is for mentions.
+ */
+val Notification.isMentionNotification: Boolean
+       get() = id == "mention-notification"