| 1 | package store |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "errors" |
| 7 | "strconv" |
| 8 | "strings" |
| 9 | "time" |
| 10 | |
| 11 | "gitlab.com/Timfa/bluegit/internal/i18n" |
| 12 | ) |
| 13 | |
| 14 | // Note is een reactie op een merge request. |
| 15 | // FilePath leeg is een algemene reactie. Suggestion is de vervangende tekst, |
| 16 | // BaseText de regels zoals ze bij het plaatsen in het bestand stonden. |
| 17 | type Note struct { |
| 18 | ID int64 |
| 19 | RepoID int64 |
| 20 | MRID int64 |
| 21 | AuthorID int64 |
| 22 | Author string |
| 23 | Body string |
| 24 | FilePath string |
| 25 | LineStart int |
| 26 | LineEnd int |
| 27 | Suggestion string |
| 28 | BaseText string |
| 29 | ParentID int64 |
| 30 | CreatedAt time.Time |
| 31 | } |
| 32 | |
| 33 | // AddNote bewaart een algemene reactie en telt hem als bijdrage. |
| 34 | func (s *Store) AddNote(ctx context.Context, repoID, mrID, authorID int64, body string) (Note, error) { |
| 35 | return s.insertNote(ctx, repoID, mrID, authorID, body, "", 0, 0, "", "", 0) |
| 36 | } |
| 37 | |
| 38 | // AddReply bewaart een reactie op een bestaande reactie, één niveau diep. |
| 39 | func (s *Store) AddReply(ctx context.Context, repoID, mrID, authorID, parentID int64, body string) (Note, error) { |
| 40 | var parentMR, parentParent int64 |
| 41 | err := s.db.QueryRowContext(ctx, `SELECT mr_id, parent_id FROM notes WHERE id = ? AND repo_id = ?`, parentID, repoID).Scan(&parentMR, &parentParent) |
| 42 | if err != nil || parentMR != mrID || parentParent != 0 { |
| 43 | return Note{}, i18n.E("err.noteParent") |
| 44 | } |
| 45 | return s.insertNote(ctx, repoID, mrID, authorID, body, "", 0, 0, "", "", parentID) |
| 46 | } |
| 47 | |
| 48 | // AddLineNote bewaart een reactie op regels, optioneel met een suggestie. |
| 49 | func (s *Store) AddLineNote(ctx context.Context, repoID, mrID, authorID int64, body, path string, start, end int, suggestion, base string) (Note, error) { |
| 50 | if path == "" || start < 1 || end < start { |
| 51 | return Note{}, i18n.E("err.sugLines") |
| 52 | } |
| 53 | return s.insertNote(ctx, repoID, mrID, authorID, body, path, start, end, suggestion, base, 0) |
| 54 | } |
| 55 | |
| 56 | func (s *Store) insertNote(ctx context.Context, repoID, mrID, authorID int64, body, path string, start, end int, suggestion, base string, parentID int64) (Note, error) { |
| 57 | body = strings.TrimSpace(body) |
| 58 | if body == "" { |
| 59 | return Note{}, i18n.E("err.noteEmpty") |
| 60 | } |
| 61 | limit := 4000 |
| 62 | if path != "" { |
| 63 | limit = 20000 |
| 64 | } |
| 65 | body = trim(body, limit) |
| 66 | tx, err := s.db.BeginTx(ctx, nil) |
| 67 | if err != nil { |
| 68 | return Note{}, err |
| 69 | } |
| 70 | defer func() { _ = tx.Rollback() }() |
| 71 | ts := now() |
| 72 | res, err := tx.ExecContext(ctx, `INSERT INTO notes (repo_id, mr_id, author_id, body, created_at, file_path, line_start, line_end, suggestion, base_text, parent_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, |
| 73 | repoID, mrID, authorID, body, ts, path, start, end, suggestion, base, parentID) |
| 74 | if err != nil { |
| 75 | return Note{}, err |
| 76 | } |
| 77 | id, _ := res.LastInsertId() |
| 78 | if _, err := tx.ExecContext(ctx, `INSERT OR IGNORE INTO contributions (user_id, repo_id, kind, ref, occurred_at) VALUES (?, ?, 'comment', ?, ?)`, |
| 79 | authorID, repoID, "note:"+strconv.FormatInt(id, 10), ts); err != nil { |
| 80 | return Note{}, err |
| 81 | } |
| 82 | if err := tx.Commit(); err != nil { |
| 83 | return Note{}, err |
| 84 | } |
| 85 | return Note{ |
| 86 | ID: id, RepoID: repoID, MRID: mrID, AuthorID: authorID, Body: body, |
| 87 | FilePath: path, LineStart: start, LineEnd: end, Suggestion: suggestion, BaseText: base, |
| 88 | ParentID: parentID, CreatedAt: parseTime(ts), |
| 89 | }, nil |
| 90 | } |
| 91 | |
| 92 | // ListNotes geeft de reacties van één merge request, oudste eerst. |
| 93 | func (s *Store) ListNotes(ctx context.Context, mrID int64) ([]Note, error) { |
| 94 | rows, err := s.db.QueryContext(ctx, `SELECT n.id, n.repo_id, n.mr_id, n.author_id, u.username, n.body, n.created_at, n.file_path, n.line_start, n.line_end, n.suggestion, n.base_text, n.parent_id |
| 95 | FROM notes n JOIN users u ON u.id = n.author_id WHERE n.mr_id = ? ORDER BY n.id`, mrID) |
| 96 | if err != nil { |
| 97 | return nil, err |
| 98 | } |
| 99 | defer rows.Close() |
| 100 | var out []Note |
| 101 | for rows.Next() { |
| 102 | var n Note |
| 103 | var created string |
| 104 | if err := rows.Scan(&n.ID, &n.RepoID, &n.MRID, &n.AuthorID, &n.Author, &n.Body, &created, &n.FilePath, &n.LineStart, &n.LineEnd, &n.Suggestion, &n.BaseText, &n.ParentID); err != nil { |
| 105 | return nil, err |
| 106 | } |
| 107 | n.CreatedAt = parseTime(created) |
| 108 | out = append(out, n) |
| 109 | } |
| 110 | return out, rows.Err() |
| 111 | } |
| 112 | |
| 113 | // NoteByID leest één reactie in deze repository. |
| 114 | func (s *Store) NoteByID(ctx context.Context, repoID, id int64) (Note, error) { |
| 115 | var n Note |
| 116 | var created string |
| 117 | err := s.db.QueryRowContext(ctx, `SELECT n.id, n.repo_id, n.mr_id, n.author_id, u.username, n.body, n.created_at, n.file_path, n.line_start, n.line_end, n.suggestion, n.base_text, n.parent_id |
| 118 | FROM notes n JOIN users u ON u.id = n.author_id WHERE n.id = ? AND n.repo_id = ?`, id, repoID). |
| 119 | Scan(&n.ID, &n.RepoID, &n.MRID, &n.AuthorID, &n.Author, &n.Body, &created, &n.FilePath, &n.LineStart, &n.LineEnd, &n.Suggestion, &n.BaseText, &n.ParentID) |
| 120 | if errors.Is(err, sql.ErrNoRows) { |
| 121 | return Note{}, errNotFound |
| 122 | } |
| 123 | if err != nil { |
| 124 | return Note{}, err |
| 125 | } |
| 126 | n.CreatedAt = parseTime(created) |
| 127 | return n, nil |
| 128 | } |
| 129 | |
| 130 | // UpdateNoteBody vervangt de tekst van een reactie. |
| 131 | func (s *Store) UpdateNoteBody(ctx context.Context, repoID, id int64, body string) error { |
| 132 | if strings.TrimSpace(body) == "" { |
| 133 | return i18n.E("err.noteEmpty") |
| 134 | } |
| 135 | if len([]rune(body)) > 20000 { |
| 136 | return i18n.E("err.issueBody") |
| 137 | } |
| 138 | res, err := s.db.ExecContext(ctx, `UPDATE notes SET body = ? WHERE id = ? AND repo_id = ?`, body, id, repoID) |
| 139 | if err != nil { |
| 140 | return err |
| 141 | } |
| 142 | n, _ := res.RowsAffected() |
| 143 | if n == 0 { |
| 144 | return errNotFound |
| 145 | } |
| 146 | return nil |
| 147 | } |