Exemple #1
0
func AdminComments(context *GoInk.Context) {
	if context.Method == "DELETE" {
		id := context.Int("id")
		cmt := model.GetCommentById(id)
		model.RemoveComment(cmt.Cid, id)
		Json(context, true).End()
		context.Do("comment_delete", id)
		return
	}
	if context.Method == "PUT" {
		id := context.Int("id")
		cmt2 := model.GetCommentById(id)
		cmt2.Status = "approved"
		cmt2.GetReader().Active = true
		model.SaveComment(cmt2)
		Json(context, true).End()
		context.Do("comment_change_status", cmt2)
		return
	}
	if context.Method == "POST" {
		// get required data
		pid := context.Int("pid")
		cid := model.GetCommentById(pid).Cid
		uid, _ := strconv.Atoi(context.Cookie("token-user"))
		user := model.GetUserById(uid)

		co := new(model.Comment)
		co.Author = user.Nick
		co.Email = user.Email
		co.Url = user.Url
		co.Content = context.String("content")
		co.Avatar = utils.Gravatar(co.Email, "50")
		co.Pid = pid
		co.Ip = context.Ip
		co.UserAgent = context.UserAgent
		co.IsAdmin = true
		model.CreateComment(cid, co)
		Json(context, true).Set("comment", co.ToJson()).End()
		model.CreateMessage("comment", co)
		context.Do("comment_reply", co)
		return
	}
	page := context.IntOr("page", 1)
	comments, pager := model.GetCommentList(page, 10)
	context.Layout("admin/admin")
	context.Render("admin/comments", map[string]interface{}{
		"Title":    "评论",
		"Comments": comments,
		"Pager":    pager,
	})
}
Exemple #2
0
func (p *EmailPlugin) sendEmail(co *model.Comment, isCreate bool) {
	var (
		tpl     *template.Template
		buff    bytes.Buffer
		pco     *model.Comment
		content *model.Content
		err     error
		user    *model.User
		title   string
		from    mail.Address
		to      mail.Address
	)

	// get article or page content instance
	content = model.GetContentById(co.Cid)
	if content == nil {
		println("error content getting in email plugin")
		return
	}
	// email for notify new commment creation
	if co.Pid < 1 {
		tpl = p.templates["created"]

		err = tpl.Execute(&buff, map[string]interface{}{
			"link":      model.GetSetting("site_url"),
			"site":      model.GetSetting("site_title"),
			"author":    co.Author,
			"text":      template.HTML(co.Content),
			"title":     content.Title,
			"permalink": path.Join(model.GetSetting("site_url"), content.Link()),
		})
		if err != nil {
			fmt.Println(err)
			return
		}
		user = model.GetUsersByRole("ADMIN")[0]
		from = mail.Address{"no-reply@" + model.GetSetting("site_url"), p.settings["smtp_email_user"]}
		to = mail.Address{user.Nick, user.Email}
		title = co.Author + "在您的网站发表新评论"
		p.sendSmtp(from, to, title, buff.Bytes())
		return
	}
	// send mail for the author of comment replying to
	pco = model.GetCommentById(co.Pid)
	tpl = p.templates["reply"]
	err = tpl.Execute(&buff, map[string]interface{}{
		"link":      model.GetSetting("site_url"),
		"site":      model.GetSetting("site_title"),
		"author_p":  pco.Author,
		"text_p":    template.HTML(pco.Content),
		"author":    co.Author,
		"text":      template.HTML(co.Content),
		"title":     content.Title,
		"permalink": path.Join(model.GetSetting("site_url"), content.Link()),
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	user = model.GetUsersByRole("ADMIN")[0]

	from = mail.Address{pco.Author + "@" + model.GetSetting("site_url"), p.settings["smtp_email_user"]}
	to = mail.Address{pco.Author, pco.Email}
	title = "您的评论有了回复"
	p.sendSmtp(from, to, title, buff.Bytes())
	// send email to notice admin new comment creation
	// this comment is a reply comment
	if isCreate {
		go func() {
			tpl = p.templates["created"]

			err = tpl.Execute(&buff, map[string]interface{}{
				"link":      model.GetSetting("site_url"),
				"site":      model.GetSetting("site_title"),
				"author":    co.Author,
				"text":      template.HTML("回复" + pco.Author + ":<br/>" + co.Content),
				"title":     content.Title,
				"permalink": path.Join(model.GetSetting("site_url"), content.Link()),
			})
			if err != nil {
				fmt.Println(err)
				return
			}
			user = model.GetUsersByRole("ADMIN")[0]
			from = mail.Address{"no-reply@" + model.GetSetting("site_url"), p.settings["smtp_email_user"]}
			to = mail.Address{user.Nick, user.Email}
			title = co.Author + "在您的网站发表新评论"
			p.sendSmtp(from, to, title, buff.Bytes())
		}()
	}
}