Fix method name
[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.annotation.Nullable;
27 import javax.inject.Inject;
28 import javax.inject.Singleton;
29
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;
34
35 import com.google.common.base.Optional;
36
37 /**
38  * Filter for {@link ListNotification}s.
39  *
40  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41  */
42 @Singleton
43 public class ListNotificationFilter {
44
45         private final PostVisibilityFilter postVisibilityFilter;
46         private final ReplyVisibilityFilter replyVisibilityFilter;
47
48         @Inject
49         public ListNotificationFilter(@Nonnull PostVisibilityFilter postVisibilityFilter, @Nonnull ReplyVisibilityFilter replyVisibilityFilter) {
50                 this.postVisibilityFilter = postVisibilityFilter;
51                 this.replyVisibilityFilter = replyVisibilityFilter;
52         }
53
54         /**
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.
61          *
62          * @param notifications
63          *              The notifications to filter
64          * @param currentSone
65          *              The current Sone, or {@code null} if not logged in
66          * @return The filtered notifications
67          */
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()) {
74                                         continue;
75                                 }
76                                 filteredNotifications.add(notification);
77                         } else if (notification.getId().equals("new-post-notification")) {
78                                 if (currentSone == null) {
79                                         continue;
80                                 }
81                                 if (!currentSone.getOptions().isShowNewPostNotifications()) {
82                                         continue;
83                                 }
84                                 Optional<ListNotification<Post>> filteredNotification = filterPostNotification((ListNotification<Post>) notification, currentSone);
85                                 if (filteredNotification.isPresent()) {
86                                         filteredNotifications.add(filteredNotification.get());
87                                 }
88                         } else if (notification.getId().equals("new-reply-notification")) {
89                                 if (currentSone == null) {
90                                         continue;
91                                 }
92                                 if (!currentSone.getOptions().isShowNewReplyNotifications()) {
93                                         continue;
94                                 }
95                                 Optional<ListNotification<PostReply>> filteredNotification =
96                                                 filterNewReplyNotification((ListNotification<PostReply>) notification, currentSone);
97                                 if (filteredNotification.isPresent()) {
98                                         filteredNotifications.add(filteredNotification.get());
99                                 }
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());
104                                 }
105                         } else {
106                                 filteredNotifications.add(notification);
107                         }
108                 }
109                 return filteredNotifications;
110         }
111
112         /**
113          * Filters the posts of the given notification.
114          *
115          * @param postNotification
116          *              The post notification
117          * @param currentSone
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
120          */
121         @Nonnull
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();
127                 }
128                 if (newPosts.size() == postNotification.getElements().size()) {
129                         return Optional.of(postNotification);
130                 }
131                 ListNotification<Post> filteredNotification = new ListNotification<Post>(postNotification);
132                 filteredNotification.setElements(newPosts);
133                 filteredNotification.setLastUpdateTime(postNotification.getLastUpdatedTime());
134                 return Optional.of(filteredNotification);
135         }
136
137         /**
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.
143          *
144          * @param newReplyNotification
145          *              The new-reply notification
146          * @param currentSone
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
150          */
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();
156                 }
157                 if (newReplies.size() == newReplyNotification.getElements().size()) {
158                         return Optional.of(newReplyNotification);
159                 }
160                 ListNotification<PostReply> filteredNotification = new ListNotification<PostReply>(newReplyNotification);
161                 filteredNotification.setElements(newReplies);
162                 filteredNotification.setLastUpdateTime(newReplyNotification.getLastUpdatedTime());
163                 return Optional.of(filteredNotification);
164         }
165
166 }