diff --git a/apps/threads.lua b/apps/threads.lua index 4ff7d6e..a9107fa 100644 --- a/apps/threads.lua +++ b/apps/threads.lua @@ -94,6 +94,7 @@ app:get("thread", "/:slug", function(self) "WHERE posts.thread_id = ? ORDER BY posts.created_at ASC LIMIT ? OFFSET ?") local posts = db.query(query, thread.id, POSTS_PER_PAGE, (self.page - 1) * POSTS_PER_PAGE) self.topic = Topics:find(thread.topic_id) + self.other_topics = db.query("SELECT topics.id, topics.name FROM topics") self.me = util.get_logged_in_user_or_transient(self) self.posts = posts @@ -169,4 +170,40 @@ app:post("thread_sticky", "/:slug/sticky", function(self) return {redirect_to = self:url_for("thread", {slug = self.params.slug})} end) +app:post("thread_move", "/:slug/move", function(self) + local user = util.get_logged_in_user(self) + if not user then + return {redirect_to = self:url_for("thread", {slug = self.params.slug})} + end + + if not user:is_mod() then + return {redirect_to = self:url_for("thread", {slug = self.params.slug})} + end + + if not self.params.new_topic_id then + util.inject_err_infobox(self, "Thread already in this topic.") + return {redirect_to = self:url_for("thread", {slug = self.params.slug})} + end + + local new_topic = Topics:find({id = self.params.new_topic_id}) + if not new_topic then + return {redirect_to = self:url_for("thread", {slug = self.params.slug})} + end + local thread = Threads:find({slug = self.params.slug}) + if not thread then + return {redirect_to = self:url_for("thread", {slug = self.params.slug})} + end + if new_topic.id == thread.topic_id then + util.inject_err_infobox(self, "Thread already in this topic.") + return {redirect_to = self:url_for("thread", {slug = self.params.slug})} + end + + local old_topic = Topics:find({id = thread.topic_id}) + + thread:update({topic_id = new_topic.id}) + util.inject_infobox(self, ("Thread moved from \"%s\" to \"%s\"."):format(old_topic.name, new_topic.name)) + + return {redirect_to = self:url_for("thread", {slug = self.params.slug})} +end) + return app diff --git a/views/threads/thread.etlua b/views/threads/thread.etlua index 6bddbb0..fa8b374 100644 --- a/views/threads/thread.etlua +++ b/views/threads/thread.etlua @@ -25,6 +25,15 @@ "> +
<% end %> <% end %>