Update year in copyright lines
[Sone.git] / src / main / java / net / pterodactylus / sone / notify / ListNotificationFilter.java
1 /*
2  * Sone - ListNotificationFilter.java - Copyright © 2010–2019 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 @Singleton
41 public class ListNotificationFilter {
42
43         private final PostVisibilityFilter postVisibilityFilter;
44         private final ReplyVisibilityFilter replyVisibilityFilter;
45
46         @Inject
47         public ListNotificationFilter(@Nonnull PostVisibilityFilter postVisibilityFilter, @Nonnull ReplyVisibilityFilter replyVisibilityFilter) {
48                 this.postVisibilityFilter = postVisibilityFilter;
49                 this.replyVisibilityFilter = replyVisibilityFilter;
50         }
51
52         /**
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.
59          *
60          * @param notifications
61          *              The notifications to filter
62          * @param currentSone
63          *              The current Sone, or {@code null} if not logged in
64          * @return The filtered notifications
65          */
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()) {
72                                         continue;
73                                 }
74                                 filteredNotifications.add(notification);
75                         } else if (notification.getId().equals("new-post-notification")) {
76                                 if (currentSone == null) {
77                                         continue;
78                                 }
79                                 if (!currentSone.getOptions().isShowNewPostNotifications()) {
80                                         continue;
81                                 }
82                                 Optional<ListNotification<Post>> filteredNotification = filterPostNotification((ListNotification<Post>) notification, currentSone);
83                                 if (filteredNotification.isPresent()) {
84                                         filteredNotifications.add(filteredNotification.get());
85                                 }
86                         } else if (notification.getId().equals("new-reply-notification")) {
87                                 if (currentSone == null) {
88                                         continue;
89                                 }
90                                 if (!currentSone.getOptions().isShowNewReplyNotifications()) {
91                                         continue;
92                                 }
93                                 Optional<ListNotification<PostReply>> filteredNotification =
94                                                 filterNewReplyNotification((ListNotification<PostReply>) notification, currentSone);
95                                 if (filteredNotification.isPresent()) {
96                                         filteredNotifications.add(filteredNotification.get());
97                                 }
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());
102                                 }
103                         } else {
104                                 filteredNotifications.add(notification);
105                         }
106                 }
107                 return filteredNotifications;
108         }
109
110         /**
111          * Filters the posts of the given notification.
112          *
113          * @param postNotification
114          *              The post notification
115          * @param currentSone
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
118          */
119         @Nonnull
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();
125                 }
126                 if (newPosts.size() == postNotification.getElements().size()) {
127                         return Optional.of(postNotification);
128                 }
129                 ListNotification<Post> filteredNotification = new ListNotification<>(postNotification);
130                 filteredNotification.setElements(newPosts);
131                 filteredNotification.setLastUpdateTime(postNotification.getLastUpdatedTime());
132                 return Optional.of(filteredNotification);
133         }
134
135         /**
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.
141          *
142          * @param newReplyNotification
143          *              The new-reply notification
144          * @param currentSone
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
148          */
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();
154                 }
155                 if (newReplies.size() == newReplyNotification.getElements().size()) {
156                         return Optional.of(newReplyNotification);
157                 }
158                 ListNotification<PostReply> filteredNotification = new ListNotification<>(newReplyNotification);
159                 filteredNotification.setElements(newReplies);
160                 filteredNotification.setLastUpdateTime(newReplyNotification.getLastUpdatedTime());
161                 return Optional.of(filteredNotification);
162         }
163
164 }