From 087dbfd60a45aae1bfdcc21082865c1c105a6cc2 Mon Sep 17 00:00:00 2001 From: Federico Ceratto Date: Sun, 21 Jun 2015 17:13:00 +0100 Subject: [PATCH 1/2] Implement basic mailing list mirroring #57 --- forum.nim | 9 +++++++++ utils.nim | 13 +++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/forum.nim b/forum.nim index dbf3d8f..fbb0011 100644 --- a/forum.nim +++ b/forum.nim @@ -483,6 +483,7 @@ template setPreviewData(c: expr) {.immediate, dirty.} = c.currentPost.content = content template writeToDb(c, cr, setPostId: expr) = + # insert a comment in the DB let retID = insertID(db, crud(cr, "post", "author", "ip", "header", "content", "thread"), c.userId, c.req.ip, subject, content, $c.threadId, "") discard tryExec(db, crud(cr, "post_fts", "id", "header", "content"), @@ -490,6 +491,10 @@ template writeToDb(c, cr, setPostId: expr) = if setPostId: c.postId = retID.int +template sendToMailingList(c) = + # send comment to a mailing list (if configured to do so) + discard sendMailToMailingList(c.config, c.username, c.email, subject, content) + proc edit(c: var TForumData, postId: int): bool = checkLogin(c) if c.isPreview: @@ -530,6 +535,7 @@ proc edit(c: var TForumData, postId: int): bool = result = true proc reply(c: var TForumData): bool = + # reply to an existing thread checkLogin(c) retrPost(c) if c.isPreview: @@ -539,9 +545,11 @@ proc reply(c: var TForumData): bool = exec(db, sql"update thread set modified = DATETIME('now') where id = ?", $c.threadId) + sendToMailingList(c) result = true proc newThread(c: var TForumData): bool = + # create new conversation thread (permanent or transient) const query = sql"insert into thread(name, views, modified) values (?, 0, DATETIME('now'))" checkLogin(c) retrPost(c) @@ -556,6 +564,7 @@ proc newThread(c: var TForumData): bool = writeToDb(c, crCreate, false) discard tryExec(db, sql"insert into post_fts(post_fts) values('optimize')") discard tryExec(db, sql"insert into post_fts(thread_fts) values('optimize')") + sendToMailingList(c) result = true proc login(c: var TForumData, name, pass: string): bool = diff --git a/utils.nim b/utils.nim index 4d3e1c5..44a8de4 100644 --- a/utils.nim +++ b/utils.nim @@ -6,6 +6,7 @@ type smtpPort: int smtpUser: string smtpPassword: string + mlistAddress: string proc loadConfig*(filename = getCurrentDir() / "forum.json"): Config = result = Config(smtpAddress: "localhost", smtpPort: 25, smtpUser: "", @@ -16,10 +17,11 @@ proc loadConfig*(filename = getCurrentDir() / "forum.json"): Config = result.smtpPort = root["smtpPort"].getNum(25).int result.smtpUser = root["smtpUser"].getStr("") result.smtpPassword = root["smtpPassword"].getStr("") + result.mlistAddress = root["mlistAddress"].getStr("") except: echo("[WARNING] Couldn't read config file: ./forum.json") -proc sendMail(config: Config, subject, message, recipient: string) {.async.} = +proc sendMail(config: Config, subject, message, recipient: string, from_addr = "forum@nim-lang.org") {.async.} = var client = newAsyncSmtp(config.smtpAddress, Port(config.smtpPort)) await client.connect() if config.smtpUser.len > 0: @@ -29,9 +31,16 @@ proc sendMail(config: Config, subject, message, recipient: string) {.async.} = let encoded = createMessage(subject, message, toList, @[], []) - await client.sendMail("forum@nim-lang.org", toList, + await client.sendMail(from_addr, toList, $encoded) +proc sendMailToMailingList*(config: Config, username, user_email_addr, subject, message: string) {.async.} = + # send message to a mailing list + let from_addr = "$# <$#>" % [username, user_email_addr] + + if config.mlistAddress != "": + await sendMail(config, subject, message, config.mlistAddress, from_addr=from_addr) + proc sendPassReset*(config: Config, email, user, resetUrl: string) {.async.} = let message = """Hello $1, A password reset has been requested for your account on the Nim Forum. From 633435a2fe9012d0281a4f3a3b158de61de7e3f0 Mon Sep 17 00:00:00 2001 From: Dominik Picheta Date: Sun, 23 Aug 2015 22:38:57 +0100 Subject: [PATCH 2/2] Added asyncCheck to discarded future and moved out of template. --- forum.nim | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/forum.nim b/forum.nim index fbb0011..c5c8f13 100644 --- a/forum.nim +++ b/forum.nim @@ -491,10 +491,6 @@ template writeToDb(c, cr, setPostId: expr) = if setPostId: c.postId = retID.int -template sendToMailingList(c) = - # send comment to a mailing list (if configured to do so) - discard sendMailToMailingList(c.config, c.username, c.email, subject, content) - proc edit(c: var TForumData, postId: int): bool = checkLogin(c) if c.isPreview: @@ -545,7 +541,8 @@ proc reply(c: var TForumData): bool = exec(db, sql"update thread set modified = DATETIME('now') where id = ?", $c.threadId) - sendToMailingList(c) + asyncCheck sendMailToMailingList(c.config, c.username, c.email, + subject, content) result = true proc newThread(c: var TForumData): bool = @@ -564,7 +561,8 @@ proc newThread(c: var TForumData): bool = writeToDb(c, crCreate, false) discard tryExec(db, sql"insert into post_fts(post_fts) values('optimize')") discard tryExec(db, sql"insert into post_fts(thread_fts) values('optimize')") - sendToMailingList(c) + asyncCheck sendMailToMailingList(c.config, c.username, c.email, + subject, content) result = true proc login(c: var TForumData, name, pass: string): bool =