Skip to content
quarry Artifact [87412404f6]
Login

Artifact [87412404f6]

Artifact 87412404f6d94e093d89fb457b147def5246f5cdebe702bd04dd0cc71a5efcda:


package api

import (
	"context"
	"net/url"
	"strings"
)

// A notify row is the companion's copy of a message: subject, body and
// a link, with no repository field. A takedown (RFC 0023) hides what the
// forge already delivered, so the repository a row is about is worked out
// from the link it carries, whose path starts /r/<key> or
// /r/<namespace>/<handle>.

// repoWorkflows are the workflows whose every message is about one
// repository. One of them with no repository to be found cannot be shown
// to be safe, so it is withheld; the others (changelog, signoffs, an
// operator's alert) name none.
var repoWorkflows = map[string]bool{
	"quarry-mention": true, "quarry-forum-reply": true, "quarry-landing": true,
	"quarry-moderation": true, "quarry-activity": true, "quarry-chat": true,
}

// linkPath is the path a link points at, whether it is relative or
// carries the forge's origin.
func linkPath(link string) string {
	if u, err := url.Parse(link); err == nil && u.Path != "" {
		return u.Path
	}
	return link
}

// repoOfLink is the repository a link's path names, "" when it names
// none. A key is tried before an address, as the router reads /r/A/B:
// a repository name always wins, so /r/A/B is the repository A with B
// beneath it whenever A names one, and the pair is an address only when
// A does not (internal/web, namespaced).
func (d Deps) repoOfLink(link string, byAddress func() map[string]string) string {
	rest, ok := strings.CutPrefix(linkPath(link), "/r/")
	if !ok {
		return ""
	}
	segs := strings.SplitN(rest, "/", 3)
	if d.Repos != nil {
		if _, ok := d.Repos.Get(segs[0]); ok {
			return segs[0]
		}
	}
	if k, ok := byAddress()[segs[0]]; ok {
		return k
	}
	if len(segs) > 1 {
		if k, ok := byAddress()[segs[0]+"/"+segs[1]]; ok {
			return k
		}
	}
	return ""
}

// inboxFilter returns the check for one request's rows: true when the
// notify row may still be shown to the caller. It is nil when the forge
// takes nothing down, so the common path pays nothing.
func (d Deps) inboxFilter(ctx context.Context) func(InboxMessage) bool {
	if d.Held == nil {
		return nil
	}
	var book map[string]string
	byAddress := func() map[string]string {
		if book == nil {
			book = map[string]string{}
			if d.Repos != nil {
				for _, n := range d.Repos.Names() {
					h, _, addr := d.repoIdentity(n)
					book[addr], book[h] = n, n
				}
			}
		}
		return book
	}
	return func(m InboxMessage) bool {
		repo := d.repoOfLink(m.Link, byAddress)
		if repo != "" {
			return d.listable(ctx, repo)
		}
		// A link that is not a repository's address at all (/t/..., /me,
		// /people/..., /fleet/...) is not about one, whatever workflow
		// sent it. Withheld is only a row that says it is about a
		// repository and names none we can find: an unresolvable /r/
		// path, or a repository workflow with no link.
		if p := linkPath(m.Link); p != "" && !strings.HasPrefix(p, "/r/") {
			return true
		}
		return !repoWorkflows[m.Workflow] && !strings.HasPrefix(linkPath(m.Link), "/r/")
	}
}

// unheldInbox drops the rows a takedown hides from the caller.
func (d Deps) unheldInbox(ctx context.Context, rows []InboxMessage) []InboxMessage {
	ok := d.inboxFilter(ctx)
	if ok == nil {
		return rows
	}
	kept := rows[:0:0]
	for _, m := range rows {
		if ok(m) {
			kept = append(kept, m)
		}
	}
	return kept
}

// unreadVisible is the badge's notify half, counted from the rows the
// list would show. Without a takedown plane it is notify's own count.
func (d Deps) unreadVisible(ctx context.Context, who string) (int, error) {
	if d.Held == nil {
		return d.Inbox.Unread(ctx, who)
	}
	n, after := 0, ""
	for page := 0; page < inboxMaxNotifyPages; page++ {
		rows, next, err := d.Inbox.List(ctx, who, after, inboxLocalScan)
		if err != nil {
			return 0, err
		}
		for _, m := range d.unheldInbox(ctx, rows) {
			if !m.Read {
				n++
			}
		}
		if next == "" {
			break
		}
		after = next
	}
	return n, nil
}

// attentionPendingVisible counts the unread attention events the list
// would show; without a takedown plane it is the plane's own count.
func (d Deps) attentionPendingVisible(ctx context.Context, who string, since uint64) (int, error) {
	if d.Held == nil {
		return d.Attention.AttentionPending(who, since)
	}
	evs, err := d.Attention.RecentAttention(who, inboxLocalScan)
	if err != nil {
		return 0, err
	}
	n := 0
	for _, e := range evs {
		if e.Kind != AttentionGap && e.Seq > since && d.attentionListable(ctx, e) {
			n++
		}
	}
	return n, nil
}