| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "html/template" |
| 5 | "net/http" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | |
| 9 | "gitlab.com/Timfa/bluegit/internal/access" |
| 10 | "gitlab.com/Timfa/bluegit/internal/gitrepo" |
| 11 | "gitlab.com/Timfa/bluegit/internal/i18n" |
| 12 | "gitlab.com/Timfa/bluegit/internal/markdown" |
| 13 | "gitlab.com/Timfa/bluegit/internal/store" |
| 14 | ) |
| 15 | |
| 16 | type noteRender struct { |
| 17 | RepoPath string |
| 18 | Post string |
| 19 | CSRF string |
| 20 | Count string |
| 21 | Toggle string |
| 22 | ViewerID int64 |
| 23 | } |
| 24 | |
| 25 | func buildNoteRows(notes []store.Note, lang i18n.Lang, opt noteRender) []NoteRow { |
| 26 | rows := make([]NoteRow, len(notes)) |
| 27 | index := map[int64]int{} |
| 28 | for i, n := range notes { |
| 29 | url := "" |
| 30 | if n.AuthorID != 0 && n.Author != "" { |
| 31 | url = "/users/" + n.Author |
| 32 | } |
| 33 | rows[i] = NoteRow{ |
| 34 | ID: n.ID, Author: n.Author, AuthorURL: url, Body: n.Body, Path: n.FilePath, |
| 35 | Start: n.LineStart, End: n.LineEnd, HasSuggestion: n.Suggestion != "", |
| 36 | When: i18n.FormatDate(lang, n.CreatedAt), At: n.CreatedAt, ParentID: n.ParentID, |
| 37 | HTML: template.HTML(markdown.RenderTasks(n.Body, "", "", opt.RepoPath, markdown.Tasks{ |
| 38 | Post: opt.Post, CSRF: opt.CSRF, Kind: "mr-note", ID: strconv.FormatInt(n.ID, 10), |
| 39 | CanToggle: opt.ViewerID != 0 && opt.ViewerID == n.AuthorID, Count: opt.Count, Toggle: opt.Toggle, |
| 40 | })), |
| 41 | } |
| 42 | index[n.ID] = i |
| 43 | } |
| 44 | for i := range rows { |
| 45 | parent := rows[i].ParentID |
| 46 | if parent == 0 { |
| 47 | continue |
| 48 | } |
| 49 | pi, ok := index[parent] |
| 50 | if !ok || rows[pi].ParentID != 0 { |
| 51 | rows[i].ParentID = 0 |
| 52 | continue |
| 53 | } |
| 54 | rows[pi].Replies = append(rows[pi].Replies, rows[i]) |
| 55 | } |
| 56 | return rows |
| 57 | } |
| 58 | |
| 59 | func arrangeNotes(notes []store.Note, files []gitrepo.DiffFile, lang i18n.Lang, opt noteRender) ([]NoteRow, []DiffFileView) { |
| 60 | rows := buildNoteRows(notes, lang, opt) |
| 61 | placed := map[int64]bool{} |
| 62 | var views []DiffFileView |
| 63 | for _, f := range files { |
| 64 | view := DiffFileView{Path: f.Path} |
| 65 | for _, ln := range f.Lines { |
| 66 | row := DiffLineView{Kind: ln.Kind, Text: ln.Text, Old: ln.Old, New: ln.New} |
| 67 | if ln.New > 0 { |
| 68 | for _, note := range rows { |
| 69 | if note.ParentID != 0 { |
| 70 | continue |
| 71 | } |
| 72 | if note.Path == f.Path && note.End == ln.New { |
| 73 | row.Notes = append(row.Notes, note) |
| 74 | placed[note.ID] = true |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | view.Lines = append(view.Lines, row) |
| 79 | } |
| 80 | views = append(views, view) |
| 81 | } |
| 82 | var general []NoteRow |
| 83 | for _, note := range rows { |
| 84 | if note.ParentID != 0 { |
| 85 | continue |
| 86 | } |
| 87 | if note.Path == "" || !placed[note.ID] { |
| 88 | general = append(general, note) |
| 89 | } |
| 90 | } |
| 91 | return general, views |
| 92 | } |
| 93 | |
| 94 | func repoPath(raw string) (string, bool) { |
| 95 | raw = strings.TrimSpace(raw) |
| 96 | if raw == "" { |
| 97 | return "", true |
| 98 | } |
| 99 | p := strings.ReplaceAll(raw, `\`, "/") |
| 100 | if strings.HasPrefix(p, "/") || strings.Contains(p, "..") || strings.ContainsAny(p, "\r\n") { |
| 101 | return "", false |
| 102 | } |
| 103 | return p, true |
| 104 | } |
| 105 | |
| 106 | func (s *Server) applySuggestions(w http.ResponseWriter, r *http.Request) { |
| 107 | u, repo, _, ok := s.guard(w, r, access.RoleDeveloper) |
| 108 | if !ok || !s.csrfOK(w, r) { |
| 109 | return |
| 110 | } |
| 111 | n, err := strconv.ParseInt(r.PathValue("number"), 10, 64) |
| 112 | if err != nil { |
| 113 | http.NotFound(w, r) |
| 114 | return |
| 115 | } |
| 116 | back := repoURL(repo) + "/-/merge-requests/" + r.PathValue("number") |
| 117 | var ids []int64 |
| 118 | for _, raw := range r.Form["id"] { |
| 119 | id, err := strconv.ParseInt(raw, 10, 64) |
| 120 | if err == nil { |
| 121 | ids = append(ids, id) |
| 122 | } |
| 123 | } |
| 124 | if !s.uiOK(w, r, back, s.opApplySuggestions(r.Context(), u, repo, n, r.FormValue("title"), r.FormValue("description"), ids)) { |
| 125 | return |
| 126 | } |
| 127 | s.redirect(w, r, back, s.tr(r, "flash.suggestions"), "") |
| 128 | } |