Skip to content
quarry Artifact [32aeb17dbe]
Login

Artifact [32aeb17dbe]

Artifact 32aeb17dbeb8429b4977c06791e2f35afeb325051924c71b6b4351d44b4f3639:


package web

import (
	"net/http"
	"net/url"

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

// fossilUsersPanel is Advanced's Fossil users block: the repository's
// synced fossil user table, kept as a reference an admin links to
// accounts. nil where the forge keeps no such list.
type fossilUsersPanel struct {
	Upstream string
	Refused  string
	// Warned is the guard's remark on a link it allowed, such as a site
	// admin linking away a repository's last admin.
	Warned string
	Users  []fossilUserView
}

type fossilUserView struct {
	Login   string
	Caps    string
	Decoded []string
	Info    string
	Linked  string
	Suggest bool
}

// linkedTo maps each linked quarry account to its fossil login, for
// the People chip.
func (p *fossilUsersPanel) linkedTo() map[string]string {
	out := map[string]string{}
	if p == nil {
		return out
	}
	for _, u := range p.Users {
		if u.Linked != "" {
			out[u.Linked] = u.Login
		}
	}
	return out
}

func fossilUsersPanelFor(r *ops.Registry, req *http.Request, repo string) *fossilUsersPanel {
	out, err := invoke(req.Context(), r, "repo.fossil.users", url.Values{"repo": {repo}})
	if err != nil {
		return nil
	}
	var v struct {
		Upstream string `json:"upstream"`
		Users    []struct {
			Login   string   `json:"login"`
			Caps    string   `json:"caps"`
			Decoded []string `json:"decoded"`
			Info    string   `json:"info"`
			Linked  string   `json:"linked"`
			Suggest bool     `json:"suggest"`
		} `json:"users"`
	}
	remarshal(out, &v)
	p := &fossilUsersPanel{Upstream: v.Upstream, Refused: req.URL.Query().Get("fe"), Warned: req.URL.Query().Get("fw")}
	for _, u := range v.Users {
		p.Users = append(p.Users, fossilUserView{Login: u.Login, Caps: u.Caps, Decoded: u.Decoded,
			Info: u.Info, Linked: u.Linked, Suggest: u.Suggest})
	}
	return p
}