Mercurial Hosting > freedit
comparison src/lib/Post.luan @ 9:9674275019bb
reply and edit
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Thu, 30 Jun 2022 00:02:28 -0600 |
| parents | be36282b556a |
| children | de0cbf515ef5 |
comparison
equal
deleted
inserted
replaced
| 8:be36282b556a | 9:9674275019bb |
|---|---|
| 1 local Luan = require "luan:Luan.luan" | 1 local Luan = require "luan:Luan.luan" |
| 2 local error = Luan.error | 2 local error = Luan.error |
| 3 local ipairs = Luan.ipairs or error() | |
| 3 local set_metatable = Luan.set_metatable or error() | 4 local set_metatable = Luan.set_metatable or error() |
| 4 local Number = require "luan:Number.luan" | 5 local Number = require "luan:Number.luan" |
| 5 local long = Number.long or error() | 6 local long = Number.long or error() |
| 6 local Time = require "luan:Time.luan" | 7 local Time = require "luan:Time.luan" |
| 7 local time_now = Time.now or error() | 8 local time_now = Time.now or error() |
| 9 local Html = require "luan:Html.luan" | |
| 10 local html_encode = Html.encode or error() | |
| 8 local Db = require "site:/lib/Db.luan" | 11 local Db = require "site:/lib/Db.luan" |
| 9 | 12 |
| 10 | 13 |
| 11 local Post = {} | 14 local Post = {} |
| 12 | 15 |
| 13 local function from_doc(doc) | 16 local function from_doc(doc) |
| 14 doc.type == "post" or error "wrong type" | 17 doc.type == "post" or error "wrong type" |
| 15 return Post.new { | 18 return Post.new { |
| 16 id = doc.id | 19 id = doc.id |
| 17 subject = doc.subject | |
| 18 content = doc.content | 20 content = doc.content |
| 19 date = doc.date | 21 date = doc.date |
| 20 author_name = doc.post_author_name | 22 author_name = doc.post_author_name |
| 21 author_id = doc.post_author_id | 23 author_id = doc.post_author_id |
| 24 root_id = doc.post_root_id | |
| 25 | |
| 26 -- root only | |
| 27 subject = doc.subject | |
| 22 is_root = doc.post_is_root == "true" | 28 is_root = doc.post_is_root == "true" |
| 23 root_id = doc.post_root_id | 29 |
| 30 -- replies only | |
| 31 parent_id = doc.parent_id | |
| 24 } | 32 } |
| 25 end | 33 end |
| 26 | 34 |
| 27 local function to_doc(post) | 35 local function to_doc(post) |
| 28 return { | 36 return { |
| 29 type = "post" | 37 type = "post" |
| 30 id = post.id | 38 id = post.id |
| 31 subject = post.subject or error() | |
| 32 content = post.content or error() | 39 content = post.content or error() |
| 33 date = post.date or time_now() | 40 date = post.date or time_now() |
| 34 post_author_name = post.author_name or error() | 41 post_author_name = post.author_name or error() |
| 35 post_author_id = long(post.author_id) | 42 post_author_id = long(post.author_id) |
| 43 post_root_id = post.root_id and long(post.root_id) | |
| 44 | |
| 45 -- root only | |
| 46 subject = post.subject | |
| 36 post_is_root = post.is_root and "true" or nil | 47 post_is_root = post.is_root and "true" or nil |
| 37 post_root_id = post.root_id and long(post.root_id) | 48 |
| 49 -- replies only | |
| 50 parent_id = post.parent_id | |
| 38 } | 51 } |
| 39 end | 52 end |
| 40 | 53 |
| 41 local metatable = {} | 54 local metatable = {} |
| 42 function metatable.__index(post,key) | 55 function metatable.__index(post,key) |
| 43 if key == "author" then | 56 if key == "author" then |
| 44 local User = require "site:/lib/User.luan" | 57 local User = require "site:/lib/User.luan" |
| 45 return User.get_by_id(post.author_id) | 58 return User.get_by_id(post.author_id) |
| 59 end | |
| 60 if key == "subject_html" then | |
| 61 return post.subject and html_encode(post.subject) | |
| 62 end | |
| 63 if key == "root" then | |
| 64 return post.is_root and post or Post.get_by_id(post.root_id) | |
| 46 end | 65 end |
| 47 return nil | 66 return nil |
| 48 end | 67 end |
| 49 | 68 |
| 50 function Post.new(post) | 69 function Post.new(post) |
| 53 local doc = to_doc(post) | 72 local doc = to_doc(post) |
| 54 Db.save(doc) | 73 Db.save(doc) |
| 55 post.id = doc.id | 74 post.id = doc.id |
| 56 end | 75 end |
| 57 | 76 |
| 77 function post.reply(author,content) | |
| 78 return Db.run_in_transaction( function() | |
| 79 local post = Post.new{ | |
| 80 root_id = post.root_id | |
| 81 parent_id = post.id | |
| 82 content = content | |
| 83 author_name = author.name | |
| 84 author_id = author.id | |
| 85 } | |
| 86 post.save() | |
| 87 return post | |
| 88 end ) | |
| 89 end | |
| 90 | |
| 58 set_metatable(post,metatable) | 91 set_metatable(post,metatable) |
| 59 return post | 92 return post |
| 60 end | 93 end |
| 61 | 94 |
| 62 function Post.get_by_id(id) | 95 function Post.get_by_id(id) |
| 63 local doc = Db.get_document("id:"..id) | 96 local doc = Db.get_document("id:"..id) |
| 64 return doc and Post.from_doc(doc) | 97 return doc and from_doc(doc) |
| 65 end | 98 end |
| 66 | 99 |
| 67 function Post.new_thread(author,subject,content) | 100 function Post.new_thread(author,subject,content) |
| 68 return Db.run_in_transaction( function() | 101 return Db.run_in_transaction( function() |
| 69 local post = Post.new{ | 102 local post = Post.new{ |
| 70 subject = subject | 103 subject = subject or error() |
| 71 content = content | 104 content = content |
| 72 author_name = author.name | 105 author_name = author.name |
| 73 author_id = author.id | 106 author_id = author.id |
| 74 is_root = true | 107 is_root = true |
| 75 } | 108 } |
| 78 post.save() | 111 post.save() |
| 79 return post | 112 return post |
| 80 end ) | 113 end ) |
| 81 end | 114 end |
| 82 | 115 |
| 116 function Post.from_docs(docs) | |
| 117 local posts = {} | |
| 118 for _, doc in ipairs(docs) do | |
| 119 local post = from_doc(doc) | |
| 120 posts[#posts+1] = post | |
| 121 end | |
| 122 return posts | |
| 123 end | |
| 124 | |
| 83 return Post | 125 return Post |
