Unmarshal to `webfinger.Resource` instead of interface{}

(https://github.com/writeas/writefreely/pull/195#discussion_r334567408)
This commit is contained in:
Michael Demetriou 2019-10-15 09:59:24 +03:00
parent 972ec00c58
commit 1bda0434de
1 changed files with 11 additions and 7 deletions

View File

@ -103,26 +103,30 @@ func RemoteLookup(handle string) string {
return "" return ""
} }
var result map[string]interface{} var result webfinger.Resource
json.Unmarshal(body, &result) err = json.Unmarshal(body, &result)
if err != nil {
log.Error("Unsupported webfinger response received", err)
return ""
}
var href string var href string
// iterate over webfinger links and find the one with // iterate over webfinger links and find the one with
// a self "rel" // a self "rel"
for _, link := range result["links"].([]interface{}) { for _, link := range result.Links {
if link.(map[string]interface{})["rel"] == "self" { if link.Rel == "self" {
href = link.(map[string]interface{})["href"].(string) href = link.HRef
} }
} }
// if we didn't find it with the above then // if we didn't find it with the above then
// try using aliases // try using aliases
if href == "" { if href == "" {
aliases := result["aliases"].([]interface{})
// take the last alias because mastodon has the // take the last alias because mastodon has the
// https://instance.tld/@user first which // https://instance.tld/@user first which
// doesn't work as an href // doesn't work as an href
href = aliases[len(aliases)-1].(string) href = result.Aliases[len(result.Aliases)-1]
} }
return href return href