Also support "normal" MF2 requests for micropub

This commit is contained in:
Peter Stuifzand 2018-07-03 22:45:13 +02:00
parent 209e7510c1
commit b8c00a22d6

View File

@ -8,6 +8,7 @@ import (
"github.com/garyburd/redigo/redis" "github.com/garyburd/redigo/redis"
"github.com/pstuifzand/ekster/microsub" "github.com/pstuifzand/ekster/microsub"
"willnorris.com/go/microformats"
) )
type micropubHandler struct { type micropubHandler struct {
@ -29,26 +30,39 @@ func (h *micropubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return return
} }
var item microsub.Item
ok := false
if r.Header.Get("Content-Type") == "application/jf2+json" { if r.Header.Get("Content-Type") == "application/jf2+json" {
var item microsub.Item
dec := json.NewDecoder(r.Body) dec := json.NewDecoder(r.Body)
err := dec.Decode(&item) err := dec.Decode(&item)
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("Error decoding: %v", err), 400) http.Error(w, fmt.Sprintf("Error decoding: %v", err), 400)
return 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 item = mapToItem(simplifyMicroformat(&mfItem))
id, err := redis.Int(conn.Do("INCR", "source:"+sourceID+"next_id")) ok = true
item.ID = fmt.Sprintf("%x", sha1.Sum([]byte(fmt.Sprintf("source:%s:%d", sourceID, id))))
h.Backend.channelAddItem(channel, item)
} else { } else {
http.Error(w, "Unsupported Content-Type", 400) http.Error(w, "Unsupported Content-Type", 400)
return 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") w.Header().Set("Content-Type", "application/json")
enc := json.NewEncoder(w) enc := json.NewEncoder(w)