fix: avatar, add logging (#109)

Co-authored-by: xuu <me@sour.is>
Reviewed-on: https://git.mills.io/saltyim/saltyim/pulls/109
This commit is contained in:
xuu 2022-03-31 21:19:45 +00:00
parent 3373a58908
commit b11d14a274
7 changed files with 25 additions and 8 deletions

View File

@ -70,7 +70,7 @@ func (a *API) SendEndpoint() httprouter.Handle {
// TODO: Queue up an internal retry and return immediately on failure?
if err := saltyim.Send(req.Endpoint, req.Message, req.Capabilities); err != nil {
log.WithError(err).Errorf("error sending message to %s: %w", req.Endpoint, err)
log.WithError(err).Errorf("error sending message to %s: %s", req.Endpoint, err)
http.Error(w, "Send Error", http.StatusInternalServerError)
return
}

View File

@ -24,6 +24,7 @@ func (s *Server) ConfigHandler() httprouter.Handle {
configDir := filepath.Join(s.config.Data, wellknownPath)
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
w.Header().Set("Accept-Encoding", "br, gzip, deflate")
w.Header().Set("X-Salty-Accept-Encoding", "br, gzip, deflate")
http.ServeFile(w, r, filepath.Join(configDir, p.ByName("config")))
}
}

View File

@ -30,6 +30,9 @@ func NewRouter() *Router {
GlobalOPTIONS: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "*")
w.Header().Set("Access-Control-Expose-Headers", "*")
w.Header().Set("Accept-Encoding", "br, gzip, deflate")
w.Header().Set("X-Salty-Accept-Encoding", "br, gzip, deflate")
}),
},
}

View File

@ -372,6 +372,8 @@ func NewServer(bind string, options ...Option) (*Server, error) {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "*")
w.Header().Set("Access-Control-Expose-Headers", "*")
next(w, r, p)
}
}))

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:caa6b74f9e2ef34c0a5fba82ef67570e64428dde4d327d2672c0a3f06c374166
size 28443698
oid sha256:5cf0093c079ea104fea508c9ae0e1ba65dd6f6c81164dc24a4e1547b46d2ab03
size 28437569

View File

@ -35,7 +35,7 @@ func fetchConfig(addr string) (Config, Capabilities, error) {
if err := json.NewDecoder(res.Body).Decode(&config); err != nil {
return Config{}, Capabilities{}, err
}
log.Debug(res.Header)
cap := Capabilities{
AcceptEncoding: res.Header.Get("Accept-Encoding"),
}
@ -54,6 +54,10 @@ type Capabilities struct {
AcceptEncoding string
}
func (c Capabilities) String() string {
return fmt.Sprint("accept-encoding: ", c.AcceptEncoding)
}
// Addr represents a Salty IM User's Address
type Addr struct {
User string
@ -78,12 +82,14 @@ func (a *Addr) MarshalJSON() ([]byte, error) {
Domain string
Key string
Endpoint string
Avatar string
}{
User: a.User,
Domain: a.Domain,
Addr: a.String(),
Key: a.key.ID().String(),
Endpoint: a.Endpoint().String(),
Avatar: a.Avatar(),
})
}
@ -94,6 +100,7 @@ func (a *Addr) UnmarshalJSON(data []byte) error {
Domain string
Key string
Endpoint string
Avatar string
}{}
if err := json.Unmarshal(data, &res); err != nil {
@ -114,6 +121,7 @@ func (a *Addr) UnmarshalJSON(data []byte) error {
return fmt.Errorf("error parsing public key %q: %w", res.Key, err)
}
a.key = key
a.avatar = res.Avatar
return nil
}
@ -196,6 +204,9 @@ func (a *Addr) Refresh() error {
a.endpoint = u
a.capabilities = cap
log.Debugf("Discovered endpoint: %v", a.endpoint)
log.Debugf("Discovered capability: %v", a.capabilities)
return nil
}
@ -205,9 +216,9 @@ func (a *Addr) Avatar() string {
}
log.Debugf("Looking up SRV record for _avatars._tcp.%s", a.Domain)
if target, err := resolver.LookupSRV("salty", "tcp", a.Domain); err == nil {
a.avatar = fmt.Sprintf("%s/avatar/%x", target, a.Hash())
}
if target, err := resolver.LookupSRV("avatars", "tcp", a.Domain); err == nil {
a.avatar = fmt.Sprintf("https://%s/avatar/%s", target, a.Hash())
} else { log.Info(target, err) }
return a.avatar
}

View File

@ -37,7 +37,7 @@ type StandardResolver struct{}
func (r *StandardResolver) LookupSRV(service, proto, domain string) (string, error) {
log.Debugf("Using StandardResolver, looking up SRV _%s._%s.%s", service, proto, domain)
_, records, err := net.LookupSRV("salty", "tcp", domain)
_, records, err := net.LookupSRV(service, proto, domain)
if err != nil {
return "", fmt.Errorf("error looking up _%s._%s.%s : %w", service, proto, domain, err)
}