From 35b69560c4bd6c8babfd8e93b8fd6403b2754f3d Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 29 Jun 2020 00:10:43 +0200 Subject: [PATCH] =?utf8?q?=F0=9F=8E=A8=20Replace=20list=20notification=20f?= =?utf8?q?ilter=20with=20Kotlin=20version?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../sone/notify/ListNotificationFilter.java | 164 --------------------- .../sone/notify/ListNotificationFilter.kt | 90 +++++++++++ .../net/pterodactylus/sone/notify/Notifications.kt | 25 ++++ 3 files changed, 115 insertions(+), 164 deletions(-) delete mode 100644 src/main/java/net/pterodactylus/sone/notify/ListNotificationFilter.java create mode 100644 src/main/kotlin/net/pterodactylus/sone/notify/ListNotificationFilter.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 index 76bfbff..0000000 --- a/src/main/java/net/pterodactylus/sone/notify/ListNotificationFilter.java +++ /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 . - */ - -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 null, 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 filterNotifications(Collection notifications, Sone currentSone) { - List 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> filteredNotification = filterPostNotification((ListNotification) 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> filteredNotification = - filterNewReplyNotification((ListNotification) notification, currentSone); - if (filteredNotification.isPresent()) { - filteredNotifications.add(filteredNotification.get()); - } - } else if (notification.getId().equals("mention-notification")) { - Optional> filteredNotification = filterPostNotification((ListNotification) 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> filterPostNotification(@Nonnull ListNotification postNotification, - @Nullable Sone currentSone) { - List 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 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> filterNewReplyNotification(ListNotification newReplyNotification, - @Nonnull Sone currentSone) { - List 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 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 index 0000000..aedb180 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/notify/ListNotificationFilter.kt @@ -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 . + */ +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, 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).filterNotification { postVisibilityFilter.isPostVisible(currentSone, it) } + } + } + isNewReplyNotification -> { + (currentSone != null && currentSone.options.isShowNewReplyNotifications).ifTrue { + (this as ListNotification).filterNotification { replyVisibilityFilter.isReplyVisible(currentSone, it) } + } + } + isMentionNotification -> { + (this as ListNotification).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 ListNotification.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) + } + } + } diff --git a/src/main/kotlin/net/pterodactylus/sone/notify/Notifications.kt b/src/main/kotlin/net/pterodactylus/sone/notify/Notifications.kt index 5df9484..86d1316 100644 --- a/src/main/kotlin/net/pterodactylus/sone/notify/Notifications.kt +++ b/src/main/kotlin/net/pterodactylus/sone/notify/Notifications.kt @@ -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" -- 2.7.4