Fix LookupAll functions

This commit is contained in:
Elara 2024-03-10 15:34:40 -07:00
parent 2c3e0b3df6
commit 526b73aa99

View File

@ -65,23 +65,23 @@ type Client struct {
// Lookup looks up the profile descriptor for the given resource. // Lookup looks up the profile descriptor for the given resource.
func (c Client) Lookup(resource string) (*Descriptor, error) { func (c Client) Lookup(resource string) (*Descriptor, error) {
out := &Descriptor{} out := &Descriptor{}
return out, c.lookup(resource, "", out) return out, c.lookup(resource, "", false, out)
} }
// LookupID looks up the profile descriptor that matches the given ID // LookupID looks up the profile descriptor that matches the given ID
// for the given resource. // for the given resource.
func (c Client) LookupID(resource, id string) (*Descriptor, error) { func (c Client) LookupID(resource, id string) (*Descriptor, error) {
out := &Descriptor{} out := &Descriptor{}
return out, c.lookup(resource, id, out) return out, c.lookup(resource, id, false, out)
} }
// Lookup looks up all the available profile descriptors for the given resource. // Lookup looks up all the available profile descriptors for the given resource.
func (c Client) LookupAll(resource string) (map[string]*Descriptor, error) { func (c Client) LookupAll(resource string) (map[string]*Descriptor, error) {
out := map[string]*Descriptor{} out := map[string]*Descriptor{}
return out, c.lookup(resource, "", &out) return out, c.lookup(resource, "", true, &out)
} }
func (c Client) lookup(resource, id string, dest any) error { func (c Client) lookup(resource, id string, all bool, dest any) error {
wfdesc, err := webfinger.LookupAcct(resource) wfdesc, err := webfinger.LookupAcct(resource)
if err != nil { if err != nil {
return err return err
@ -97,12 +97,6 @@ func (c Client) lookup(resource, id string, dest any) error {
return err return err
} }
if id != "" {
q := pfdURL.Query()
q.Set("id", id)
pfdURL.RawQuery = q.Encode()
}
pubkeySaved := false pubkeySaved := false
pubkey, err := c.GetPubkey(pfdURL.Host) pubkey, err := c.GetPubkey(pfdURL.Host)
if errors.Is(err, ErrPubkeyNotFound) { if errors.Is(err, ErrPubkeyNotFound) {
@ -125,6 +119,14 @@ func (c Client) lookup(resource, id string, dest any) error {
return err return err
} }
q := pfdURL.Query()
if all {
q.Set("all", "1")
} else if id != "" {
q.Set("id", id)
}
pfdURL.RawQuery = q.Encode()
res, err := http.Get(pfdURL.String()) res, err := http.Get(pfdURL.String())
if err != nil { if err != nil {
return err return err