131 lines
3.6 KiB
Lua
131 lines
3.6 KiB
Lua
local app = require("lapis").Application()
|
|
local lapis_util = require("lapis.util")
|
|
|
|
local db = require("lapis.db")
|
|
local constants = require("constants")
|
|
|
|
local util = require("util")
|
|
|
|
local models = require("models")
|
|
local Users = models.Users
|
|
local Avatars = models.Avatars
|
|
local Topics = models.Topics
|
|
local Threads = models.Threads
|
|
|
|
local ThreadCreateError = {
|
|
OK = 0,
|
|
GUEST = 1,
|
|
LOGGED_OUT = 2,
|
|
TOPIC_LOCKED = 3,
|
|
}
|
|
|
|
app:get("all_topics", "", function(self)
|
|
self.topic_list = db.query("select * from topics limit 25;")
|
|
self.user = util.get_logged_in_user(self) or util.TransientUser
|
|
return {render = "topics.topics"}
|
|
end)
|
|
|
|
app:get("topic_create", "/create", function(self)
|
|
local user = util.get_logged_in_user(self) or util.TransientUser
|
|
if not user:is_mod() then
|
|
return {status = 403}
|
|
end
|
|
|
|
self.page_title = "creating topic"
|
|
|
|
return {render = "topics.create"}
|
|
end)
|
|
|
|
app:post("topic_create", "/create", function(self)
|
|
local user = util.get_logged_in_user(self) or util.TransientUser
|
|
if not user:is_mod() then
|
|
return {redirect_to = "all_topics"}
|
|
end
|
|
|
|
local topic_name = lapis_util.trim(self.params.name)
|
|
local topic_description = self.params.description
|
|
local time = os.time()
|
|
local slug = lapis_util.slugify(topic_name) .. "-" .. time
|
|
|
|
local topic = Topics:create({
|
|
name = topic_name,
|
|
description = topic_description,
|
|
slug = slug,
|
|
})
|
|
|
|
return {redirect_to = self:url_for("all_topics")}
|
|
end)
|
|
|
|
app:get("topic", "/:slug", function(self)
|
|
local topic = Topics:find({
|
|
slug = self.params.slug
|
|
})
|
|
if not topic then
|
|
return {status = 404}
|
|
end
|
|
self.topic = topic
|
|
self.threads_list = db.query("SELECT * FROM threads WHERE topic_id = ? ORDER BY is_stickied DESC, created_at DESC", topic.id)
|
|
local user = util.get_logged_in_user_or_transient(self)
|
|
print(topic.is_locked, type(topic.is_locked))
|
|
self.user = user
|
|
self.ThreadCreateError = ThreadCreateError
|
|
self.thread_create_error = ThreadCreateError.OK
|
|
if user:is_logged_in_guest() then
|
|
self.thread_create_error = ThreadCreateError.GUEST
|
|
elseif user:is_guest() then
|
|
self.thread_create_error = ThreadCreateError.LOGGED_OUT
|
|
elseif util.ntob(topic.is_locked) and not user:is_mod() then
|
|
self.thread_create_error = ThreadCreateError.TOPIC_LOCKED
|
|
end
|
|
|
|
self.page_title = "all threads in " .. topic.name
|
|
|
|
return {render = "topics.topic"}
|
|
end)
|
|
|
|
app:get("topic_edit", "/:slug/edit", function(self)
|
|
local user = util.get_logged_in_user_or_transient(self)
|
|
if not user:is_mod() then
|
|
return {redirect_to = self:url_for("topic", {slug = self.params.slug})}
|
|
end
|
|
local topic = Topics:find({
|
|
slug = self.params.slug
|
|
})
|
|
if not topic then
|
|
return {redirect_to = self:url_for("all_topics")}
|
|
end
|
|
self.topic = topic
|
|
|
|
self.page_title = "editing topic " .. topic.name
|
|
|
|
return {render = "topics.edit"}
|
|
end)
|
|
|
|
app:post("topic_edit", "/:slug/edit", function(self)
|
|
local user = util.get_logged_in_user_or_transient(self)
|
|
if not user:is_mod() then
|
|
return {redirect_to = self:url_for("topic", {slug = self.params.slug})}
|
|
end
|
|
local topic = Topics:find({
|
|
slug = self.params.slug
|
|
})
|
|
if not topic then
|
|
return {redirect_to = self:url_for("all_topics")}
|
|
end
|
|
local name = self.params.name or topic.name
|
|
local description = self.params.description or topic.description
|
|
local is_locked = topic.is_locked
|
|
if self.params.is_locked ~= nil then
|
|
is_locked = util.form_bool_to_sqlite(self.params.is_locked)
|
|
end
|
|
|
|
topic:update({
|
|
name = name,
|
|
description = description,
|
|
is_locked = is_locked,
|
|
})
|
|
return {redirect_to = self:url_for("topic", {slug = self.params.slug})}
|
|
end)
|
|
|
|
return app
|