Mercurial Hosting > freedit
changeset 8:be36282b556a
add new_thread
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 29 Jun 2022 00:04:09 -0600 |
parents | 0472897e790d |
children | 9674275019bb |
files | .hgignore src/index.html.luan src/lib/Db.luan src/lib/Post.luan src/lib/User.luan src/new_thread.html.luan |
diffstat | 6 files changed, 151 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/.hgignore Thu Jun 23 23:38:03 2022 -0600 +++ b/.hgignore Wed Jun 29 00:04:09 2022 -0600 @@ -2,3 +2,4 @@ err local/ +*.bck
--- a/src/index.html.luan Thu Jun 23 23:38:03 2022 -0600 +++ b/src/index.html.luan Wed Jun 29 00:04:09 2022 -0600 @@ -22,7 +22,7 @@ <body> <% header() %> <div content> - qqq + <p><a href="/new_thread.html">New Thread</a></p> </div> <% footer() %> </body>
--- a/src/lib/Db.luan Thu Jun 23 23:38:03 2022 -0600 +++ b/src/lib/Db.luan Wed Jun 29 00:04:09 2022 -0600 @@ -12,4 +12,9 @@ Db.indexed_fields.user_email = Lucene.type.lowercase Db.indexed_fields.user_name = Lucene.type.lowercase +Db.indexed_fields.post_author_id = Lucene.type.long +Db.indexed_fields.post_author_name = Lucene.type.string +Db.indexed_fields.post_is_root = Lucene.type.string +Db.indexed_fields.post_root_id = Lucene.type.long + return Db
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lib/Post.luan Wed Jun 29 00:04:09 2022 -0600 @@ -0,0 +1,83 @@ +local Luan = require "luan:Luan.luan" +local error = Luan.error +local set_metatable = Luan.set_metatable or error() +local Number = require "luan:Number.luan" +local long = Number.long or error() +local Time = require "luan:Time.luan" +local time_now = Time.now or error() +local Db = require "site:/lib/Db.luan" + + +local Post = {} + +local function from_doc(doc) + doc.type == "post" or error "wrong type" + return Post.new { + id = doc.id + subject = doc.subject + content = doc.content + date = doc.date + author_name = doc.post_author_name + author_id = doc.post_author_id + is_root = doc.post_is_root == "true" + root_id = doc.post_root_id + } +end + +local function to_doc(post) + return { + type = "post" + id = post.id + subject = post.subject or error() + content = post.content or error() + date = post.date or time_now() + post_author_name = post.author_name or error() + post_author_id = long(post.author_id) + post_is_root = post.is_root and "true" or nil + post_root_id = post.root_id and long(post.root_id) + } +end + +local metatable = {} +function metatable.__index(post,key) + if key == "author" then + local User = require "site:/lib/User.luan" + return User.get_by_id(post.author_id) + end + return nil +end + +function Post.new(post) + + function post.save() + local doc = to_doc(post) + Db.save(doc) + post.id = doc.id + end + + set_metatable(post,metatable) + return post +end + +function Post.get_by_id(id) + local doc = Db.get_document("id:"..id) + return doc and Post.from_doc(doc) +end + +function Post.new_thread(author,subject,content) + return Db.run_in_transaction( function() + local post = Post.new{ + subject = subject + content = content + author_name = author.name + author_id = author.id + is_root = true + } + post.save() + post.root_id = post.id or error() + post.save() + return post + end ) +end + +return Post
--- a/src/lib/User.luan Thu Jun 23 23:38:03 2022 -0600 +++ b/src/lib/User.luan Wed Jun 29 00:04:09 2022 -0600 @@ -64,6 +64,11 @@ return user end +function User.get_by_id(id) + local doc = Db.get_document("id:"..id) + return doc and User.from_doc(doc) +end + function User.get_by_email(email) local doc = Db.get_document("user_email:"..lucene_quote(email)) return doc and from_doc(doc) @@ -88,6 +93,12 @@ return user end +function User.current_required() + local user = User.current() + user or Http.response.send_redirect "/login.html" + return user +end + local password_chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" do local t = {}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/new_thread.html.luan Wed Jun 29 00:04:09 2022 -0600 @@ -0,0 +1,50 @@ +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" + + +return function() + local user = User.current_required() + if user==nil then return end + if Http.request.method == "POST" then + local subject = Http.request.parameters.subject or error() + local content = Http.request.parameters.content or error() + local post = Post.new_thread(user,subject,content) + Http.response.send_redirect("/thread.html?root="..post.id) + return + end + Io.stdout = Http.response.text_writer() +%> +<!doctype html> +<html> + <head> +<% head() %> + <title><%=forum_title%></title> + </head> + <body> +<% header() %> + <div content> + <h1>New Thread</h1> + <form method=post> + <p> + <label>Subject</label> + <input name=subject required> + </p> + <p><textarea name=content></textarea></p> + <p><input type=submit></p> + </form> + </div> +<% footer() %> + </body> +</html> +<% +end