return database.newPostBuilder();
}
- /**
- * {@inheritDoc}
- */
+ @Nullable
@Override
- public Optional<Post> getPost(String postId) {
+ public Post getPost(@Nonnull String postId) {
return database.getPost(postId);
}
package net.pterodactylus.sone.data.impl;
+import static com.google.common.base.Optional.fromNullable;
+
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.PostReply;
import net.pterodactylus.sone.database.PostProvider;
*/
@Override
public Optional<Post> getPost() {
- return postProvider.getPost(postId);
+ return fromNullable(postProvider.getPost(postId));
}
}
package net.pterodactylus.sone.database.memory;
+import static com.google.common.base.Optional.fromNullable;
import static com.google.common.collect.FluentIterable.from;
import java.util.HashSet;
new Function<String, Post>() {
@Override
public Post apply(String postId) {
- return memoryDatabase.getPost(postId)
+ return fromNullable(memoryDatabase.getPost(postId))
.or(new EmptyPost(postId));
}
}).toSet();
import net.pterodactylus.util.config.Configuration;
import net.pterodactylus.util.config.ConfigurationException;
-import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
// POSTPROVIDER METHODS
//
- /** {@inheritDocs} */
+ @Nullable
@Override
- public Optional<Post> getPost(String postId) {
+ public Post getPost(@Nonnull String postId) {
lock.readLock().lock();
try {
- return fromNullable(allPosts.get(postId));
+ return allPosts.get(postId);
} finally {
lock.readLock().unlock();
}
package net.pterodactylus.sone.database.memory;
+import static com.google.common.base.Optional.fromNullable;
+
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.PostReply;
import net.pterodactylus.sone.data.Sone;
*/
@Override
public Optional<Post> getPost() {
- return database.getPost(postId);
+ return fromNullable(database.getPost(postId));
}
//
protected Post getPost(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
try {
String postId = simpleFieldSet.getString(parameterName);
- Optional<Post> post = core.getPost(postId);
- if (!post.isPresent()) {
+ Post post = core.getPost(postId);
+ if (post == null) {
throw new FcpException("Could not load post from “" + postId + "”.");
}
- return post.get();
+ return post;
} catch (FSParseException fspe1) {
throw new FcpException("Could not post ID from “" + parameterName + "”.", fspe1);
}
@ImplementedBy(MemoryDatabase::class)
interface PostProvider {
- fun getPost(postId: String): Optional<Post>
+ fun getPost(postId: String): Post?
fun getPosts(soneId: String): Collection<Post>
fun getDirectedPosts(recipientId: String): Collection<Post>
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
override fun createJsonObject(request: FreenetRequest) =
request.parameters["post"].emptyToNull
- ?.let(core::getPost)
- ?.also(core::bookmarkPost)
- ?.let { createSuccessJsonObject() }
+ ?.let { postId ->
+ core.getPost(postId)?.also(core::bookmarkPost)
+ createSuccessJsonObject()
+ }
?: createErrorJsonObject("invalid-post-id")
}
import net.pterodactylus.sone.freenet.L10nFilter
import net.pterodactylus.sone.text.TimeTextConverter
import net.pterodactylus.sone.utils.jsonObject
-import net.pterodactylus.sone.utils.let
import net.pterodactylus.sone.utils.parameters
import net.pterodactylus.sone.web.WebInterface
import net.pterodactylus.sone.web.page.FreenetRequest
override fun createJsonObject(request: FreenetRequest) =
createSuccessJsonObject().apply {
- put("postTimes", request.parameters["posts"]!!.idsToJson { core.getPost(it).let { it.id to it.time } })
+ put("postTimes", request.parameters["posts"]!!.idsToJson { core.getPost(it)?.let { it.id to it.time } })
put("replyTimes", request.parameters["replies"]!!.idsToJson { core.getPostReply(it)?.let { it.id to it.time } })
}
package net.pterodactylus.sone.web.ajax
-import com.google.common.base.Optional
-import net.pterodactylus.sone.utils.asOptional
-import net.pterodactylus.sone.utils.mapPresent
import net.pterodactylus.sone.utils.parameters
import net.pterodactylus.sone.web.WebInterface
import net.pterodactylus.sone.web.page.FreenetRequest
override val requiresLogin = false
override fun createJsonObject(request: FreenetRequest) = when (request.parameters["type"]) {
- "sone" -> processIds(request, { core.getSone(it).asOptional() }, core::markSoneKnown)
+ "sone" -> processIds(request, core::getSone, core::markSoneKnown)
"post" -> processIds(request, core::getPost, core::markPostKnown)
- "reply" -> processIds(request, { core.getPostReply(it).asOptional() }, core::markReplyKnown)
+ "reply" -> processIds(request, core::getPostReply, core::markReplyKnown)
else -> createErrorJsonObject("invalid-type")
}
- private fun <T> processIds(request: FreenetRequest, getter: (String) -> Optional<T>, marker: (T) -> Unit) =
+ private fun <T : Any> processIds(request: FreenetRequest, getter: (String) -> T?, marker: (T) -> Unit) =
request.parameters["id"]
?.split(Regex(" +"))
- ?.mapPresent(getter)
+ ?.mapNotNull(getter)
?.onEach(marker)
.let { createSuccessJsonObject() }
if (freenetRequest.isPOST) {
val returnPage = freenetRequest.httpRequest.getPartAsStringFailsafe("returnPage", 256)
val postId = freenetRequest.httpRequest.getPartAsStringFailsafe("post", 36)
- webInterface.core.getPost(postId).orNull()?.let {
+ webInterface.core.getPost(postId)?.let {
webInterface.core.bookmarkPost(it)
}
throw RedirectException(returnPage)
templateContext["errorTextEmpty"] = true
return
}
- val post = webInterface.core.getPost(postId).orNull() ?: throw RedirectException("noPermission.html")
+ val post = webInterface.core.getPost(postId) ?: throw RedirectException("noPermission.html")
val sender = webInterface.core.getLocalSone(freenetRequest.httpRequest.getPartAsStringFailsafe("sender", 43)) ?: getCurrentSone(freenetRequest.toadletContext)
webInterface.core.createReply(sender, post, TextFilter.filter(freenetRequest.httpRequest.getHeader("Host"), text))
throw RedirectException(returnPage)
override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) {
if (freenetRequest.isPOST) {
- val post = webInterface.core.getPost(freenetRequest.httpRequest.getPartAsStringFailsafe("post", 36)).orNull() ?: throw RedirectException("noPermission.html")
+ val post = webInterface.core.getPost(freenetRequest.httpRequest.getPartAsStringFailsafe("post", 36)) ?: throw RedirectException("noPermission.html")
val returnPage = freenetRequest.httpRequest.getPartAsStringFailsafe("returnPage", 256)
if (!post.sone.isLocal) {
throw RedirectException("noPermission.html")
templateContext["returnPage"] = returnPage
return
}
- templateContext["post"] = webInterface.core.getPost(freenetRequest.httpRequest.getParam("post")).orNull() ?: throw RedirectException("noPermission.html")
+ templateContext["post"] = webInterface.core.getPost(freenetRequest.httpRequest.getParam("post")) ?: throw RedirectException("noPermission.html")
templateContext["returnPage"] = freenetRequest.httpRequest.getParam("returnPage")
}
val ids = freenetRequest.parameters["id", 65536]!!.split(" ")
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)
+ "post" -> ids.mapNotNull(webInterface.core::getPost).forEach(webInterface.core::markPostKnown)
"reply" -> ids.mapNotNull(webInterface.core::getPostReply).forEach(webInterface.core::markReplyKnown)
else -> throw RedirectException("invalid.html")
}
1 -> phrases.first().phrase.also { word ->
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("post://").let(webInterface.core::getPost) != null -> redirect("viewPost.html?post=${word.removePrefix("post://")}")
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://")}")
SoneTemplatePage("viewPost.html", template, "Page.ViewPost.Title", webInterface, false) {
override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) {
- templateContext["post"] = freenetRequest.parameters["post"]?.let(webInterface.core::getPost)?.orNull()
+ templateContext["post"] = freenetRequest.parameters["post"]?.let(webInterface.core::getPost)
templateContext["raw"] = freenetRequest.parameters["raw"] == "true"
}
package net.pterodactylus.sone.database.memory;
-import static com.google.common.base.Optional.fromNullable;
import static net.pterodactylus.sone.test.Matchers.isPostWithId;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import net.pterodactylus.sone.data.Post;
-import com.google.common.base.Optional;
import org.junit.Before;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
@Before
public void setupMemoryDatabase() {
when(memoryDatabase.getPost(anyString())).thenAnswer(
- new Answer<Optional<Post>>() {
+ new Answer<Post>() {
@Override
- public Optional<Post> answer(
+ public Post answer(
InvocationOnMock invocation) {
- return fromNullable(
- posts.get(invocation.getArguments()[0]));
+ return posts.get(invocation.getArguments()[0]);
}
});
}
firstAlbum.addImage(thirdImage);
secondAlbum.addImage(secondImage);
memoryDatabase.storeSone(sone);
- assertThat(memoryDatabase.getPost("post1").get(),
+ assertThat(memoryDatabase.getPost("post1"),
isPost(firstPost.getId(), 1000L, "post1",
Optional.<String>absent()));
- assertThat(memoryDatabase.getPost("post2").get(),
+ assertThat(memoryDatabase.getPost("post2"),
isPost(secondPost.getId(), 2000L, "post2", of(RECIPIENT_ID)));
- assertThat(memoryDatabase.getPost("post3").isPresent(), is(false));
+ assertThat(memoryDatabase.getPost("post3"), nullValue());
assertThat(memoryDatabase.getPostReply("reply1"),
isPostReply("reply1", "post1", 3000L, "reply1"));
assertThat(memoryDatabase.getPostReply("reply2"),
private static class TestPostProvider implements PostProvider {
+ @Nullable
@Override
- public Optional<Post> getPost(final String postId) {
- return Optional.<Post>of(new Post() {
+ public Post getPost(@Nonnull final String postId) {
+ return new Post() {
@Override
public String getId() {
return postId;
public Post setKnown(boolean known) {
return null;
}
- });
+ };
}
@Override
private static class AbsentPostProvider extends TestPostProvider {
+ @Nullable
@Override
- public Optional<Post> getPost(String postId) {
- return Optional.absent();
+ public Post getPost(@Nonnull String postId) {
+ return null;
}
}
private fun addValidPostParameter() {
parameters += "Post" to "ValidPostId"
- whenever(core.getPost("ValidPostId")).thenReturn(of(post))
+ whenever(core.getPost("ValidPostId")).thenReturn(post)
}
@Test
@Test
fun `request with post from remote sone returns error response`() {
parameters += "Post" to "RemotePostId"
- whenever(core.getPost("RemotePostId")).thenReturn(of(postFromRemoteSone))
+ whenever(core.getPost("RemotePostId")).thenReturn(postFromRemoteSone)
val response = command.execute(parameters)
assertThat(response.replyParameters["Message"], equalTo("Error"))
assertThat(response.replyParameters["ErrorCode"], equalTo("401"))
@Test
fun `request with post from local sone deletes posts`() {
parameters += "Post" to "LocalPostId"
- whenever(core.getPost("LocalPostId")).thenReturn(of(postFromLocalSone))
+ whenever(core.getPost("LocalPostId")).thenReturn(postFromLocalSone)
val response = command.execute(parameters)
assertThat(response.replyParameters["Message"], equalTo("PostDeleted"))
verify(core).deletePost(postFromLocalSone)
@Before
fun setupPostWithLikesAndReplies() {
- whenever(core.getPost("ValidPostId")).thenReturn(post.asOptional())
+ whenever(core.getPost("ValidPostId")).thenReturn(post)
whenever(core.getLikes(post)).thenReturn(setOf(sone1, sone2))
val replies = listOf(postReply1, postReply2)
whenever(core.getReplies("ValidPostId")).thenReturn(replies)
@Before
fun setupPostAndSones() {
- whenever(core.getPost("PostId")).thenReturn(post.asOptional())
+ whenever(core.getPost("PostId")).thenReturn(post)
whenever(core.getSone("RemoteSoneId")).thenReturn(remoteSone)
whenever(core.getSone("LocalSoneId")).thenReturn(localSone)
}
@Before
fun setupCore() {
whenever(core.getSone(anyString())).thenReturn(null)
- whenever(core.getPost(anyString())).thenReturn(absent())
+ whenever(core.getPost(anyString())).thenReturn(null)
whenever(core.getPostReply(anyString())).thenReturn(null)
}
whenever(core.updateChecker).thenReturn(updateChecker)
whenever(core.getSone(ArgumentMatchers.anyString())).thenAnswer { (localSones + remoteSones)[it.getArgument(0)] }
whenever(core.getLocalSone(ArgumentMatchers.anyString())).thenAnswer { localSones[it[0]] }
- whenever(core.getPost(ArgumentMatchers.anyString())).thenAnswer { (posts + newPosts)[it[0]].asOptional() }
+ whenever(core.getPost(ArgumentMatchers.anyString())).thenAnswer { (posts + newPosts)[it[0]] }
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]] }
whenever(core.getSone(anyString())).then { allSones[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.getPost(anyString())).then { allPosts[it[0]] }
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]] }