Compare commits

..

5 Commits

3 changed files with 36 additions and 8 deletions

View File

@ -182,7 +182,7 @@ Commands:
if len(commands) == 2 && commands[0] == "channels" { if len(commands) == 2 && commands[0] == "channels" {
name := commands[1] name := commands[1]
channel := sub.ChannelsCreate(name) channel := sub.ChannelsCreate(name)
fmt.Printf("Channel created %s %s\n", channel.Name, channel.UID) fmt.Printf("%s\n", channel.UID)
} }
if len(commands) == 3 && commands[0] == "channels" { if len(commands) == 3 && commands[0] == "channels" {

View File

@ -162,6 +162,10 @@ func (b *memoryBackend) ChannelsGetList() []microsub.Channel {
// ChannelsCreate creates a channels // ChannelsCreate creates a channels
func (b *memoryBackend) ChannelsCreate(name string) microsub.Channel { func (b *memoryBackend) ChannelsCreate(name string) microsub.Channel {
defer b.save() defer b.save()
conn := pool.Get()
defer conn.Close()
uid := fmt.Sprintf("%04d", b.NextUid) uid := fmt.Sprintf("%04d", b.NextUid)
channel := microsub.Channel{ channel := microsub.Channel{
UID: uid, UID: uid,
@ -171,6 +175,9 @@ func (b *memoryBackend) ChannelsCreate(name string) microsub.Channel {
b.Channels[channel.UID] = channel b.Channels[channel.UID] = channel
b.Feeds[channel.UID] = []microsub.Feed{} b.Feeds[channel.UID] = []microsub.Feed{}
b.NextUid++ b.NextUid++
conn.Do("SADD", "channels", uid)
conn.Do("SETNX", "channel_sortorder_"+uid, 99999)
return channel return channel
} }

View File

@ -4,10 +4,12 @@ import (
"crypto/sha1" "crypto/sha1"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"net/http" "net/http"
"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 {
@ -20,6 +22,12 @@ func (h *micropubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
conn := pool.Get() conn := pool.Get()
defer conn.Close() defer conn.Close()
r.ParseForm()
log.Printf("%s %s\n", r.Method, r.URL)
log.Println(r.URL.Query())
log.Println(r.PostForm)
log.Println(r.Header)
if r.Method == http.MethodPost { if r.Method == http.MethodPost {
sourceID := r.URL.Query().Get("source_id") sourceID := r.URL.Query().Get("source_id")
@ -29,26 +37,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)