🎨 Replace post accessor with Kotlin version
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / template / PostAccessor.kt
1 /*
2  * Sone - PostAccessor.java - Copyright Â© 2010–2019 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 package net.pterodactylus.sone.template
18
19 import net.pterodactylus.sone.core.*
20 import net.pterodactylus.sone.data.*
21 import net.pterodactylus.util.template.*
22
23 /**
24  * Accessor for [Post] objects that adds additional properties:
25  *
26  * * `replies`: All replies to this post, sorted by time, oldest first
27  * * `likes`: All Sones that have liked the post
28  * * `liked`: `true` if the current Sone from the [template context][TemplateContext] has liked the post
29  * * `new`: `true` if the post is not known
30  * * `bookmarked`: `true` if the post is bookmarked
31  */
32 class PostAccessor(private val core: Core) : ReflectionAccessor() {
33
34         override fun get(templateContext: TemplateContext?, `object`: Any?, member: String): Any? =
35                         (`object` as Post).let { post ->
36                                 when (member) {
37                                         "replies" -> core.getReplies(post.id).filter { Reply.FUTURE_REPLY_FILTER.apply(it) }
38                                         "likes" -> core.getLikes(post)
39                                         "liked" -> (templateContext?.get("currentSone") as? Sone)?.isLikedPostId(post.id) ?: false
40                                         "new" -> !post.isKnown
41                                         "bookmarked" -> core.isBookmarked(post)
42                                         else -> super.get(templateContext, `object`, member)
43                                 }
44                         }
45
46 }