smr/src/lua/read_get.lua

141 lines
3.2 KiB
Lua

local sql = require("sqlite3")
local session = require("session")
local tags = require("tags")
local db = require("db")
local queries = require("queries")
local stmnt_read, stmnt_update_views, stmnt_comments
local oldconfigure = configure
function configure(...)
stmnt_read = assert(db.conn:prepare(queries.select_post))
stmnt_update_views = assert(db.conn:prepare(queries.update_views))
stmnt_comments = assert(db.conn:prepare(queries.select_comments))
return configure(...)
end
--[[
Increases a story's hit counter by 1
]]
local function add_view(storyid)
stmnt_update_views:bind_names{
id = storyid
}
local err = do_sql(stmnt_update_views)
assert(err == sql.DONE, "Failed to update view counter:"..tostring(err))
stmnt_update_views:reset()
end
--[[
Populates ps with story settings, returns true if story was found,
or nil if it wasn't
]]
local function populate_ps_story(req,ps)
--Make sure our story exists
stmnt_read:bind_names{
id = ps.storyid
}
local err = do_sql(stmnt_read)
if err == sql.DONE then
--We got no story
stmnt_read:reset()
return nil
end
--If we've made it here, we have a story. Populate our settings
--with title, text, ect.
assert(err == sql.ROW)
add_view(storyid)
local title, storytext, tauthor, isanon, authorname, views = unpack(
stmnt_read:get_values()
)
ps.title = title
ps.storytext = zlib.decompress(storytext)
ps.tauthor = tauthor
ps.isanon = isanon == 1
ps.authorname = authorname
ps.views = views
stmnt_read:reset()
--Tags
ps.tags = tags.get(id)
return true
end
--[[
Get the comments for a story
]]
local function get_comments(req,ps)
end
--[[
The author is viewing their own story, give them an edit button
]]
local function read_get_author(req,storyid,author,authorid,comments)
end
--[[
An author is viewing a story, allow them to post comments as themselves
]]
local function read_get_loggedin(req,ps)
if ps.tauthor == ps.authorid then
--The story exists and we're logged in as the
--owner, display the edit button
return read_get_author(req,ps)
end
return pages.read(ps)
end
local function read_get(req)
--Pages settings
local ps = {
host = http_request_get_host(req),
path = http_request_get_path(req),
method = http_method_text(req),
}
--Get our story id
assert(string.len(ps.path) > 0,"Tried to read 0-length story id")
local idp = string.sub(ps.path,2)--remove leading "/"
ps.storyid = util.decode_id(idp)
--If we're logged in, set author and authorid
local author, authorid = session.get(req)
if author and authorid then
ps.author = author
ps.authorid = authorid
end
--If we need to show comments
http_request_populate_qs(req)
ps.show_comments = http_argument_get_string(req,"comments")
if ps.show_comments then
ps.comments = get_comments(req,ps)
end
--normal story display
if (not ps.author) then
local cachestr = string.format("%s%s%s",
ps.host,
ps.path,
ps.show_comments and "?comments=1" or ""
)
local text = cache.render(cachestr,function()
populate_ps_story(req,ps)
return pages.read(ps)
end)
else --we are logged in, don't cache
populate_ps_story(req,ps)
ps.owner = (ps.author == ps.tauthor)
text = pages.read(ps)
end
assert(text)
http_response(req,200,text)
return
end
return read_get