eksterd: make preview also available as a post request
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Peter Stuifzand 2021-10-20 21:09:38 +02:00
parent 44b73e1c79
commit 9df63af33c
Signed by: peter
GPG Key ID: 374322D56E5209E8
2 changed files with 11 additions and 4 deletions

View File

@ -88,7 +88,7 @@ func (c *Client) microsubPostRequest(action string, args map[string]string) (*ht
if res.StatusCode != 200 {
msg, _ := ioutil.ReadAll(res.Body)
return nil, fmt.Errorf("unsuccessful response: %d: %q", res.StatusCode, string(msg))
return nil, fmt.Errorf("unsuccessful response: %d: %q", res.StatusCode, strings.TrimSpace(string(msg)))
}
return res, err
@ -116,7 +116,7 @@ func (c *Client) microsubPostFormRequest(action string, args map[string]string,
if res.StatusCode != 200 {
msg, _ := ioutil.ReadAll(res.Body)
return nil, fmt.Errorf("unsuccessful response: %d: %q", res.StatusCode, string(msg))
return nil, fmt.Errorf("unsuccessful response: %d: %q", res.StatusCode, strings.TrimSpace(string(msg)))
}
return res, err
@ -180,7 +180,7 @@ func (c *Client) TimelineGet(before, after, channel string) (microsub.Timeline,
func (c *Client) PreviewURL(url string) (microsub.Timeline, error) {
args := make(map[string]string)
args["url"] = url
res, err := c.microsubGetRequest("preview", args)
res, err := c.microsubPostRequest("preview", args)
if err != nil {
return microsub.Timeline{}, err
}

View File

@ -125,7 +125,7 @@ func (h *microsubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
http.Error(w, "internal server error", 500)
}
} else {
http.Error(w, fmt.Sprintf("unknown action %s\n", action), 400)
http.Error(w, fmt.Sprintf("unknown action %s", action), 400)
return
}
return
@ -182,6 +182,13 @@ func (h *microsubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
respondJSON(w, []string{})
} else if action == "preview" {
timeline, err := h.backend.PreviewURL(values.Get("url"))
if err != nil {
http.Error(w, err.Error(), 500)
return
}
respondJSON(w, timeline)
} else if action == "search" {
query := values.Get("query")
channel := values.Get("channel")