Rename list notification filters class
[Sone.git] / src / main / java / net / pterodactylus / sone / notify / ListNotificationFilter.java
1 /*
2  * Sone - ListNotificationFilters.java - Copyright © 2010–2015 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.notify;
19
20 import static com.google.common.collect.FluentIterable.from;
21
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;
28
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;
33
34 import com.google.common.base.Optional;
35
36 /**
37  * Filter for {@link ListNotification}s.
38  *
39  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40  */
41 @Singleton
42 public class ListNotificationFilter {
43
44         private final PostVisibilityFilter postVisibilityFilter;
45         private final ReplyVisibilityFilter replyVisibilityFilter;
46
47         @Inject
48         public ListNotificationFilter(@Nonnull PostVisibilityFilter postVisibilityFilter, @Nonnull ReplyVisibilityFilter replyVisibilityFilter) {
49                 this.postVisibilityFilter = postVisibilityFilter;
50                 this.replyVisibilityFilter = replyVisibilityFilter;
51         }
52
53         /**
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.
60          *
61          * @param notifications
62          *              The notifications to filter
63          * @param currentSone
64          *              The current Sone, or {@code null} if not logged in
65          * @return The filtered notifications
66          */
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()) {
73                                         continue;
74                                 }
75                                 filteredNotifications.add(notification);
76                         } else if (notification.getId().equals("new-post-notification")) {
77                                 if (currentSone == null) {
78                                         continue;
79                                 }
80                                 if (!currentSone.getOptions().isShowNewPostNotifications()) {
81                                         continue;
82                                 }
83                                 Optional<ListNotification<Post>> filteredNotification = filterNewPostNotification((ListNotification<Post>) notification, currentSone);
84                                 if (filteredNotification.isPresent()) {
85                                         filteredNotifications.add(filteredNotification.get());
86                                 }
87                         } else if (notification.getId().equals("new-reply-notification")) {
88                                 if (currentSone == null) {
89                                         continue;
90                                 }
91                                 if (!currentSone.getOptions().isShowNewReplyNotifications()) {
92                                         continue;
93                                 }
94                                 Optional<ListNotification<PostReply>> filteredNotification =
95                                                 filterNewReplyNotification((ListNotification<PostReply>) notification, currentSone);
96                                 if (filteredNotification.isPresent()) {
97                                         filteredNotifications.add(filteredNotification.get());
98                                 }
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());
103                                 }
104                         } else {
105                                 filteredNotifications.add(notification);
106                         }
107                 }
108                 return filteredNotifications;
109         }
110
111         /**
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.
117          *
118          * @param newPostNotification
119          *              The new-post notification
120          * @param currentSone
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
124          */
125         @Nonnull
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();
131                 }
132                 if (newPosts.size() == newPostNotification.getElements().size()) {
133                         return Optional.of(newPostNotification);
134                 }
135                 ListNotification<Post> filteredNotification = new ListNotification<Post>(newPostNotification);
136                 filteredNotification.setElements(newPosts);
137                 filteredNotification.setLastUpdateTime(newPostNotification.getLastUpdatedTime());
138                 return Optional.of(filteredNotification);
139         }
140
141         /**
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.
147          *
148          * @param newReplyNotification
149          *              The new-reply notification
150          * @param currentSone
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
154          */
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();
160                 }
161                 if (newReplies.size() == newReplyNotification.getElements().size()) {
162                         return Optional.of(newReplyNotification);
163                 }
164                 ListNotification<PostReply> filteredNotification = new ListNotification<PostReply>(newReplyNotification);
165                 filteredNotification.setElements(newReplies);
166                 filteredNotification.setLastUpdateTime(newReplyNotification.getLastUpdatedTime());
167                 return Optional.of(filteredNotification);
168         }
169
170 }