Esempio n. 1
0
func Feed(context *GoInk.Context) {
	baseUrl := model.GetSetting("site_url")
	article, _ := model.GetArticleList(1, 20)
	feed := new(feeds.Feed)
	feed.Title = model.GetSetting("site_title")
	feed.Link = &feeds.Link{Href: baseUrl}
	feed.Description = model.GetSetting("site_description")
	author := model.GetUsersByRole("ADMIN")[0]
	feed.Author = &feeds.Author{author.Nick, author.Email}
	feed.Items = make([]*feeds.Item, 0)
	var create int64
	if len(article) > 0 {
		create = article[0].EditTime
	} else {
		create = utils.Now()
	}
	feed.Created = time.Unix(create, 0)
	for _, a := range article {
		item := new(feeds.Item)
		item.Title = a.Title
		item.Link = &feeds.Link{Href: path.Join(baseUrl, a.Link())}
		item.Author = feed.Author
		item.Created = time.Unix(a.CreateTime, 0)
		item.Description = utils.Html2str(a.Summary())
		feed.Items = append(feed.Items, item)
	}
	str, e := feed.ToRss()
	if e != nil {
		panic(e)
	}
	context.ContentType("application/rss+xml;charset=UTF-8")
	context.Body = []byte(str)
}
Esempio n. 2
0
// CreateComment creates a comment and links it to the cid content.
func CreateComment(cid int, c *Comment) {
	commentMaxId += Storage.TimeInc(4)
	c.Id = commentMaxId
	c.CreateTime = utils.Now()
	c.Status = "check"
	c.Cid = cid
	// escape content
	c.Content = strings.Replace(utils.Html2str(template.HTMLEscapeString(c.Content)), "\n", "<br/>", -1)
	// if empty url, use # instead.
	if c.Url == "" {
		c.Url = "#"
	}
	// if admin comment, must be approved.
	if c.IsAdmin {
		c.Status = "approved"
	} else {
		// if common comment, get reader status for checking status.
		r := c.GetReader()
		if r != nil {
			if r.Active {
				c.Status = "approved"
			}
		} else {
			CreateReader(c)
		}
	}
	// update comment memory data
	comments[c.Id] = c
	commentsIndex = append([]int{c.Id}, commentsIndex...)
	// append to content
	content := GetContentById(cid)
	content.Comments = append(content.Comments, c)
	go SyncContent(content)
}
Esempio n. 3
0
func generateCommentMessage(co interface{}) string {
	c, ok := co.(*Comment)
	if !ok {
		return ""
	}
	cnt := GetContentById(c.Cid)
	s := ""
	if c.Pid < 1 {
		s = "<p>" + c.Author + "同学,在文章《" + cnt.Title + "》发表评论:"
		s += utils.Html2str(c.Content) + "</p>"
	} else {
		p := GetCommentById(c.Pid)
		s = "<p>" + p.Author + "同学,在文章《" + cnt.Title + "》的评论:"
		s += utils.Html2str(p.Content) + "</p>"
		s += "<p>" + c.Author + "同学的回复:"
		s += utils.Html2str(c.Content) + "</p>"
	}
	return s
}
Esempio n. 4
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()
		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()
		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 = strings.Replace(utils.Html2str(context.String("content")), "\n", "<br/>", -1)
		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()
		return
	}
	page := context.IntOr("page", 1)
	comments, pager := model.GetCommentList(page, 6)
	context.Layout("admin")
	context.Render("admin/comments", map[string]interface{}{
		"Title":    "评论",
		"Comments": comments,
		"Pager":    pager,
	})
}
Esempio n. 5
0
func Comment(context *GoInk.Context) {
	cid, _ := strconv.Atoi(context.Param("id"))
	if cid < 1 {
		Json(context, false).End()
		return
	}
	if model.GetContentById(cid) == nil {
		Json(context, false).End()
		return
	}
	data := context.Input()
	co := new(model.Comment)
	co.Author = data["user"]
	co.Email = data["email"]
	co.Url = data["url"]
	co.Content = strings.Replace(utils.Html2str(data["content"]), "\n", "<br/>", -1)
	co.Avatar = utils.Gravatar(co.Email, "50")
	co.Pid, _ = strconv.Atoi(data["pid"])
	co.Ip = context.Ip
	co.UserAgent = context.UserAgent
	co.IsAdmin = false
	model.CreateComment(cid, co)
	Json(context, true).Set("comment", co.ToJson()).End()
}