Compare commits

...

2 Commits

1 changed files with 50 additions and 8 deletions

58
bot.go
View File

@ -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()
@ -146,16 +181,23 @@ func RunBot() {
add_to_db(acct)
InfoLogger.Printf("%s added to database", acct)
}
message := fmt.Sprintf("@%s %s", acct, Conf.NotFollowedMessage)
_, err := postToot(message, "direct")
if err != nil {
ErrorLogger.Printf("Notify %s", acct)
if notif.Status.InReplyToID == nil { // Prevent spam in DM if status is reply
message := fmt.Sprintf("@%s %s", acct, Conf.NotFollowedMessage)
_, err := postToot(message, "direct")
if err != nil {
ErrorLogger.Printf("Notify %s", acct)
}
InfoLogger.Printf("%s has been notified", acct)
mark_notice(acct)
if got_notice(acct) == 0 {
InfoLogger.Printf("Dooble notice marked")
mark_notice(acct)
}
InfoLogger.Printf("%s marked notification in database", acct)
} else {
InfoLogger.Printf("%s their status is reply, not notified", acct)
}
InfoLogger.Printf("%s has been notified", acct)
mark_notice(acct)
InfoLogger.Printf("%s marked notification in database", acct)
}
}
}