🎨 Add container for new posts/replies
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / NewElements.kt
1 /**
2  * Sone - NewElements.kt - Copyright Â© 2020 David â€˜Bombe’ 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 package net.pterodactylus.sone.web
18
19 import net.pterodactylus.sone.data.Post
20 import net.pterodactylus.sone.data.PostReply
21 import net.pterodactylus.sone.notify.ListNotification
22 import net.pterodactylus.sone.notify.PostVisibilityFilter
23 import net.pterodactylus.sone.notify.ReplyVisibilityFilter
24 import javax.inject.Inject
25 import javax.inject.Named
26
27 /**
28  * Container for new elements that should be shown in the web interface.
29  *
30  * This is just a wrapper around the notifications that store the new elements.
31  */
32 class NewElements @Inject constructor(
33                 @Named("newRemotePost") private val newPostNotification: ListNotification<Post>,
34                 @Named("newRemotePostReply") private val newReplyNotification: ListNotification<PostReply>,
35                 @Named("localPost") private val localPostNotification: ListNotification<Post>,
36                 @Named("localReply") private val localReplyNotification: ListNotification<PostReply>,
37                 private val postVisibilityFilter: PostVisibilityFilter,
38                 private val replyVisibilityFilter: ReplyVisibilityFilter
39 ) {
40
41         val newPosts: Collection<Post>
42                 get() = listOf(newPostNotification, localPostNotification)
43                                 .flatMap(ListNotification<Post>::elements)
44                                 .filter { postVisibilityFilter.isPostVisible(null, it) }
45
46         val newReplies: Collection<PostReply>
47                 get() = listOf(newReplyNotification, localReplyNotification)
48                                 .flatMap(ListNotification<PostReply>::elements)
49                                 .filter { replyVisibilityFilter.isReplyVisible(null, it) }
50 }
51
52 private fun <R> Collection<*>.cast(): List<R> = map { it as R }