func commentHandler(w http.ResponseWriter, req *http.Request, ctx *Context) error { refURL := httputil.ExtractReferer(req) postID, err := ctx.Db.postID(refURL) if err != nil { return logger.LogIff(err, "ctx.Db.postID('%s') failed", refURL) } commenter := prepareCommenter(req) body := req.FormValue("text") commenterID, err := ctx.Db.commenterID(commenter) commentURL := "" switch err { case nil: // This is a returning commenter, pass his comment through: commentURL, err = PublishComment(ctx.Db, postID, commenterID, body) case gorm.ErrRecordNotFound: if !captchaNewCommenter(w, req, ctx) { return nil } commentURL, err = PublishCommentAndCommenter(ctx.Db, postID, commenter, body) default: logger.LogIf(err) return WrongCaptchaReply(w, req, "rejected", ctx.Captcha.NextTask()) } if err != nil { return err } redir := "/" + refURL + commentURL sendNewCommentNotif(req, redir, commenter) return RightCaptchaReply(w, redir) }
func sendNewCommentNotif(req *http.Request, redir string, commenter *Commenter) { if !conf.Notifications.SendEmail { return } refURL := httputil.ExtractReferer(req) url := httputil.GetHost(req) + redir text := req.FormValue("text") subj, body := mkCommentNotifEmail(commenter, text, url, refURL) go sendEmail(subj, body) }