Paginate things a bit differently
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / pages / ImageBrowserPage.kt
1 package net.pterodactylus.sone.web.pages
2
3 import net.pterodactylus.sone.data.Album
4 import net.pterodactylus.sone.data.Sone
5 import net.pterodactylus.sone.utils.paginate
6 import net.pterodactylus.sone.utils.parameters
7 import net.pterodactylus.sone.web.WebInterface
8 import net.pterodactylus.sone.web.page.FreenetRequest
9 import net.pterodactylus.util.template.Template
10 import net.pterodactylus.util.template.TemplateContext
11 import java.net.URI
12
13 /**
14  * The image browser page is the entry page for the image management.
15  */
16 class ImageBrowserPage(template: Template, webInterface: WebInterface):
17                 LoggedInPage("imageBrowser.html", template, "Page.ImageBrowser.Title", webInterface) {
18
19         override fun handleRequest(freenetRequest: FreenetRequest, currentSone: Sone, templateContext: TemplateContext) {
20                 if ("album" in freenetRequest.parameters) {
21                         templateContext["albumRequested"] = true
22                         templateContext["album"] = webInterface.core.getAlbum(freenetRequest.parameters["album"]!!)
23                         templateContext["page"] = freenetRequest.parameters["page"]
24                 } else if ("image" in freenetRequest.parameters) {
25                         templateContext["imageRequested"] = true
26                         templateContext["image"] = webInterface.core.getImage(freenetRequest.parameters["image"])
27                 } else if (freenetRequest.parameters["mode"] == "gallery") {
28                         templateContext["galleryRequested"] = true
29                         webInterface.core.sones
30                                         .map(Sone::getRootAlbum)
31                                         .flatMap(Album::getAlbums)
32                                         .flatMap { Album.FLATTENER.apply(it)!! }
33                                         .filterNot(Album::isEmpty)
34                                         .sortedBy(Album::getTitle)
35                                         .also { albums ->
36                                                 albums.paginate(webInterface.core.preferences.imagesPerPage)
37                                                                 .turnTo(freenetRequest.parameters["page"]?.toIntOrNull() ?: 0)
38                                                                 .also { pagination ->
39                                                         templateContext["albumPagination"] = pagination
40                                                         templateContext["albums"] = pagination.items
41                                                 }
42                                         }
43                 } else {
44                         templateContext["soneRequested"] = true
45                         templateContext["sone"] = webInterface.core.getSone(freenetRequest.httpRequest.getParam("sone")) ?: currentSone
46                 }
47         }
48
49         override fun isLinkExcepted(link: URI?) = true
50
51 }