2020-12-20 09:16:23 +01:00
local sql = require ( " lsqlite3 " )
local zlib = require ( " zlib " )
2020-12-21 05:22:22 +01:00
local db = require ( " db " )
2020-12-20 09:16:23 +01:00
local queries = require ( " queries " )
local pages = require ( " pages " )
local parsers = require ( " parsers " )
local util = require ( " util " )
local tagslib = require ( " tags " )
local cache = require ( " cache " )
local config = require ( " config " )
2020-12-21 05:22:22 +01:00
local session = require ( " session " )
2020-12-20 09:16:23 +01:00
local stmnt_author_of , stmnt_update_raw , stmnt_update
local oldconfigure = configure
function configure ( ... )
stmnt_author_of = assert ( db.conn : prepare ( queries.select_author_of_post ) )
stmnt_update_raw = assert ( db.conn : prepare ( queries.update_raw ) )
stmnt_update = assert ( db.conn : prepare ( queries.update_post ) )
return oldconfigure ( ... )
end
local function edit_post ( req )
local host = http_request_get_host ( req )
local path = http_request_get_path ( req )
2020-12-21 05:22:22 +01:00
local author , author_id = session.get ( req )
2020-12-20 09:16:23 +01:00
http_request_populate_post ( req )
local storyid = tonumber ( assert ( http_argument_get_string ( req , " story " ) ) )
local title = assert ( http_argument_get_string ( req , " title " ) )
local text = assert ( http_argument_get_string ( req , " text " ) )
local pasteas = assert ( http_argument_get_string ( req , " pasteas " ) )
local markup = assert ( http_argument_get_string ( req , " markup " ) )
local tags_str = http_argument_get_string ( req , " tags " )
stmnt_author_of : bind_names {
id = storyid
}
2020-12-21 05:22:22 +01:00
local err = util.do_sql ( stmnt_author_of )
2020-12-20 09:16:23 +01:00
if err ~= sql.ROW then
stmnt_author_of : reset ( )
error ( " No author found for story: " .. storyid )
end
local data = stmnt_author_of : get_values ( )
stmnt_author_of : reset ( )
local realauthor = data [ 1 ]
assert ( realauthor == author_id ) --Make sure the author of the story is the currently logged in user
local parsed = parsers [ markup ] ( text )
local compr_raw = zlib.compress ( text )
local compr = zlib.compress ( parsed )
local tags = { }
if tags_str then
tags = util.parse_tags ( tags_str )
end
assert ( stmnt_update_raw : bind_blob ( 1 , compr_raw ) == sql.OK )
assert ( stmnt_update_raw : bind ( 2 , markup ) == sql.OK )
assert ( stmnt_update_raw : bind ( 3 , storyid ) == sql.OK )
assert ( util.do_sql ( stmnt_update_raw ) == sql.DONE , " Failed to update raw " )
stmnt_update_raw : reset ( )
assert ( stmnt_update : bind ( 1 , title ) == sql.OK )
assert ( stmnt_update : bind_blob ( 2 , compr ) == sql.OK )
assert ( stmnt_update : bind ( 3 , pasteas == " anonymous " and 1 or 0 ) == sql.OK )
assert ( stmnt_update : bind ( 4 , storyid ) == sql.OK )
2020-12-21 05:22:22 +01:00
assert ( util.do_sql ( stmnt_update ) == sql.DONE , " Failed to update text " )
2020-12-20 09:16:23 +01:00
stmnt_update : reset ( )
tagslib.set ( storyid , tags )
--[[
assert ( stmnt_drop_tags : bind_names { postid = storyid } == sql.OK )
do_sql ( stmnt_drop_tags )
stmnt_drop_tags : reset ( )
for _ , tag in pairs ( tags ) do
print ( " Looking at tag " , tag )
assert ( stmnt_ins_tag : bind ( 1 , storyid ) == sql.OK )
assert ( stmnt_ins_tag : bind ( 2 , tag ) == sql.OK )
err = do_sql ( stmnt_ins_tag )
stmnt_ins_tag : reset ( )
end
] ]
local id_enc = util.encode_id ( storyid )
local loc = string.format ( " https://%s/%s " , config.domain , id_enc )
cache.dirty ( string.format ( " %s/%s " , config.domain , id_enc ) ) -- This place to read this post
cache.dirty ( string.format ( " %s " , config.domain ) ) -- The site index (ex, if the author changed the paste from their's to "Anonymous", the cache should reflect that).
cache.dirty ( string.format ( " %s.%s " , author , config.domain ) ) -- The author's index, same reasoning as above.
http_response_header ( req , " Location " , loc )
http_response ( req , 303 , " " )
return
end
2020-12-21 05:22:22 +01:00
return edit_post