- /**
- * Filters new-post and new-reply notifications in the given list of
- * notifications. If `currentSone` is `null`, new-post and
- * new-reply notifications are removed completely. If `currentSone` is
- * not `null`, only posts that are posted by a friend Sone or the Sone
- * itself, and replies that are replies to posts of friend Sones or the Sone
- * itself will be retained in the notifications.
- *
- * @param notifications
- * The notifications to filter
- * @param currentSone
- * The current Sone, or `null` if not logged in
- * @return The filtered notifications
- */
- fun filterNotifications(notifications: Collection<Notification>, currentSone: Sone?) =
- notifications.mapNotNull { it.filtered(currentSone) }
-
- @Suppress("UNCHECKED_CAST")
- private fun Notification.filtered(currentSone: Sone?): Notification? = when {
- isNewSoneNotification -> {
- takeIf { currentSone == null || currentSone.options.isShowNewSoneNotifications }
- }
- isNewPostNotification -> {
- (currentSone != null && currentSone.options.isShowNewPostNotifications).ifTrue {
- (this as ListNotification<Post>).filterNotification { postVisibilityFilter.isPostVisible(currentSone, it) }
- }
- }
- isNewReplyNotification -> {
- (currentSone != null && currentSone.options.isShowNewReplyNotifications).ifTrue {
- (this as ListNotification<PostReply>).filterNotification { replyVisibilityFilter.isReplyVisible(currentSone, it) }
- }
- }
- isMentionNotification -> {
- (this as ListNotification<Post>).filterNotification { postVisibilityFilter.isPostVisible(currentSone, it) }
- }
- else -> this
- }