2 * Sone - ListNotificationFilter.java - Copyright © 2010–2019 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;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.List;
25 import javax.annotation.Nonnull;
26 import javax.annotation.Nullable;
27 import javax.inject.Inject;
28 import javax.inject.Singleton;
30 import net.pterodactylus.sone.data.Post;
31 import net.pterodactylus.sone.data.PostReply;
32 import net.pterodactylus.sone.data.Sone;
33 import net.pterodactylus.util.notify.Notification;
35 import com.google.common.base.Optional;
38 * Filter for {@link ListNotification}s.
41 public class ListNotificationFilter {
43 private final PostVisibilityFilter postVisibilityFilter;
44 private final ReplyVisibilityFilter replyVisibilityFilter;
47 public ListNotificationFilter(@Nonnull PostVisibilityFilter postVisibilityFilter, @Nonnull ReplyVisibilityFilter replyVisibilityFilter) {
48 this.postVisibilityFilter = postVisibilityFilter;
49 this.replyVisibilityFilter = replyVisibilityFilter;
53 * Filters new-post and new-reply notifications in the given list of
54 * notifications. If {@code currentSone} is <code>null</code>, new-post and
55 * new-reply notifications are removed completely. If {@code currentSone} is
56 * not {@code null}, only posts that are posted by a friend Sone or the Sone
57 * itself, and replies that are replies to posts of friend Sones or the Sone
58 * itself will be retained in the notifications.
60 * @param notifications
61 * The notifications to filter
63 * The current Sone, or {@code null} if not logged in
64 * @return The filtered notifications
66 @SuppressWarnings("unchecked")
67 public List<Notification> filterNotifications(Collection<? extends Notification> notifications, Sone currentSone) {
68 List<Notification> filteredNotifications = new ArrayList<>();
69 for (Notification notification : notifications) {
70 if (notification.getId().equals("new-sone-notification")) {
71 if ((currentSone != null) && !currentSone.getOptions().isShowNewSoneNotifications()) {
74 filteredNotifications.add(notification);
75 } else if (notification.getId().equals("new-post-notification")) {
76 if (currentSone == null) {
79 if (!currentSone.getOptions().isShowNewPostNotifications()) {
82 Optional<ListNotification<Post>> filteredNotification = filterPostNotification((ListNotification<Post>) notification, currentSone);
83 if (filteredNotification.isPresent()) {
84 filteredNotifications.add(filteredNotification.get());
86 } else if (notification.getId().equals("new-reply-notification")) {
87 if (currentSone == null) {
90 if (!currentSone.getOptions().isShowNewReplyNotifications()) {
93 Optional<ListNotification<PostReply>> filteredNotification =
94 filterNewReplyNotification((ListNotification<PostReply>) notification, currentSone);
95 if (filteredNotification.isPresent()) {
96 filteredNotifications.add(filteredNotification.get());
98 } else if (notification.getId().equals("mention-notification")) {
99 Optional<ListNotification<Post>> filteredNotification = filterPostNotification((ListNotification<Post>) notification, null);
100 if (filteredNotification.isPresent()) {
101 filteredNotifications.add(filteredNotification.get());
104 filteredNotifications.add(notification);
107 return filteredNotifications;
111 * Filters the posts of the given notification.
113 * @param postNotification
114 * The post notification
116 * The current Sone, or {@code null} if not logged in
117 * @return The filtered post notification, or {@link Optional#absent()} if the notification should be removed
120 private Optional<ListNotification<Post>> filterPostNotification(@Nonnull ListNotification<Post> postNotification,
121 @Nullable Sone currentSone) {
122 List<Post> newPosts = from(postNotification.getElements()).filter(postVisibilityFilter.isVisible(currentSone)).toList();
123 if (newPosts.isEmpty()) {
124 return Optional.absent();
126 if (newPosts.size() == postNotification.getElements().size()) {
127 return Optional.of(postNotification);
129 ListNotification<Post> filteredNotification = new ListNotification<>(postNotification);
130 filteredNotification.setElements(newPosts);
131 filteredNotification.setLastUpdateTime(postNotification.getLastUpdatedTime());
132 return Optional.of(filteredNotification);
136 * Filters the new replies of the given notification. If {@code currentSone}
137 * is {@code null}, {@code null} is returned and the notification is
138 * subsequently removed. Otherwise only replies that are replies to posts
139 * that are posted by friend Sones of the given Sone are retained; all other
140 * replies are removed.
142 * @param newReplyNotification
143 * The new-reply notification
145 * The current Sone, or {@code null} if not logged in
146 * @return The filtered new-reply notification, or {@code null} if the
147 * notification should be removed
149 private Optional<ListNotification<PostReply>> filterNewReplyNotification(ListNotification<PostReply> newReplyNotification,
150 @Nonnull Sone currentSone) {
151 List<PostReply> newReplies = from(newReplyNotification.getElements()).filter(replyVisibilityFilter.isVisible(currentSone)).toList();
152 if (newReplies.isEmpty()) {
153 return Optional.absent();
155 if (newReplies.size() == newReplyNotification.getElements().size()) {
156 return Optional.of(newReplyNotification);
158 ListNotification<PostReply> filteredNotification = new ListNotification<>(newReplyNotification);
159 filteredNotification.setElements(newReplies);
160 filteredNotification.setLastUpdateTime(newReplyNotification.getLastUpdatedTime());
161 return Optional.of(filteredNotification);