diff --git a/cmd/server/fetch.go b/cmd/server/fetch.go index 801a0bb..08908af 100644 --- a/cmd/server/fetch.go +++ b/cmd/server/fetch.go @@ -223,6 +223,8 @@ func convertMfToItem(mf *microformats.Microformat) microsub.Item { if item.Name == strings.TrimSpace(item.Content.Text) { item.Name = "" } + + // TODO: for like name is the field that is set if item.Content.HTML == "" && len(item.LikeOf) > 0 { item.Name = "" } diff --git a/cmd/server/main.go b/cmd/server/main.go index 9f1cb6c..0b88e1c 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -24,11 +24,16 @@ import ( "log" "net/http" + // "github.com/garyburd/redigo/redis" "github.com/pstuifzand/microsub-server/microsub" "willnorris.com/go/microformats" ) -var port int +var ( + // pool redis.Pool + port int + // redisServer = flag.String("redis", "redis:6379", "") +) func init() { flag.IntVar(&port, "port", 80, "port for serving api") @@ -54,14 +59,21 @@ func simplify(item map[string][]interface{}) map[string]interface{} { if content, ok := v[0].(map[string]interface{}); ok { if text, e := content["value"]; e { delete(content, "value") - if _, e := content["html"]; !e { - content["text"] = text - } + content["text"] = text + // if _, e := content["html"]; !e { + // content["text"] = text + // } } feedItem[k] = content } } else if k == "photo" { - feedItem[k] = v + if len(v) == 1 { + if value, ok := v[0].(string); ok { + feedItem[k] = value + } + } else { + feedItem[k] = v + } } else if k == "video" { feedItem[k] = v } else if k == "featured" { @@ -78,6 +90,20 @@ func simplify(item map[string][]interface{}) map[string]interface{} { feedItem[k] = value } } + + // Remove "name" when it's equals to "content[text]" + if name, e := feedItem["name"]; e { + if content, e2 := feedItem["content"]; e2 { + if contentMap, ok := content.(map[string]interface{}); ok { + if text, e3 := contentMap["text"]; e3 { + if name == text { + delete(feedItem, "name") + } + } + } + } + } + return feedItem } @@ -120,8 +146,64 @@ func simplifyMicroformatData(md *microformats.Data) []map[string]interface{} { return items } +// TokenResponse is the information that we get back from the token endpoint of the user... +type TokenResponse struct { + Me string `json:"me"` + ClientID string `json:"client_id"` + Scope string `json:"scope"` + IssuedAt int64 `json:"issued_at"` + Nonce int64 `json:"nonce"` +} + +func (h *microsubHandler) checkAuthToken(header string, token *TokenResponse) bool { + req, err := http.NewRequest("GET", "https://publog.stuifzandapp.com/authtoken", nil) + if err != nil { + log.Println(err) + return false + } + + req.Header.Add("Authorization", header) + req.Header.Add("Accept", "application/json") + + client := http.Client{} + res, err := client.Do(req) + if err != nil { + log.Println(err) + return false + } + defer res.Body.Close() + + if res.StatusCode < 200 || res.StatusCode >= 300 { + return false + } + + dec := json.NewDecoder(res.Body) + err = dec.Decode(&token) + + if err != nil { + return false + } + + return true +} + func (h *microsubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - fmt.Println(r.URL.String()) + //conn := pool.Get() + //defer conn.Close() + + //authorization := r.Header.Get("Authorization") + + // var token TokenResponse + + // if !h.checkAuthToken(authorization, &token) { + // http.Error(w, "Can't validate token", 403) + // return + // } + + // if token.Me != "https://publog.stuifzandapp.com/" { + // http.Error(w, "Wrong me", 403) + // return + // } if r.Method == http.MethodGet { values := r.URL.Query() @@ -219,6 +301,14 @@ func (h *microsubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } +// func newPool(addr string) *redis.Pool { +// return &redis.Pool{ +// MaxIdle: 3, +// IdleTimeout: 240 * time.Second, +// Dial: func() (redis.Conn, error) { return redis.Dial("tcp", addr) }, +// } +// } + func main() { flag.Parse() @@ -239,6 +329,8 @@ func main() { backend = loadMemoryBackend() } + //pool = newPool(*redisServer) + http.Handle("/microsub", µsubHandler{backend}) log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil)) } diff --git a/cmd/server/memory.go b/cmd/server/memory.go index 6b7e145..e90ad1c 100644 --- a/cmd/server/memory.go +++ b/cmd/server/memory.go @@ -30,8 +30,7 @@ import ( type memoryBackend struct { Channels map[string]microsub.Channel Feeds map[string][]microsub.Feed - //Items map[string]map[string][]microsub.Item - NextUid int + NextUid int } type Debug interface { @@ -71,10 +70,10 @@ func createMemoryBackend() microsub.Microsub { backend.Channels = make(map[string]microsub.Channel) backend.Feeds = make(map[string][]microsub.Feed) channels := []microsub.Channel{ - microsub.Channel{"0000", "default"}, - microsub.Channel{"0001", "notifications"}, - microsub.Channel{"1000", "Friends"}, - microsub.Channel{"1001", "Family"}, + microsub.Channel{UID: "0000", Name: "default"}, + microsub.Channel{UID: "0001", Name: "notifications"}, + microsub.Channel{UID: "1000", Name: "Friends"}, + microsub.Channel{UID: "1001", Name: "Family"}, } for _, c := range channels { backend.Channels[c.UID] = c diff --git a/microsub/protocol.go b/microsub/protocol.go index 5113c8e..5abbb13 100644 --- a/microsub/protocol.go +++ b/microsub/protocol.go @@ -15,6 +15,8 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ + +// Package microsub describes the protocol methods of the Microsub protocol package microsub /*