Skip to content
quarry Artifact [4909f17421]
Login

Artifact [4909f17421]

Artifact 4909f17421667330ed1a5668ade82b60e4280ce6bfb61e459fd9fb1d3441db21:


package api

import (
	"net/url"
	"strings"

	"craftdesign.group/quarry/internal/index"
)

// An inbox row is "settled" when the thing it asked of the reader has
// already happened: the landing it proposed is no longer open, the arm
// it said lapsed has been set again, the request has been decided, the
// ticket it mentioned is closed. A reply is never settled by state: it
// waits until it is read. Settled is computed at read time from the
// row's subject; nothing is marked read.

// lapseTxn is the idempotency-key prefix of the arm-lapsed event.
const lapseTxn = "landing:lapse:"

// settleRows sets Settled on rows. It reads each repository's landings
// once and each ticket once, however many rows name them.
func (d Deps) settleRows(who string, rows []inboxRow) {
	landings := map[string]map[string]string{} // repo -> landing id -> state
	tickets := map[string]bool{}               // repo+"/"+id -> closed
	landingState := func(repo, id string) (string, bool) {
		m, ok := landings[repo]
		if !ok {
			m = map[string]string{}
			landings[repo] = m
			if r, err := d.repo(repo); err == nil && r.Index != nil {
				if all, err := r.Index.Landings("", 0); err == nil {
					for _, l := range all {
						m[l.ID] = l.State
					}
				}
			}
		}
		st, ok := m[id]
		return st, ok
	}
	for i := range rows {
		row := &rows[i]
		u, err := url.Parse(row.Link)
		if err != nil || row.Link == "" {
			continue
		}
		path := u.Path
		if id, ok := strings.CutPrefix(path, "/me/requests/"); ok && d.SignOffs != nil && id != "" {
			if s, found, err := d.SignOffs.SignOffByID(id); err == nil && found && (s.Owner == who || s.Agent == who) {
				row.Settled = s.State != "pending"
			}
			continue
		}
		repo, rest := row.Repo, ""
		if k := strings.Index(path, "/landing/"); k >= 0 {
			rest = path[k+len("/landing/"):]
			if repo == "" {
				repo = repoKeyOf(path[:k])
			}
			if st, ok := landingState(repo, rest); ok {
				row.Settled = st != "open"
				if !row.Settled && strings.HasPrefix(row.txn, lapseTxn) && d.Auto != nil {
					_, armed := d.Auto.AutoAcceptOf(repo, rest)
					row.Settled = armed
				}
			}
			continue
		}
		if k := strings.Index(path, "/ticket/"); k >= 0 && row.Reason != "reply" && row.Reason != "chat" {
			rest = path[k+len("/ticket/"):]
			if repo == "" {
				repo = repoKeyOf(path[:k])
			}
			key := repo + "/" + rest
			closed, seen := tickets[key]
			if !seen {
				if r, err := d.repo(repo); err == nil && r.Index != nil {
					if t, found, err := r.Index.TicketByPrefix(rest); err == nil && found {
						closed = index.IsClosedStatus(ticketField(t, "status"))
					}
				}
				tickets[key] = closed
			}
			row.Settled = closed
		}
	}
}

// repoKeyOf is the repository named by the "/r/..." head of a link's
// path: the last segment, which is the key on a flat address and the
// handle on a namespaced one.
func repoKeyOf(head string) string {
	head = strings.TrimPrefix(head, "/r/")
	return head[strings.LastIndex(head, "/")+1:]
}