func UpdateCommentContent(ctx *context.Context) { comment, err := models.GetCommentByID(ctx.ParamsInt64(":id")) if err != nil { ctx.HandleError("GetCommentByID", models.IsErrCommentNotExist, err, 404) return } if !ctx.IsSigned || (ctx.User.ID != comment.PosterID && !ctx.Repo.IsAdmin()) { ctx.Error(403) return } else if comment.Type != models.COMMENT_TYPE_COMMENT { ctx.Error(204) return } comment.Content = ctx.Query("content") if len(comment.Content) == 0 { ctx.JSON(200, map[string]interface{}{ "content": "", }) return } if err = models.UpdateComment(comment); err != nil { ctx.Handle(500, "UpdateComment", err) return } ctx.JSON(200, map[string]interface{}{ "content": string(markdown.Render([]byte(comment.Content), ctx.Query("context"), ctx.Repo.Repository.ComposeMetas())), }) }
func DeleteComment(ctx *context.Context) { comment, err := models.GetCommentByID(ctx.ParamsInt64(":id")) if err != nil { ctx.HandleError("GetCommentByID", models.IsErrCommentNotExist, err, 404) return } if !ctx.IsSigned || (ctx.User.ID != comment.PosterID && !ctx.Repo.IsAdmin()) { ctx.Error(403) return } else if comment.Type != models.COMMENT_TYPE_COMMENT { ctx.Error(204) return } if err = models.DeleteCommentByID(comment.ID); err != nil { ctx.Handle(500, "DeleteCommentByID", err) return } ctx.Status(200) }
func NewComment(ctx *context.Context, form auth.CreateCommentForm) { issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) if err != nil { ctx.HandleError("GetIssueByIndex", models.IsErrIssueNotExist, err, 404) return } if issue.IsPull { if err = issue.GetPullRequest(); err != nil { ctx.Handle(500, "GetPullRequest", err) return } } var attachments []string if setting.AttachmentEnabled { attachments = form.Attachments } if ctx.HasError() { ctx.Flash.Error(ctx.Data["ErrorMsg"].(string)) ctx.Redirect(fmt.Sprintf("%s/issues/%d", ctx.Repo.RepoLink, issue.Index)) return } var comment *models.Comment defer func() { // Check if issue admin/poster changes the status of issue. if (ctx.Repo.IsWriter() || (ctx.IsSigned && issue.IsPoster(ctx.User.ID))) && (form.Status == "reopen" || form.Status == "close") && !(issue.IsPull && issue.HasMerged) { // Duplication and conflict check should apply to reopen pull request. var pr *models.PullRequest if form.Status == "reopen" && issue.IsPull { pull := issue.PullRequest pr, err = models.GetUnmergedPullRequest(pull.HeadRepoID, pull.BaseRepoID, pull.HeadBranch, pull.BaseBranch) if err != nil { if !models.IsErrPullRequestNotExist(err) { ctx.Handle(500, "GetUnmergedPullRequest", err) return } } // Regenerate patch and test conflict. if pr == nil { if err = issue.UpdatePatch(); err != nil { ctx.Handle(500, "UpdatePatch", err) return } issue.AddToTaskQueue() } } if pr != nil { ctx.Flash.Info(ctx.Tr("repo.pulls.open_unmerged_pull_exists", pr.Index)) } else { if err = issue.ChangeStatus(ctx.User, ctx.Repo.Repository, form.Status == "close"); err != nil { log.Error(4, "ChangeStatus: %v", err) } else { log.Trace("Issue [%d] status changed to closed: %v", issue.ID, issue.IsClosed) } } } // Redirect to comment hashtag if there is any actual content. typeName := "issues" if issue.IsPull { typeName = "pulls" } if comment != nil { ctx.Redirect(fmt.Sprintf("%s/%s/%d#%s", ctx.Repo.RepoLink, typeName, issue.Index, comment.HashTag())) } else { ctx.Redirect(fmt.Sprintf("%s/%s/%d", ctx.Repo.RepoLink, typeName, issue.Index)) } }() // Fix #321: Allow empty comments, as long as we have attachments. if len(form.Content) == 0 && len(attachments) == 0 { return } comment, err = models.CreateIssueComment(ctx.User, ctx.Repo.Repository, issue, form.Content, attachments) if err != nil { ctx.Handle(500, "CreateIssueComment", err) return } log.Trace("Comment created: %d/%d/%d", ctx.Repo.Repository.ID, issue.ID, comment.ID) }