/* MastoGem, A Mastodon proxy for Gemini Copyright (C) 2021 Romain de Laage This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ package main import ( "net/http" "log" "encoding/json" "io/ioutil" "fmt" ) func getBlog(baseURL, account string) []Blog { if baseURL == "" || account == "" { log.Println("baseURL or account is empty") return nil } resp, err := http.Get(baseURL + "/api/v1/accounts/" + account + "/statuses?exclude_reblogs=true&exlude_replies=true") if err != nil { log.Println("Mastodon API request: %s", err) return nil } defer resp.Body.Close() if resp.StatusCode != 200 { log.Println("Mastodon API response: %s", resp.Status) return nil } body, err := ioutil.ReadAll(resp.Body) if err != nil { log.Println("Mastodon response body: %s", err) } var blogs []Blog json.Unmarshal(body, &blogs) return blogs } func getAccount(baseURL, accountId string) (Account, error) { if baseURL == "" || accountId == "" { log.Println("baseURL or account is empty") return Account{}, fmt.Errorf("baseURL or account is empty") } resp, err := http.Get(baseURL + "/api/v1/accounts/" + accountId) if err != nil { log.Println("Mastodon API request: %s", err) return Account{}, fmt.Errorf("API request failed") } defer resp.Body.Close() if resp.StatusCode != 200 { log.Println("Mastodon API response: %s", resp.Status) return Account{}, fmt.Errorf("API response is not 200") } body, err := ioutil.ReadAll(resp.Body) if err != nil { log.Println("Mastodon response body: %s", err) return Account{}, fmt.Errorf("Failed to read response") } var account Account json.Unmarshal(body, &account) return account, nil } func getToot(baseURL, tootId string) (Blog, error) { if baseURL == "" || tootId == "" { log.Println("baseURL or tootID is empty") return Blog{}, fmt.Errorf("baseURL or tootID is empty") } resp, err := http.Get(baseURL + "/api/v1/statuses/" + tootId) if err != nil { log.Println("Mastodon API request: %s", err) return Blog{}, fmt.Errorf("API request failed") } defer resp.Body.Close() if resp.StatusCode != 200 { log.Println("Mastodon API response: %s", resp.Status) return Blog{}, fmt.Errorf("API response is not 200") } body, err := ioutil.ReadAll(resp.Body) if err != nil { log.Println("Mastodon response body: %s", err) return Blog{}, fmt.Errorf("Failed to read response") } var toot Blog json.Unmarshal(body, &toot) return toot, nil } func getThread(baseURL, tootId string) (Thread, error) { if baseURL == "" || tootId == "" { log.Println("baseURL or tootID is empty") return Thread{}, fmt.Errorf("baseURL or tootID is empty") } resp, err := http.Get(baseURL + "/api/v1/statuses/" + tootId + "/context") if err != nil { log.Println("Mastodon API request: %s", err) return Thread{}, fmt.Errorf("API request failed") } defer resp.Body.Close() if resp.StatusCode != 200 { log.Println("Mastodon API response: %s", resp.Status) return Thread{}, fmt.Errorf("API response is not 200") } body, err := ioutil.ReadAll(resp.Body) if err != nil { log.Println("Mastodon response body: %s", err) return Thread{}, fmt.Errorf("Failed to read response") } var thread Thread json.Unmarshal(body, &thread) return thread, nil }