diff options
author | Ben Morrison <ben@gbmor.dev> | 2019-06-03 18:25:27 -0400 |
---|---|---|
committer | Ben Morrison <ben@gbmor.dev> | 2019-06-03 18:25:27 -0400 |
commit | 90638ac1c20301257ddc08a77451efb4c7d99a66 (patch) | |
tree | 8faf5a781329754a9332bdc0e7a67a1dd44a3f9b /query.go | |
parent | 3bc43585acf951864993b8b7cf742575d34d5d87 (diff) | |
download | getwtxt-90638ac1c20301257ddc08a77451efb4c7d99a66.tar.gz |
?page=N query added
Diffstat (limited to 'query.go')
-rw-r--r-- | query.go | 58 |
1 files changed, 41 insertions, 17 deletions
diff --git a/query.go b/query.go index 554a53c..dbfb6ee 100644 --- a/query.go +++ b/query.go @@ -5,8 +5,10 @@ import ( "fmt" "log" "net/http" + "strconv" "strings" + "github.com/getwtxt/registry" "github.com/gorilla/mux" ) @@ -55,9 +57,16 @@ func uniq(str []string) []string { func apiEndpointQuery(w http.ResponseWriter, r *http.Request) error { query := r.FormValue("q") urls := r.FormValue("url") + pageVal := r.FormValue("page") var out []string var err error + pageVal = strings.TrimSpace(pageVal) + page, err := strconv.Atoi(pageVal) + if err != nil { + log.Printf("%v\n", err.Error()) + } + vars := mux.Vars(r) endpoint := vars["endpoint"] @@ -78,8 +87,9 @@ func apiEndpointQuery(w http.ResponseWriter, r *http.Request) error { apiErrCheck(err, r) } - out = append(out, out2...) - out = uniq(out) + if query != "" && urls != "" { + out = joinQueryOuts(out2) + } case "mentions": if urls == "" { @@ -90,26 +100,13 @@ func apiEndpointQuery(w http.ResponseWriter, r *http.Request) error { apiErrCheck(err, r) case "tweets": - query = strings.ToLower(query) - out, err = twtxtCache.QueryInStatus(query) - apiErrCheck(err, r) - - query = strings.Title(query) - out2, err := twtxtCache.QueryInStatus(query) - apiErrCheck(err, r) - - query = strings.ToUpper(query) - out3, err := twtxtCache.QueryInStatus(query) - apiErrCheck(err, r) - - out = append(out, out2...) - out = append(out, out3...) - out = uniq(out) + out = compositeStatusQuery(query, r) default: return fmt.Errorf("endpoint query, no cases match") } + out = registry.ReduceToPage(page, out) data := parseQueryOut(out) etag := fmt.Sprintf("%x", sha256.Sum256(data)) @@ -120,3 +117,30 @@ func apiEndpointQuery(w http.ResponseWriter, r *http.Request) error { return err } + +func joinQueryOuts(data ...[]string) []string { + single := []string{} + for _, e := range data { + single = append(single, e...) + } + single = uniq(single) + + return single +} + +func compositeStatusQuery(query string, r *http.Request) []string { + query = strings.ToLower(query) + out, err := twtxtCache.QueryInStatus(query) + apiErrCheck(err, r) + + query = strings.Title(query) + out2, err := twtxtCache.QueryInStatus(query) + apiErrCheck(err, r) + + query = strings.ToUpper(query) + out3, err := twtxtCache.QueryInStatus(query) + apiErrCheck(err, r) + + final := joinQueryOuts(out, out2, out3) + return final +} |