2 * Sone - ListNotificationFilters.java - Copyright © 2010–2015 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.inject.Inject;
27 import javax.inject.Singleton;
29 import net.pterodactylus.sone.data.Post;
30 import net.pterodactylus.sone.data.PostReply;
31 import net.pterodactylus.sone.data.Sone;
32 import net.pterodactylus.util.notify.Notification;
34 import com.google.common.base.Optional;
37 * Filter for {@link ListNotification}s.
39 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
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<Notification>();
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 = filterNewPostNotification((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 = filterNewPostNotification((ListNotification<Post>) notification, null);
101 if (filteredNotification.isPresent()) {
102 filteredNotifications.add(filteredNotification.get());
105 filteredNotifications.add(notification);
108 return filteredNotifications;
112 * Filters the new posts of the given notification. If {@code currentSone}
113 * is {@code null} and {@code soneRequired} is {@code true}, {@code null} is
114 * returned and the notification is subsequently removed. Otherwise only
115 * posts that are posted by friend Sones of the given Sone are retained; all
116 * other posts are removed.
118 * @param newPostNotification
119 * The new-post notification
121 * The current Sone, or {@code null} if not logged in
122 * @return The filtered new-post notification, or {@code null} if the
123 * notification should be removed
126 private Optional<ListNotification<Post>> filterNewPostNotification(@Nonnull ListNotification<Post> newPostNotification,
127 @Nonnull Sone currentSone) {
128 List<Post> newPosts = from(newPostNotification.getElements()).filter(postVisibilityFilter.isVisible(currentSone)).toList();
129 if (newPosts.isEmpty()) {
130 return Optional.absent();
132 if (newPosts.size() == newPostNotification.getElements().size()) {
133 return Optional.of(newPostNotification);
135 ListNotification<Post> filteredNotification = new ListNotification<Post>(newPostNotification);
136 filteredNotification.setElements(newPosts);
137 filteredNotification.setLastUpdateTime(newPostNotification.getLastUpdatedTime());
138 return Optional.of(filteredNotification);
142 * Filters the new replies of the given notification. If {@code currentSone}
143 * is {@code null}, {@code null} is returned and the notification is
144 * subsequently removed. Otherwise only replies that are replies to posts
145 * that are posted by friend Sones of the given Sone are retained; all other
146 * replies are removed.
148 * @param newReplyNotification
149 * The new-reply notification
151 * The current Sone, or {@code null} if not logged in
152 * @return The filtered new-reply notification, or {@code null} if the
153 * notification should be removed
155 private Optional<ListNotification<PostReply>> filterNewReplyNotification(ListNotification<PostReply> newReplyNotification,
156 @Nonnull Sone currentSone) {
157 List<PostReply> newReplies = from(newReplyNotification.getElements()).filter(replyVisibilityFilter.isVisible(currentSone)).toList();
158 if (newReplies.isEmpty()) {
159 return Optional.absent();
161 if (newReplies.size() == newReplyNotification.getElements().size()) {
162 return Optional.of(newReplyNotification);
164 ListNotification<PostReply> filteredNotification = new ListNotification<PostReply>(newReplyNotification);
165 filteredNotification.setElements(newReplies);
166 filteredNotification.setLastUpdateTime(newReplyNotification.getLastUpdatedTime());
167 return Optional.of(filteredNotification);