From b8c00a22d63b8988c4b99b51ea6bade3031adc5c Mon Sep 17 00:00:00 2001 From: Peter Stuifzand Date: Tue, 3 Jul 2018 22:45:13 +0200 Subject: [PATCH] Also support "normal" MF2 requests for micropub --- cmd/eksterd/micropub.go | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/cmd/eksterd/micropub.go b/cmd/eksterd/micropub.go index d151111..fc8769e 100644 --- a/cmd/eksterd/micropub.go +++ b/cmd/eksterd/micropub.go @@ -8,6 +8,7 @@ import ( "github.com/garyburd/redigo/redis" "github.com/pstuifzand/ekster/microsub" + "willnorris.com/go/microformats" ) type micropubHandler struct { @@ -29,26 +30,39 @@ func (h *micropubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } + var item microsub.Item + ok := false if r.Header.Get("Content-Type") == "application/jf2+json" { - var item microsub.Item - dec := json.NewDecoder(r.Body) - err := dec.Decode(&item) if err != nil { http.Error(w, fmt.Sprintf("Error decoding: %v", err), 400) return } + ok = true + } else if r.Header.Get("Content-Type") == "application/json" { + var mfItem microformats.Microformat + dec := json.NewDecoder(r.Body) + err := dec.Decode(&mfItem) + if err != nil { + http.Error(w, fmt.Sprintf("Error decoding: %v", err), 400) + return + } - item.Read = false - id, err := redis.Int(conn.Do("INCR", "source:"+sourceID+"next_id")) - item.ID = fmt.Sprintf("%x", sha1.Sum([]byte(fmt.Sprintf("source:%s:%d", sourceID, id)))) - h.Backend.channelAddItem(channel, item) + item = mapToItem(simplifyMicroformat(&mfItem)) + ok = true } else { http.Error(w, "Unsupported Content-Type", 400) return } + if ok { + item.Read = false + id, _ := redis.Int(conn.Do("INCR", "source:"+sourceID+"next_id")) + item.ID = fmt.Sprintf("%x", sha1.Sum([]byte(fmt.Sprintf("source:%s:%d", sourceID, id)))) + h.Backend.channelAddItem(channel, item) + } + w.Header().Set("Content-Type", "application/json") enc := json.NewEncoder(w)