🚧 Add handler for image-insert notifications
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / ajax / EditAlbumAjaxPage.kt
1 package net.pterodactylus.sone.web.ajax
2
3 import net.pterodactylus.sone.text.TextFilter
4 import net.pterodactylus.sone.utils.headers
5 import net.pterodactylus.sone.utils.ifTrue
6 import net.pterodactylus.sone.utils.parameters
7 import net.pterodactylus.sone.web.WebInterface
8 import net.pterodactylus.sone.web.page.*
9 import javax.inject.Inject
10
11 /**
12  * Page that stores a user’s album modifications.
13  */
14 @ToadletPath("editAlbum.ajax")
15 class EditAlbumAjaxPage @Inject constructor(webInterface: WebInterface) : JsonPage(webInterface) {
16
17         override fun createJsonObject(request: FreenetRequest) =
18                         request.parameters["album"]!!
19                                         .let(core::getAlbum)
20                                         ?.let { album ->
21                                                 album.sone.isLocal.ifTrue {
22                                                         when {
23                                                                 request.parameters["moveLeft"] == "true" -> createSuccessJsonObject().apply {
24                                                                         put("sourceAlbumId", album.id)
25                                                                         put("destinationAlbumId", album.parent.moveAlbumUp(album).id)
26                                                                 }
27                                                                 request.parameters["moveRight"] == "true" -> createSuccessJsonObject().apply {
28                                                                         put("sourceAlbumId", album.id)
29                                                                         put("destinationAlbumId", album.parent.moveAlbumDown(album).id)
30                                                                 }
31                                                                 else -> try {
32                                                                         album.modify()
33                                                                                         .setTitle(request.parameters["title"])
34                                                                                         .setDescription(TextFilter.filter(request.headers["Host"], request.parameters["description"]))
35                                                                                         .update()
36                                                                         createSuccessJsonObject()
37                                                                                         .put("albumId", album.id)
38                                                                                         .put("title", album.title)
39                                                                                         .put("description", album.description)
40                                                                 } catch (e: IllegalStateException) {
41                                                                         createErrorJsonObject("invalid-album-title")
42                                                                 }
43                                                         }
44                                                 } ?: createErrorJsonObject("not-authorized")
45                                         } ?: createErrorJsonObject("invalid-album-id")
46
47 }