X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fnotify%2FListNotificationFilters.java;h=56a4939a7b51055d894c43e186bc094875aede46;hb=11fe0018704e60612cd47cb1de8639a52eaac435;hp=207f4cc135c73dd64a104a551c433857244a2e61;hpb=eb776984f2bf7c1f218dcc65fca80ef0298ed2f2;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/notify/ListNotificationFilters.java b/src/main/java/net/pterodactylus/sone/notify/ListNotificationFilters.java index 207f4cc..56a4939 100644 --- a/src/main/java/net/pterodactylus/sone/notify/ListNotificationFilters.java +++ b/src/main/java/net/pterodactylus/sone/notify/ListNotificationFilters.java @@ -50,28 +50,25 @@ public class ListNotificationFilters { * The current Sone, or {@code null} if not logged in * @return The filtered notifications */ - public static List filterNotifications(List notifications, Sone currentSone) { - ListNotification newPostNotification = getNotification(notifications, "new-post-notification", Post.class); - if (newPostNotification != null) { - ListNotification filteredNotification = filterNewPostNotification(newPostNotification, currentSone); - int notificationIndex = notifications.indexOf(newPostNotification); - if (filteredNotification == null) { - notifications.remove(notificationIndex); - } else { - notifications.set(notificationIndex, filteredNotification); - } - } - ListNotification newReplyNotification = getNotification(notifications, "new-replies-notification", Reply.class); - if (newReplyNotification != null) { - ListNotification filteredNotification = filterNewReplyNotification(newReplyNotification, currentSone); - int notificationIndex = notifications.indexOf(newReplyNotification); - if (filteredNotification == null) { - notifications.remove(notificationIndex); + @SuppressWarnings("unchecked") + public static List filterNotifications(Collection notifications, Sone currentSone) { + List filteredNotifications = new ArrayList(); + for (Notification notification : notifications) { + if (notification.getId().equals("new-post-notification")) { + ListNotification filteredNotification = filterNewPostNotification((ListNotification) notification, currentSone); + if (filteredNotification != null) { + filteredNotifications.add(filteredNotification); + } + } else if (notification.getId().equals("new-reply-notification")) { + ListNotification filteredNotification = filterNewReplyNotification((ListNotification) notification, currentSone); + if (filteredNotification != null) { + filteredNotifications.add(filteredNotification); + } } else { - notifications.set(notificationIndex, filteredNotification); + filteredNotifications.add(notification); } } - return notifications; + return filteredNotifications; } /** @@ -105,6 +102,7 @@ public class ListNotificationFilters { } ListNotification filteredNotification = new ListNotification(newPostNotification); filteredNotification.setElements(newPosts); + filteredNotification.setLastUpdateTime(newPostNotification.getLastUpdatedTime()); return filteredNotification; } @@ -128,7 +126,7 @@ public class ListNotificationFilters { } List newReplies = new ArrayList(); for (Reply reply : newReplyNotification.getElements()) { - if (isPostVisible(currentSone, reply.getPost())) { + if (isReplyVisible(currentSone, reply)) { newReplies.add(reply); } } @@ -140,40 +138,22 @@ public class ListNotificationFilters { } ListNotification filteredNotification = new ListNotification(newReplyNotification); filteredNotification.setElements(newReplies); + filteredNotification.setLastUpdateTime(newReplyNotification.getLastUpdatedTime()); return filteredNotification; } /** - * Finds the notification with the given ID in the list of notifications and - * returns it. - * - * @param - * The type of the item in the notification - * @param notifications - * The notification to search - * @param notificationId - * The ID of the requested notification - * @param notificationElementClass - * The class of the notification item - * @return The requested notification, or {@code null} if no notification - * with the given ID could be found - */ - @SuppressWarnings("unchecked") - private static ListNotification getNotification(Collection notifications, String notificationId, Class notificationElementClass) { - for (Notification notification : notifications) { - if (!notificationId.equals(notification.getId())) { - continue; - } - return (ListNotification) notification; - } - return null; - } - - /** * Checks whether a post is visible to the given Sone. A post is not * considered visible if one of the following statements is true: *
    *
  • The post does not have a Sone.
  • + *
  • The post’s {@link Post#getTime() time} is in the future.
  • + *
+ *

+ * If {@code post} is not {@code null} more checks are performed, and the + * post will be invisible if: + *

+ *
    *
  • The Sone of the post is not the given Sone, the given Sone does not * follow the post’s Sone, and the given Sone is not the recipient of the * post.
  • @@ -186,30 +166,78 @@ public class ListNotificationFilters { * If none of these statements is true the post is considered visible. * * @param sone - * The Sone that checks for a post’s visibility + * The Sone that checks for a post’s visibility (may be + * {@code null} to skip Sone-specific checks, such as trust) * @param post * The post to check for visibility * @return {@code true} if the post is considered visible, {@code false} * otherwise */ public static boolean isPostVisible(Sone sone, Post post) { - Validation.begin().isNotNull("Sone", sone).isNotNull("Post", post).check().isNotNull("Sone’s Identity", sone.getIdentity()).check().isInstanceOf("Sone’s Identity", sone.getIdentity(), OwnIdentity.class).check(); + Validation.begin().isNotNull("Post", post).check(); Sone postSone = post.getSone(); if (postSone == null) { return false; } - Trust trust = postSone.getIdentity().getTrust((OwnIdentity) sone.getIdentity()); - if (trust != null) { - if ((trust.getExplicit() != null) && (trust.getExplicit() < 0)) { + if (sone != null) { + Trust trust = postSone.getIdentity().getTrust((OwnIdentity) sone.getIdentity()); + if (trust != null) { + if ((trust.getExplicit() != null) && (trust.getExplicit() < 0)) { + return false; + } + if ((trust.getExplicit() == null) && (trust.getImplicit() != null) && (trust.getImplicit() < 0)) { + return false; + } + } else { return false; } - if ((trust.getExplicit() == null) && (trust.getImplicit() != null) && (trust.getImplicit() < 0)) { + if ((!postSone.equals(sone)) && !sone.hasFriend(postSone.getId()) && !sone.equals(post.getRecipient())) { return false; } - } else { + } + if (post.getTime() > System.currentTimeMillis()) { + return false; + } + return true; + } + + /** + * Checks whether a reply is visible to the given Sone. A reply is not + * considered visible if one of the following statements is true: + *
      + *
    • The reply does not have a post.
    • + *
    • The reply’s post does not have a Sone.
    • + *
    • The Sone of the reply’s post is not the given Sone, the given Sone + * does not follow the reply’s post’s Sone, and the given Sone is not the + * recipient of the reply’s post.
    • + *
    • The trust relationship between the two Sones can not be retrieved.
    • + *
    • The given Sone has explicitely assigned negative trust to the post’s + * Sone.
    • + *
    • The given Sone has not explicitely assigned negative trust to the + * reply’s post’s Sone but the implicit trust is negative.
    • + *
    • The reply’s post’s {@link Post#getTime() time} is in the future.
    • + *
    • The reply’s {@link Reply#getTime() time} is in the future.
    • + *
    + * If none of these statements is true the reply is considered visible. + * + * @param sone + * The Sone that checks for a post’s visibility (may be + * {@code null} to skip Sone-specific checks, such as trust) + * @param reply + * The reply to check for visibility + * @return {@code true} if the reply is considered visible, {@code false} + * otherwise + */ + public static boolean isReplyVisible(Sone sone, Reply reply) { + Validation.begin().isNotNull("Reply", reply).check(); + Post post = reply.getPost(); + if (post == null) { + return false; + } + if (!isPostVisible(sone, post)) { return false; } - if ((!postSone.equals(sone)) && !sone.hasFriend(postSone.getId()) && !sone.equals(post.getRecipient())) { + if (reply.getTime() > System.currentTimeMillis()) { return false; } return true;