/**
* {@inheritDoc}
*/
+ @Nullable
@Override
- public Optional<PostReply> getPostReply(String replyId) {
+ public PostReply getPostReply(String replyId) {
return database.getPostReply(replyId);
}
import java.util.concurrent.locks.ReentrantReadWriteLock;
import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
import net.pterodactylus.sone.data.Album;
import net.pterodactylus.sone.data.Image;
// POSTREPLYPROVIDER METHODS
//
- /** {@inheritDocs} */
+ @Nullable
@Override
- public Optional<PostReply> getPostReply(String id) {
+ public PostReply getPostReply(String id) {
lock.readLock().lock();
try {
- return fromNullable(allPostReplies.get(id));
+ return allPostReplies.get(id);
} finally {
lock.readLock().unlock();
}
protected PostReply getReply(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
try {
String replyId = simpleFieldSet.getString(parameterName);
- Optional<PostReply> reply = core.getPostReply(replyId);
- if (!reply.isPresent()) {
+ PostReply reply = core.getPostReply(replyId);
+ if (reply == null) {
throw new FcpException("Could not load reply from “" + replyId + "”.");
}
- return reply.get();
+ return reply;
} catch (FSParseException fspe1) {
throw new FcpException("Could not reply ID from “" + parameterName + "”.", fspe1);
}
*/
interface PostReplyProvider {
- fun getPostReply(id: String): Optional<PostReply>
+ fun getPostReply(id: String): PostReply?
/**
* Returns all replies for the given post, order ascending by time.
override fun createJsonObject(currentSone: Sone, request: FreenetRequest) =
request.parameters["reply"]
- .let(core::getPostReply)
+ ?.let(core::getPostReply)
?.let { reply ->
reply.sone.isLocal.ifTrue {
createSuccessJsonObject().also {
?.toReply()
?: createErrorJsonObject("invalid-post-id")
"reply" -> request.parameters["reply"]
- .let(core::getPostReply)
+ ?.let(core::getPostReply)
?.let(core::getLikes)
?.toReply()
?: createErrorJsonObject("invalid-reply-id")
override fun createJsonObject(currentSone: Sone, request: FreenetRequest) =
request.parameters["reply"]
- .let(core::getPostReply)
+ ?.let(core::getPostReply)
?.let { it.toJson(currentSone, request) }
?.let { replyJson ->
createSuccessJsonObject().apply {
?.let { createSuccessJsonObject() }
?: createErrorJsonObject("invalid-post-id")
"reply" -> request.parameters["reply"]
- .let(core::getPostReply)
+ ?.let(core::getPostReply)
?.let { currentSone.addLikedReplyId(it.id) }
?.also { core.touchConfiguration() }
?.let { createSuccessJsonObject() }
override fun createJsonObject(request: FreenetRequest) = when (request.parameters["type"]) {
"sone" -> processIds(request, { core.getSone(it).asOptional() }, core::markSoneKnown)
"post" -> processIds(request, core::getPost, core::markPostKnown)
- "reply" -> processIds(request, core::getPostReply, core::markReplyKnown)
+ "reply" -> processIds(request, { core.getPostReply(it).asOptional() }, core::markReplyKnown)
else -> createErrorJsonObject("invalid-type")
}
override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) {
if (freenetRequest.isPOST) {
val replyId = freenetRequest.httpRequest.getPartAsStringFailsafe("reply", 36)
- val reply = webInterface.core.getPostReply(replyId).orNull() ?: throw RedirectException("noPermission.html")
+ val reply = webInterface.core.getPostReply(replyId) ?: throw RedirectException("noPermission.html")
if (!reply.sone.isLocal) {
throw RedirectException("noPermission.html")
}
when (freenetRequest.parameters["type", 5]) {
"sone" -> ids.mapNotNull(webInterface.core::getSone).forEach(webInterface.core::markSoneKnown)
"post" -> ids.mapPresent(webInterface.core::getPost).forEach(webInterface.core::markPostKnown)
- "reply" -> ids.mapPresent(webInterface.core::getPostReply).forEach(webInterface.core::markReplyKnown)
+ "reply" -> ids.mapNotNull(webInterface.core::getPostReply).forEach(webInterface.core::markReplyKnown)
else -> throw RedirectException("invalid.html")
}
throw RedirectException(freenetRequest.parameters["returnPage", 256]!!)
when {
word.removePrefix("sone://").let(webInterface.core::getSone) != null -> redirect("viewSone.html?sone=${word.removePrefix("sone://")}")
word.removePrefix("post://").let(webInterface.core::getPost).isPresent -> redirect("viewPost.html?post=${word.removePrefix("post://")}")
- word.removePrefix("reply://").let(webInterface.core::getPostReply).isPresent -> redirect("viewPost.html?post=${word.removePrefix("reply://").let(webInterface.core::getPostReply).get().postId}")
+ word.removePrefix("reply://").let(webInterface.core::getPostReply) != null -> redirect("viewPost.html?post=${word.removePrefix("reply://").let(webInterface.core::getPostReply)?.postId}")
word.removePrefix("album://").let(webInterface.core::getAlbum) != null -> redirect("imageBrowser.html?album=${word.removePrefix("album://")}")
word.removePrefix("image://").let { webInterface.core.getImage(it, false) } != null -> redirect("imageBrowser.html?image=${word.removePrefix("image://")}")
}
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.emptyIterable;
+import static org.hamcrest.Matchers.nullValue;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
assertThat(memoryDatabase.getPost("post2").get(),
isPost(secondPost.getId(), 2000L, "post2", of(RECIPIENT_ID)));
assertThat(memoryDatabase.getPost("post3").isPresent(), is(false));
- assertThat(memoryDatabase.getPostReply("reply1").get(),
+ assertThat(memoryDatabase.getPostReply("reply1"),
isPostReply("reply1", "post1", 3000L, "reply1"));
- assertThat(memoryDatabase.getPostReply("reply2").get(),
+ assertThat(memoryDatabase.getPostReply("reply2"),
isPostReply("reply2", "post2", 4000L, "reply2"));
- assertThat(memoryDatabase.getPostReply("reply3").get(),
+ assertThat(memoryDatabase.getPostReply("reply3"),
isPostReply("reply3", "post1", 5000L, "reply3"));
- assertThat(memoryDatabase.getPostReply("reply4").isPresent(),
- is(false));
+ assertThat(memoryDatabase.getPostReply("reply4"), nullValue());
assertThat(memoryDatabase.getAlbum("album1").get(),
isAlbum("album1", null, "album1", "album-description1"));
assertThat(memoryDatabase.getAlbum("album2").get(),
@Test
fun `request with remote post reply parameter results in error response`() {
parameters += "Reply" to "RemoteReplyId"
- whenever(core.getPostReply("RemoteReplyId")).thenReturn(of(remotePostReply))
+ whenever(core.getPostReply("RemoteReplyId")).thenReturn(remotePostReply)
val response = command.execute(parameters)
assertThat(response.replyParameters["Message"], equalTo("Error"))
assertThat(response.replyParameters["ErrorCode"], equalTo("401"))
@Test
fun `request with local post reply parameter deletes reply`() {
parameters += "Reply" to "RemoteReplyId"
- whenever(core.getPostReply("RemoteReplyId")).thenReturn(of(localPostReply))
+ whenever(core.getPostReply("RemoteReplyId")).thenReturn(localPostReply)
val response = command.execute(parameters)
assertThat(response.replyParameters["Message"], equalTo("ReplyDeleted"))
verify(core).deleteReply(localPostReply)
@Before
fun setupRepliesAndSones() {
- whenever(core.getPostReply("ReplyId")).thenReturn(reply.asOptional())
+ whenever(core.getPostReply("ReplyId")).thenReturn(reply)
whenever(core.getSone("RemoteSoneId")).thenReturn(remoteSone)
whenever(core.getSone("LocalSoneId")).thenReturn(localSone)
}
fun setupCore() {
whenever(core.getSone(anyString())).thenReturn(null)
whenever(core.getPost(anyString())).thenReturn(absent())
- whenever(core.getPostReply(anyString())).thenReturn(absent())
+ whenever(core.getPostReply(anyString())).thenReturn(null)
}
protected fun createSone(id: String, name: String, firstName: String, lastName: String, time: Long) = mock<Sone>().apply {
whenever(core.getPost(ArgumentMatchers.anyString())).thenAnswer { (posts + newPosts)[it[0]].asOptional() }
whenever(core.getLikes(ArgumentMatchers.any<Post>())).then { postLikes[it[0]] ?: emptySet<Sone>() }
whenever(core.getLikes(ArgumentMatchers.any<PostReply>())).then { replyLikes[it[0]] ?: emptySet<Sone>() }
- whenever(core.getPostReply(ArgumentMatchers.anyString())).then { replies[it[0]].asOptional() }
+ whenever(core.getPostReply(ArgumentMatchers.anyString())).then { replies[it[0]] }
whenever(core.getAlbum(ArgumentMatchers.anyString())).then { albums[it[0]] }
whenever(core.getImage(ArgumentMatchers.anyString())).then { images[it[0]] }
whenever(core.getImage(ArgumentMatchers.anyString(), ArgumentMatchers.anyBoolean())).then { images[it[0]] }
whenever(core.localSones).then { localSones.values }
whenever(core.getLocalSone(anyString())).then { localSones[it[0]] }
whenever(core.getPost(anyString())).then { allPosts[it[0]].asOptional() }
- whenever(core.getPostReply(anyString())).then { allPostReplies[it[0]].asOptional() }
+ whenever(core.getPostReply(anyString())).then { allPostReplies[it[0]] }
whenever(core.getReplies(anyString())).then { perPostReplies[it[0]].asList() }
whenever(core.getAlbum(anyString())).then { allAlbums[it[0]] }
whenever(core.getImage(anyString())).then { allImages[it[0]]}