2020-12-21 05:22:22 +01:00
|
|
|
local sql = require("lsqlite3")
|
|
|
|
|
|
|
|
local cache = require("cache")
|
|
|
|
local queries = require("queries")
|
|
|
|
local db = require("db")
|
|
|
|
local util = require("util")
|
|
|
|
local config = require("config")
|
|
|
|
local pages = require("pages")
|
|
|
|
local libtags = require("tags")
|
2021-01-11 04:34:22 +01:00
|
|
|
local session = require("session")
|
2022-11-21 01:32:49 +01:00
|
|
|
local parsers = require("parsers")
|
2020-12-21 05:22:22 +01:00
|
|
|
|
|
|
|
local stmnt_index, stmnt_author, stmnt_author_bio
|
|
|
|
|
|
|
|
local oldconfigure = configure
|
|
|
|
function configure(...)
|
|
|
|
stmnt_index = assert(db.conn:prepare(queries.select_site_index))
|
|
|
|
--TODO: actually let authors edit their bio
|
|
|
|
stmnt_author_bio = assert(db.conn:prepare([[
|
|
|
|
SELECT authors.biography FROM authors WHERE authors.name = :author;
|
|
|
|
]]))
|
|
|
|
stmnt_author = assert(db.conn:prepare(queries.select_author_index))
|
|
|
|
return oldconfigure(...)
|
|
|
|
end
|
|
|
|
|
2021-01-11 04:34:22 +01:00
|
|
|
local function get_site_home(req, loggedin)
|
2020-12-23 07:02:02 +01:00
|
|
|
log(LOG_DEBUG,"Cache miss, rendering site index")
|
2020-12-21 05:22:22 +01:00
|
|
|
stmnt_index:bind_names{}
|
|
|
|
local latest = {}
|
2021-01-04 03:48:29 +01:00
|
|
|
for idr, title, iar, dater, author, hits in util.sql_rows(stmnt_index) do
|
2020-12-21 05:22:22 +01:00
|
|
|
table.insert(latest,{
|
2020-12-23 07:02:02 +01:00
|
|
|
url = util.encode_id(idr),
|
|
|
|
title = title,
|
|
|
|
isanon = tonumber(iar) == 1,
|
|
|
|
posted = os.date("%B %d %Y",tonumber(dater)),
|
|
|
|
author = author,
|
2020-12-29 20:10:35 +01:00
|
|
|
tags = libtags.get(idr),
|
2021-01-04 03:48:29 +01:00
|
|
|
hits = hits,
|
2020-12-21 05:22:22 +01:00
|
|
|
})
|
|
|
|
end
|
|
|
|
return pages.index{
|
|
|
|
domain = config.domain,
|
2021-01-11 04:34:22 +01:00
|
|
|
stories = latest,
|
|
|
|
loggedin = loggedin
|
2020-12-21 05:22:22 +01:00
|
|
|
}
|
|
|
|
end
|
2021-09-12 03:47:40 +02:00
|
|
|
local function get_author_home(req, loggedin)
|
2021-01-16 22:02:20 +01:00
|
|
|
--print("Looking at author home...")
|
2020-12-21 05:22:22 +01:00
|
|
|
local host = http_request_get_host(req)
|
|
|
|
local subdomain = host:match("([^\\.]+)")
|
|
|
|
stmnt_author_bio:bind_names{author=subdomain}
|
|
|
|
local err = util.do_sql(stmnt_author_bio)
|
2021-01-11 04:34:22 +01:00
|
|
|
local author, authorid = session.get(req)
|
2020-12-21 05:22:22 +01:00
|
|
|
if err == sql.DONE then
|
2020-12-23 07:02:02 +01:00
|
|
|
log(LOG_INFO,"No such author:" .. subdomain)
|
2020-12-21 05:22:22 +01:00
|
|
|
stmnt_author_bio:reset()
|
|
|
|
return pages.noauthor{
|
|
|
|
author = subdomain
|
|
|
|
}
|
|
|
|
end
|
2022-11-21 01:32:49 +01:00
|
|
|
if err ~= sql.ROW then
|
|
|
|
stmnt_author_bio:reset()
|
|
|
|
error(string.format("Failed to get author %q error: %q",subdomain, tostring(err)))
|
|
|
|
end
|
2020-12-21 05:22:22 +01:00
|
|
|
local data = stmnt_author_bio:get_values()
|
2022-11-21 01:32:49 +01:00
|
|
|
local bio = parsers.plain(zlib.decompress(data[1]))
|
2020-12-21 05:22:22 +01:00
|
|
|
stmnt_author_bio:reset()
|
|
|
|
stmnt_author:bind_names{author=subdomain}
|
|
|
|
local stories = {}
|
2021-01-11 04:34:22 +01:00
|
|
|
for id, title, time, hits, unlisted, hash in util.sql_rows(stmnt_author) do
|
2021-01-16 22:02:20 +01:00
|
|
|
--print("Looking at:",id,title,time,hits,unlisted)
|
2021-01-11 04:34:22 +01:00
|
|
|
if unlisted == 1 and author == subdomain then
|
|
|
|
local url = util.encode_id(id) .. "?pwd=" .. util.encode_unlisted(hash)
|
|
|
|
table.insert(stories,{
|
|
|
|
url = url,
|
|
|
|
title = title,
|
|
|
|
author=subdomain,
|
|
|
|
posted = os.date("%B %d %Y",tonumber(time)),
|
|
|
|
tags = libtags.get(id),
|
|
|
|
hits = hits,
|
|
|
|
unlisted = true,
|
|
|
|
})
|
|
|
|
elseif unlisted == 0 then
|
|
|
|
table.insert(stories,{
|
|
|
|
url = util.encode_id(id),
|
|
|
|
title = title,
|
|
|
|
author=subdomain,
|
|
|
|
posted = os.date("%B %d %Y",tonumber(time)),
|
|
|
|
tags = libtags.get(id),
|
|
|
|
hits = hits,
|
|
|
|
unlisted = false,
|
|
|
|
})
|
|
|
|
end
|
2020-12-21 05:22:22 +01:00
|
|
|
end
|
|
|
|
return pages.author_index{
|
|
|
|
domain=config.domain,
|
|
|
|
author=subdomain,
|
2021-01-11 04:34:22 +01:00
|
|
|
loggedin = author,
|
2020-12-21 05:22:22 +01:00
|
|
|
stories=stories,
|
|
|
|
bio=bio
|
|
|
|
}
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
local function index_get(req)
|
|
|
|
local method = http_method_text(req)
|
|
|
|
local host = http_request_get_host(req)
|
|
|
|
local subdomain = host:match("([^\\.]+)")
|
2021-01-11 04:34:22 +01:00
|
|
|
local author, authorid = session.get(req)
|
2020-12-21 05:22:22 +01:00
|
|
|
local text
|
2021-01-11 04:34:22 +01:00
|
|
|
if host == config.domain and author == nil then
|
2020-12-23 07:02:02 +01:00
|
|
|
--Default home page
|
2020-12-21 05:22:22 +01:00
|
|
|
local cachepath = string.format("%s",config.domain)
|
|
|
|
text = cache.render(cachepath, function()
|
|
|
|
return get_site_home(req)
|
|
|
|
end)
|
2021-01-11 04:34:22 +01:00
|
|
|
elseif host == config.domain and author then
|
|
|
|
--Display home page with "log out" button
|
2021-08-27 03:08:02 +02:00
|
|
|
local cachepath = string.format("%s-logout",config.domain)
|
|
|
|
text = cache.render(cachepath, function()
|
2021-01-11 04:34:22 +01:00
|
|
|
return get_site_home(req,true)
|
|
|
|
end)
|
|
|
|
elseif host ~= config.domain and author ~= subdomain then
|
2020-12-23 07:02:02 +01:00
|
|
|
--author home page
|
2020-12-21 05:22:22 +01:00
|
|
|
local cachepath = string.format("%s.%s",subdomain,config.domain)
|
|
|
|
text = cache.render(cachepath, function()
|
2021-09-12 03:47:40 +02:00
|
|
|
return get_author_home(req, author ~= nil)
|
2020-12-21 05:22:22 +01:00
|
|
|
end)
|
2021-01-11 04:34:22 +01:00
|
|
|
elseif host ~= config.domain and author == subdomain then
|
|
|
|
--author's home page for the author, don't cache, display unlisted
|
2021-09-12 03:47:40 +02:00
|
|
|
text = get_author_home(req, author ~= nil)
|
2020-12-21 05:22:22 +01:00
|
|
|
end
|
|
|
|
assert(text)
|
|
|
|
http_response(req,200,text)
|
|
|
|
end
|
|
|
|
|
|
|
|
return index_get
|