func (this *PostRouter) Post() { if this.CheckActiveRedirect() { return } var postMd models.Post if this.loadPost(&postMd, &this.User) { return } if !postMd.CanEdit { this.FlashRedirect(postMd.Path(), 302, "CanNotEditPost") } form := post.PostForm{} form.SetFromPost(&postMd) models.FindTopics(&form.Topics) if !this.ValidFormSets(&form) { return } if err := form.UpdatePost(&postMd, &this.User); err == nil { this.JsStorage("deleteKey", "post/edit") this.Redirect(postMd.Link()) return } this.Render("post/edit.html", this.Data) }
//New Comment func (this *SinglePost) Post() { if this.CheckActiveRedirect() { return } var postMd models.Post if this.loadPost(&postMd, nil) { return } var redir bool defer func() { if !redir { var comments []*models.Comment this.loadComments(&postMd, &comments) } }() form := post.CommentForm{} if !this.ValidFormSets(&form) { return } comment := models.Comment{} if err := form.SaveComment(&comment, &this.User, &postMd); err == nil { post.FilterCommentMentions(&this.User, &postMd, &comment) this.JsStorage("deleteKey", "post/comment") this.Redirect(postMd.Link(), 302) redir = true post.PostReplysCount(&postMd) } this.Render("post/post.html", this.Data) }
func (form *PostForm) UpdatePost(post *models.Post, user *models.User) error { changes := utils.FormChanges(post, form) if len(changes) == 0 { return nil } utils.SetFormValues(form, post) post.CategoryId = form.Category post.TopicId = form.Topic for _, c := range changes { if c == "Content" { post.ContentCache = utils.RenderMarkdown(form.Content) changes = append(changes, "ContentCache") } } // update last edit author if post.LastAuthorId != user.Id { post.LastAuthorId = user.Id changes = append(changes, "LastAuthor") } changes = append(changes, "Updated") return models.UpdateById(post.Id, post, models.Obj2Table(changes)...) }
func (this *NewPost) Post() error { if this.CheckActiveRedirect() { return nil } var err error form := post.PostForm{Locale: this.Locale} topicSlug := this.GetString("topic") if len(topicSlug) > 0 { topic, err := models.GetTopicBySlug(topicSlug) if err == nil { form.Category = topic.CategoryId form.Topic = topic.Id this.Data["Topic"] = topic } else { log.Error("Can not find topic by slug:", topicSlug) } } else { topicId, err := this.GetInt("Topic") if err == nil { topic, err := models.GetTopicById(topicId) if err == nil { form.Category = topic.CategoryId form.Topic = topic.Id this.Data["Topic"] = topic } else { log.Error("Can not find topic by id:", topicId) } } else { log.Error("Parse param Topic from request failed", err) } } if categorySlug := this.GetString("category"); categorySlug != "" { log.Debug("Find category slug:", categorySlug) category, err := models.GetCategoryBySlug(categorySlug) if err != nil { log.Error("Get category error", err) } this.Data["Category"] = &category } err = models.FindTopics(&form.Topics) if err != nil { return err } if !this.ValidFormSets(&form) { this.Redirect("/new") return nil } var post models.Post if err := form.SavePost(&post, &this.User); err == nil { this.JsStorage("deleteKey", "post/new") this.Redirect(post.Link()) return nil } return this.Render("post/new.html", this.Data) }
func PostReplysCount(post *models.Post) { cnt, err := models.CountCommentsByPostId(post.Id) if err == nil { post.Replys = int(cnt) //disable post editable post.CanEdit = false err = models.UpdateById(post.Id, post, "replys", "can_edit") } if err != nil { log.Error("PostReplysCount ", err) } }
func (form *PostForm) SavePost(post *models.Post, user *models.User) error { utils.SetFormValues(form, post) post.CategoryId = form.Category post.TopicId = form.Topic post.UserId = user.Id post.LastReplyId = user.Id post.LastAuthorId = user.Id post.CanEdit = true post.ContentCache = utils.RenderMarkdown(form.Content) // mentioned follow users FilterMentions(user, post.ContentCache) return post.Insert() }
func (this *EditPost) Get() { if this.CheckActiveRedirect() { return } var postMd models.Post if this.loadPost(&postMd, &this.User) { return } if !postMd.CanEdit { this.Redirect(postMd.Link()) return } form := post.PostForm{} form.SetFromPost(&postMd) models.FindTopics(&form.Topics) this.SetFormSets(&form) this.Render("post/edit.html", this.Data) }
func (form *CommentForm) SaveComment(comment *models.Comment, user *models.User, post *models.Post) error { comment.Message = form.Message comment.MessageCache = utils.RenderMarkdown(form.Message) comment.UserId = user.Id comment.PostId = post.Id if err := models.InsertComment(comment); err == nil { post.LastReplyId = user.Id models.UpdateById(post.Id, post, "last_reply_id", "last_replied") cnt, _ := models.CountCommentsLTEId(comment.Id) comment.Floor = int(cnt) return models.UpdateById(comment.Id, comment, "floor") } else { return err } }
func (form *PostAdminForm) SetToPost(post *models.Post) { utils.SetFormValues(form, post) post.UserId = form.User post.LastReplyId = form.LastReply post.LastAuthorId = form.LastAuthor post.TopicId = form.Topic //get category if topic, err := models.GetTopicById(form.Topic); err == nil { post.CategoryId = topic.CategoryId } post.ContentCache = utils.RenderMarkdown(post.Content) }
func (this *Post) Post() { if this.CheckActiveRedirect() { return } if !this.IsAjax() { return } result := map[string]interface{}{ "success": false, } action := this.GetString("action") switch action { case "toggle-best": if this.User.IsAdmin { if postId, err := this.GetInt("post"); err == nil { //set post best var post models.Post if err := models.GetById(postId, &post); err == nil { post.IsBest = !post.IsBest if models.UpdateById(post.Id, post, "is_best") == nil { result["success"] = true } } } else { this.Logger.Error("post value is not int:", this.GetString("post")) } } case "toggle-fav": if postId, err := this.GetInt("post"); err == nil { var post models.Post if err := models.GetById(postId, &post); err == nil { var favoritePost = models.FavoritePost{ PostId: post.Id, UserId: this.User.Id, } if err := models.GetByExample(&favoritePost); err == nil { //toogle IsFav favoritePost.IsFav = !favoritePost.IsFav if models.UpdateById(favoritePost.Id, favoritePost, "is_fav") == nil { //update user fav post count if favoritePost.IsFav { this.User.FavPosts += 1 } else { this.User.FavPosts -= 1 } if models.UpdateById(this.User.Id, this.User, "fav_posts") == nil { result["success"] = true } } } else if err == models.ErrNotExist { favoritePost = models.FavoritePost{ UserId: this.User.Id, PostId: post.Id, IsFav: true, } if models.Insert(favoritePost) == nil { //update user fav post count this.User.FavPosts += 1 if models.UpdateById(this.User.Id, this.User, "fav_posts") == nil { result["success"] = true } } } else { this.Logger.Error("Get favorite post err:", err) } } } } this.Data["json"] = result this.ServeJson(this.Data) }