val pagePath = pagePathParts.joinToString("/")
val formParams = verifyCsrfToken()
- val loggedInAs = currentNation() ?: redirectWithError("/auth/login", "You must be logged in to write comments")
+ val loggedInAs = currentNation() ?: redirectWithError("/auth/login", "You must be logged in to write comments", "comments")
val contents = formParams.getOrFail("comment")
+ if (contents.isBlank())
+ redirectWithError("/lore/$pagePath", "Comments may not be blank", "comments")
+
val comment = Comment(
id = Id(),
submittedBy = loggedInAs.id,
val submitter = nationCache.getNation(comment.submittedBy)
if (submitter.isBanned && currentNation?.id != comment.submittedBy && currentNation?.id != OwnerNationId)
- redirectWithError("/lore/${comment.submittedIn}", "The user who posted that comment is banned from commenting")
+ redirectWithError("/lore/${comment.submittedIn}", "The user who posted that comment is banned from commenting", "comments")
+
+ val queryParams = if (request.queryParameters.isEmpty())
+ ""
+ else "?${request.queryParameters.formUrlEncode()}"
- redirect("/lore/${comment.submittedIn}#comment-$commentId")
+ redirect("/lore/${comment.submittedIn}$queryParams#comment-$commentId")
}
suspend fun ApplicationCall.editCommentRoute(): Nothing {
val newContents = formParams.getOrFail("comment")
+ if (newContents.isBlank())
+ redirectWithError("/comment/view/$commentId", "Comments may not be blank")
+
// Check for null edits, i.e. edits that don't change anything
if (newContents == oldComment.contents)
redirect("/comment/view/$commentId")
fun redirect(url: String, permanent: Boolean = false): Nothing = throw HttpRedirectException(url, permanent)
-fun redirectWithError(url: String, error: String): Nothing {
- val urlWithError = url + "?" + parametersOf("error", error).formUrlEncode()
+fun redirectWithError(url: String, error: String, hash: String? = null): Nothing {
+ val parameters = parametersOf("error", error).formUrlEncode()
+ val markedHash = hash?.let { "#$it" } ?: ""
+ val urlWithError = "$url?$parameters$markedHash"
redirect(urlWithError, false)
}
package info.mechyrdia.lore
+import io.ktor.http.*
import io.ktor.server.application.*
+import io.ktor.server.request.*
+import io.ktor.util.*
import kotlinx.html.*
fun ApplicationCall.page(pageTitle: String, navBar: List<NavItem>? = null, sidebar: Sidebar? = null, content: SECTIONS.() -> Unit): HTML.() -> Unit {
}
}
+ request.queryParameters["error"]?.let { errorMessage ->
+ div {
+ id = "error-popup"
+
+ val paramsWithoutError = parametersOf(request.queryParameters.toMap() - "error")
+ val newQueryString = if (paramsWithoutError.isEmpty())
+ ""
+ else "?${paramsWithoutError.formUrlEncode()}"
+ attributes["data-redirect-url"] = "${request.path()}$newQueryString"
+
+ div(classes = "bg")
+ div(classes = "msg") {
+ p { +errorMessage }
+ p { +"Click to close this popup" }
+ }
+ }
+ }
+
script(src = "/static/init.js") {}
}
}
window.addEventListener("load", function () {
// Error popup
- const queryParams = new URLSearchParams(window.location.search);
- if (queryParams.has("error")) {
- const errorMessage = queryParams.get("error");
-
- const errorPopup = document.createElement("div");
- errorPopup.id = "error-popup";
-
- const errorBg = document.createElement("div");
- errorBg.classList.add("bg");
- errorPopup.append(errorBg);
-
- const errorMsg = document.createElement("div");
- errorMsg.classList.add("msg");
-
- const errorP1 = document.createElement("p");
- errorP1.append(document.createTextNode(errorMessage));
- errorMsg.append(errorP1);
-
- const errorP2 = document.createElement("p");
- errorP2.append(document.createTextNode("Click to close this popup"));
- errorMsg.append(errorP2);
-
- errorPopup.append(errorMsg);
-
- document.body.append(errorPopup);
+ const errorPopup = document.getElementById("error-popup");
+ if (errorPopup != null) {
errorPopup.addEventListener("click", e => {
e.preventDefault();
- errorPopup.remove();
+
+ const thisElement = e.currentTarget;
+ const newUrl = window.location.origin + thisElement.getAttribute("data-redirect-url") + window.location.hash;
+ window.history.replaceState({}, '', newUrl);
+
+ thisElement.remove();
});
}
});