2018-07-28 16:03:21 +00:00
|
|
|
/*
|
|
|
|
ekster - microsub server
|
|
|
|
Copyright (C) 2018 Peter Stuifzand
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2018-05-22 18:29:07 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
2018-07-28 15:52:59 +00:00
|
|
|
"p83.nl/go/ekster/pkg/microsub"
|
|
|
|
|
2018-08-15 17:04:15 +00:00
|
|
|
"github.com/gomodule/redigo/redis"
|
2018-05-22 18:29:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type microsubHandler struct {
|
|
|
|
Backend microsub.Microsub
|
|
|
|
HubIncomingBackend HubBackend
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *microsubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var logger = log.New(os.Stdout, "logger: ", log.Lshortfile)
|
2018-08-28 19:15:22 +00:00
|
|
|
conn := redis.NewLoggingConn(pool.Get(), logger, "microsub")
|
|
|
|
defer conn.Close()
|
2018-05-22 18:29:07 +00:00
|
|
|
|
|
|
|
r.ParseForm()
|
|
|
|
log.Printf("%s %s\n", r.Method, r.URL)
|
|
|
|
log.Println(r.URL.Query())
|
|
|
|
log.Println(r.PostForm)
|
|
|
|
|
2018-08-26 16:54:45 +00:00
|
|
|
if r.Method == http.MethodOptions {
|
|
|
|
w.Header().Add("Access-Control-Allow-Origin", "*")
|
|
|
|
w.Header().Add("Access-Control-Allow-Methods", "GET, POST")
|
2018-09-11 18:53:48 +00:00
|
|
|
w.Header().Add("Access-Control-Allow-Headers", "Authorization, Cache-Control")
|
2018-08-26 16:54:45 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-23 19:53:00 +00:00
|
|
|
if auth {
|
|
|
|
authorization := r.Header.Get("Authorization")
|
2018-05-22 18:29:07 +00:00
|
|
|
|
2018-05-23 19:53:00 +00:00
|
|
|
var token TokenResponse
|
|
|
|
|
2018-08-28 19:17:42 +00:00
|
|
|
if !h.cachedCheckAuthToken(conn, authorization, &token) {
|
2018-05-23 19:53:00 +00:00
|
|
|
log.Printf("Token could not be validated")
|
|
|
|
http.Error(w, "Can't validate token", 403)
|
|
|
|
return
|
|
|
|
}
|
2018-05-22 18:29:07 +00:00
|
|
|
|
2018-05-23 19:53:00 +00:00
|
|
|
if token.Me != h.Backend.(*memoryBackend).Me {
|
|
|
|
log.Printf("Missing \"me\" in token response: %#v\n", token)
|
|
|
|
http.Error(w, "Wrong me", 403)
|
|
|
|
return
|
|
|
|
}
|
2018-05-22 18:29:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if r.Method == http.MethodGet {
|
2018-08-27 20:54:39 +00:00
|
|
|
w.Header().Add("Access-Control-Allow-Origin", "*")
|
2018-05-22 18:29:07 +00:00
|
|
|
values := r.URL.Query()
|
|
|
|
action := values.Get("action")
|
|
|
|
if action == "channels" {
|
2018-07-07 14:40:04 +00:00
|
|
|
channels, err := h.Backend.ChannelsGetList()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
2018-05-22 18:29:07 +00:00
|
|
|
jw := json.NewEncoder(w)
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
2018-07-07 14:40:04 +00:00
|
|
|
err = jw.Encode(map[string][]microsub.Channel{
|
2018-05-22 18:29:07 +00:00
|
|
|
"channels": channels,
|
|
|
|
})
|
2018-07-07 14:40:04 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
2018-05-22 18:29:07 +00:00
|
|
|
} else if action == "timeline" {
|
2018-07-07 14:40:04 +00:00
|
|
|
timeline, err := h.Backend.TimelineGet(values.Get("before"), values.Get("after"), values.Get("channel"))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
2018-05-22 18:29:07 +00:00
|
|
|
jw := json.NewEncoder(w)
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
jw.SetIndent("", " ")
|
2018-07-28 11:42:32 +00:00
|
|
|
jw.SetEscapeHTML(false)
|
2018-07-07 14:40:04 +00:00
|
|
|
err = jw.Encode(timeline)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
2018-05-22 18:29:07 +00:00
|
|
|
} else if action == "preview" {
|
2018-07-07 14:40:04 +00:00
|
|
|
timeline, err := h.Backend.PreviewURL(values.Get("url"))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
2018-05-22 18:29:07 +00:00
|
|
|
jw := json.NewEncoder(w)
|
|
|
|
jw.SetIndent("", " ")
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
2018-07-07 14:40:04 +00:00
|
|
|
err = jw.Encode(timeline)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
2018-05-22 18:29:07 +00:00
|
|
|
} else if action == "follow" {
|
|
|
|
channel := values.Get("channel")
|
2018-07-07 14:40:04 +00:00
|
|
|
following, err := h.Backend.FollowGetList(channel)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
2018-05-22 18:29:07 +00:00
|
|
|
jw := json.NewEncoder(w)
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
2018-07-07 14:40:04 +00:00
|
|
|
err = jw.Encode(map[string][]microsub.Feed{
|
2018-05-22 18:29:07 +00:00
|
|
|
"items": following,
|
|
|
|
})
|
2018-07-07 14:40:04 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
2018-09-08 15:49:20 +00:00
|
|
|
} else if action == "events" {
|
2018-09-08 17:12:53 +00:00
|
|
|
conn, _, _ := w.(http.Hijacker).Hijack()
|
2018-09-08 17:56:54 +00:00
|
|
|
cons := newConsumer(conn)
|
|
|
|
h.Backend.AddEventListener(cons)
|
2018-05-22 18:29:07 +00:00
|
|
|
} else {
|
2018-07-07 14:40:04 +00:00
|
|
|
http.Error(w, fmt.Sprintf("unknown action %s\n", action), 500)
|
|
|
|
return
|
2018-05-22 18:29:07 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
} else if r.Method == http.MethodPost {
|
2018-08-27 20:54:39 +00:00
|
|
|
w.Header().Add("Access-Control-Allow-Origin", "*")
|
|
|
|
|
2018-05-22 18:29:07 +00:00
|
|
|
values := r.URL.Query()
|
|
|
|
action := values.Get("action")
|
|
|
|
if action == "channels" {
|
|
|
|
name := values.Get("name")
|
|
|
|
method := values.Get("method")
|
|
|
|
uid := values.Get("channel")
|
|
|
|
if method == "delete" {
|
2018-07-07 14:40:04 +00:00
|
|
|
err := h.Backend.ChannelsDelete(uid)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
2018-05-22 18:29:07 +00:00
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
fmt.Fprintln(w, "[]")
|
|
|
|
h.Backend.(Debug).Debug()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jw := json.NewEncoder(w)
|
|
|
|
if uid == "" {
|
2018-07-07 14:40:04 +00:00
|
|
|
channel, err := h.Backend.ChannelsCreate(name)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
2018-05-22 18:29:07 +00:00
|
|
|
w.Header().Add("Content-Type", "application/json")
|
2018-07-07 14:40:04 +00:00
|
|
|
err = jw.Encode(channel)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
2018-05-22 18:29:07 +00:00
|
|
|
} else {
|
2018-07-07 14:40:04 +00:00
|
|
|
channel, err := h.Backend.ChannelsUpdate(uid, name)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
2018-05-22 18:29:07 +00:00
|
|
|
w.Header().Add("Content-Type", "application/json")
|
2018-07-07 14:40:04 +00:00
|
|
|
err = jw.Encode(channel)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
2018-05-22 18:29:07 +00:00
|
|
|
}
|
|
|
|
h.Backend.(Debug).Debug()
|
|
|
|
} else if action == "follow" {
|
|
|
|
uid := values.Get("channel")
|
|
|
|
url := values.Get("url")
|
|
|
|
h.HubIncomingBackend.CreateFeed(url, uid)
|
2018-07-07 14:40:04 +00:00
|
|
|
feed, err := h.Backend.FollowURL(uid, url)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
2018-05-22 18:29:07 +00:00
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
jw := json.NewEncoder(w)
|
2018-07-07 14:40:04 +00:00
|
|
|
err = jw.Encode(feed)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
2018-05-22 18:29:07 +00:00
|
|
|
} else if action == "unfollow" {
|
|
|
|
uid := values.Get("channel")
|
|
|
|
url := values.Get("url")
|
2018-07-07 14:40:04 +00:00
|
|
|
err := h.Backend.UnfollowURL(uid, url)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
2018-05-22 18:29:07 +00:00
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
fmt.Fprintln(w, "[]")
|
|
|
|
} else if action == "search" {
|
|
|
|
query := values.Get("query")
|
2018-07-07 14:40:04 +00:00
|
|
|
feeds, err := h.Backend.Search(query)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
2018-05-22 18:29:07 +00:00
|
|
|
jw := json.NewEncoder(w)
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
2018-07-07 14:40:04 +00:00
|
|
|
err = jw.Encode(map[string][]microsub.Feed{
|
2018-05-22 18:29:07 +00:00
|
|
|
"results": feeds,
|
|
|
|
})
|
2018-07-07 14:40:04 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
2018-05-22 18:29:07 +00:00
|
|
|
} else if action == "timeline" || r.PostForm.Get("action") == "timeline" {
|
|
|
|
method := values.Get("method")
|
|
|
|
|
|
|
|
if method == "mark_read" || r.PostForm.Get("method") == "mark_read" {
|
|
|
|
values = r.Form
|
|
|
|
channel := values.Get("channel")
|
2018-08-15 17:04:44 +00:00
|
|
|
var markAsRead []string
|
2018-05-22 18:29:07 +00:00
|
|
|
if uids, e := values["entry"]; e {
|
2018-08-15 17:04:44 +00:00
|
|
|
markAsRead = uids
|
2018-05-22 18:29:07 +00:00
|
|
|
} else if uids, e := values["entry[]"]; e {
|
2018-08-15 17:04:44 +00:00
|
|
|
markAsRead = uids
|
2018-05-22 18:29:07 +00:00
|
|
|
} else {
|
|
|
|
uids := []string{}
|
|
|
|
for k, v := range values {
|
|
|
|
if entryRegex.MatchString(k) {
|
|
|
|
uids = append(uids, v...)
|
|
|
|
}
|
|
|
|
}
|
2018-08-15 17:04:44 +00:00
|
|
|
markAsRead = uids
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(markAsRead) > 0 {
|
|
|
|
err := h.Backend.MarkRead(channel, markAsRead)
|
2018-07-07 14:40:04 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
2018-05-22 18:29:07 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-07-07 14:40:04 +00:00
|
|
|
http.Error(w, fmt.Sprintf("unknown method in timeline %s\n", method), 500)
|
|
|
|
return
|
2018-05-22 18:29:07 +00:00
|
|
|
}
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
fmt.Fprintln(w, "[]")
|
|
|
|
} else {
|
2018-07-07 14:40:04 +00:00
|
|
|
http.Error(w, fmt.Sprintf("unknown action %s\n", action), 500)
|
2018-05-22 18:29:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|