summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-05-21 23:44:03 -0400
committerBen Morrison <ben@gbmor.dev>2019-05-22 00:26:16 -0400
commit37be25ffddafdf5b7ed4c86b25e3e1ad3a6b3362 (patch)
tree63dcbc8d46e1c5089fc339bc2ad07745a9d4e51b
parent54a4f6f720979dc8c8d6224acd24f04d147ef761 (diff)
downloadgetwtxt-37be25ffddafdf5b7ed4c86b25e3e1ad3a6b3362.tar.gz
tuning query handling
-rw-r--r--handlers.go41
-rw-r--r--http.go4
-rw-r--r--http_test.go2
-rw-r--r--post.go19
-rw-r--r--query.go14
5 files changed, 54 insertions, 26 deletions
diff --git a/handlers.go b/handlers.go
index 2bf74f8..947c96a 100644
--- a/handlers.go
+++ b/handlers.go
@@ -7,6 +7,7 @@ import (
 	"log"
 	"net/http"
 	"os"
+	"strings"
 
 	"github.com/gorilla/mux"
 )
@@ -75,19 +76,21 @@ func apiEndpointHandler(w http.ResponseWriter, r *http.Request) {
 	// if there's no query, return everything in
 	// registry for a given endpoint
 	var out []string
-	if r.URL.Path == "/api/plain/users" {
+	switch r.URL.Path {
+	case "/api/plain/users":
 		out, err = twtxtCache.QueryUser("")
-	} else if r.URL.Path == "/api/plain/mentions" {
+
+	case "/api/plain/mentions":
 		out, err = twtxtCache.QueryInStatus("@<")
-	} else {
+
+	default:
 		out, err = twtxtCache.QueryAllStatuses()
 	}
-	if err != nil {
-		log500(w, r, err)
-		return
-	}
 
 	data := parseQueryOut(out)
+	if err != nil {
+		data = []byte("")
+	}
 
 	w.Header().Set("Content-Type", txtutf8)
 
@@ -133,11 +136,33 @@ func apiTagsHandler(w http.ResponseWriter, r *http.Request) {
 	vars := mux.Vars(r)
 	tags := vars["tags"]
 
-	out, err := twtxtCache.QueryInStatus(tags)
+	out, err := twtxtCache.QueryInStatus("#" + tags)
+	if err != nil {
+		log500(w, r, err)
+		return
+	}
+	tags = strings.ToLower(tags)
+	out2, err := twtxtCache.QueryInStatus("#" + tags)
+	if err != nil {
+		log500(w, r, err)
+		return
+	}
+	tags = strings.Title(tags)
+	out3, err := twtxtCache.QueryInStatus("#" + tags)
 	if err != nil {
 		log500(w, r, err)
 		return
 	}
+	tags = strings.ToUpper(tags)
+	out4, err := twtxtCache.QueryInStatus("#" + tags)
+	if err != nil {
+		log500(w, r, err)
+		return
+	}
+
+	out = append(out, out2...)
+	out = append(out, out3...)
+	out = append(out, out4...)
 
 	data := parseQueryOut(out)
 
diff --git a/http.go b/http.go
index b6c1cee..8ba0442 100644
--- a/http.go
+++ b/http.go
@@ -45,10 +45,10 @@ func log200(r *http.Request) {
 }
 
 // log output for 400s
-func log400(w http.ResponseWriter, r *http.Request, err error) {
+func log400(w http.ResponseWriter, r *http.Request, err string) {
 	uip := getIPFromCtx(r.Context())
 	log.Printf("*** %v :: 400 :: %v %v :: %v\n", uip, r.Method, r.URL, err)
-	http.Error(w, "400 Bad Request: "+err.Error(), http.StatusBadRequest)
+	http.Error(w, "400 Bad Request: "+err, http.StatusBadRequest)
 }
 
 // log output for 404s
diff --git a/http_test.go b/http_test.go
index 67ccc88..a3cc90d 100644
--- a/http_test.go
+++ b/http_test.go
@@ -12,7 +12,7 @@ func Test_log400(t *testing.T) {
 	t.Run("log400", func(t *testing.T) {
 		w := httptest.NewRecorder()
 		req := httptest.NewRequest("POST", "/400", nil)
-		log400(w, req, errors.New("400 Test"))
+		log400(w, req, "400 Test")
 		resp := w.Result()
 		if resp.StatusCode != http.StatusBadRequest {
 			t.Errorf("Didn't receive 400, received: %v\n", resp.StatusCode)
diff --git a/post.go b/post.go
index 25d1902..4078cf6 100644
--- a/post.go
+++ b/post.go
@@ -14,13 +14,14 @@ import (
 // registry before adding each user to the local cache.
 func apiPostUser(w http.ResponseWriter, r *http.Request) {
 	if err := r.ParseForm(); err != nil {
-		log400(w, r, err)
+		log400(w, r, err.Error())
 		return
 	}
+
 	nick := r.FormValue("nickname")
 	urls := r.FormValue("url")
 	if nick == "" || urls == "" {
-		log400(w, r, fmt.Errorf("Nickname or URL missing"))
+		log400(w, r, "Nickname or URL missing")
 		return
 	}
 
@@ -28,7 +29,7 @@ func apiPostUser(w http.ResponseWriter, r *http.Request) {
 
 	out, remoteRegistry, err := registry.GetTwtxt(urls)
 	if err != nil {
-		log400(w, r, err)
+		log400(w, r, err.Error())
 		return
 	}
 
@@ -37,9 +38,8 @@ func apiPostUser(w http.ResponseWriter, r *http.Request) {
 		remoteRegistries.List = append(remoteRegistries.List, urls)
 		remoteRegistries.Mu.Unlock()
 
-		err := twtxtCache.ScrapeRemoteRegistry(urls)
-		if err != nil {
-			log400(w, r, err)
+		if err := twtxtCache.ScrapeRemoteRegistry(urls); err != nil {
+			log400(w, r, err.Error())
 			return
 		}
 		log200(r)
@@ -48,13 +48,12 @@ func apiPostUser(w http.ResponseWriter, r *http.Request) {
 
 	statuses, err := registry.ParseUserTwtxt(out, nick, urls)
 	if err != nil {
-		log400(w, r, err)
+		log400(w, r, err.Error())
 		return
 	}
 
-	err = twtxtCache.AddUser(nick, urls, uip, statuses)
-	if err != nil {
-		log400(w, r, err)
+	if err := twtxtCache.AddUser(nick, urls, uip, statuses); err != nil {
+		log400(w, r, err.Error())
 		return
 	}
 
diff --git a/query.go b/query.go
index c47cfb0..156ccda 100644
--- a/query.go
+++ b/query.go
@@ -55,19 +55,23 @@ func apiEndpointQuery(w http.ResponseWriter, r *http.Request) error {
 	// something went very wrong.
 	switch endpoint {
 	case "users":
-		if urls != "" {
-			out2, err = twtxtCache.QueryUser(urls)
+		if query != "" {
+			out2, err = twtxtCache.QueryUser(query)
 			out = append(out, out2...)
 			apiErrCheck(err, r)
 		}
-		if query != "" {
-			out2, err = twtxtCache.QueryUser(query)
+		if urls != "" {
+			out2, err = twtxtCache.QueryUser(urls)
 			out = append(out, out2...)
 			apiErrCheck(err, r)
 		}
 
 	case "mentions":
-		out, err = twtxtCache.QueryInStatus(query)
+		if urls == "" {
+			return fmt.Errorf("missing URL in mention query")
+		}
+		urls += ">"
+		out, err = twtxtCache.QueryInStatus(urls)
 		apiErrCheck(err, r)
 
 	case "tweets":