Skip to content

Commit

Permalink
ActivityPub: add cli option to refetch followers (inbox, username)
Browse files Browse the repository at this point in the history
  • Loading branch information
jlelse committed Dec 26, 2024
1 parent 48cc603 commit 8253ad5
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 9 deletions.
54 changes: 45 additions & 9 deletions activityPub.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,23 @@ func (a *goBlog) initActivityPub() error {
})
// Prepare webfinger
a.prepareWebfinger()
// Init base
if err := a.initActivityPubBase(); err != nil {
return err
}
// Init send queue
a.initAPSendQueue()
// Send profile updates
go func() {
// First wait a bit
time.Sleep(time.Second * 10)
// Then send profile update
a.apSendProfileUpdates()
}()
return nil
}

func (a *goBlog) initActivityPubBase() error {
// Read key and prepare signing
err := a.loadActivityPubPrivateKey()
if err != nil {
Expand All @@ -83,15 +100,6 @@ func (a *goBlog) initActivityPub() error {
}),
)
}
// Init send queue
a.initAPSendQueue()
// Send profile updates
go func() {
// First wait a bit
time.Sleep(time.Second * 10)
// Then send profile update
a.apSendProfileUpdates()
}()
return nil
}

Expand Down Expand Up @@ -651,3 +659,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
}
21 changes: 21 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.initActivityPubBase(); err != nil {
app.logErrAndQuit("Failed to init ActivityPub base", "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()

Expand Down

0 comments on commit 8253ad5

Please sign in to comment.