127 lines
2.3 KiB
Go
127 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
)
|
|
|
|
type AkkomaAccount struct {
|
|
Id string `json:"id"`
|
|
Note string `json:"note"`
|
|
}
|
|
|
|
type AkkomaFollowee struct {
|
|
Fqn string `json:"fqn"`
|
|
}
|
|
|
|
func AkkomaGetUser(u User) AkkomaAccount {
|
|
client := &http.Client{}
|
|
|
|
req, err := http.NewRequest("GET", "https://"+u.Instance+"/api/v1/accounts/"+u.Name, nil)
|
|
if err != nil {
|
|
fmt.Printf("error: %s\n", err)
|
|
return AkkomaAccount{
|
|
Id: "",
|
|
Note: "",
|
|
}
|
|
}
|
|
req.Header.Add("Content-Type", "application/json")
|
|
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
fmt.Printf("error: %s\n", err)
|
|
return AkkomaAccount{
|
|
Id: "",
|
|
Note: "",
|
|
}
|
|
}
|
|
|
|
jsonDecoder := json.NewDecoder(resp.Body)
|
|
var account AkkomaAccount
|
|
err = jsonDecoder.Decode(&account)
|
|
if err != nil {
|
|
fmt.Printf("error: %s\n", err)
|
|
return AkkomaAccount{
|
|
Id: "",
|
|
Note: "",
|
|
}
|
|
}
|
|
|
|
return account
|
|
}
|
|
|
|
func AkkomaGetId(u User) string {
|
|
return AkkomaGetUser(u).Id
|
|
}
|
|
|
|
func AkkomaGetFollowing(u User) []User {
|
|
client := &http.Client{}
|
|
|
|
if !checkConsent(AkkomaGetUser(u).Note) {
|
|
return nil
|
|
}
|
|
|
|
req, err := http.NewRequest("GET", "https://"+u.Instance+"/api/v1/accounts/"+AkkomaGetId(u)+"/following?limit=100", nil)
|
|
if err != nil {
|
|
fmt.Printf("error: %s\n", err)
|
|
return nil
|
|
}
|
|
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
fmt.Printf("error: %s\n", err)
|
|
return nil
|
|
}
|
|
|
|
jsonDecoder := json.NewDecoder(resp.Body)
|
|
var following []AkkomaFollowee
|
|
err = jsonDecoder.Decode(&following)
|
|
if err != nil {
|
|
fmt.Printf("error: %s\n", err)
|
|
return nil
|
|
}
|
|
|
|
var users []User
|
|
for _, followee := range following {
|
|
users = append(users, MakeUser(followee.Fqn))
|
|
}
|
|
|
|
return users
|
|
}
|
|
|
|
func AkkomaGetFollowers(u User) []User {
|
|
client := &http.Client{}
|
|
|
|
if !checkConsent(AkkomaGetUser(u).Note) {
|
|
return nil
|
|
}
|
|
|
|
req, err := http.NewRequest("GET", "https://"+u.Instance+"/api/v1/accounts/"+AkkomaGetId(u)+"/followers?limit=100", nil)
|
|
if err != nil {
|
|
fmt.Printf("error: %s\n", err)
|
|
return nil
|
|
}
|
|
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
fmt.Printf("error: %s\n", err)
|
|
return nil
|
|
}
|
|
|
|
jsonDecoder := json.NewDecoder(resp.Body)
|
|
var followers []AkkomaFollowee
|
|
err = jsonDecoder.Decode(&followers)
|
|
if err != nil {
|
|
fmt.Printf("error: %s\n", err)
|
|
return nil
|
|
}
|
|
|
|
var users []User
|
|
for _, follower := range followers {
|
|
users = append(users, MakeUser(follower.Fqn))
|
|
}
|
|
|
|
return users
|
|
}
|