summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-06-03 18:25:27 -0400
committerBen Morrison <ben@gbmor.dev>2019-06-03 18:25:27 -0400
commit90638ac1c20301257ddc08a77451efb4c7d99a66 (patch)
tree8faf5a781329754a9332bdc0e7a67a1dd44a3f9b
parent3bc43585acf951864993b8b7cf742575d34d5d87 (diff)
downloadgetwtxt-90638ac1c20301257ddc08a77451efb4c7d99a66.tar.gz
?page=N query added
-rw-r--r--handlers.go40
-rw-r--r--query.go58
2 files changed, 81 insertions, 17 deletions
diff --git a/handlers.go b/handlers.go
index edfb333..63b8c26 100644
--- a/handlers.go
+++ b/handlers.go
@@ -4,8 +4,10 @@ import (
 	"crypto/sha256"
 	"fmt"
 	"net/http"
+	"strconv"
 	"strings"
 
+	"github.com/getwtxt/registry"
 	"github.com/gorilla/mux"
 )
 
@@ -42,6 +44,30 @@ func apiFormatHandler(w http.ResponseWriter, r *http.Request) {
 	indexHandler(w, r)
 }
 
+func apiAllTweetsHandler(w http.ResponseWriter, r *http.Request) {
+	out, err := twtxtCache.QueryAllStatuses()
+	if err != nil {
+		log500(w, r, err)
+	}
+
+	data := parseQueryOut(out)
+	if err != nil {
+		data = []byte("")
+	}
+
+	etag := fmt.Sprintf("%x", sha256.Sum256(data))
+	w.Header().Set("ETag", etag)
+	w.Header().Set("Content-Type", txtutf8)
+
+	_, err = w.Write(data)
+	if err != nil {
+		log500(w, r, err)
+		return
+	}
+
+	log200(r)
+}
+
 // handles "/api/plain/(users|mentions|tweets)"
 func apiEndpointHandler(w http.ResponseWriter, r *http.Request) {
 
@@ -61,18 +87,30 @@ func apiEndpointHandler(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
+	page := 1
+	pageVal := r.FormValue("page")
+	if pageVal != "" {
+		page, err = strconv.Atoi(pageVal)
+		if err != nil || page == 0 {
+			page = 1
+		}
+	}
+
 	// if there's no query, return everything in
 	// registry for a given endpoint
 	var out []string
 	switch r.URL.Path {
 	case "/api/plain/users":
 		out, err = twtxtCache.QueryUser("")
+		out = registry.ReduceToPage(page, out)
 
 	case "/api/plain/mentions":
 		out, err = twtxtCache.QueryInStatus("@<")
+		out = registry.ReduceToPage(page, out)
 
 	default:
 		out, err = twtxtCache.QueryAllStatuses()
+		out = registry.ReduceToPage(page, out)
 	}
 
 	data := parseQueryOut(out)
@@ -107,6 +145,7 @@ func apiTagsBaseHandler(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
+	out = registry.ReduceToPage(1, out)
 	data := parseQueryOut(out)
 
 	etag := fmt.Sprintf("%x", sha256.Sum256(data))
@@ -151,6 +190,7 @@ func apiTagsHandler(w http.ResponseWriter, r *http.Request) {
 	out = append(out, out3...)
 	out = uniq(out)
 
+	out = registry.ReduceToPage(1, out)
 	data := parseQueryOut(out)
 
 	etag := fmt.Sprintf("%x", sha256.Sum256(data))
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
+}