changeset 43:298c71e0c854

caching
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 09 Nov 2022 23:05:01 -0700
parents 0c1b820fff34
children 96f0c3d65698
files src/edit.html.luan src/lib/Bbcode.luan src/lib/Post.luan src/lib/User.luan src/reply.html.luan
diffstat 5 files changed, 19 insertions(+), 107 deletions(-) [+]
line wrap: on
line diff
--- a/src/edit.html.luan	Tue Nov 08 14:02:28 2022 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,54 +0,0 @@
-local Luan = require "luan:Luan.luan"
-local error = Luan.error
-local Html = require "luan:Html.luan"
-local html_encode = Html.encode or error()
-local Io = require "luan:Io.luan"
-local Http = require "luan:http/Http.luan"
-local Shared = require "site:/lib/Shared.luan"
-local head = Shared.head or error()
-local header = Shared.header or error()
-local footer = Shared.footer or error()
-local Forum = require "site:/lib/Forum.luan"
-local forum_title = Forum.title or error()
-local Post = require "site:/lib/Post.luan"
-local User = require "site:/lib/User.luan"
-local Bbcode = require "site:/lib/Bbcode.luan"
-local bbcode_preprocess = Bbcode.preprocess or error()
-
-
-return function()
-	local user = User.current_required()
-	if user==nil then return end
-	local post_id = Http.request.parameters.post or error()
-	local post = Post.get_by_id(post_id) or error()
-	if Http.request.method == "POST" then
-		local content = Http.request.parameters.content or error()
-		content = bbcode_preprocess(content)
-		post.content = content
-		post.save()
-		Http.response.send_redirect("/thread.html?root="..post.root_id)
-		return
-	end
-	Io.stdout = Http.response.text_writer()
-%>
-<!doctype html>
-<html>
-	<head>
-<%		head() %>
-		<title><%=forum_title%>: edit</title>
-	</head>
-	<body>
-<%		header() %>
-		<div content>
-			<h1>edit: <%=post.root.subject_html%></h1>
-			<p>This page will almost certainly be replaced by ajax, but whatever.  Hack for now.</p>
-			<form method=post>
-				<p><textarea name=content><%=html_encode(post.content)%></textarea></p>
-				<p><input type=submit></p>
-			</form>
-		</div>
-<%		footer() %>
-	</body>
-</html>
-<%
-end
--- a/src/lib/Bbcode.luan	Tue Nov 08 14:02:28 2022 -0700
+++ b/src/lib/Bbcode.luan	Wed Nov 09 23:05:01 2022 -0700
@@ -298,6 +298,7 @@
 	end
 end
 
+-- url handling
 function Bbcode.preprocess(bbcode)
 	bbcode = bbcode_parse(bbcode)
 	return output_of(function()
--- a/src/lib/Post.luan	Tue Nov 08 14:02:28 2022 -0700
+++ b/src/lib/Post.luan	Wed Nov 09 23:05:01 2022 -0700
@@ -2,6 +2,8 @@
 local error = Luan.error
 local ipairs = Luan.ipairs or error()
 local set_metatable = Luan.set_metatable or error()
+local set_local_only = Luan.set_local_only or error()
+local get_local_only = Luan.get_local_only or error()
 local Time = require "luan:Time.luan"
 local time_now = Time.now or error()
 local Html = require "luan:Html.luan"
@@ -36,9 +38,11 @@
 	}
 }
 
+local posts_by_id = {}
+
 local function from_doc(doc)
 	doc.type == "post" or error "wrong type"
-	return Post.new {
+	local post = Post.new {
 		id = doc.id
 		content = doc.content
 		date = doc.date
@@ -49,6 +53,8 @@
 		-- root only
 		subject = doc.subject
 	}
+	set_local_only(posts_by_id,post.id,post)
+	return post
 end
 
 local function to_doc(post)
@@ -123,6 +129,8 @@
 end
 
 function Post.get_by_id(id)
+	local post = get_local_only(posts_by_id,id)
+	if post ~= nil then return post end
 	local doc = Db.get_document("id:"..id)
 	return doc and from_doc(doc)
 end
--- a/src/lib/User.luan	Tue Nov 08 14:02:28 2022 -0700
+++ b/src/lib/User.luan	Wed Nov 09 23:05:01 2022 -0700
@@ -2,6 +2,8 @@
 local error = Luan.error
 local set_metatable = Luan.set_metatable or error()
 local range = Luan.range or error()
+local set_local_only = Luan.set_local_only or error()
+local get_local_only = Luan.get_local_only or error()
 local String = require "luan:String.luan"
 local sub_string = String.sub or error()
 local Table = require "luan:Table.luan"
@@ -20,15 +22,19 @@
 
 local User = {}
 
+local users_by_name = {}
+
 local function from_doc(doc)
 	doc.type == "user" or error "wrong type"
-	return User.new {
+	local user = User.new {
 		id = doc.id
 		email = doc.user_email
 		password = doc.password
 		name = doc.user_name
 		created = doc.created
 	}
+	set_local_only(users_by_name,user.name,user)
+	return user
 end
 
 local function to_doc(user)
@@ -70,6 +76,8 @@
 end
 
 local function get_by_name(name)
+	local user = get_local_only(users_by_name,name)
+	if user ~= nil then return user end
 	local doc = Db.get_document("user_name:"..lucene_quote(name))
 	return doc and from_doc(doc)
 end
--- a/src/reply.html.luan	Tue Nov 08 14:02:28 2022 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,51 +0,0 @@
-local Luan = require "luan:Luan.luan"
-local error = Luan.error
-local Io = require "luan:Io.luan"
-local Http = require "luan:http/Http.luan"
-local Shared = require "site:/lib/Shared.luan"
-local head = Shared.head or error()
-local header = Shared.header or error()
-local footer = Shared.footer or error()
-local Forum = require "site:/lib/Forum.luan"
-local forum_title = Forum.title or error()
-local Post = require "site:/lib/Post.luan"
-local User = require "site:/lib/User.luan"
-local Bbcode = require "site:/lib/Bbcode.luan"
-local bbcode_preprocess = Bbcode.preprocess or error()
-
-
-return function()
-	local user = User.current_required()
-	if user==nil then return end
-	local parent_id = Http.request.parameters.parent or error()
-	local parent = Post.get_by_id(parent_id) or error()
-	if Http.request.method == "POST" then
-		local content = Http.request.parameters.content or error()
-		content = bbcode_preprocess(content)
-		local post = parent.reply(user,content)
-		Http.response.send_redirect("/thread.html?root="..post.root_id)
-		return
-	end
-	Io.stdout = Http.response.text_writer()
-%>
-<!doctype html>
-<html>
-	<head>
-<%		head() %>
-		<title><%=forum_title%>: reply</title>
-	</head>
-	<body>
-<%		header() %>
-		<div content>
-			<h1>reply to: <%=parent.root.subject_html%></h1>
-			<p>This page will almost certainly be replaced by ajax, but whatever.  Hack for now.</p>
-			<form method=post>
-				<p><textarea name=content></textarea></p>
-				<p><input type=submit></p>
-			</form>
-		</div>
-<%		footer() %>
-	</body>
-</html>
-<%
-end