Skip to content

Commit

Permalink
Handle ActivityPub mentions
Browse files Browse the repository at this point in the history
  • Loading branch information
jlelse committed Nov 28, 2024
1 parent c9fc8bb commit f3e13df
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions activityPub.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"cmp"
"context"
"crypto/rand"
"crypto/rsa"
Expand Down Expand Up @@ -291,40 +292,44 @@ func (a *goBlog) apOnCreateUpdate(blog *configBlog, requestActor *ap.Actor, acti
// ignore other objects for now
return
}
visible := true
if !object.To.Contains(ap.PublicNS) && !object.CC.Contains(ap.PublicNS) {
visible = false
}
// Get information from the note
noteUri := object.GetLink().String()
actorName := cmp.Or(requestActor.Name.First().Value.String(), apUsername(requestActor))
actorLink := cmp.Or(requestActor.URL.GetLink(), requestActor.GetLink()).String()
content := object.Content.First().Value.String()
// Handle reply
if inReplyTo := object.InReplyTo; inReplyTo != nil {
if replyTarget := inReplyTo.GetLink().String(); replyTarget != "" && strings.HasPrefix(replyTarget, a.cfg.Server.PublicAddress) {
// It's a reply
original := object.GetLink().String()
name := requestActor.Name.First().Value.String()
if username := apUsername(requestActor); name == "" && username != "" {
name = username
}
website := requestActor.GetLink().String()
if actorUrl := requestActor.URL.GetLink(); actorUrl != "" {
website = actorUrl.String()
}
content := object.Content.First().Value.String()
if visible {
_, _, _ = a.createComment(blog, replyTarget, content, name, website, original)
if object.To.Contains(ap.PublicNS) || object.CC.Contains(ap.PublicNS) {
// Public reply - comment
_, _, _ = a.createComment(blog, replyTarget, content, actorName, actorLink, noteUri)
return
} else {
// Private reply - notification
buf := bufferpool.Get()
defer bufferpool.Put(buf)
fmt.Fprintf(buf, "New private ActivityPub reply to %s from %s\n", cleanHTMLText(replyTarget), cleanHTMLText(original))
fmt.Fprintf(buf, "Author: %s (%s)", cleanHTMLText(name), cleanHTMLText(website))
fmt.Fprintf(buf, "New private ActivityPub reply to %s from %s\n", cleanHTMLText(replyTarget), cleanHTMLText(noteUri))
fmt.Fprintf(buf, "Author: %s (%s)", cleanHTMLText(actorName), cleanHTMLText(actorLink))
buf.WriteString("\n\n")
buf.WriteString(cleanHTMLText(content))
a.sendNotification(buf.String())
return
}
}
}
// Might be a private reply or mention etc.
// TODO: handle them
// Handle mention
if blogIri := ap.IRI(a.apIri(blog)); object.To.Contains(blogIri) || object.CC.Contains(blogIri) {
// Notification
buf := bufferpool.Get()
defer bufferpool.Put(buf)
fmt.Fprintf(buf, "New ActivityPub mention on %s\n", cleanHTMLText(noteUri))
fmt.Fprintf(buf, "Author: %s (%s)", cleanHTMLText(actorName), cleanHTMLText(actorLink))
buf.WriteString("\n\n")
buf.WriteString(cleanHTMLText(content))
a.sendNotification(buf.String())
return
}
// Ignore other cases, maybe it's just spam
}

func (a *goBlog) apVerifySignature(r *http.Request, blog string) (*ap.Actor, error) {
Expand Down

0 comments on commit f3e13df

Please sign in to comment.