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" {
name := commands[1]
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" {

View File

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

View File

@ -4,10 +4,12 @@ import (
"crypto/sha1"
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/garyburd/redigo/redis"
"github.com/pstuifzand/ekster/microsub"
"willnorris.com/go/microformats"
)
type micropubHandler struct {
@ -20,6 +22,12 @@ func (h *micropubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
conn := pool.Get()
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 {
sourceID := r.URL.Query().Get("source_id")
@ -29,26 +37,39 @@ func (h *micropubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
if r.Header.Get("Content-Type") == "application/jf2+json" {
var item microsub.Item
ok := false
if r.Header.Get("Content-Type") == "application/jf2+json" {
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)