57815084303ef12841b07ee4e241b6e204efa7a5
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / data / Post.kt
1 package net.pterodactylus.sone.data
2
3 import net.pterodactylus.sone.database.PostBuilder
4 import java.util.Comparator.comparing
5
6 /**
7  * Predicate that returns whether a post is _not_ from the future,
8  * i.e. whether it should be visible now.
9  */
10 @get:JvmName("noFuturePost")
11 val noFuturePost: (Post) -> Boolean = { it.time <= System.currentTimeMillis() }
12
13 /**
14  * Comparator that orders posts by their time, newest posts first.
15  */
16 @get:JvmName("newestPostFirst")
17 val newestPostFirst: Comparator<Post> = comparing(Post::getTime).reversed()
18
19 data class PostShell(val id: String, val soneId: String, val recipientId: String?, val time: Long, val text: String) {
20
21         fun build(postBuilder: PostBuilder) =
22                         postBuilder.withId(id).from(soneId).let { if (recipientId != null) it.to(recipientId) else it }.withTime(time).withText(text).build()
23
24 }
25
26 fun Post.toShell() = PostShell(id, sone!!.id, recipient.orNull()?.id, time, text)