smr/src/lua/endpoints/edit_get.lua

85 lines
2.0 KiB
Lua

local zlib = require("zlib")
local sql = require("lsqlite3")
local db = require("db")
local queries = require("queries")
local util = require("util")
local pages = require("pages")
local tags = require("tags")
local session = require("session")
local config = require("config")
local api = require("hooks")
local stmnt_edit
local oldconfigure = configure
function configure(...)
stmnt_edit = assert(db.conn:prepare(queries.select_edit))
return oldconfigure(...)
end
local oldspec = api.get.page_owner
api.get.page_owner = function(env)
local ret = oldspec(env)
table.insert(ret,{
endpoint = string.format("https://%s/_edit",env.domain),
method = "GET",
fields = {story = env.short},
text = "Edit"
})
return ret
end
local function edit_get(req)
local host = http_request_get_host(req)
local path = http_request_get_path(req)
local author, authorid = session.get(req)
http_request_populate_qs(req)
local story = assert(http_argument_get_string(req,"story"))
local story_id = util.decode_id(story)
local ret
--Check that the logged in user is the owner of the story
--sql-side. If we're not the owner, we'll get 0 rows back.
stmnt_edit:bind_names{
postid = story_id,
authorid = authorid
}
local err = db.do_sql(stmnt_edit)
if err == sql.DONE then
--No rows, we're probably not the owner (it might
--also be because there's no such story)
ret = pages.cantedit{
path = story,
}
stmnt_edit:reset()
http_response(req,200,ret)
return
end
assert(err == sql.ROW)
local data = stmnt_edit:get_values()
local txt_compressed, markup, isanon, title, unlisted = unpack(data)
local text = zlib.decompress(txt_compressed)
local tags = tags.get(story_id)
local tags_txt = table.concat(tags,";")
stmnt_edit:reset()
ret = pages.edit{
title = title,
text = text,
markup = markup,
user = author,
isanon = isanon == 1,
domain = config.domain,
story = story_id,
err = "",
tags = tags_txt,
unlisted = unlisted == 1,
extra_load = {
'<script src="/_js/suggest_tags.js"></script>'
}
}
http_response(req,200,ret)
end
return edit_get