2020-09-22 16:18:12 +02:00
|
|
|
BaseController:subclass("CommentController",
|
|
|
|
{registry = {}, models = {"comment", "pages"}})
|
|
|
|
|
|
|
|
local function process_md(input)
|
|
|
|
local md = require("md")
|
|
|
|
local content = ""
|
|
|
|
local callback = function(s) content = content .. s end
|
|
|
|
md.to_html(input, callback)
|
|
|
|
return content
|
|
|
|
end
|
|
|
|
|
|
|
|
local function sendmail(to, subject, content)
|
2023-04-26 18:51:03 +02:00
|
|
|
LOG_DEBUG("Sending email to %s", to)
|
2020-09-22 16:18:12 +02:00
|
|
|
|
2024-07-26 20:37:14 +02:00
|
|
|
local setting = JSON.decodeFile(SMTP_SETTING)
|
|
|
|
|
|
|
|
local socket = require 'socket'
|
|
|
|
local smtp = require 'socket.smtp'
|
|
|
|
local ssl = require 'ssl'
|
|
|
|
local https = require 'ssl.https'
|
|
|
|
local ltn12 = require 'ltn12'
|
|
|
|
|
|
|
|
local sslCreate = function()
|
|
|
|
local sock = socket.tcp()
|
|
|
|
return setmetatable({
|
|
|
|
connect = function(_, host, port)
|
|
|
|
local r, e = sock:connect(host, port)
|
|
|
|
if not r then return r, e end
|
|
|
|
sock = ssl.wrap(sock, {mode='client', protocol='tlsv1_2'})
|
|
|
|
return sock:dohandshake()
|
|
|
|
end
|
|
|
|
}, {
|
|
|
|
__index = function(t,n)
|
|
|
|
return function(_, ...)
|
|
|
|
return sock[n](sock, ...)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
local setting = JSON.decodeFile(SMTP_SETTING)
|
|
|
|
|
|
|
|
if not setting then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
local from = "contact@iohub.dev"
|
|
|
|
|
|
|
|
local msg = {
|
|
|
|
headers = {
|
|
|
|
from = string.format("QuickTalk <%s>", from),
|
|
|
|
to = string.format("%s <%s>",to,to),
|
|
|
|
subject = subject
|
|
|
|
},
|
|
|
|
body = content
|
|
|
|
}
|
|
|
|
LOG_INFO("Send mail on server %s user %s port %d: %s", setting.server, setting.user, setting.port, JSON.encode(msg))
|
|
|
|
local ok, err = smtp.send {
|
|
|
|
from = string.format("<%s>",from),
|
|
|
|
rcpt = string.format('<%s>', to),
|
|
|
|
source = smtp.message(msg),
|
|
|
|
user = setting.user,
|
|
|
|
password = setting.password,
|
|
|
|
server = setting.server,
|
|
|
|
port = math.floor(setting.port),
|
|
|
|
create = sslCreate
|
|
|
|
}
|
|
|
|
|
|
|
|
if not ok then return false end
|
|
|
|
return true
|
2020-09-22 16:18:12 +02:00
|
|
|
|
|
|
|
end
|
|
|
|
function CommentController:index(...)
|
|
|
|
if (REQUEST.method == "OPTIONS") then
|
|
|
|
result("")
|
|
|
|
return false
|
|
|
|
end
|
2020-09-22 16:51:01 +02:00
|
|
|
if not REQUEST.json then
|
|
|
|
fail("Invalid request")
|
|
|
|
return false
|
|
|
|
end
|
2020-09-22 16:18:12 +02:00
|
|
|
local rq = (JSON.decodeString(REQUEST.json))
|
|
|
|
if (rq) then
|
2023-04-26 18:51:03 +02:00
|
|
|
local pages, order = self.pages:find({where = {uri = rq.page}})
|
2020-09-22 16:18:12 +02:00
|
|
|
if not pages or #order == 0 then
|
|
|
|
fail("Be the first to comment")
|
|
|
|
else
|
|
|
|
local pid = pages[1].id
|
2023-04-26 18:51:03 +02:00
|
|
|
local comments, order = self.comment:find({
|
|
|
|
where = {
|
|
|
|
pid = pid,
|
|
|
|
rid = 0
|
2020-09-22 16:18:12 +02:00
|
|
|
},
|
2023-04-26 18:51:03 +02:00
|
|
|
order = {"time$asc"},
|
2020-09-22 16:18:12 +02:00
|
|
|
fields = {"id", "time", "name", "rid", "pid", "content"}
|
|
|
|
})
|
|
|
|
if not comments or #order == 0 then
|
|
|
|
fail("Be the first to comment")
|
|
|
|
else
|
|
|
|
for idx, v in pairs(order) do
|
|
|
|
local data = comments[v]
|
|
|
|
data.content = process_md(data.content)
|
|
|
|
data.children = {}
|
|
|
|
-- find all the replies to this thread
|
|
|
|
local sub_comments, suborder =
|
2023-05-22 13:53:41 +02:00
|
|
|
self.comment:find({
|
|
|
|
where = {
|
|
|
|
pid = pid,
|
|
|
|
rid = data.id
|
|
|
|
},
|
|
|
|
order = {"time$asc"}
|
2020-09-22 16:18:12 +02:00
|
|
|
|
2023-05-22 13:53:41 +02:00
|
|
|
})
|
2020-09-22 16:18:12 +02:00
|
|
|
if sub_comments and #suborder ~= 0 then
|
|
|
|
for i, subc in pairs(suborder) do
|
|
|
|
sub_comments[subc].content =
|
|
|
|
process_md(sub_comments[subc].content)
|
|
|
|
end
|
|
|
|
data.children = sub_comments
|
|
|
|
end
|
|
|
|
end
|
|
|
|
result(comments)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
fail("Invalid request")
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
function CommentController:post(...)
|
|
|
|
if (REQUEST.method == "OPTIONS") then
|
|
|
|
result("")
|
|
|
|
return false
|
|
|
|
end
|
2020-09-22 16:51:01 +02:00
|
|
|
if not REQUEST.json then
|
|
|
|
fail("Invalid request")
|
|
|
|
return false
|
|
|
|
end
|
2020-09-22 16:18:12 +02:00
|
|
|
local rq = (JSON.decodeString(REQUEST.json))
|
|
|
|
if rq then
|
2023-04-26 18:51:03 +02:00
|
|
|
local pages, order = self.pages:find({where = rq.page})
|
2020-09-22 16:18:12 +02:00
|
|
|
if not pages or #order == 0 then
|
|
|
|
-- insert data
|
|
|
|
if self.pages:create(rq.page) then
|
|
|
|
rq.comment.pid = self.pages.db:lastInsertID()
|
|
|
|
else
|
|
|
|
fail("Unable to initialize comment thread for page: " ..
|
|
|
|
rq.page.uri)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
else
|
|
|
|
rq.comment.pid = pages[1].id
|
|
|
|
end
|
|
|
|
-- now insert the comment
|
|
|
|
rq.comment.time = os.time(os.date("!*t"))
|
|
|
|
if (self.comment:create(rq.comment)) then
|
|
|
|
rq.comment.id = self.comment.db:lastInsertID()
|
|
|
|
|
|
|
|
rq.comment.content = process_md(rq.comment.content)
|
2020-09-22 19:50:59 +02:00
|
|
|
-- notify the author
|
|
|
|
if rq.author then
|
|
|
|
sendmail(rq.author, rq.comment.name ..
|
|
|
|
" has commented on one of your pages",
|
|
|
|
rq.comment.name .. " has commented on your page: " ..
|
|
|
|
rq.page.uri ..
|
|
|
|
".\nBest regards,\nEmail automatically sent by QuickTalk API")
|
|
|
|
end
|
2020-09-22 16:18:12 +02:00
|
|
|
-- send mail to all users of current page
|
2023-04-26 18:51:03 +02:00
|
|
|
local cmts, cmti = self.comment:find(
|
|
|
|
{
|
|
|
|
where = {
|
|
|
|
pid = rq.comment.pid,
|
|
|
|
["email$ne"] = rq.comment.email
|
|
|
|
},
|
|
|
|
fields = {"id", "email"}
|
|
|
|
})
|
|
|
|
-- check duplicate email
|
2020-09-22 16:18:12 +02:00
|
|
|
if cmts and #cmti > 0 then
|
2023-04-26 18:51:03 +02:00
|
|
|
local sent = {}
|
2020-09-22 16:18:12 +02:00
|
|
|
for idx, v in pairs(cmti) do
|
2023-04-26 18:51:03 +02:00
|
|
|
if not sent[cmts[v].email] then
|
|
|
|
sendmail(cmts[v].email, rq.comment.name ..
|
2020-09-22 16:18:12 +02:00
|
|
|
" has written something on a page that you've commented on",
|
|
|
|
rq.comment.name ..
|
|
|
|
" has written something on a page that you've commented. \nPlease visit this page: " ..
|
2023-04-26 18:51:03 +02:00
|
|
|
rq.page.uri..
|
2020-09-22 19:50:59 +02:00
|
|
|
" for updates on the discussion.\nBest regards,\nEmail automatically sent by QuickTalk API")
|
2023-04-26 18:51:03 +02:00
|
|
|
sent[cmts[v].email] = true
|
|
|
|
end
|
2020-09-22 16:18:12 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
rq.comment.email = ""
|
|
|
|
result(rq.comment)
|
|
|
|
else
|
|
|
|
fail("Unable to save comment")
|
|
|
|
end
|
|
|
|
else
|
|
|
|
fail("Invalid request")
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
function CommentController:preview(...)
|
|
|
|
if (REQUEST.method == "OPTIONS") then
|
|
|
|
result("")
|
|
|
|
return false
|
|
|
|
end
|
2020-09-22 16:51:01 +02:00
|
|
|
if not REQUEST.json then
|
|
|
|
fail("Invalid request")
|
|
|
|
return false
|
|
|
|
end
|
2020-09-22 16:18:12 +02:00
|
|
|
local rq = (JSON.decodeString(REQUEST.json))
|
|
|
|
if (rq and rq.data) then
|
|
|
|
result(process_md(rq.data))
|
|
|
|
else
|
|
|
|
fail("Invalid request")
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|