Remove notification if no posts and replies remain.
[Sone.git] / src / main / java / net / pterodactylus / sone / notify / ListNotificationFilters.java
1 /*
2  * Sone - ListNotificationFilters.java - Copyright © 2010 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 java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.List;
23
24 import net.pterodactylus.sone.data.Post;
25 import net.pterodactylus.sone.data.Reply;
26 import net.pterodactylus.sone.data.Sone;
27 import net.pterodactylus.util.notify.Notification;
28
29 /**
30  * Filter for {@link ListNotification}s.
31  *
32  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
33  */
34 public class ListNotificationFilters {
35
36         /**
37          * Filters new-post and new-reply notifications in the given list of
38          * notifications. If {@code currentSone} is <code>null</code>, new-post and
39          * new-reply notifications are removed completely. If {@code currentSone} is
40          * not {@code null}, only posts that are posted by a friend Sone or the Sone
41          * itself, and replies that are replies to posts of friend Sones or the Sone
42          * itself will be retained in the notifications.
43          *
44          * @param notifications
45          *            The notifications to filter
46          * @param currentSone
47          *            The current Sone, or {@code null} if not logged in
48          * @return The filtered notifications
49          */
50         public static List<Notification> filterNotifications(List<Notification> notifications, Sone currentSone) {
51                 ListNotification<Post> newPostNotification = getNotification(notifications, "new-post-notification", Post.class);
52                 System.out.println("Found new-post-notification with " + ((newPostNotification != null) ? newPostNotification.getElements().size() : -1) + " posts.");
53                 if (newPostNotification != null) {
54                         ListNotification<Post> filteredNotification = filterNewPostNotification(newPostNotification, currentSone);
55                         int notificationIndex = notifications.indexOf(newPostNotification);
56                         if (filteredNotification == null) {
57                                 System.out.println("Removing notification.");
58                                 notifications.remove(notificationIndex);
59                         } else {
60                                 System.out.println("Replacing Notification.");
61                                 notifications.set(notificationIndex, filteredNotification);
62                         }
63                 }
64                 ListNotification<Reply> newReplyNotification = getNotification(notifications, "new-replies-notification", Reply.class);
65                 System.out.println("Found new-reply-notification with " + ((newReplyNotification != null) ? newReplyNotification.getElements().size() : -1) + " replies.");
66                 if (newReplyNotification != null) {
67                         ListNotification<Reply> filteredNotification = filterNewReplyNotification(newReplyNotification, currentSone);
68                         int notificationIndex = notifications.indexOf(newReplyNotification);
69                         if (filteredNotification == null) {
70                                 System.out.println("Removing Notification.");
71                                 notifications.remove(notificationIndex);
72                         } else {
73                                 System.out.println("Replacing Notification.");
74                                 notifications.set(notificationIndex, filteredNotification);
75                         }
76                 }
77                 return notifications;
78         }
79
80         /**
81          * Filters the new posts of the given notification. If {@code currentSone}
82          * is {@code null}, {@code null} is returned and the notification is
83          * subsequently removed. Otherwise only posts that are posted by friend
84          * Sones of the given Sone are retained; all other posts are removed.
85          *
86          * @param newPostNotification
87          *            The new-post notification
88          * @param currentSone
89          *            The current Sone, or {@code null} if not logged in
90          * @return The filtered new-post notification, or {@code null} if the
91          *         notification should be removed
92          */
93         private static ListNotification<Post> filterNewPostNotification(ListNotification<Post> newPostNotification, Sone currentSone) {
94                 if (currentSone == null) {
95                         return null;
96                 }
97                 List<Post> newPosts = new ArrayList<Post>();
98                 for (Post post : newPostNotification.getElements()) {
99                         System.out.println("Checking Post: " + post);
100                         if (currentSone.hasFriend(post.getSone().getId()) || currentSone.equals(post.getSone())) {
101                                 System.out.println("  CS.hF: " + currentSone.hasFriend(post.getSone().getId()));
102                                 System.out.println("  CS.e:" + currentSone.equals(post.getSone()));
103                                 newPosts.add(post);
104                         }
105                 }
106                 if (newPosts.isEmpty()) {
107                         return null;
108                 }
109                 if (newPosts.size() == newPostNotification.getElements().size()) {
110                         return newPostNotification;
111                 }
112                 ListNotification<Post> filteredNotification = new ListNotification<Post>(newPostNotification);
113                 filteredNotification.setElements(newPosts);
114                 return filteredNotification;
115         }
116
117         /**
118          * Filters the new replies of the given notification. If {@code currentSone}
119          * is {@code null}, {@code null} is returned and the notification is
120          * subsequently removed. Otherwise only replies that are replies to posts
121          * that are posted by friend Sones of the given Sone are retained; all other
122          * replies are removed.
123          *
124          * @param newReplyNotification
125          *            The new-reply notification
126          * @param currentSone
127          *            The current Sone, or {@code null} if not logged in
128          * @return The filtered new-reply notification, or {@code null} if the
129          *         notification should be removed
130          */
131         private static ListNotification<Reply> filterNewReplyNotification(ListNotification<Reply> newReplyNotification, Sone currentSone) {
132                 if (currentSone == null) {
133                         return null;
134                 }
135                 List<Reply> newReplies = new ArrayList<Reply>();
136                 for (Reply reply : newReplyNotification.getElements()) {
137                         System.out.println("Checking Reply: " + reply);
138                         if (currentSone.hasFriend(reply.getPost().getSone().getId()) || currentSone.equals(reply.getPost().getSone())) {
139                                 System.out.println("  CS.hF: " + currentSone.hasFriend(reply.getPost().getSone().getId()));
140                                 System.out.println("  CS.e: " + currentSone.equals(reply.getPost().getSone()));
141                                 newReplies.add(reply);
142                         }
143                 }
144                 if (newReplies.isEmpty()) {
145                         return null;
146                 }
147                 if (newReplies.size() == newReplyNotification.getElements().size()) {
148                         return newReplyNotification;
149                 }
150                 ListNotification<Reply> filteredNotification = new ListNotification<Reply>(newReplyNotification);
151                 filteredNotification.setElements(newReplies);
152                 return filteredNotification;
153         }
154
155         /**
156          * Finds the notification with the given ID in the list of notifications and
157          * returns it.
158          *
159          * @param <T>
160          *            The type of the item in the notification
161          * @param notifications
162          *            The notification to search
163          * @param notificationId
164          *            The ID of the requested notification
165          * @param notificationElementClass
166          *            The class of the notification item
167          * @return The requested notification, or {@code null} if no notification
168          *         with the given ID could be found
169          */
170         @SuppressWarnings("unchecked")
171         private static <T> ListNotification<T> getNotification(Collection<? extends Notification> notifications, String notificationId, Class<T> notificationElementClass) {
172                 for (Notification notification : notifications) {
173                         if (!notificationId.equals(notification.getId())) {
174                                 continue;
175                         }
176                         return (ListNotification<T>) notification;
177                 }
178                 return null;
179         }
180
181 }