Some fixes and improvments get AP object

This commit is contained in:
localhost_frssoft 2022-11-26 21:44:11 +03:00
parent fa1b4eed29
commit bf14452ab7
1 changed files with 19 additions and 7 deletions

26
bot.go
View File

@ -6,8 +6,10 @@ import (
"fmt" "fmt"
"regexp" "regexp"
"strings" "strings"
"io"
"net/http" "net/http"
"encoding/json" "encoding/json"
"compress/gzip"
"github.com/mattn/go-mastodon" "github.com/mattn/go-mastodon"
) )
@ -29,6 +31,9 @@ type APobject struct {
InReplyTo *string `json:"inReplyTo"` InReplyTo *string `json:"inReplyTo"`
} }
//CheckAPReply return is reply bool of status
//Note: Not working with servers when they required Authorized fetch
//Note: By default false
func CheckAPReply(tooturl string) (bool) { func CheckAPReply(tooturl string) (bool) {
var apobj APobject var apobj APobject
client := &http.Client{} client := &http.Client{}
@ -40,25 +45,32 @@ func CheckAPReply(tooturl string) (bool) {
req.Header.Set("Accept", "application/activity+json") req.Header.Set("Accept", "application/activity+json")
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
ErrorLogger.Println("get AP object") ErrorLogger.Printf("Server was not return AP object: %s", err)
return false return false
} }
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
ErrorLogger.Println(resp.Body) ErrorLogger.Printf("AP get: Server was return %s http code %s", resp.StatusCode, resp.Body)
ErrorLogger.Println("Failed AP object")
return false return false
} }
err = json.NewDecoder(resp.Body).Decode(&apobj)
var reader io.ReadCloser
switch resp.Header.Get("Content-Encoding") {
case "gzip":
reader, err = gzip.NewReader(resp.Body)
defer reader.Close()
default:
reader = resp.Body
}
err = json.NewDecoder(reader).Decode(&apobj)
if err != nil { if err != nil {
ErrorLogger.Println("Failed decoding AP object") ErrorLogger.Println("Failed decoding AP object")
return false return false
} }
InfoLogger.Println(resp.Body)
if apobj.InReplyTo != nil { if apobj.InReplyTo != nil {
InfoLogger.Println("AP object of status detected reply") WarnLogger.Printf("AP object of status detected reply: %s", apobj.InReplyTo)
InfoLogger.Println(apobj.InReplyTo)
return true return true
} }
return false return false