Add support for timeline

This commit is contained in:
Romain de Laage 2022-04-29 10:35:43 +02:00
parent 1c5ead27b0
commit 1ae21d1c2b
Signed by: rdelaage
GPG Key ID: 534845FADDF0C329
2 changed files with 69 additions and 1 deletions

View File

@ -95,6 +95,42 @@ func getBlog(baseURL, account string) ([]Blog, error) {
return blogs, nil
}
func getTimeline(baseURL string) ([]Blog, error) {
var toots []Blog
if baseURL == "" {
log.Println("baseURL is empty")
return toots, fmt.Errorf("baseURL is empty")
}
resp, err := http.Get(baseURL + "/api/v1/timelines/public")
if err != nil {
log.Println("Mastodon API request: %s", err)
return toots, fmt.Errorf("API request failed")
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
log.Println("Mastodon API response: %s", resp.Status)
return toots, fmt.Errorf("API response is not 200")
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("Mastodon response body: %s", err)
return toots, fmt.Errorf("Failed to read response")
}
err = json.Unmarshal(body, &toots)
if err != nil {
log.Println("Mastodon response %s", err)
return toots, fmt.Errorf("Failed to parse response")
}
return toots, nil
}
func getAccount(baseURL, accountId string) (Account, error) {
if baseURL == "" || accountId == "" {
log.Println("baseURL or account is empty")

View File

@ -115,7 +115,7 @@ func handleConn(conn *tls.Conn, baseURL, title, home_message string, rateLimit i
if path == "" || path == "/" {
log.Println("Received request for home page")
_, err = fmt.Fprintf(conn, "20 text/gemini\r\n# %s\n\n%s\n\n=> /tag Search for a tag\n=> /about About MastoGem", title, home_message)
_, err = fmt.Fprintf(conn, "20 text/gemini\r\n# %s\n\n%s\n\n=> /timeline View public timeline\n=> /tag Search for a tag\n=> /about About MastoGem", title, home_message)
if err != nil {
log.Println("send error: %s", err)
return
@ -230,6 +230,10 @@ This capsule use %s Mastodon instance.
log.Println("Received request for toot " + path)
printToot(conn, baseURL, path)
} /* timeline */ else if strings.HasPrefix(path, "/timeline") {
log.Println("Received request for timeline")
printTimeline(conn, baseURL)
} else {
_, err = fmt.Fprintf(conn, "59 Invalid request\r\n")
if err != nil {
@ -323,6 +327,34 @@ func printProfileWithReblog(conn *tls.Conn, baseURL, profileID string) {
}
}
func printTimeline(conn *tls.Conn, baseURL string) {
toots, err := getTimeline(baseURL)
if err != nil {
_, err = fmt.Fprintf(conn, "40 Remote mastodon instance failed\r\n")
if err != nil {
log.Println("handleConn: %s", err)
return
}
return
}
// Print header
_, err = fmt.Fprintf(conn, "20 text/gemini\r\n")
if err != nil {
log.Println("handleConn: %s", err)
return
}
// Print toots
for _, toot := range toots {
_, err = fmt.Fprintf(conn, "\n%s\n=> /thread/%s View the thread\n", formatBlog(toot), toot.Id)
if err != nil {
log.Println("read timeline: %s", err)
return
}
}
}
func printToot(conn *tls.Conn, baseURL, tootID string) {
toot, err := getToot(baseURL, tootID)
if err != nil {