cic-dw/internal/admin/user.go

60 lines
1.1 KiB
Go
Raw Normal View History

package admin
import (
2022-05-31 10:35:49 +02:00
"bytes"
"encoding/base64"
"net/http"
2022-05-31 10:35:49 +02:00
"strings"
2022-05-31 10:35:49 +02:00
"github.com/grassrootseconomics/cic-go/meta"
"github.com/labstack/echo/v4"
2022-05-31 10:35:49 +02:00
"github.com/mapaiva/vcard-go"
)
2022-05-31 10:35:49 +02:00
type metaRes struct {
Person meta.PersonResponse `json:"person"`
Name string `json:"name"`
}
func handleMetaProxy(c echo.Context) error {
var (
api = c.Get("api").(*api)
address = c.Param("address")
)
person, err := api.m.GetPersonMetadata(address)
if err != nil {
if strings.Contains(err.Error(), "404") {
return c.String(http.StatusNotFound, "meta resource not found")
} else {
return err
}
}
vCard, err := parseVCard(person.VCard)
if err != nil {
return err
}
return c.JSON(http.StatusOK, &metaRes{
Person: person,
Name: vCard.FormattedName,
})
}
func parseVCard(encodedVCard string) (vcard.VCard, error) {
data, err := base64.StdEncoding.DecodeString(encodedVCard)
if err != nil {
return vcard.VCard{}, err
}
reader := bytes.NewReader(data)
vCards, err := vcard.GetVCardsByReader(reader)
if err != nil {
return vcard.VCard{}, nil
}
return vCards[0], nil
}