pageToadletRegistry.addPage(new RedirectPage<FreenetRequest>("", "index.html"));
pageToadletRegistry.addPage(new IndexPage(this, loaders, templateRenderer, postVisibilityFilter));
- pageToadletRegistry.addPage(new NewPage(this, loaders, templateRenderer));
+ pageToadletRegistry.addPage(new NewPage(this, loaders, templateRenderer, newElements));
pageToadletRegistry.addPage(new CreateSonePage(this, loaders, templateRenderer));
pageToadletRegistry.addPage(new KnownSonesPage(this, loaders, templateRenderer));
pageToadletRegistry.addPage(new EditProfilePage(this, loaders, templateRenderer));
pageToadletRegistry.addPage(new TemplatePage<FreenetRequest>("OpenSearch.xml", "application/opensearchdescription+xml", templateContextFactory, openSearchTemplate));
pageToadletRegistry.addPage(new GetImagePage(this));
pageToadletRegistry.addPage(new GetTranslationAjaxPage(this));
- pageToadletRegistry.addPage(new GetStatusAjaxPage(this, elementLoader, timeTextConverter, l10nFilter, TimeZone.getDefault()));
+ pageToadletRegistry.addPage(new GetStatusAjaxPage(this, elementLoader, newElements, timeTextConverter, l10nFilter, TimeZone.getDefault()));
pageToadletRegistry.addPage(new GetNotificationsAjaxPage(this));
pageToadletRegistry.addPage(new DismissNotificationAjaxPage(this));
pageToadletRegistry.addPage(new CreatePostAjaxPage(this));
import net.pterodactylus.sone.text.TimeTextConverter
import net.pterodactylus.sone.utils.jsonObject
import net.pterodactylus.sone.utils.toArray
+import net.pterodactylus.sone.web.NewElements
import net.pterodactylus.sone.web.WebInterface
import net.pterodactylus.sone.web.page.*
import java.text.SimpleDateFormat
* update the web interface in real-time.
*/
@ToadletPath("getStatus.ajax")
-class GetStatusAjaxPage(webInterface: WebInterface, private val elementLoader: ElementLoader, private val timeTextConverter: TimeTextConverter, private val l10nFilter: L10nFilter, timeZone: TimeZone):
+class GetStatusAjaxPage(webInterface: WebInterface, private val elementLoader: ElementLoader, private val newElements: NewElements, private val timeTextConverter: TimeTextConverter, private val l10nFilter: L10nFilter, timeZone: TimeZone):
JsonPage(webInterface) {
- @Inject constructor(webInterface: WebInterface, elementLoader: ElementLoader, timeTextConverter: TimeTextConverter, l10nFilter: L10nFilter):
- this(webInterface, elementLoader, timeTextConverter, l10nFilter, TimeZone.getDefault())
+ @Inject constructor(webInterface: WebInterface, elementLoader: ElementLoader, newElements: NewElements, timeTextConverter: TimeTextConverter, l10nFilter: L10nFilter):
+ this(webInterface, elementLoader, newElements, timeTextConverter, l10nFilter, TimeZone.getDefault())
private val dateFormatter = SimpleDateFormat("MMM d, yyyy, HH:mm:ss").apply {
this.timeZone = timeZone
this["options"] = currentSone?.options?.toJsonOptions() ?: jsonObject {}
this["notificationHash"] = webInterface.getNotifications(currentSone).sortedBy { it.createdTime }.hashCode()
this["sones"] = request.httpRequest.getParam("soneIds").split(',').mapNotNull(core::getSone).plus(currentSone).filterNotNull().toJsonSones()
- this["newPosts"] = webInterface.getNewPosts(currentSone).toJsonPosts()
- this["newReplies"] = webInterface.getNewReplies(currentSone).toJsonReplies()
+ this["newPosts"] = newElements.newPosts.toJsonPosts()
+ this["newReplies"] = newElements.newReplies.toJsonReplies()
this["linkedElements"] = request.httpRequest.getParam("elements", "[]").asJson().map(JsonNode::asText).map(elementLoader::loadElement).toJsonElements()
}
}
package net.pterodactylus.sone.web.pages
-import net.pterodactylus.sone.main.*
-import net.pterodactylus.sone.utils.*
-import net.pterodactylus.sone.web.*
-import net.pterodactylus.sone.web.page.*
-import net.pterodactylus.util.template.*
-import javax.inject.*
+import net.pterodactylus.sone.main.Loaders
+import net.pterodactylus.sone.utils.mapPresent
+import net.pterodactylus.sone.utils.paginate
+import net.pterodactylus.sone.utils.parameters
+import net.pterodactylus.sone.web.NewElements
+import net.pterodactylus.sone.web.WebInterface
+import net.pterodactylus.sone.web.page.MenuName
+import net.pterodactylus.sone.web.page.SoneRequest
+import net.pterodactylus.sone.web.page.TemplatePath
+import net.pterodactylus.sone.web.page.TemplateRenderer
+import net.pterodactylus.sone.web.page.ToadletPath
+import net.pterodactylus.util.template.TemplateContext
+import javax.inject.Inject
/**
* Page that displays all new posts and replies. The posts are filtered using
@MenuName("New")
@TemplatePath("/templates/new.html")
@ToadletPath("new.html")
-class NewPage @Inject constructor(webInterface: WebInterface, loaders: Loaders, templateRenderer: TemplateRenderer) :
+class NewPage @Inject constructor(webInterface: WebInterface, loaders: Loaders, templateRenderer: TemplateRenderer, private val newElements: NewElements) :
SoneTemplatePage(webInterface, loaders, templateRenderer, pageTitleKey = "Page.New.Title") {
override fun handleRequest(soneRequest: SoneRequest, templateContext: TemplateContext) =
getCurrentSone(soneRequest.toadletContext).let { currentSone ->
- (soneRequest.webInterface.getNewPosts(currentSone) + soneRequest.webInterface.getNewReplies(currentSone).mapPresent { it.post })
+ (newElements.newPosts + newElements.newReplies.mapPresent { it.post })
.distinct()
.sortedByDescending { it.time }
.let { posts ->
FreenetInterface::class.isProvidedByMock(),
Template::class.isProvidedByMock(),
WebInterface::class.isProvidedByDeepMock(),
- TemplateRenderer::class.isProvidedByMock()
+ TemplateRenderer::class.isProvidedByMock(),
+ NewElements::class.isProvidedByMock()
)!!
}
import net.pterodactylus.sone.text.TimeText
import net.pterodactylus.sone.text.TimeTextConverter
import net.pterodactylus.sone.utils.jsonArray
+import net.pterodactylus.sone.web.NewElements
import net.pterodactylus.sone.web.baseInjector
import net.pterodactylus.util.notify.Notification
import org.hamcrest.MatcherAssert.assertThat
private val timeTextConverter = mock<TimeTextConverter>()
private val l10nFilter = mock<L10nFilter>()
- override var page: JsonPage = GetStatusAjaxPage(webInterface, elementLoader, timeTextConverter, l10nFilter, TimeZone.getTimeZone("UTC"))
+ private val newElements = mock<NewElements>().apply {
+ whenever(newPosts).then { this@GetStatusAjaxPageTest.newPosts.values }
+ whenever(newReplies).then { this@GetStatusAjaxPageTest.newReplies.values }
+ }
+ override var page: JsonPage = GetStatusAjaxPage(webInterface, elementLoader, newElements, timeTextConverter, l10nFilter, TimeZone.getTimeZone("UTC"))
@Before
fun setupTimeTextConverter() {
/**
* Unit test for [NewPage].
*/
-class NewPageTest : WebPageTest(::NewPage) {
+class NewPageTest : WebPageTest() {
+
+ private val newElements = mock<NewElements>()
+ override val page: SoneTemplatePage
+ get() = NewPage(webInterface, loaders, templateRenderer, newElements)
@Before
fun setupNumberOfPostsPerPage() {
val postReplies = asList(mock<PostReply>(), mock())
whenever(postReplies[0].post).thenReturn(posts[0].asOptional())
whenever(postReplies[1].post).thenReturn(extraPost.asOptional())
- whenever(webInterface.getNewPosts(currentSone)).thenReturn(posts)
- whenever(webInterface.getNewReplies(currentSone)).thenReturn(postReplies)
+ whenever(newElements.newPosts).thenReturn(posts)
+ whenever(newElements.newReplies).thenReturn(postReplies)
verifyNoRedirect {
val renderedPosts = templateContext.get<List<Post>>("posts", List::class.java)
fun `posts are paginated properly`() {
webInterface.core.preferences.newPostsPerPage = 2
val posts = listOf(mock<Post>().withTime(2000), mock<Post>().withTime(3000), mock<Post>().withTime(1000))
- whenever(webInterface.getNewPosts(currentSone)).thenReturn(posts)
+ whenever(newElements.newPosts).thenReturn(posts)
verifyNoRedirect {
assertThat((templateContext["pagination"] as Pagination<Post>).items, contains(posts[1], posts[0]))
}
webInterface.core.preferences.newPostsPerPage = 2
addHttpRequestParameter("page", "1")
val posts = listOf(mock<Post>().withTime(2000), mock<Post>().withTime(3000), mock<Post>().withTime(1000))
- whenever(webInterface.getNewPosts(currentSone)).thenReturn(posts)
+ whenever(newElements.newPosts).thenReturn(posts)
verifyNoRedirect {
assertThat((templateContext["pagination"] as Pagination<Post>).items, contains(posts[2]))
}