From d61addc29e901a5959c6d3becb99d8008c24a8c3 Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Thu, 26 Dec 2024 19:46:20 +0100 Subject: [PATCH] ActivityPub: add cli option to refetch followers (inbox, username) --- activityPub.go | 28 ++++++++++++++++++++++++++++ main.go | 21 +++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/activityPub.go b/activityPub.go index 0251fcc..fddf1c8 100644 --- a/activityPub.go +++ b/activityPub.go @@ -651,3 +651,31 @@ func (a *goBlog) signRequest(r *http.Request, blogIri string) error { defer a.apSignMutex.Unlock() return a.apSigner.SignRequest(a.apPrivateKey, blogIri+"#main-key", r, bodyBuf.Bytes()) } + +func (a *goBlog) apRefetchFollowers(blogName string) error { + followers, err := a.db.apGetAllFollowers(blogName) + if err != nil { + return err + } + for _, fol := range followers { + actor, err := a.apGetRemoteActor(ap.IRI(fol.follower), blogName) + if err != nil || actor == nil { + a.error("ActivityPub: Failed to retrieve remote actor info", "actor", fol.follower) + continue + } + inbox := actor.Inbox.GetLink() + if endpoints := actor.Endpoints; endpoints != nil && endpoints.SharedInbox != nil && endpoints.SharedInbox.GetLink() != "" { + inbox = endpoints.SharedInbox.GetLink() + } + if inbox == "" { + a.error("ActivityPub: Failed to get inbox for actor", "actor", fol.follower) + continue + } + username := apUsername(actor) + if err = a.db.apAddFollower(blogName, actor.GetLink().String(), inbox.String(), username); err != nil { + a.error("ActivityPub: Failed to update follower info", "err", err) + return err + } + } + return nil +} diff --git a/main.go b/main.go index 58f51e0..b6dd429 100644 --- a/main.go +++ b/main.go @@ -167,6 +167,27 @@ func main() { return } + // ActivityPub refetch followers + if len(os.Args) >= 2 && os.Args[1] == "activitypub" { + if !app.apEnabled() { + app.logErrAndQuit("ActivityPub not enabled") + return + } + if err = app.initActivityPub(); err != nil { + app.logErrAndQuit("Failed to init ActivityPub", "err", err) + return + } + if len(os.Args) >= 4 && os.Args[2] == "refetch-followers" { + blog := os.Args[3] + if err = app.apRefetchFollowers(blog); err != nil { + app.logErrAndQuit("Failed to refetch activitypub followers", "blog", blog, "err", err) + return + } + app.shutdown.ShutdownAndWait() + return + } + } + // Initialize components app.initComponents()