From 9daa1acca225428fc08efdac5026a3d661e8f021 Mon Sep 17 00:00:00 2001 From: localhost_frssoft Date: Fri, 25 Nov 2022 03:10:27 +0300 Subject: [PATCH] Add check status reply by AP object --- bot.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/bot.go b/bot.go index 3812196..69f3d10 100644 --- a/bot.go +++ b/bot.go @@ -6,6 +6,8 @@ import ( "fmt" "regexp" "strings" + "net/http" + "encoding/json" "github.com/mattn/go-mastodon" ) @@ -23,6 +25,10 @@ var ( my_account, _ = c.GetAccountCurrentUser(ctx) ) +type APobject struct { + InReplyTo *string `json:"inReplyTo"` +} + func RunBot() { events, err := c.StreamingUser(ctx) if err != nil { @@ -37,6 +43,7 @@ func RunBot() { } notif := notifEvent.Notification + client := &http.Client{} // New follower if notif.Type == "follow" { @@ -73,6 +80,34 @@ func RunBot() { // Follow check if relationship[0].FollowedBy { if notif.Status.Visibility == "public" { // Reblog toot + if notif.Status.InReplyToID == nil { + // Replies protection by get ActivityPub object + // (if breaking threads) + var apobj APobject + req, err := http.NewRequest(http.MethodGet, tooturl, nil) + if err != nil { + ErrorLogger.Println("Failed http request status AP") + } + req.Header.Set("Accept", "application/activity+json") + resp, err := client.Do(req) + if err != nil { + ErrorLogger.Println("get AP object") + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + ErrorLogger.Println("Failed AP object") + } + InfoLogger.Println(resp.Body) + err = json.NewDecoder(resp.Body).Decode(&apobj) + if err != nil { + ErrorLogger.Println("Failed decoding AP object") + } + if &apobj.InReplyTo != nil { + InfoLogger.Println("AP object of status detected reply") + notif.Status.InReplyToID = &apobj.InReplyTo + } + } if notif.Status.InReplyToID == nil { // Not boost replies // Duplicate protection content_hash := sha512.New()