diff --git a/apps/post.lua b/apps/post.lua index 9bdfffe..b0b18db 100644 --- a/apps/post.lua +++ b/apps/post.lua @@ -8,7 +8,7 @@ local util = require("util") local models = require("models") local Posts = models.Posts local Threads = models.Threads - +local PostHistory = models.PostHistory app:get("single_post", "/:post_id", function(self) local query = constants.FULL_POSTS_QUERY .. "WHERE posts.id = ?" @@ -22,6 +22,33 @@ app:get("single_post", "/:post_id", function(self) return {render = "post.single-post"} end) +app:post("delete_post", "/:post_id/delete", function(self) + local user = util.get_logged_in_user(self) + if not user then + return {redirect_to = self:url_for"all_topics"} + end + print("id is " .. self.params.post_id) + local post = Posts:find({id = self.params.post_id}) + if not post then + return {redirect_to = self:url_for"all_topics"} + end + + local thread = Threads:find({id = post.thread_id}) + if user:is_mod() then + post:delete() + util.inject_infobox(self, "Post deleted.") + return {redirect_to = self:url_for("thread", {slug = thread.slug})} + end + + if post.user_id ~= user.id then + return {redirect_to = self:url_for"all_topics"} + end + + post:delete() + util.inject_infobox(self, "Post deleted.") + return {redirect_to = self:url_for("thread", {slug = thread.slug})} +end) + app:get("edit_post", "/:post_id/edit", function(self) local user = util.get_logged_in_user(self) if not user then @@ -74,4 +101,4 @@ app:post("edit_post", "/:post_id/edit", function(self) return {redirect_to = link} end) -return app \ No newline at end of file +return app diff --git a/data/static/style.css b/data/static/style.css index 674dfe6..4da9631 100644 --- a/data/static/style.css +++ b/data/static/style.css @@ -136,6 +136,20 @@ pre code { font-size: 1rem; } +#delete-dialog { + padding: 0; + border-radius: 4px; + border: 2px solid black; + box-shadow: 0 0 30px rgba(0, 0, 0, 0.25); +} + +.delete-dialog-inner { + display: flex; + flex-direction: column; + align-items: center; + padding: 20px; +} + .copy-code-container { position: sticky; width: calc(100% - 4px); diff --git a/js/thread.js b/js/thread.js index 05ba3fa..89c74cd 100644 --- a/js/thread.js +++ b/js/thread.js @@ -1,10 +1,38 @@ { const ta = document.getElementById("post_content"); - + for (let button of document.querySelectorAll(".reply-button")) { button.addEventListener("click", (e) => { ta.value += button.value; ta.scrollIntoView() }) } + + const deleteDialog = document.getElementById("delete-dialog"); + const deleteDialogCloseButton = document.getElementById("post-delete-dialog-close"); + let deletionTargetPostContainer; + + function closeDeleteDialog() { + deletionTargetPostContainer.style.removeProperty("background-color"); + deleteDialog.close(); + } + + deleteDialogCloseButton.addEventListener("click", (e) => { + closeDeleteDialog(); + }) + deleteDialog.addEventListener("click", (e) => { + if (e.target === deleteDialog) { + closeDeleteDialog(); + } + }) + for (let button of document.querySelectorAll(".post-delete-button")) { + button.addEventListener("click", (e) => { + deleteDialog.showModal(); + const postId = button.value; + deletionTargetPostContainer = document.getElementById("post-" + postId).querySelector(".post-content-container"); + deletionTargetPostContainer.style.setProperty("background-color", "#fff"); + const form = document.getElementById("post-delete-form"); + form.action = `/post/${postId}/delete` + }) + } } diff --git a/sass/style.scss b/sass/style.scss index 2f8109d..b0717dc 100644 --- a/sass/style.scss +++ b/sass/style.scss @@ -172,6 +172,20 @@ pre code { font-size: 1rem; } +#delete-dialog { + padding: 0; + border-radius: 4px; + border: 2px solid black; + box-shadow: 0 0 30px rgba(0, 0, 0, 0.25); +} + +.delete-dialog-inner { + display: flex; + flex-direction: column; + align-items: center; + padding: 20px; +} + .copy-code-container { position: sticky; // width: 100%; diff --git a/views/threads/post.etlua b/views/threads/post.etlua index 813e577..84343f3 100644 --- a/views/threads/post.etlua +++ b/views/threads/post.etlua @@ -53,7 +53,13 @@ local reply_text = ("%s\n[quote]%s[/quote]\n---\n\n"):format(quote_src_text, post.original_markup) %> - <% end %> + <% end %> + <% + local show_delete = (post.user_id == me.id and not ntob(thread.is_locked)) or me:is_mod() + if show_delete then + %> + + <% end %>