private final PostVisibilityFilter postVisibilityFilter;
private final ReplyVisibilityFilter replyVisibilityFilter;
+ private final ElementLoader elementLoader;
private final TimeTextConverter timeTextConverter = new TimeTextConverter();
private final L10nFilter l10nFilter = new L10nFilter(this);
this.listNotificationFilter = listNotificationFilter;
this.postVisibilityFilter = postVisibilityFilter;
this.replyVisibilityFilter = replyVisibilityFilter;
+ this.elementLoader = elementLoader;
formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
soneTextParser = new SoneTextParser(getCore(), getCore());
pageToadlets.add(pageToadletFactory.createPageToadlet(new TemplatePage<FreenetRequest>("OpenSearch.xml", "application/opensearchdescription+xml", templateContextFactory, openSearchTemplate)));
pageToadlets.add(pageToadletFactory.createPageToadlet(new GetImagePage(this)));
pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
- pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this, timeTextConverter, l10nFilter)));
+ pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this, elementLoader, timeTextConverter, l10nFilter)));
pageToadlets.add(pageToadletFactory.createPageToadlet(new GetNotificationsAjaxPage(this)));
pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this)));
import com.fasterxml.jackson.databind.node.ObjectNode
fun jsonObject(block: ObjectNode.() -> Unit): ObjectNode = ObjectNode(instance).apply(block)
+fun jsonArray(vararg objects: String?): ArrayNode = objects.fold(ArrayNode(instance), ArrayNode::add)
fun Iterable<ObjectNode>.toArray(): ArrayNode = fold(ArrayNode(instance), ArrayNode::add)
package net.pterodactylus.sone.web.ajax
import com.fasterxml.jackson.databind.JsonNode
+import com.fasterxml.jackson.databind.ObjectMapper
+import net.pterodactylus.sone.core.ElementLoader
+import net.pterodactylus.sone.core.LinkedElement
import net.pterodactylus.sone.data.Post
import net.pterodactylus.sone.data.PostReply
import net.pterodactylus.sone.data.Sone
* The “get status” AJAX handler returns all information that is necessary to
* update the web interface in real-time.
*/
-class GetStatusAjaxPage(webInterface: WebInterface, private val timeTextConverter: TimeTextConverter, private val l10nFilter: L10nFilter):
+class GetStatusAjaxPage(webInterface: WebInterface, private val elementLoader: ElementLoader, private val timeTextConverter: TimeTextConverter, private val l10nFilter: L10nFilter):
JsonPage("getStatus.ajax", webInterface) {
private val dateFormatter = SimpleDateFormat("MMM d, yyyy, HH:mm:ss")
this["sones"] = request.httpRequest.getParam("soneIds").split(',').map { webInterface.core.getSone(it).orNull() }.plus(currentSone).filterNotNull().toJsonSones()
this["newPosts"] = webInterface.getNewPosts(currentSone).toJsonPosts()
this["newReplies"] = webInterface.getNewReplies(currentSone).toJsonReplies()
+ this["loadedElements"] = request.httpRequest.getParam("elements", "[]").asJson().map(JsonNode::asText).map(elementLoader::loadElement).toJsonElements()
}
}
private operator fun JsonReturnObject.set(key: String, value: Int) = put(key, value)
private operator fun JsonReturnObject.set(key: String, value: Boolean) = put(key, value)
+ private fun String.asJson() = ObjectMapper().readTree(this).asIterable()
+
override fun needsFormPassword() = false
override fun requiresLogin() = false
}
}.toArray()
+ private fun Iterable<LinkedElement>.toJsonElements() = map { (link, failed, loading) ->
+ jsonObject {
+ put("link", link)
+ put("loading", loading)
+ put("failed", failed)
+ }
+ }.toArray()
+
}
assertThat(arrayNode.toString(), equalTo("[{\"foo\":\"bar\"},{\"baz\":\"quo\"}]"))
}
+ @Test
+ fun `array is created correctly for strings`() {
+ val arrayNode = jsonArray("foo", "bar", "baz")
+ assertThat(arrayNode.toString(), equalTo("[\"foo\",\"bar\",\"baz\"]"))
+ }
+
}
import net.pterodactylus.sone.test.whenever
import net.pterodactylus.sone.text.TimeText
import net.pterodactylus.sone.text.TimeTextConverter
+import net.pterodactylus.sone.utils.jsonArray
import net.pterodactylus.util.notify.Notification
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.allOf
private val timeTextConverter = mock<TimeTextConverter>()
private val l10nFilter = mock<L10nFilter>()
- override var page: JsonPage = GetStatusAjaxPage(webInterface, timeTextConverter, l10nFilter)
+ override var page: JsonPage = GetStatusAjaxPage(webInterface, elementLoader, timeTextConverter, l10nFilter)
@Before
fun setupTimeTextConverter() {
))
}
+ @Test
+ fun `page returns information about loaded elements`() {
+ addLoadedElement("KSK@test.png", loading = false, failed = false)
+ addLoadedElement("KSK@test.html", loading = true, failed = false)
+ addLoadedElement("KSK@test.jpeg", loading = false, failed = true)
+ addRequestParameter("elements", jsonArray("KSK@test.png", "KSK@test.html", "KSK@test.jpeg").toString())
+ assertThat(json.get("loadedElements").elements().asSequence().map { it.toMap() }.toList(), containsInAnyOrder(
+ mapOf<String, String?>("link" to "KSK@test.png", "loading" to "false", "failed" to "false"),
+ mapOf("link" to "KSK@test.html", "loading" to "true", "failed" to "false"),
+ mapOf("link" to "KSK@test.jpeg", "loading" to "false", "failed" to "true")
+ ))
+ }
+
private fun JsonNode.toMap() = fields().asSequence().map { it.key!! to if (it.value.isNull) null else it.value.asText()!! }.toMap()
}
import freenet.clients.http.ToadletContext
import freenet.support.api.HTTPRequest
import net.pterodactylus.sone.core.Core
+import net.pterodactylus.sone.core.ElementLoader
+import net.pterodactylus.sone.core.LinkedElement
import net.pterodactylus.sone.data.Post
import net.pterodactylus.sone.data.PostReply
import net.pterodactylus.sone.data.Sone
protected val webInterface = mock<WebInterface>()
protected val core = mock<Core>()
+ protected val elementLoader = mock<ElementLoader>()
protected open lateinit var page: JsonPage
protected val json by lazy { page.createJsonObject(freenetRequest)!! }
private val remoteSones = mutableMapOf<String, Sone>()
private val newPosts = mutableMapOf<String, Post>()
private val newReplies = mutableMapOf<String, PostReply>()
+ private val loadedElements = mutableMapOf<String, LinkedElement>()
private val notifications = mutableListOf<Notification>()
@Before
}
@Before
+ fun setupElementLoader() {
+ whenever(elementLoader.loadElement(anyString())).thenAnswer {
+ loadedElements[it.getArgument(0)] ?: LinkedElement(it.getArgument(0), loading = true)
+ }
+ }
+
+ @Before
fun setupCurrentSone() {
currentSone.mock("soneId", "Sone_Id", true, 1000, idle)
}
@Before
fun setupHttpRequest() {
whenever(httpRequest.getParam(anyString())).thenAnswer { requestParameters[it.getArgument(0)] ?: "" }
+ whenever(httpRequest.getParam(anyString(), anyString())).thenAnswer { requestParameters[it.getArgument(0)] ?: it.getArgument(1) }
}
protected fun Sone.mock(id: String, name: String, local: Boolean = false, time: Long, status: SoneStatus = idle) = apply {
}
}
+ protected fun addLoadedElement(link: String, loading: Boolean, failed: Boolean) {
+ loadedElements[link] = LinkedElement(link, failed, loading)
+ }
+
}