internal/control/thread.go

132 lines · 4511 bytes

  1package control
  2
  3import (
  4	"fmt"
  5	"io"
  6	"strconv"
  7	"strings"
  8
  9	"gitbay.org/gitbay/internal/policy"
 10	"gitbay.org/gitbay/internal/protocol"
 11	"gitbay.org/gitbay/internal/store"
 12)
 13
 14// Issues and merge requests share their shape: a numbered thread in a
 15// repository with an author, comments and participants. What used to be
 16// two copies of the same code, one per noun, lives here once (#110).
 17
 18// refArgs resolves "<owner/name> <n>" to the repository and the number.
 19func refArgs(c *Ctx, args []string, perm func(store.User, store.Repo, string) bool, noun string) (store.Repo, int64, int) {
 20	if len(args) < 2 {
 21		return store.Repo{}, 0, c.usageWith("expected <owner/name> <number>")
 22	}
 23	repo, code := resolveRepo(c, args[0], perm)
 24	if code >= 0 {
 25		return repo, 0, code
 26	}
 27	n, err := strconv.ParseInt(args[1], 10, 64)
 28	if err != nil {
 29		return repo, 0, c.fail(protocol.ExitUsage, "bad %s number %q", noun, args[1])
 30	}
 31	return repo, n, -1
 32}
 33
 34// authorOrWrite admits the thread's author and anyone with write access,
 35// which is who may change what a thread says or whether it is open.
 36// -1 means proceed; what names the action in the refusal.
 37func authorOrWrite(c *Ctx, repo store.Repo, author, what string) int {
 38	if author == c.User.Username {
 39		return -1
 40	}
 41	grant, err := c.Store.AccessRole(repo.ID, c.User.ID)
 42	if err != nil {
 43		return c.fail(protocol.ExitFailure, "%v", err)
 44	}
 45	if !policy.CanWrite(c.User, repo, grant) {
 46		return c.fail(protocol.ExitDenied, "only the author or users with write access can %s", what)
 47	}
 48	return -1
 49}
 50
 51// commentOut is one comment as show emits it, for both nouns.
 52type commentOut struct {
 53	ID         int64  `json:"id"`
 54	Author     string `json:"author"`
 55	Body       string `json:"body"`
 56	BodyFormat string `json:"body_format,omitempty"`
 57	CreatedAt  string `json:"created_at"`
 58	// Kind is "system" for a line the server posted and "" for a comment,
 59	// so the wire shape of a comment is unchanged.
 60	Kind      string        `json:"kind,omitempty"`
 61	Reactions []ReactionOut `json:"reactions,omitempty"`
 62}
 63
 64// wireKind is the stored comment kind as commentOut carries it.
 65func wireKind(kind string) string {
 66	if kind == "system" {
 67		return kind
 68	}
 69	return ""
 70}
 71
 72// thread is what a comment command needs to know about its noun.
 73type thread struct {
 74	symbol  string // "#" or "!"
 75	segment string // "issues" or "mrs"
 76	event   string // issue.commented or mr.commented
 77	kind    string // the noun an inbox row is filed under
 78}
 79
 80var (
 81	issueThread = thread{"#", "issues", "issue.commented", "issue"}
 82	mrThread    = thread{"!", "mrs", "mr.commented", "mr"}
 83)
 84
 85// runComment is issue comment and mr comment: the noun's resolver hands
 86// back the thread's id, number and title, and the rest is the same.
 87func runComment(c *Ctx, args []string, t thread, noun string,
 88	resolve func(rest []string) (repo store.Repo, id, number int64, title string, code int),
 89	add func(id, userID int64, body, format string) error,
 90	participants func(id int64) ([]int64, error),
 91) int {
 92	f, err := c.parseArgs(args, flagSpec{Values: []string{"--message", "--file", "--format"}, MaxPos: -1,
 93		Usage: noun + " comment <owner/name> <n> [--message <m> | --file -] [--format md|org]"})
 94	if err != nil {
 95		return c.fail(protocol.ExitUsage, "%v", err)
 96	}
 97	fmtName, err := markupFormat(f.Value("--format"))
 98	if err != nil {
 99		return c.failInput(err)
100	}
101	if fmtName == "" {
102		fmtName = "md"
103	}
104	repo, id, number, title, code := resolve(f.Pos)
105	if code >= 0 {
106		return code
107	}
108	if code := refuseArchived(c, repo); code >= 0 {
109		return code
110	}
111	body, err := bodyFrom(c, f.Value("--message"), f.Value("--file"))
112	if err != nil {
113		return c.failInput(err)
114	}
115	if strings.TrimSpace(body) == "" {
116		return c.fail(protocol.ExitUsage, "empty comment; use --message or --file -")
117	}
118	if err := add(id, c.User.ID, body, fmtName); err != nil {
119		return c.fail(protocol.ExitFailure, "%v", err)
120	}
121	c.Store.RecordEvent(repo.ID, c.User.ID, t.event, fmt.Sprintf(`{"number":%d}`, number))
122	if parts, err := participants(id); err == nil {
123		notify(c, parts, notice{repo: repo, kind: t.kind, number: number,
124			subject: fmt.Sprintf("[%s] %s%d: %s", repo.Path(), t.symbol, number, title),
125			action:  fmt.Sprintf("commented on %s%d", t.symbol, number),
126			excerpt: body, path: fmt.Sprintf("%s/%s/%d", repo.Path(), t.segment, number)})
127	}
128	notifyMentions(c, repo, t, id, number, title, body)
129	return c.emit(Created{Number: number}, func(w io.Writer) {
130		fmt.Fprintf(w, "commented on %s%s%d\n", repo.Path(), t.symbol, number)
131	})
132}