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":
/akspecs/ranger/blame/TODO?h=v1.5.0&id=039c03efad1df9ef7d9c1965454e98c46c53ef87'>^
f0df3fa5 ^
a986e2bd ^

3fe38754 ^

6a8d5d23 ^
aad61455 ^
7a268c8b ^

efdc7b16 ^
636f69d3 ^
d3c262a9 ^
cf8b174e ^
e02d47ed ^
48a8eab5 ^
2e631f44 ^
f3bc52e5 ^
3a917b8a ^
ff86460b ^
2f3326a4 ^
4be8b401 ^



d955e3f0 ^
75013dc7 ^
67bb838c ^
a808a661 ^
bba8d293 ^
2a64495f ^
5e449699 ^
8895b130 ^

7b04e507 ^
87db0130 ^
dd4a4145 ^
8f2f1767 ^
db1721dd ^
b624bd94 ^
83868c7a ^
6099d9a3 ^
9207e83c ^
f7db061b ^
6099d9a3 ^
f3bc52e5 ^
f01ef1a0 ^
ff86460b ^

e1d0613a ^
6f43de0a ^




fca1fc4f ^
f70ee6b2 ^
0db4c9b2 ^
338bbba4 ^

6908d0cc ^
338bbba4 ^


338bbba4 ^
c776804d ^
dab4db44 ^
cc4210ff ^
34c131ef ^
32f93303 ^
c1a9373c ^
ff86460b ^
66c5bb93 ^
338bbba4 ^





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117