202 lines
5.4 KiB
Go
202 lines
5.4 KiB
Go
/*
|
|
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 <https://www.gnu.org/licenses/>.
|
|
*/
|
|
package main
|
|
|
|
import (
|
|
"net/http"
|
|
"log"
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
"fmt"
|
|
)
|
|
|
|
func getBlog(baseURL, account string) ([]Blog, error) {
|
|
if baseURL == "" || account == "" {
|
|
log.Println("baseURL or account is empty")
|
|
return nil, fmt.Errorf("BaseURL or account is empty")
|
|
}
|
|
|
|
resp, err := http.Get(baseURL + "/api/v1/accounts/" + account + "/statuses?exclude_reblogs=true&exclude_replies=true")
|
|
if err != nil {
|
|
log.Println("Mastodon API request: %s", err)
|
|
return nil, fmt.Errorf("Failed to request Mastodon API")
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != 200 {
|
|
log.Println("Mastodon API response: %s", resp.Status)
|
|
return nil, fmt.Errorf("Mastodon instance failed")
|
|
}
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
log.Println("Mastodon response body: %s", err)
|
|
return nil, fmt.Errorf("Failed to read Mastodon response body")
|
|
}
|
|
|
|
var blogs []Blog
|
|
err = json.Unmarshal(body, &blogs)
|
|
if err != nil {
|
|
log.Println("Mastodon response %s", err)
|
|
return nil, fmt.Errorf("Failed to parse Mastodon response")
|
|
}
|
|
|
|
return blogs, nil
|
|
}
|
|
|
|
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
|
|
err = json.Unmarshal(body, &account)
|
|
if err != nil {
|
|
log.Println("Mastodon response %s", err)
|
|
return Account{}, fmt.Errorf("Failed to parse response")
|
|
}
|
|
|
|
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
|
|
err = json.Unmarshal(body, &toot)
|
|
if err != nil {
|
|
log.Println("Mastodon response %s", err)
|
|
return Blog{}, fmt.Errorf("Failed to parse response")
|
|
}
|
|
|
|
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
|
|
err = json.Unmarshal(body, &thread)
|
|
if err != nil {
|
|
log.Println("Mastodon response %s", err)
|
|
return Thread{}, fmt.Errorf("Failed to parse response")
|
|
}
|
|
|
|
return thread, nil
|
|
}
|
|
|
|
func getTag(baseURL, tag string) ([]Blog, error) {
|
|
if baseURL == "" || tag == "" {
|
|
log.Println("baseURL or tag is empty")
|
|
return nil, fmt.Errorf("baseURL or tag is empty")
|
|
}
|
|
|
|
resp, err := http.Get(baseURL + "/api/v1/timelines/tag/" + tag)
|
|
if err != nil {
|
|
log.Println("Mastodon API request: %s", err)
|
|
return nil, fmt.Errorf("API request failed")
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != 200 {
|
|
log.Println("Mastodon API response: %s", resp.Status)
|
|
return nil, fmt.Errorf("API response is not 200")
|
|
}
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
log.Println("Mastodon response body: %s", err)
|
|
return nil, fmt.Errorf("Failed to read response")
|
|
}
|
|
|
|
var blogs []Blog
|
|
err = json.Unmarshal(body, &blogs)
|
|
if err != nil {
|
|
log.Println("Mastodon response: %s", err)
|
|
return nil, fmt.Errorf("Failed to parse response")
|
|
}
|
|
|
|
return blogs, nil
|
|
}
|