summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--assets/tmpl/index.html8
-rw-r--r--go.mod2
-rw-r--r--go.sum4
-rw-r--r--handlers.go40
-rw-r--r--main.go40
-rw-r--r--query.go58
6 files changed, 117 insertions, 35 deletions
diff --git a/assets/tmpl/index.html b/assets/tmpl/index.html
index 9827646..15856f7 100644
--- a/assets/tmpl/index.html
+++ b/assets/tmpl/index.html
@@ -34,13 +34,15 @@
 /api/plain/mentions
 /api/plain/tweets
 /api/plain/tags</code></pre>
+      <p>All queries accept <code>?page=N</code> as a parameter, returning sets of 20 results. 
+      This may be omitted for the first page of results.</p>
       <p>Query by user:</p>
         <pre><code>$ curl '{{.URL}}/api/plain/users?q=foo'
 foo               https://example.com/twtxt.txt     2019-05-09T08:42:23.000Z
 foobar            https://example2.com/twtxt.txt    2019-03-14T19:23:00.000Z
 foo_barrington    https://example3.com/twtxt.txt    2019-05-01T15:59:39.000Z</code></pre>
       <p>Query by tweet content:</p>
-        <pre><code>$ curl '{{.URL}}/api/plain/tweets?q=getwtxt'
+        <pre><code>$ curl '{{.URL}}/api/plain/tweets?q=getwtxt&amp;page=2'
 foo_barrington    https://example3.com/twtxt.txt    2019-04-30T06:00:09.000Z    I just built getwtxt, time to set it up!</code></pre>
       <p>Query by mention:</p>
         <pre><code>$ curl '{{.URL}}/api/plain/mentions?url=https://foobarrington.co.uk/twtxt.txt'
@@ -48,8 +50,8 @@ foo    https://example.com/twtxt.txt    2019-02-26T11:06:44.000Z    @&lt;foo_bar
       <p>Query by tag:</p>
         <pre><code>$ curl '{{.URL}}/api/plain/tags/programming'
 foo    https://example.com/twtxt.txt    2019-03-01T09:31:02.000Z    I love #programming!</code></pre>
-      <p>Get latest 20 tweets:</p>
-        <pre><code>$ curl '{{.URL}}/api/plain/tweets'
+      <p>Get 20 tweets:</p>
+        <pre><code>$ curl '{{.URL}}/api/plain/tweets?page=2'
 foobar    https://example2.com/twtxt.txt    2019-05-13T12:46:20.000Z    It's been a busy day at work!
 ...</code></pre>
       <p>Get all users:</p>
diff --git a/go.mod b/go.mod
index ec32001..4bb494a 100644
--- a/go.mod
+++ b/go.mod
@@ -5,7 +5,7 @@ go 1.11
 require (
 	github.com/BurntSushi/toml v0.3.1 // indirect
 	github.com/fsnotify/fsnotify v1.4.7
-	github.com/getwtxt/registry v0.0.0-20190531085138-9a0f6b50332c
+	github.com/getwtxt/registry v0.2.2
 	github.com/gorilla/handlers v1.4.0
 	github.com/gorilla/mux v1.7.1
 	github.com/spf13/pflag v1.0.3
diff --git a/go.sum b/go.sum
index d9c4404..ac90169 100644
--- a/go.sum
+++ b/go.sum
@@ -8,8 +8,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
 github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
-github.com/getwtxt/registry v0.0.0-20190531085138-9a0f6b50332c h1:qALsi03YJHPb5CXBxYyuGREh0v8OLravaNMwkkHu944=
-github.com/getwtxt/registry v0.0.0-20190531085138-9a0f6b50332c/go.mod h1:BGSIALOFqIRj+ACLB8etWGUOgFAKN8oFDpCsw6YOdYQ=
+github.com/getwtxt/registry v0.2.2 h1:vaDgmyojGcgzBVArFbO4RwpF3zLTIGHOALmHsYZpjLQ=
+github.com/getwtxt/registry v0.2.2/go.mod h1:BGSIALOFqIRj+ACLB8etWGUOgFAKN8oFDpCsw6YOdYQ=
 github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
 github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
 github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db h1:woRePGFeVFfLKN/pOkfl+p/TAqKOfFu+7KPlMVpok/w=
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/main.go b/main.go
index 6f50f68..a3f0133 100644
--- a/main.go
+++ b/main.go
@@ -20,34 +20,38 @@ func main() {
 	api := index.PathPrefix("/api").Subrouter()
 
 	index.Path("/").
-		Methods("GET").
+		Methods("GET", "HEAD").
 		HandlerFunc(indexHandler)
 
 	index.Path("/css").
-		Methods("GET").
+		Methods("GET", "HEAD").
 		HandlerFunc(cssHandler)
 
 	index.Path("/api").
-		Methods("GET").
+		Methods("GET", "HEAD").
 		HandlerFunc(apiBaseHandler)
 
 	// twtxt will add support for other formats later.
 	// Maybe json? Making this future-proof.
 	api.Path("/{format:(?:plain)}").
-		Methods("GET").
+		Methods("GET", "HEAD").
 		HandlerFunc(apiFormatHandler)
 
+	// Non-standard API call to list *all* tweets
+	// in a single request.
+	api.Path("/{format:(?:plain)}/tweets/all").
+		Methods("GET", "HEAD").
+		HandlerFunc(apiAllTweetsHandler)
+
 	// Specifying the endpoint with and without query information.
 	// Will return 404 on empty queries otherwise.
 	api.Path("/{format:(?:plain)}/{endpoint:(?:mentions|users|tweets)}").
-		Methods("GET").
+		Methods("GET", "HEAD").
 		HandlerFunc(apiEndpointHandler)
 
-	// Using stdlib net/url to validate the input URLs rather than regex.
-	// Validating a URL with regex is unwieldy
 	api.Path("/{format:(?:plain)}/{endpoint:(?:mentions|users|tweets)}").
-		Queries("url", "{url}", "q", "{query}").
-		Methods("GET").
+		Queries("url", "{url}", "q", "{query}", "page", "{[0-9]+}").
+		Methods("GET", "HEAD").
 		HandlerFunc(apiEndpointHandler)
 
 	// This is for submitting new users. Both query variables must exist
@@ -64,7 +68,7 @@ func main() {
 		Methods("POST").
 		HandlerFunc(apiEndpointPOSTHandler)
 
-	// This is for submitting new users incorrectly
+	// This is also for submitting new users incorrectly
 	// and letting the requester know about their error.
 	api.Path("/{format:(?:plain)}/{endpoint:users}").
 		Queries("nickname", "{nickname:[a-zA-Z0-9_-]+}").
@@ -73,12 +77,24 @@ func main() {
 
 	// Show all observed tags
 	api.Path("/{format:(?:plain)}/tags").
-		Methods("GET").
+		Methods("GET", "HEAD").
+		HandlerFunc(apiTagsBaseHandler)
+
+	// Show Nth page of all observed tags
+	api.Path("/{format:(?:plain)}/tags").
+		Queries("page", "{[0-9]+}").
+		Methods("GET", "HEAD").
 		HandlerFunc(apiTagsBaseHandler)
 
 	// Requests statuses with a specific tag
 	api.Path("/{format:(?:plain)}/tags/{tags:[a-zA-Z0-9_-]+}").
-		Methods("GET").
+		Methods("GET", "HEAD").
+		HandlerFunc(apiTagsHandler)
+
+	// Requests Nth page of statuses with a specific tag
+	api.Path("/{format:(?:plain)}/tags/{tags:[a-zA-Z0-9_-]+}").
+		Queries("page", "{[0-9]+}").
+		Methods("GET", "HEAD").
 		HandlerFunc(apiTagsHandler)
 
 	confObj.Mu.RLock()
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
+}
5-21 14:31:14 -0400 Use kebab-case for cred-cmds' href='/akspecs/aerc/commit/doc/aerc-config.5.scd?h=0.3.0&id=176245208d40a9ca2ec324be7863a22819de29bc'>1762452 ^
b820850 ^
1762452 ^
b820850 ^
fc719e4 ^






7a26b48 ^
0016775 ^
fc719e4 ^


1762452 ^
ca2cd00 ^
1762452 ^
b820850 ^
e2d5c45 ^







b820850 ^
fc719e4 ^


























































































4df8e7f ^
fc719e4 ^
































































4875813 ^
0016775 ^
fc719e4 ^





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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513