summary refs log tree commit diff stats
path: root/svc
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-06-06 02:21:00 -0400
committerBen Morrison <ben@gbmor.dev>2019-06-06 02:22:56 -0400
commiteca8525e5811cad371e407507594fa95d8db98c1 (patch)
tree8f1467964c280dffd17f6c432efe0b03bf42bf3e /svc
parent1a15258ee5d93ffef8f8c768ff725d7429a3248c (diff)
downloadgetwtxt-eca8525e5811cad371e407507594fa95d8db98c1.tar.gz
deduplicated duplicate deduplication functions
Diffstat (limited to 'svc')
-rw-r--r--svc/handlers.go26
-rw-r--r--svc/query.go32
2 files changed, 18 insertions, 40 deletions
diff --git a/svc/handlers.go b/svc/handlers.go
index 45f3022..7f7c730 100644
--- a/svc/handlers.go
+++ b/svc/handlers.go
@@ -5,7 +5,6 @@ import (
 	"fmt"
 	"net/http"
 	"strconv"
-	"strings"
 
 	"github.com/getwtxt/registry"
 	"github.com/gorilla/mux"
@@ -167,28 +166,7 @@ func apiTagsHandler(w http.ResponseWriter, r *http.Request) {
 	vars := mux.Vars(r)
 	tags := vars["tags"]
 
-	tags = strings.ToLower(tags)
-	out, err := twtxtCache.QueryInStatus("#" + tags)
-	if err != nil {
-		log500(w, r, err)
-		return
-	}
-	tags = strings.Title(tags)
-	out2, err := twtxtCache.QueryInStatus("#" + tags)
-	if err != nil {
-		log500(w, r, err)
-		return
-	}
-	tags = strings.ToUpper(tags)
-	out3, err := twtxtCache.QueryInStatus("#" + tags)
-	if err != nil {
-		log500(w, r, err)
-		return
-	}
-
-	out = append(out, out2...)
-	out = append(out, out3...)
-	out = uniq(out)
+	out := compositeStatusQuery("#"+tags, r)
 
 	out = registry.ReduceToPage(1, out)
 	data := parseQueryOut(out)
@@ -197,7 +175,7 @@ func apiTagsHandler(w http.ResponseWriter, r *http.Request) {
 	w.Header().Set("ETag", etag)
 	w.Header().Set("Content-Type", txtutf8)
 
-	_, err = w.Write(data)
+	_, err := w.Write(data)
 	if err != nil {
 		log500(w, r, err)
 		return
diff --git a/svc/query.go b/svc/query.go
index 037b0e5..a27747d 100644
--- a/svc/query.go
+++ b/svc/query.go
@@ -19,6 +19,20 @@ func apiErrCheck(err error, r *http.Request) {
 	}
 }
 
+func dedupe(list []string) []string {
+	out := []string{}
+	seen := make(map[string]bool)
+
+	for _, e := range list {
+		if !seen[e] {
+			out = append(out, e)
+			seen[e] = true
+		}
+	}
+
+	return out
+}
+
 // Takes the output of queries and formats it for
 // an HTTP response. Iterates over the string slice,
 // appending each entry to a byte slice, and adding
@@ -37,20 +51,7 @@ func parseQueryOut(out []string) []byte {
 	return data
 }
 
-// Removes duplicate statuses from query output
-func uniq(str []string) []string {
-	keys := make(map[string]bool)
-	out := []string{}
-	for _, e := range str {
-		if _, ok := keys[e]; !ok {
-			keys[e] = true
-			out = append(out, e)
-		}
-	}
-	return out
-}
-
-// apiUserQuery is called via apiEndpointHandler when
+// apiEndpointQuery is called via apiEndpointHandler when
 // the endpoint is "users" and r.FormValue("q") is not empty.
 // It queries the registry cache for users or user URLs
 // matching the term supplied via r.FormValue("q")
@@ -123,9 +124,8 @@ func joinQueryOuts(data ...[]string) []string {
 	for _, e := range data {
 		single = append(single, e...)
 	}
-	single = uniq(single)
 
-	return single
+	return dedupe(single)
 }
 
 func compositeStatusQuery(query string, r *http.Request) []string {