1 package net.pterodactylus.sone.web.pages
3 import com.google.common.base.Ticker
4 import com.google.common.cache.Cache
5 import com.google.common.cache.CacheBuilder
6 import net.pterodactylus.sone.data.Post
7 import net.pterodactylus.sone.data.PostReply
8 import net.pterodactylus.sone.data.Sone
9 import net.pterodactylus.sone.utils.Pagination
10 import net.pterodactylus.sone.utils.emptyToNull
11 import net.pterodactylus.sone.utils.paginate
12 import net.pterodactylus.sone.utils.parameters
13 import net.pterodactylus.sone.web.WebInterface
14 import net.pterodactylus.sone.web.page.FreenetRequest
15 import net.pterodactylus.sone.web.pages.SearchPage.Optionality.FORBIDDEN
16 import net.pterodactylus.sone.web.pages.SearchPage.Optionality.OPTIONAL
17 import net.pterodactylus.sone.web.pages.SearchPage.Optionality.REQUIRED
18 import net.pterodactylus.util.template.Template
19 import net.pterodactylus.util.template.TemplateContext
20 import net.pterodactylus.util.text.StringEscaper
21 import net.pterodactylus.util.text.TextException
22 import java.util.concurrent.TimeUnit.MINUTES
25 * This page lets the user search for posts and replies that contain certain
28 class SearchPage @JvmOverloads constructor(template: Template, webInterface: WebInterface, ticker: Ticker = Ticker.systemTicker()):
29 SoneTemplatePage("search.html", template, "Page.Search.Title", webInterface, false) {
31 private val cache: Cache<Iterable<Phrase>, Pagination<Post>> = CacheBuilder.newBuilder().ticker(ticker).expireAfterAccess(5, MINUTES).build()
33 override fun handleRequest(request: FreenetRequest, templateContext: TemplateContext) {
35 request.parameters["query"].emptyToNull?.parse()
36 } catch (te: TextException) {
37 redirect("index.html")
39 ?: redirect("index.html")
42 0 -> redirect("index.html")
43 1 -> phrases.first().phrase.also { word ->
45 word.removePrefix("sone://").let(webInterface.core::getSone).isPresent -> redirect("viewSone.html?sone=${word.removePrefix("sone://")}")
46 word.removePrefix("post://").let(webInterface.core::getPost).isPresent -> redirect("viewPost.html?post=${word.removePrefix("post://")}")
47 word.removePrefix("reply://").let(webInterface.core::getPostReply).isPresent -> redirect("viewPost.html?post=${word.removePrefix("reply://").let(webInterface.core::getPostReply).get().postId}")
48 word.removePrefix("album://").let(webInterface.core::getAlbum) != null -> redirect("imageBrowser.html?album=${word.removePrefix("album://")}")
49 word.removePrefix("image://").let { webInterface.core.getImage(it, false) } != null -> redirect("imageBrowser.html?image=${word.removePrefix("image://")}")
54 val sonePagination = webInterface.core.sones
55 .scoreAndPaginate(phrases) { it.allText() }
56 .apply { page = request.parameters["sonePage"].emptyToNull?.toIntOrNull() ?: 0 }
57 val postPagination = cache.get(phrases) {
58 webInterface.core.sones
59 .flatMap(Sone::getPosts)
60 .filter { Post.FUTURE_POSTS_FILTER.apply(it) }
61 .scoreAndPaginate(phrases) { it.allText() }
62 }.apply { page = request.parameters["postPage"].emptyToNull?.toIntOrNull() ?: 0 }
64 templateContext["sonePagination"] = sonePagination
65 templateContext["soneHits"] = sonePagination.items
66 templateContext["postPagination"] = postPagination
67 templateContext["postHits"] = postPagination.items
70 private fun <T> Iterable<T>.scoreAndPaginate(phrases: Iterable<Phrase>, texter: (T) -> String) =
71 map { it to score(texter(it), phrases) }
72 .filter { it.second > 0 }
73 .sortedByDescending { it.second }
75 .paginate(webInterface.core.preferences.postsPerPage)
77 private fun Sone.names() =
78 listOf(name, profile.firstName, profile.middleName, profile.lastName)
82 private fun Sone.allText() =
83 (names() + profile.fields.map { "${it.name} ${it.value}" }.joinToString(" ", " ")).toLowerCase()
85 private fun Post.allText() =
86 (text + recipient.orNull()?.let { " ${it.names()}" } + webInterface.core.getReplies(id)
87 .filter { PostReply.FUTURE_REPLY_FILTER.apply(it) }
88 .map { "${it.sone.names()} ${it.text}" }.joinToString(" ", " ")).toLowerCase()
90 private fun score(text: String, phrases: Iterable<Phrase>): Double {
91 val requiredPhrases = phrases.count { it.required }
92 val requiredHits = phrases.filter(Phrase::required)
94 .flatMap { text.findAll(it) }
95 .map { Math.pow(1 - it / text.length.toDouble(), 2.0) }
97 val optionalHits = phrases.filter(Phrase::optional)
99 .flatMap { text.findAll(it) }
100 .map { Math.pow(1 - it / text.length.toDouble(), 2.0) }
102 val forbiddenHits = phrases.filter(Phrase::forbidden)
104 .map { text.findAll(it).size }
106 return requiredHits * 3 + optionalHits + (requiredHits - requiredPhrases) * 5 - (forbiddenHits * 2)
109 private fun String.findAll(needle: String): List<Int> {
110 var nextIndex = indexOf(needle)
111 val positions = mutableListOf<Int>()
112 while (nextIndex != -1) {
113 positions += nextIndex
114 nextIndex = indexOf(needle, nextIndex + 1)
119 private fun String.parse() =
120 StringEscaper.parseLine(this)
121 .map(String::toLowerCase)
124 it == "+" || it == "-" -> Phrase(it, OPTIONAL)
125 it.startsWith("+") -> Phrase(it.drop(1), REQUIRED)
126 it.startsWith("-") -> Phrase(it.drop(1), FORBIDDEN)
127 else -> Phrase(it, OPTIONAL)
131 private fun redirect(target: String): Nothing = throw RedirectException(target)
133 enum class Optionality {
139 private data class Phrase(val phrase: String, val optionality: Optionality) {
140 val required = optionality == REQUIRED
141 val forbidden = optionality == FORBIDDEN
142 val optional = optionality == OPTIONAL