0
0
package request
import (
"cmp"
"context"
"fmt"
"strings"
"github.com/bluesky-social/indigo/atproto/identity"
"github.com/bluesky-social/indigo/atproto/syntax"
)
const DefaultPLCDirectoryURL = "https://plc.directory"
func AssociatedHandle(ctx context.Context, plcURL, label, owner string) (string, error) {
did, err := syntax.ParseDID(strings.TrimSpace(owner))
if err != nil {
return "", fmt.Errorf("the owner %q is not a DID: %w", owner, err)
}
dir := &identity.BaseDirectory{PLCURL: strings.TrimRight(cmp.Or(strings.TrimSpace(plcURL), DefaultPLCDirectoryURL), "/")}
ident, err := dir.LookupDID(ctx, did)
if err != nil {
return "", fmt.Errorf("resolving the owner %s: %w", did, err)
}
if ident.Handle.IsInvalidHandle() || ident.Handle == "" {
return "", fmt.Errorf("the owner %s has no verified handle to declare the identity's handle under", did)
}
handle, err := syntax.ParseHandle(strings.Trim(label, ".") + "." + ident.Handle.Normalize().String())
if err != nil {
return "", fmt.Errorf("the handle under %s: %w", ident.Handle, err)
}
return handle.String(), nil
}