+++ /dev/null
-/*
- * Sone - BookmarkAjaxPage.java - Copyright © 2011–2016 David Roden
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-package net.pterodactylus.sone.web.ajax;
-
-import net.pterodactylus.sone.data.Post;
-import net.pterodactylus.sone.web.WebInterface;
-import net.pterodactylus.sone.web.page.FreenetRequest;
-
-import com.google.common.base.Optional;
-
-/**
- * AJAX page that lets the user bookmark a post.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public class BookmarkAjaxPage extends JsonPage {
-
- /**
- * Creates a new bookmark AJAX page.
- *
- * @param webInterface
- * The Sone web interface
- */
- public BookmarkAjaxPage(WebInterface webInterface) {
- super("bookmark.ajax", webInterface);
- }
-
- //
- // JSONPAGE METHODS
- //
-
- /**
- * {@inheritDoc}
- */
- @Override
- protected JsonReturnObject createJsonObject(FreenetRequest request) {
- String id = request.getHttpRequest().getParam("post", null);
- if ((id == null) || (id.length() == 0)) {
- return createErrorJsonObject("invalid-post-id");
- }
- Optional<Post> post = webInterface.getCore().getPost(id);
- if (post.isPresent()) {
- webInterface.getCore().bookmarkPost(post.get());
- }
- return createSuccessJsonObject();
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- protected boolean requiresLogin() {
- return false;
- }
-
-}
--- /dev/null
+package net.pterodactylus.sone.web.ajax
+
+import net.pterodactylus.sone.utils.also
+import net.pterodactylus.sone.utils.emptyToNull
+import net.pterodactylus.sone.utils.parameters
+import net.pterodactylus.sone.web.WebInterface
+import net.pterodactylus.sone.web.page.FreenetRequest
+
+/**
+ * AJAX page that lets the user bookmark a post.
+ */
+class BookmarkAjaxPage(webInterface: WebInterface) : JsonPage("bookmark.ajax", webInterface) {
+
+ override fun requiresLogin() = false
+
+ override fun createJsonObject(request: FreenetRequest) =
+ request.parameters["post"].emptyToNull
+ ?.let(webInterface.core::getPost)
+ ?.also(webInterface.core::bookmarkPost)
+ ?.let { createSuccessJsonObject() }
+ ?: createErrorJsonObject("invalid-post-id")
+
+}
import net.pterodactylus.sone.web.WebInterface
import net.pterodactylus.sone.web.page.FreenetRequest
import net.pterodactylus.util.notify.Notification
+import net.pterodactylus.util.web.Method.GET
import org.junit.Before
import org.mockito.ArgumentMatchers.anyInt
import org.mockito.ArgumentMatchers.anyString
@Before
fun setupFreenetRequest() {
whenever(freenetRequest.toadletContext).thenReturn(toadletContext)
+ whenever(freenetRequest.method).thenReturn(GET)
whenever(freenetRequest.httpRequest).thenReturn(httpRequest)
}
@Before
fun setupHttpRequest() {
+ whenever(httpRequest.method).thenReturn("GET")
whenever(httpRequest.getParam(anyString())).thenAnswer { requestParameters[it.getArgument(0)] ?: "" }
whenever(httpRequest.getParam(anyString(), anyString())).thenAnswer { requestParameters[it.getArgument(0)] ?: it.getArgument(1) }
whenever(httpRequest.getParam(anyString(), isNull())).thenAnswer { requestParameters[it.getArgument(0)] }