tiny 88x31 lexicon for atproto
1package handler
2
3import (
4 "encoding/json"
5 "log"
6 "net/http"
7 "strconv"
8 "tangled.org/moth11.net/88x31/types"
9 "time"
10)
11
12func (h *Handler) getButtons(w http.ResponseWriter, r *http.Request) {
13 limit := r.URL.Query().Get("limit")
14 limitI, err := strconv.Atoi(limit)
15 if err != nil {
16 limitI = 50
17 }
18 if limitI > 100 {
19 limitI = 100
20 }
21 if limitI < 1 {
22 limitI = 1
23 }
24 cursor := r.URL.Query().Get("cursor")
25 var cursorptr *time.Time
26 if cursor != "" {
27 t, err := time.Parse(time.RFC3339, cursor)
28 if err == nil {
29 cursorptr = &t
30 }
31 }
32 btnViews, ncursor, err := h.db.GetButtons(limitI, cursorptr, r.Context())
33 if err != nil {
34 log.Println(err)
35 http.Error(w, "error getting buttons!", http.StatusInternalServerError)
36 return
37 }
38 type Resp struct {
39 BtnViews []types.ButtonView `json:"button"`
40 Cursor *time.Time `json:"cursor,omitempty"`
41 }
42 myresp := Resp{}
43 if btnViews == nil {
44 myresp.BtnViews = make([]types.ButtonView, 0)
45 myresp.Cursor = nil
46 } else {
47 myresp.BtnViews = btnViews
48 myresp.Cursor = ncursor
49 }
50 w.Header().Set("Content-Type", "application/json")
51 encoder := json.NewEncoder(w)
52 err = encoder.Encode(myresp)
53 if err != nil {
54 log.Println(err)
55 http.Error(w, "error encoding response", http.StatusInternalServerError)
56 }
57}
58
59func (h *Handler) getLikedButtons(w http.ResponseWriter, r *http.Request) {
60 did := r.URL.Query().Get("did")
61
62 limita := r.URL.Query().Get("limit")
63 limit, err := strconv.Atoi(limita)
64 if err != nil {
65 limit = 50
66 }
67 if limit < 1 {
68 limit = 1
69 }
70 if limit > 100 {
71 limit = 100
72 }
73 cursor := r.URL.Query().Get("cursor")
74 var crsr *string
75 if cursor != "" {
76 crsr = &cursor
77 }
78 btns, ncursor, err := h.db.GetUserLikes(did, limit, crsr, r.Context())
79 if err != nil {
80 log.Println(err)
81 http.Error(w, "error", http.StatusInternalServerError)
82 return
83 }
84 type glbResp struct {
85 Cursor *time.Time `json:"cursor,omitempty"`
86 Buttons []types.ButtonView `json:"buttons"`
87 }
88 glbr := glbResp{
89 ncursor,
90 btns,
91 }
92 w.Header().Set("Content-Type", "application/json")
93 encoder := json.NewEncoder(w)
94 err = encoder.Encode(glbr)
95 if err != nil {
96 log.Println(err)
97 http.Error(w, "encoding error", http.StatusInternalServerError)
98 }
99}