2 * Sone - ListNotificationFilter.java - Copyright © 2010–2016 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.
40 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43 public class ListNotificationFilter {
45 private final PostVisibilityFilter postVisibilityFilter;
46 private final ReplyVisibilityFilter replyVisibilityFilter;
49 public ListNotificationFilter(@Nonnull PostVisibilityFilter postVisibilityFilter, @Nonnull ReplyVisibilityFilter replyVisibilityFilter) {
50 this.postVisibilityFilter = postVisibilityFilter;
51 this.replyVisibilityFilter = replyVisibilityFilter;
55 * Filters new-post and new-reply notifications in the given list of
56 * notifications. If {@code currentSone} is <code>null</code>, new-post and
57 * new-reply notifications are removed completely. If {@code currentSone} is
58 * not {@code null}, only posts that are posted by a friend Sone or the Sone
59 * itself, and replies that are replies to posts of friend Sones or the Sone
60 * itself will be retained in the notifications.
62 * @param notifications
63 * The notifications to filter
65 * The current Sone, or {@code null} if not logged in
66 * @return The filtered notifications
68 @SuppressWarnings("unchecked")
69 public List<Notification> filterNotifications(Collection<? extends Notification> notifications, Sone currentSone) {
70 List<Notification> filteredNotifications = new ArrayList<Notification>();
71 for (Notification notification : notifications) {
72 if (notification.getId().equals("new-sone-notification")) {
73 if ((currentSone != null) && !currentSone.getOptions().isShowNewSoneNotifications()) {
76 filteredNotifications.add(notification);
77 } else if (notification.getId().equals("new-post-notification")) {
78 if (currentSone == null) {
81 if (!currentSone.getOptions().isShowNewPostNotifications()) {
84 Optional<ListNotification<Post>> filteredNotification = filterPostNotification((ListNotification<Post>) notification, currentSone);
85 if (filteredNotification.isPresent()) {
86 filteredNotifications.add(filteredNotification.get());
88 } else if (notification.getId().equals("new-reply-notification")) {
89 if (currentSone == null) {
92 if (!currentSone.getOptions().isShowNewReplyNotifications()) {
95 Optional<ListNotification<PostReply>> filteredNotification =
96 filterNewReplyNotification((ListNotification<PostReply>) notification, currentSone);
97 if (filteredNotification.isPresent()) {
98 filteredNotifications.add(filteredNotification.get());
100 } else if (notification.getId().equals("mention-notification")) {
101 Optional<ListNotification<Post>> filteredNotification = filterPostNotification((ListNotification<Post>) notification, null);
102 if (filteredNotification.isPresent()) {
103 filteredNotifications.add(filteredNotification.get());
106 filteredNotifications.add(notification);
109 return filteredNotifications;
113 * Filters the posts of the given notification.
115 * @param postNotification
116 * The post notification
118 * The current Sone, or {@code null} if not logged in
119 * @return The filtered post notification, or {@link Optional#absent()} if the notification should be removed
122 private Optional<ListNotification<Post>> filterPostNotification(@Nonnull ListNotification<Post> postNotification,
123 @Nullable Sone currentSone) {
124 List<Post> newPosts = from(postNotification.getElements()).filter(postVisibilityFilter.isVisible(currentSone)).toList();
125 if (newPosts.isEmpty()) {
126 return Optional.absent();
128 if (newPosts.size() == postNotification.getElements().size()) {
129 return Optional.of(postNotification);
131 ListNotification<Post> filteredNotification = new ListNotification<Post>(postNotification);
132 filteredNotification.setElements(newPosts);
133 filteredNotification.setLastUpdateTime(postNotification.getLastUpdatedTime());
134 return Optional.of(filteredNotification);
138 * Filters the new replies of the given notification. If {@code currentSone}
139 * is {@code null}, {@code null} is returned and the notification is
140 * subsequently removed. Otherwise only replies that are replies to posts
141 * that are posted by friend Sones of the given Sone are retained; all other
142 * replies are removed.
144 * @param newReplyNotification
145 * The new-reply notification
147 * The current Sone, or {@code null} if not logged in
148 * @return The filtered new-reply notification, or {@code null} if the
149 * notification should be removed
151 private Optional<ListNotification<PostReply>> filterNewReplyNotification(ListNotification<PostReply> newReplyNotification,
152 @Nonnull Sone currentSone) {
153 List<PostReply> newReplies = from(newReplyNotification.getElements()).filter(replyVisibilityFilter.isVisible(currentSone)).toList();
154 if (newReplies.isEmpty()) {
155 return Optional.absent();
157 if (newReplies.size() == newReplyNotification.getElements().size()) {
158 return Optional.of(newReplyNotification);
160 ListNotification<PostReply> filteredNotification = new ListNotification<PostReply>(newReplyNotification);
161 filteredNotification.setElements(newReplies);
162 filteredNotification.setLastUpdateTime(newReplyNotification.getLastUpdatedTime());
163 return Optional.of(filteredNotification);