Skip to content

Commit

Permalink
Merge pull request #13 from micr0-dev/feature/safetyDelete
Browse files Browse the repository at this point in the history
Delete AltBot's Reply if Post is Deleted Within 1 Hour
  • Loading branch information
micr0-dev authored Dec 3, 2024
2 parents a55908d + 1416727 commit 3b9c0d6
Showing 1 changed file with 53 additions and 3 deletions.
56 changes: 53 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"os"
"os/exec"
"strings"
"sync"
"time"

"github.com/BurntSushi/toml"
"golang.org/x/image/bmp"
Expand All @@ -27,7 +29,7 @@ import (
)

// Version of the bot
const Version = "1.2.0"
const Version = "1.2.1"

// AsciiArt is the ASCII art for the bot
const AsciiArt = ` _ _ _ ___ _
Expand Down Expand Up @@ -148,6 +150,9 @@ func main() {
go startWeeklySummaryScheduler(c)
}

// Start a goroutine for periodic cleanup of old reply entries
go cleanupOldEntries()

fmt.Println("Connected to streaming API. All systems operational. Waiting for mentions and follows...")

// Main event loop
Expand Down Expand Up @@ -197,7 +202,7 @@ func main() {
case *mastodon.ErrorEvent:
log.Printf("Error event: %v", e.Error())
case *mastodon.DeleteEvent:
log.Printf("Delete event: status ID %v", e.ID)
handleDeleteEvent(c, e.ID)
default:
log.Printf("Unhandled event type: %T", e)
}
Expand Down Expand Up @@ -492,7 +497,7 @@ func generateAndPostAltText(c *mastodon.Client, status *mastodon.Status, replyTo
visibility = "direct"
}

_, err = c.PostStatus(ctx, &mastodon.Toot{
reply, err := c.PostStatus(ctx, &mastodon.Toot{
Status: response,
InReplyToID: replyToID,
Visibility: visibility,
Expand All @@ -503,6 +508,12 @@ func generateAndPostAltText(c *mastodon.Client, status *mastodon.Status, replyTo
if err != nil {
log.Printf("Error posting reply: %v", err)
}

// Track the reply with a timestamp
mapMutex.Lock()
replyMap[status.ID] = ReplyInfo{ReplyID: reply.ID, Timestamp: time.Now()}
mapMutex.Unlock()

}
}

Expand Down Expand Up @@ -701,3 +712,42 @@ func checkOllamaModel() error {

return nil
}

// Struct to store reply information with a timestamp
type ReplyInfo struct {
ReplyID mastodon.ID
Timestamp time.Time
}

var replyMap = make(map[mastodon.ID]ReplyInfo)
var mapMutex sync.Mutex

func handleDeleteEvent(c *mastodon.Client, originalID mastodon.ID) {
mapMutex.Lock()
defer mapMutex.Unlock()

if replyInfo, exists := replyMap[originalID]; exists {
// Delete AltBot's reply
err := c.DeleteStatus(ctx, replyInfo.ReplyID)
if err != nil {
log.Printf("Error deleting reply: %v", err)
} else {
log.Printf("Deleted reply for original post ID: %v", originalID)
delete(replyMap, originalID)
}
}
}

func cleanupOldEntries() {
for {
time.Sleep(10 * time.Minute) // Run cleanup every 10 minutes

mapMutex.Lock()
for originalID, replyInfo := range replyMap {
if time.Since(replyInfo.Timestamp) > time.Hour {
delete(replyMap, originalID)
}
}
mapMutex.Unlock()
}
}

0 comments on commit 3b9c0d6

Please sign in to comment.