+ @SuppressWarnings("unchecked")
+ public static List<Notification> filterNotifications(Collection<? extends Notification> notifications, Sone currentSone) {
+ List<Notification> filteredNotifications = new ArrayList<Notification>();
+ for (Notification notification : notifications) {
+ if (notification.getId().equals("new-sone-notification")) {
+ if ((currentSone != null) && !currentSone.getOptions().isShowNewSoneNotifications()) {
+ continue;
+ }
+ filteredNotifications.add(notification);
+ } else if (notification.getId().equals("new-post-notification")) {
+ if ((currentSone != null) && !currentSone.getOptions().isShowNewPostNotifications()) {
+ continue;
+ }
+ Optional<ListNotification<Post>> filteredNotification = filterNewPostNotification((ListNotification<Post>) notification, currentSone, true);
+ if (filteredNotification.isPresent()) {
+ filteredNotifications.add(filteredNotification.get());
+ }
+ } else if (notification.getId().equals("new-reply-notification")) {
+ if ((currentSone != null) && !currentSone.getOptions().isShowNewReplyNotifications()) {
+ continue;
+ }
+ Optional<ListNotification<PostReply>> filteredNotification = filterNewReplyNotification((ListNotification<PostReply>) notification, currentSone);
+ if (filteredNotification.isPresent()) {
+ filteredNotifications.add(filteredNotification.get());
+ }
+ } else if (notification.getId().equals("mention-notification")) {
+ Optional<ListNotification<Post>> filteredNotification = filterNewPostNotification((ListNotification<Post>) notification, null, false);
+ if (filteredNotification.isPresent()) {
+ filteredNotifications.add(filteredNotification.get());
+ }
+ } else {
+ filteredNotifications.add(notification);
+ }
+ }
+ return filteredNotifications;
+ }
+
+ /**
+ * Filters the new posts of the given notification. If {@code currentSone}
+ * is {@code null} and {@code soneRequired} is {@code true}, {@code null} is
+ * returned and the notification is subsequently removed. Otherwise only
+ * posts that are posted by friend Sones of the given Sone are retained; all
+ * other posts are removed.
+ *
+ * @param newPostNotification
+ * The new-post notification
+ * @param currentSone
+ * The current Sone, or {@code null} if not logged in
+ * @param soneRequired
+ * Whether a non-{@code null} Sone in {@code currentSone} is
+ * required
+ * @return The filtered new-post notification, or {@code null} if the
+ * notification should be removed
+ */
+ private static Optional<ListNotification<Post>> filterNewPostNotification(ListNotification<Post> newPostNotification, Sone currentSone, boolean soneRequired) {
+ if (soneRequired && (currentSone == null)) {
+ return Optional.absent();
+ }
+ List<Post> newPosts = new ArrayList<Post>();
+ for (Post post : newPostNotification.getElements()) {
+ if (isPostVisible(currentSone, post)) {
+ newPosts.add(post);
+ }
+ }
+ if (newPosts.isEmpty()) {
+ return Optional.absent();
+ }
+ if (newPosts.size() == newPostNotification.getElements().size()) {
+ return Optional.of(newPostNotification);
+ }
+ ListNotification<Post> filteredNotification = new ListNotification<Post>(newPostNotification);
+ filteredNotification.setElements(newPosts);
+ filteredNotification.setLastUpdateTime(newPostNotification.getLastUpdatedTime());
+ return Optional.of(filteredNotification);
+ }
+
+ /**
+ * Filters the new replies of the given notification. If {@code currentSone}
+ * is {@code null}, {@code null} is returned and the notification is
+ * subsequently removed. Otherwise only replies that are replies to posts
+ * that are posted by friend Sones of the given Sone are retained; all other
+ * replies are removed.
+ *
+ * @param newReplyNotification
+ * The new-reply notification
+ * @param currentSone
+ * The current Sone, or {@code null} if not logged in
+ * @return The filtered new-reply notification, or {@code null} if the
+ * notification should be removed
+ */
+ private static Optional<ListNotification<PostReply>> filterNewReplyNotification(ListNotification<PostReply> newReplyNotification, Sone currentSone) {