2 * Sone - ListNotificationFilter.java - Copyright © 2010–2020 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.notify;
20 import static com.google.common.collect.FluentIterable.from;
21 import static java.util.stream.Collectors.toList;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.List;
26 import javax.annotation.Nonnull;
27 import javax.annotation.Nullable;
28 import javax.inject.Inject;
29 import javax.inject.Singleton;
31 import net.pterodactylus.sone.data.Post;
32 import net.pterodactylus.sone.data.PostReply;
33 import net.pterodactylus.sone.data.Sone;
34 import net.pterodactylus.util.notify.Notification;
36 import com.google.common.base.Optional;
39 * Filter for {@link ListNotification}s.
42 public class ListNotificationFilter {
44 private final PostVisibilityFilter postVisibilityFilter;
45 private final ReplyVisibilityFilter replyVisibilityFilter;
48 public ListNotificationFilter(@Nonnull PostVisibilityFilter postVisibilityFilter, @Nonnull ReplyVisibilityFilter replyVisibilityFilter) {
49 this.postVisibilityFilter = postVisibilityFilter;
50 this.replyVisibilityFilter = replyVisibilityFilter;
54 * Filters new-post and new-reply notifications in the given list of
55 * notifications. If {@code currentSone} is <code>null</code>, new-post and
56 * new-reply notifications are removed completely. If {@code currentSone} is
57 * not {@code null}, only posts that are posted by a friend Sone or the Sone
58 * itself, and replies that are replies to posts of friend Sones or the Sone
59 * itself will be retained in the notifications.
61 * @param notifications
62 * The notifications to filter
64 * The current Sone, or {@code null} if not logged in
65 * @return The filtered notifications
67 @SuppressWarnings("unchecked")
68 public List<Notification> filterNotifications(Collection<? extends Notification> notifications, Sone currentSone) {
69 List<Notification> filteredNotifications = new ArrayList<>();
70 for (Notification notification : notifications) {
71 if (notification.getId().equals("new-sone-notification")) {
72 if ((currentSone != null) && !currentSone.getOptions().isShowNewSoneNotifications()) {
75 filteredNotifications.add(notification);
76 } else if (notification.getId().equals("new-post-notification")) {
77 if (currentSone == null) {
80 if (!currentSone.getOptions().isShowNewPostNotifications()) {
83 Optional<ListNotification<Post>> filteredNotification = filterPostNotification((ListNotification<Post>) notification, currentSone);
84 if (filteredNotification.isPresent()) {
85 filteredNotifications.add(filteredNotification.get());
87 } else if (notification.getId().equals("new-reply-notification")) {
88 if (currentSone == null) {
91 if (!currentSone.getOptions().isShowNewReplyNotifications()) {
94 Optional<ListNotification<PostReply>> filteredNotification =
95 filterNewReplyNotification((ListNotification<PostReply>) notification, currentSone);
96 if (filteredNotification.isPresent()) {
97 filteredNotifications.add(filteredNotification.get());
99 } else if (notification.getId().equals("mention-notification")) {
100 Optional<ListNotification<Post>> filteredNotification = filterPostNotification((ListNotification<Post>) notification, null);
101 if (filteredNotification.isPresent()) {
102 filteredNotifications.add(filteredNotification.get());
105 filteredNotifications.add(notification);
108 return filteredNotifications;
112 * Filters the posts of the given notification.
114 * @param postNotification
115 * The post notification
117 * The current Sone, or {@code null} if not logged in
118 * @return The filtered post notification, or {@link Optional#absent()} if the notification should be removed
121 private Optional<ListNotification<Post>> filterPostNotification(@Nonnull ListNotification<Post> postNotification,
122 @Nullable Sone currentSone) {
123 List<Post> newPosts = postNotification.getElements().stream().filter(postVisibilityFilter.isVisible(currentSone)).collect(toList());
124 if (newPosts.isEmpty()) {
125 return Optional.absent();
127 if (newPosts.size() == postNotification.getElements().size()) {
128 return Optional.of(postNotification);
130 ListNotification<Post> filteredNotification = new ListNotification<>(postNotification);
131 filteredNotification.setElements(newPosts);
132 filteredNotification.setLastUpdateTime(postNotification.getLastUpdatedTime());
133 return Optional.of(filteredNotification);
137 * Filters the new replies of the given notification. If {@code currentSone}
138 * is {@code null}, {@code null} is returned and the notification is
139 * subsequently removed. Otherwise only replies that are replies to posts
140 * that are posted by friend Sones of the given Sone are retained; all other
141 * replies are removed.
143 * @param newReplyNotification
144 * The new-reply notification
146 * The current Sone, or {@code null} if not logged in
147 * @return The filtered new-reply notification, or {@code null} if the
148 * notification should be removed
150 private Optional<ListNotification<PostReply>> filterNewReplyNotification(ListNotification<PostReply> newReplyNotification,
151 @Nonnull Sone currentSone) {
152 List<PostReply> newReplies = from(newReplyNotification.getElements()).filter(replyVisibilityFilter.isVisible(currentSone)).toList();
153 if (newReplies.isEmpty()) {
154 return Optional.absent();
156 if (newReplies.size() == newReplyNotification.getElements().size()) {
157 return Optional.of(newReplyNotification);
159 ListNotification<PostReply> filteredNotification = new ListNotification<>(newReplyNotification);
160 filteredNotification.setElements(newReplies);
161 filteredNotification.setLastUpdateTime(newReplyNotification.getLastUpdatedTime());
162 return Optional.of(filteredNotification);