6
1
mirror of https://git.mills.io/saltyim/saltyim.git synced 2024-06-29 18:21:06 +00:00

Refactor for better naming and terminology

This commit is contained in:
James Mills 2022-03-19 01:13:31 +10:00
parent 475a88b564
commit 82b2b10fad

@ -10,41 +10,40 @@ import (
log "github.com/sirupsen/logrus"
)
// User represents a Salty IM User and the componetns that make up a Salty Addr
type User struct {
// Addr represents a Salty IM User's Address
type Addr struct {
User string
Domain string
}
// WellKnownURI returns the User's expected Well-Known URI
func (u User) WellKnownURI() string {
return fmt.Sprintf("https://%s/.well-known/salty/%s.json", u.Domain, u.User)
// URI returns the Well-Known URI for this Addr
func (a Addr) URI() string {
return fmt.Sprintf("https://%s/.well-known/salty/%s.json", a.Domain, a.User)
}
// ParseUser parsers a user into it's user and domain parts and returns
// a User object with the User and Domain and a method for returning the
// expected User's Well-Known URI
func ParseUser(user string) (User, error) {
parts := strings.Split(user, "@")
// ParseAddr parsers a Salty Address for a user into it's user and domain
// parts and returns an Addr object with the User and Domain and a method
// for returning the expected User's Well-Known URI
func ParseAddr(addr string) (Addr, error) {
parts := strings.Split(addr, "@")
if len(parts) != 2 {
return User{}, fmt.Errorf("error parsing user %q, expected nick@domain", user)
return Addr{}, fmt.Errorf("error parsing addr %q, expected nick@domain", addr)
}
return User{parts[0], parts[1]}, nil
return Addr{parts[0], parts[1]}, nil
}
// Lookup looks up the user's Salty Config by parsing the user's domain and
// making a request to the user's Well-Known URI expected to by located at
// Lookup looks up a Salty Address for a User by parsing the user's domain and
// making a request to the user's Well-Known URI expected to be located at
// https://domain/.well-known/salty/<user>.json
// If a valid config is found, it is returned otherwise an error is returned
func Lookup(user string) (Config, error) {
u, err := ParseUser(user)
func Lookup(addr string) (Config, error) {
a, err := ParseAddr(addr)
if err != nil {
log.WithError(err).Errorf("error parsing user %q", user)
log.WithError(err).Errorf("error parsing addr %q", addr)
return Config{}, nil
}
res, err := http.Get(u.WellKnownURI())
res, err := http.Get(a.URI())
if err != nil {
log.WithError(err).Error("error requesting well-known uri")
return Config{}, nil