f2da694b336aa98e158287e5a9687c94f0efa489
[Sone.git] / src / main / java / net / pterodactylus / sone / notify / ListNotificationFilter.java
1 /*
2  * Sone - ListNotificationFilter.java - Copyright © 2010–2020 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 import static java.util.stream.Collectors.toList;
22
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.List;
26 import javax.annotation.Nonnull;
27 import javax.annotation.Nullable;
28 import javax.inject.Inject;
29 import javax.inject.Singleton;
30
31 import net.pterodactylus.sone.data.Post;
32 import net.pterodactylus.sone.data.PostReply;
33 import net.pterodactylus.sone.data.Sone;
34 import net.pterodactylus.util.notify.Notification;
35
36 import com.google.common.base.Optional;
37
38 /**
39  * Filter for {@link ListNotification}s.
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<>();
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 = filterPostNotification((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 = filterPostNotification((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 posts of the given notification.
113          *
114          * @param postNotification
115          *              The post notification
116          * @param currentSone
117          *              The current Sone, or {@code null} if not logged in
118          * @return The filtered post notification, or {@link Optional#absent()} if the notification should be removed
119          */
120         @Nonnull
121         private Optional<ListNotification<Post>> filterPostNotification(@Nonnull ListNotification<Post> postNotification,
122                         @Nullable Sone currentSone) {
123                 List<Post> newPosts = postNotification.getElements().stream().filter(postVisibilityFilter.isVisible(currentSone)).collect(toList());
124                 if (newPosts.isEmpty()) {
125                         return Optional.absent();
126                 }
127                 if (newPosts.size() == postNotification.getElements().size()) {
128                         return Optional.of(postNotification);
129                 }
130                 ListNotification<Post> filteredNotification = new ListNotification<>(postNotification);
131                 filteredNotification.setElements(newPosts);
132                 filteredNotification.setLastUpdateTime(postNotification.getLastUpdatedTime());
133                 return Optional.of(filteredNotification);
134         }
135
136         /**
137          * Filters the new replies of the given notification. If {@code currentSone}
138          * is {@code null}, {@code null} is returned and the notification is
139          * subsequently removed. Otherwise only replies that are replies to posts
140          * that are posted by friend Sones of the given Sone are retained; all other
141          * replies are removed.
142          *
143          * @param newReplyNotification
144          *              The new-reply notification
145          * @param currentSone
146          *              The current Sone, or {@code null} if not logged in
147          * @return The filtered new-reply notification, or {@code null} if the
148          * notification should be removed
149          */
150         private Optional<ListNotification<PostReply>> filterNewReplyNotification(ListNotification<PostReply> newReplyNotification,
151                         @Nonnull Sone currentSone) {
152                 List<PostReply> newReplies = from(newReplyNotification.getElements()).filter(replyVisibilityFilter.isVisible(currentSone)).toList();
153                 if (newReplies.isEmpty()) {
154                         return Optional.absent();
155                 }
156                 if (newReplies.size() == newReplyNotification.getElements().size()) {
157                         return Optional.of(newReplyNotification);
158                 }
159                 ListNotification<PostReply> filteredNotification = new ListNotification<>(newReplyNotification);
160                 filteredNotification.setElements(newReplies);
161                 filteredNotification.setLastUpdateTime(newReplyNotification.getLastUpdatedTime());
162                 return Optional.of(filteredNotification);
163         }
164
165 }