diff --git a/models/issue_comment.go b/models/issue_comment.go index 6c44e74351..8340e04e5e 100644 --- a/models/issue_comment.go +++ b/models/issue_comment.go @@ -8,6 +8,7 @@ import ( "fmt" "strings" + "code.gitea.io/gitea/modules/markup/markdown" "github.com/Unknwon/com" "github.com/go-xorm/builder" "github.com/go-xorm/xorm" @@ -788,3 +789,39 @@ func DeleteComment(comment *Comment) error { } return nil } + +func fetchCodeComments(e Engine, issue *Issue, currentUser *User) (map[string]map[int64][]*Comment, error) { + pathToLineToComment := make(map[string]map[int64][]*Comment) + comments, err := findComments(e, FindCommentsOptions{ + Type: CommentTypeCode, + IssueID: issue.ID, + }) + if err != nil { + return nil, err + } + if err = issue.loadRepo(e); err != nil { + return nil, err + } + for _, comment := range comments { + if err = comment.loadReview(e); err != nil && !IsErrReviewNotExist(err) { + return nil, err + } + if comment.Review != nil && comment.Review.Type == ReviewTypePending { + if currentUser == nil || currentUser.ID != comment.Review.ReviewerID { + continue + } + } + comment.RenderedContent = string(markdown.Render([]byte(comment.Content), issue.Repo.Link(), + issue.Repo.ComposeMetas())) + if pathToLineToComment[comment.TreePath] == nil { + pathToLineToComment[comment.TreePath] = make(map[int64][]*Comment) + } + pathToLineToComment[comment.TreePath][comment.Line] = append(pathToLineToComment[comment.TreePath][comment.Line], comment) + } + return pathToLineToComment, nil +} + +// FetchCodeComments will return a 2d-map: ["Path"]["Line"] = Comments at line +func FetchCodeComments(issue *Issue, currentUser *User) (map[string]map[int64][]*Comment, error) { + return fetchCodeComments(x, issue, currentUser) +} diff --git a/modules/templates/helper.go b/modules/templates/helper.go index bf5c0130b6..e826716b05 100644 --- a/modules/templates/helper.go +++ b/modules/templates/helper.go @@ -15,6 +15,7 @@ import ( "mime" "net/url" "path/filepath" + "reflect" "runtime" "strings" "time" @@ -186,6 +187,36 @@ func NewFuncMap() []template.FuncMap { "ParseDeadline": func(deadline string) []string { return strings.Split(deadline, "|") }, + "mul": func(first int, second int64) int64 { return second * int64(first) }, + "dict": func(values ...interface{}) (map[string]interface{}, error) { + if len(values) == 0 { + return nil, errors.New("invalid dict call") + } + + dict := make(map[string]interface{}) + + for i := 0; i < len(values); i++ { + key, isset := values[i].(string) + if !isset { + if reflect.TypeOf(values[i]).Kind() == reflect.Map { + m := values[i].(map[string]interface{}) + for i, v := range m { + dict[i] = v + } + } else { + return nil, errors.New("dict values must be maps") + } + } else { + i++ + if i == len(values) { + return nil, errors.New("specify the key for non array values") + } + dict[key] = values[i] + } + + } + return dict, nil + }, }} } diff --git a/routers/repo/pull.go b/routers/repo/pull.go index a852cee1f3..3768b3f385 100644 --- a/routers/repo/pull.go +++ b/routers/repo/pull.go @@ -471,6 +471,13 @@ func ViewPullFiles(ctx *context.Context) { ctx.Data["RawPath"] = setting.AppSubURL + "/" + path.Join(headTarget, "raw", "commit", endCommitID) ctx.Data["RequireHighlightJS"] = true + pathToLineToComment, err := models.FetchCodeComments(issue, ctx.User) + if err != nil { + ctx.ServerError("FetchCodeComments", err) + return + } + ctx.Data["CodeComments"] = pathToLineToComment + ctx.HTML(200, tplPullFiles) } diff --git a/templates/repo/diff/box.tmpl b/templates/repo/diff/box.tmpl index 26d6b3a17d..a91a1bb91c 100644 --- a/templates/repo/diff/box.tmpl +++ b/templates/repo/diff/box.tmpl @@ -114,6 +114,48 @@ + {{end}} + {{if index $.CodeComments $file.Name (mul $line.LeftIdx -1)}} + +
+
+ + {{ template "repo/diff/comments" dict "root" $ "comments" (index $.CodeComments $file.Name (mul $line.LeftIdx -1))}} + +
+
+ + + + + + + + + + +
+
+ +
+
+
+ +
+
+ + + {{end}}
{{if $line.RightIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}}
diff --git a/templates/repo/diff/comments.tmpl b/templates/repo/diff/comments.tmpl new file mode 100644 index 0000000000..e5702cfba8 --- /dev/null +++ b/templates/repo/diff/comments.tmpl @@ -0,0 +1,51 @@ +{{range .comments}} + +{{ $createdStr:= TimeSinceUnix .CreatedUnix $.root.Lang }} +
+ + + +
+
+ {{.Poster.Name}} {{$.root.i18n.Tr "repo.issues.commented_at" .HashTag $createdStr | Safe}} +
+ {{if gt .ShowTag 0}} +
+ {{if eq .ShowTag 1}} + {{$.root.i18n.Tr "repo.issues.poster"}} + {{else if eq .ShowTag 2}} + {{$.root.i18n.Tr "repo.issues.collaborator"}} + {{else if eq .ShowTag 3}} + {{$.root.i18n.Tr "repo.issues.owner"}} + {{end}} +
+ {{end}} + {{template "repo/issue/view_content/add_reaction" Dict "ctx" $ "ActionURL" (Printf "%s/comments/%d/reactions" $.root.RepoLink .ID) }} + {{if or $.root.IsRepositoryAdmin (eq .Poster.ID $.root.SignedUserID)}} +
+ + +
+ {{end}} +
+
+
+
+ {{if .RenderedContent}} + {{.RenderedContent|Str2html}} + {{else}} + {{$.root.i18n.Tr "repo.issues.no_content"}} + {{end}} +
+
{{.Content}}
+
+
+ {{$reactions := .Reactions.GroupByType}} + {{if $reactions}} +
+ {{template "repo/issue/view_content/reactions" Dict "ctx" $ "ActionURL" (Printf "%s/comments/%d/reactions" $.root.RepoLink .ID) "Reactions" $reactions }} +
+ {{end}} +
+
+{{end}}