Implement basic mailing list mirroring #57
This commit is contained in:
parent
8a22674852
commit
087dbfd60a
2 changed files with 20 additions and 2 deletions
|
|
@ -483,6 +483,7 @@ template setPreviewData(c: expr) {.immediate, dirty.} =
|
||||||
c.currentPost.content = content
|
c.currentPost.content = content
|
||||||
|
|
||||||
template writeToDb(c, cr, setPostId: expr) =
|
template writeToDb(c, cr, setPostId: expr) =
|
||||||
|
# insert a comment in the DB
|
||||||
let retID = insertID(db, crud(cr, "post", "author", "ip", "header", "content", "thread"),
|
let retID = insertID(db, crud(cr, "post", "author", "ip", "header", "content", "thread"),
|
||||||
c.userId, c.req.ip, subject, content, $c.threadId, "")
|
c.userId, c.req.ip, subject, content, $c.threadId, "")
|
||||||
discard tryExec(db, crud(cr, "post_fts", "id", "header", "content"),
|
discard tryExec(db, crud(cr, "post_fts", "id", "header", "content"),
|
||||||
|
|
@ -490,6 +491,10 @@ template writeToDb(c, cr, setPostId: expr) =
|
||||||
if setPostId:
|
if setPostId:
|
||||||
c.postId = retID.int
|
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 =
|
proc edit(c: var TForumData, postId: int): bool =
|
||||||
checkLogin(c)
|
checkLogin(c)
|
||||||
if c.isPreview:
|
if c.isPreview:
|
||||||
|
|
@ -530,6 +535,7 @@ proc edit(c: var TForumData, postId: int): bool =
|
||||||
result = true
|
result = true
|
||||||
|
|
||||||
proc reply(c: var TForumData): bool =
|
proc reply(c: var TForumData): bool =
|
||||||
|
# reply to an existing thread
|
||||||
checkLogin(c)
|
checkLogin(c)
|
||||||
retrPost(c)
|
retrPost(c)
|
||||||
if c.isPreview:
|
if c.isPreview:
|
||||||
|
|
@ -539,9 +545,11 @@ proc reply(c: var TForumData): bool =
|
||||||
|
|
||||||
exec(db, sql"update thread set modified = DATETIME('now') where id = ?",
|
exec(db, sql"update thread set modified = DATETIME('now') where id = ?",
|
||||||
$c.threadId)
|
$c.threadId)
|
||||||
|
sendToMailingList(c)
|
||||||
result = true
|
result = true
|
||||||
|
|
||||||
proc newThread(c: var TForumData): bool =
|
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'))"
|
const query = sql"insert into thread(name, views, modified) values (?, 0, DATETIME('now'))"
|
||||||
checkLogin(c)
|
checkLogin(c)
|
||||||
retrPost(c)
|
retrPost(c)
|
||||||
|
|
@ -556,6 +564,7 @@ proc newThread(c: var TForumData): bool =
|
||||||
writeToDb(c, crCreate, false)
|
writeToDb(c, crCreate, false)
|
||||||
discard tryExec(db, sql"insert into post_fts(post_fts) values('optimize')")
|
discard tryExec(db, sql"insert into post_fts(post_fts) values('optimize')")
|
||||||
discard tryExec(db, sql"insert into post_fts(thread_fts) values('optimize')")
|
discard tryExec(db, sql"insert into post_fts(thread_fts) values('optimize')")
|
||||||
|
sendToMailingList(c)
|
||||||
result = true
|
result = true
|
||||||
|
|
||||||
proc login(c: var TForumData, name, pass: string): bool =
|
proc login(c: var TForumData, name, pass: string): bool =
|
||||||
|
|
|
||||||
13
utils.nim
13
utils.nim
|
|
@ -6,6 +6,7 @@ type
|
||||||
smtpPort: int
|
smtpPort: int
|
||||||
smtpUser: string
|
smtpUser: string
|
||||||
smtpPassword: string
|
smtpPassword: string
|
||||||
|
mlistAddress: string
|
||||||
|
|
||||||
proc loadConfig*(filename = getCurrentDir() / "forum.json"): Config =
|
proc loadConfig*(filename = getCurrentDir() / "forum.json"): Config =
|
||||||
result = Config(smtpAddress: "localhost", smtpPort: 25, smtpUser: "",
|
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.smtpPort = root["smtpPort"].getNum(25).int
|
||||||
result.smtpUser = root["smtpUser"].getStr("")
|
result.smtpUser = root["smtpUser"].getStr("")
|
||||||
result.smtpPassword = root["smtpPassword"].getStr("")
|
result.smtpPassword = root["smtpPassword"].getStr("")
|
||||||
|
result.mlistAddress = root["mlistAddress"].getStr("")
|
||||||
except:
|
except:
|
||||||
echo("[WARNING] Couldn't read config file: ./forum.json")
|
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))
|
var client = newAsyncSmtp(config.smtpAddress, Port(config.smtpPort))
|
||||||
await client.connect()
|
await client.connect()
|
||||||
if config.smtpUser.len > 0:
|
if config.smtpUser.len > 0:
|
||||||
|
|
@ -29,9 +31,16 @@ proc sendMail(config: Config, subject, message, recipient: string) {.async.} =
|
||||||
let encoded = createMessage(subject, message,
|
let encoded = createMessage(subject, message,
|
||||||
toList, @[], [])
|
toList, @[], [])
|
||||||
|
|
||||||
await client.sendMail("forum@nim-lang.org", toList,
|
await client.sendMail(from_addr, toList,
|
||||||
$encoded)
|
$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.} =
|
proc sendPassReset*(config: Config, email, user, resetUrl: string) {.async.} =
|
||||||
let message = """Hello $1,
|
let message = """Hello $1,
|
||||||
A password reset has been requested for your account on the Nim Forum.
|
A password reset has been requested for your account on the Nim Forum.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue