From 652e673d3980bb4205e5221aea261c923c4adc60 Mon Sep 17 00:00:00 2001 From: Robin Malley Date: Wed, 1 Mar 2023 00:41:51 +0000 Subject: [PATCH] Show comments Show comments on a story's short description, and automatically load comments when reading a story. --- src/lua/db.lua | 7 ++- src/lua/endpoints/index_get.lua | 27 +++++++---- src/lua/endpoints/preview_post.lua | 1 + src/lua/endpoints/read_get.lua | 22 +-------- src/lua/util.lua | 37 ++++++++++++++ src/pages/parts/story_breif.etlua.in | 2 + src/pages/read.etlua.in | 72 ++++++++++++---------------- src/sql/select_author_index.sql | 6 ++- src/sql/select_site_index.sql | 9 +++- 9 files changed, 108 insertions(+), 75 deletions(-) diff --git a/src/lua/db.lua b/src/lua/db.lua index 6d4564c..4de6957 100644 --- a/src/lua/db.lua +++ b/src/lua/db.lua @@ -5,7 +5,6 @@ Notably, holds a connection to the open sqlite3 database in .conn local sql = require("lsqlite3") local queries = require("queries") -local util = require("util") local config = require("config") local db = {} @@ -16,7 +15,11 @@ message on fail, and returns true on success. ]] function db.sqlassert(r, errcode, err) if not r then - error(string.format("%d: %s",errcode, err)) + if err then + error(string.format("%d: %s",errcode, err)) + elseif errcode then + error(string.format("%d: %s",errcode, db.conn:errmsg())) + end end return r end diff --git a/src/lua/endpoints/index_get.lua b/src/lua/endpoints/index_get.lua index 479db20..7197a87 100644 --- a/src/lua/endpoints/index_get.lua +++ b/src/lua/endpoints/index_get.lua @@ -15,21 +15,21 @@ local stmnt_index, stmnt_author, stmnt_author_bio local oldconfigure = configure function configure(...) - stmnt_index = assert(db.conn:prepare(queries.select_site_index)) + stmnt_index = db.sqlassert(db.conn:prepare(queries.select_site_index)) --TODO: actually let authors edit their bio - stmnt_author_bio = assert(db.conn:prepare([[ + stmnt_author_bio = db.sqlassert(db.conn:prepare([[ SELECT authors.biography FROM authors WHERE authors.name = :author; ]])) - stmnt_author = assert(db.conn:prepare(queries.select_author_index)) + stmnt_author = db.sqlassert(db.conn:prepare(queries.select_author_index)) return oldconfigure(...) end local function get_site_home(req, loggedin) log(LOG_DEBUG,"Cache miss, rendering site index") - stmnt_index:bind_names{} + stmnt_index:bind_names{offset=0} local latest = {} - for idr, title, iar, dater, author, hits in db.sql_rows(stmnt_index) do - table.insert(latest,{ + for idr, title, iar, dater, author, hits, cmts in db.sql_rows(stmnt_index) do + local story = { url = util.encode_id(idr), title = title, isanon = tonumber(iar) == 1, @@ -37,12 +37,19 @@ local function get_site_home(req, loggedin) author = author, tags = libtags.get(idr), hits = hits, - }) + ncomments = cmts + } + table.insert(latest,story) + end return pages.index{ domain = config.domain, stories = latest, - loggedin = loggedin + loggedin = loggedin, + extra_load = { + '' + } + } end local function get_author_home(req, loggedin) @@ -71,7 +78,7 @@ local function get_author_home(req, loggedin) stmnt_author_bio:reset() stmnt_author:bind_names{author=subdomain} local stories = {} - for id, title, time, hits, unlisted, hash in db.sql_rows(stmnt_author) do + for id, title, time, hits, unlisted, hash, cmts in db.sql_rows(stmnt_author) do if unlisted == 1 and author == subdomain then local url = util.encode_id(id) .. "?pwd=" .. util.encode_unlisted(hash) table.insert(stories,{ @@ -82,6 +89,7 @@ local function get_author_home(req, loggedin) tags = libtags.get(id), hits = hits, unlisted = true, + ncomments = cmts }) elseif unlisted == 0 then table.insert(stories,{ @@ -92,6 +100,7 @@ local function get_author_home(req, loggedin) tags = libtags.get(id), hits = hits, unlisted = false, + ncomments = cmts }) end end diff --git a/src/lua/endpoints/preview_post.lua b/src/lua/endpoints/preview_post.lua index c4fdaeb..90ec057 100644 --- a/src/lua/endpoints/preview_post.lua +++ b/src/lua/endpoints/preview_post.lua @@ -24,6 +24,7 @@ local function preview_post(req) idp = "preview", text = parsed, tags = tags, + comments = {} } http_response(req,200,ret) end diff --git a/src/lua/endpoints/read_get.lua b/src/lua/endpoints/read_get.lua index fa583a1..bbb0d25 100644 --- a/src/lua/endpoints/read_get.lua +++ b/src/lua/endpoints/read_get.lua @@ -73,24 +73,6 @@ local function populate_ps_story(req,ps) return true end ---[[ -Get the comments for a story -]] -local function get_comments(req,ps) - stmnt_comments:bind_names{ - id = ps.storyid - } - local comments = {} - for com_author, com_isanon, com_text in db.sql_rows(stmnt_comments) do - table.insert(comments,{ - author = com_author, - isanon = com_isanon == 1, --int to boolean - text = com_text - }) - end - return comments -end - local function read_get(req) --Pages settings local ps = { @@ -130,9 +112,9 @@ local function read_get(req) --If we need to show comments http_request_populate_qs(req) - ps.show_comments = http_argument_get_string(req,"comments") + ps.show_comments = true if ps.show_comments then - ps.comments = get_comments(req,ps) + ps.comments = util.get_comments(ps.storyid) end --If this post is unlisted, get the hash diff --git a/src/lua/util.lua b/src/lua/util.lua index 54b7664..cc367f1 100644 --- a/src/lua/util.lua +++ b/src/lua/util.lua @@ -2,8 +2,19 @@ local sql = require("lsqlite3") local config = require("config") local types = require("types") +local db = require("db") +local queries = require("queries") + local util = {} + +local stmnt_comments +local oldconfigure = configure +function configure(...) + stmnt_comments = assert(db.conn:prepare(queries.select_comments)) + return oldconfigure(...) +end + --see https://perishablepress.com/stop-using-unsafe-characters-in-urls/ --no underscore because we use that for our operative pages local url_characters = @@ -147,6 +158,32 @@ function util.parse_tags(str) return tags end +--[[ +Get the comments for a story + +Comments are a table with the structure: +comment :: table { + author :: string - The author's text name + isanon :: boolean - True if the author is anon (author string will be "Anonymous") + text :: string - The text of the comment +} +]] +function util.get_comments(sid) + stmnt_comments:bind_names{ + id = sid + } + local comments = {} + for com_author, com_isanon, com_text in db.sql_rows(stmnt_comments) do + table.insert(comments,{ + author = com_author, + isanon = com_isanon == 1, --int to boolean + text = com_text + }) + end + return comments +end + + if config.debugging then function util.checktypes(...) local args = {...} diff --git a/src/pages/parts/story_breif.etlua.in b/src/pages/parts/story_breif.etlua.in index 1ad4b48..1d3ce31 100644 --- a/src/pages/parts/story_breif.etlua.in +++ b/src/pages/parts/story_breif.etlua.in @@ -15,6 +15,8 @@ <% end %> <%= story.hits %> hits + + <%= story.ncomments %> comments