2020-10-26 16:37:31 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-12-10 11:47:36 +00:00
|
|
|
"bytes"
|
2022-02-22 09:14:48 +00:00
|
|
|
"context"
|
2021-07-29 13:31:49 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
2020-10-26 16:37:31 +00:00
|
|
|
"crypto/x509"
|
2020-11-09 15:40:12 +00:00
|
|
|
"database/sql"
|
2021-01-21 16:59:47 +00:00
|
|
|
"encoding/json"
|
2020-10-26 16:37:31 +00:00
|
|
|
"encoding/pem"
|
2022-02-12 21:29:45 +00:00
|
|
|
"encoding/xml"
|
2020-10-26 16:37:31 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-02-12 21:29:45 +00:00
|
|
|
"io"
|
2020-10-26 16:37:31 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2022-11-23 21:16:56 +00:00
|
|
|
ap "github.com/go-ap/activitypub"
|
2022-12-10 11:47:36 +00:00
|
|
|
apc "github.com/go-ap/client"
|
2021-03-03 17:19:55 +00:00
|
|
|
"github.com/go-chi/chi/v5"
|
2020-10-26 16:37:31 +00:00
|
|
|
"github.com/go-fed/httpsig"
|
2021-11-18 16:21:50 +00:00
|
|
|
"github.com/google/uuid"
|
2022-04-10 09:46:35 +00:00
|
|
|
"go.goblog.app/app/pkgs/bufferpool"
|
2021-06-28 20:17:18 +00:00
|
|
|
"go.goblog.app/app/pkgs/contenttype"
|
2020-10-26 16:37:31 +00:00
|
|
|
)
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (a *goBlog) initActivityPub() error {
|
2022-04-21 16:18:39 +00:00
|
|
|
if !a.apEnabled() {
|
|
|
|
// ActivityPub disabled
|
2020-11-17 21:10:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Add hooks
|
2021-06-06 12:39:42 +00:00
|
|
|
a.pPostHooks = append(a.pPostHooks, func(p *post) {
|
2020-11-17 21:10:13 +00:00
|
|
|
if p.isPublishedSectionPost() {
|
2021-06-06 12:39:42 +00:00
|
|
|
a.apPost(p)
|
2020-11-17 21:10:13 +00:00
|
|
|
}
|
|
|
|
})
|
2021-06-06 12:39:42 +00:00
|
|
|
a.pUpdateHooks = append(a.pUpdateHooks, func(p *post) {
|
2020-11-17 21:10:13 +00:00
|
|
|
if p.isPublishedSectionPost() {
|
2021-06-06 12:39:42 +00:00
|
|
|
a.apUpdate(p)
|
2020-11-17 21:10:13 +00:00
|
|
|
}
|
|
|
|
})
|
2021-06-06 12:39:42 +00:00
|
|
|
a.pDeleteHooks = append(a.pDeleteHooks, func(p *post) {
|
|
|
|
a.apDelete(p)
|
2020-11-17 21:10:13 +00:00
|
|
|
})
|
2022-01-03 12:55:44 +00:00
|
|
|
a.pUndeleteHooks = append(a.pUndeleteHooks, func(p *post) {
|
2022-02-22 15:52:03 +00:00
|
|
|
if p.isPublishedSectionPost() {
|
|
|
|
a.apUndelete(p)
|
|
|
|
}
|
2022-01-03 12:55:44 +00:00
|
|
|
})
|
2021-02-19 13:32:34 +00:00
|
|
|
// Prepare webfinger
|
2022-04-28 21:09:38 +00:00
|
|
|
a.prepareWebfinger()
|
2020-11-17 21:10:13 +00:00
|
|
|
// Read key and prepare signing
|
2021-07-29 13:31:49 +00:00
|
|
|
err := a.loadActivityPubPrivateKey()
|
2020-10-26 16:37:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-10 11:47:36 +00:00
|
|
|
a.apSigner, _, err = httpsig.NewSigner(
|
2022-12-14 22:24:06 +00:00
|
|
|
[]httpsig.Algorithm{httpsig.RSA_SHA256},
|
2021-03-19 13:26:45 +00:00
|
|
|
httpsig.DigestSha256,
|
|
|
|
[]string{httpsig.RequestTarget, "date", "host", "digest"},
|
|
|
|
httpsig.Signature,
|
|
|
|
0,
|
|
|
|
)
|
2020-10-26 16:37:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-10 11:47:36 +00:00
|
|
|
// Init http client
|
|
|
|
a.apHttpClients = map[string]*apc.C{}
|
|
|
|
for blog, bc := range a.cfg.Blogs {
|
|
|
|
a.apHttpClients[blog] = apc.New(
|
|
|
|
apc.WithHTTPClient(a.httpClient),
|
|
|
|
apc.WithSignFn(func(r *http.Request) error {
|
|
|
|
return a.signRequest(r, a.apIri(bc))
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
}
|
2020-11-22 15:10:59 +00:00
|
|
|
// Init send queue
|
2021-06-06 12:39:42 +00:00
|
|
|
a.initAPSendQueue()
|
2022-04-21 16:18:39 +00:00
|
|
|
// Send profile updates
|
|
|
|
go func() {
|
|
|
|
// First wait a bit
|
|
|
|
time.Sleep(time.Second * 10)
|
|
|
|
// Then send profile update
|
|
|
|
a.apSendProfileUpdates()
|
|
|
|
}()
|
2020-10-26 16:37:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-21 16:18:39 +00:00
|
|
|
func (a *goBlog) apEnabled() bool {
|
|
|
|
if a.isPrivate() {
|
|
|
|
// Private mode, no AP
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if apc := a.cfg.ActivityPub; apc == nil || !apc.Enabled {
|
|
|
|
// Disabled
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-04-28 21:09:38 +00:00
|
|
|
func (a *goBlog) prepareWebfinger() {
|
|
|
|
a.webfingerResources = map[string]*configBlog{}
|
|
|
|
a.webfingerAccts = map[string]string{}
|
|
|
|
for name, blog := range a.cfg.Blogs {
|
|
|
|
acct := "acct:" + name + "@" + a.cfg.Server.publicHostname
|
|
|
|
a.webfingerResources[acct] = blog
|
|
|
|
a.webfingerResources[a.apIri(blog)] = blog
|
|
|
|
a.webfingerAccts[a.apIri(blog)] = acct
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (a *goBlog) apHandleWebfinger(w http.ResponseWriter, r *http.Request) {
|
|
|
|
blog, ok := a.webfingerResources[r.URL.Query().Get("resource")]
|
2021-02-19 13:32:34 +00:00
|
|
|
if !ok {
|
2021-06-06 12:39:42 +00:00
|
|
|
a.serveError(w, r, "Resource not found", http.StatusNotFound)
|
2020-10-26 16:37:31 +00:00
|
|
|
return
|
|
|
|
}
|
2021-06-23 17:20:50 +00:00
|
|
|
apIri := a.apIri(blog)
|
2022-04-10 09:46:35 +00:00
|
|
|
// Encode
|
|
|
|
buf := bufferpool.Get()
|
|
|
|
defer bufferpool.Put(buf)
|
2022-04-28 21:09:38 +00:00
|
|
|
if err := json.NewEncoder(buf).Encode(map[string]any{
|
2021-06-23 17:20:50 +00:00
|
|
|
"subject": a.webfingerAccts[apIri],
|
2021-02-19 13:32:34 +00:00
|
|
|
"aliases": []string{
|
2021-06-23 17:20:50 +00:00
|
|
|
a.webfingerAccts[apIri],
|
|
|
|
apIri,
|
2021-02-19 13:32:34 +00:00
|
|
|
},
|
2020-10-26 16:37:31 +00:00
|
|
|
"links": []map[string]string{
|
|
|
|
{
|
|
|
|
"rel": "self",
|
2021-06-18 12:32:03 +00:00
|
|
|
"type": contenttype.AS,
|
2021-06-23 17:20:50 +00:00
|
|
|
"href": apIri,
|
2020-10-26 16:37:31 +00:00
|
|
|
},
|
2021-02-19 13:32:34 +00:00
|
|
|
{
|
|
|
|
"rel": "http://webfinger.net/rel/profile-page",
|
|
|
|
"type": "text/html",
|
2021-06-23 17:20:50 +00:00
|
|
|
"href": apIri,
|
2021-02-19 13:32:34 +00:00
|
|
|
},
|
2020-10-26 16:37:31 +00:00
|
|
|
},
|
2022-04-10 09:46:35 +00:00
|
|
|
}); err != nil {
|
|
|
|
a.serveError(w, r, "Encoding failed", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2021-06-18 12:32:03 +00:00
|
|
|
w.Header().Set(contentType, "application/jrd+json"+contenttype.CharsetUtf8Suffix)
|
2022-04-10 09:46:35 +00:00
|
|
|
_ = a.min.Get().Minify(contenttype.JSON, w, buf)
|
2020-10-26 16:37:31 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (a *goBlog) apHandleInbox(w http.ResponseWriter, r *http.Request) {
|
2020-10-26 16:37:31 +00:00
|
|
|
blogName := chi.URLParam(r, "blog")
|
2021-06-23 17:20:50 +00:00
|
|
|
blog, ok := a.cfg.Blogs[blogName]
|
|
|
|
if !ok || blog == nil {
|
2021-06-06 12:39:42 +00:00
|
|
|
a.serveError(w, r, "Inbox not found", http.StatusNotFound)
|
2020-10-26 16:37:31 +00:00
|
|
|
return
|
|
|
|
}
|
2020-11-13 14:19:09 +00:00
|
|
|
// Verify request
|
2022-12-10 11:47:36 +00:00
|
|
|
requestActor, err := a.apVerifySignature(r, blogName)
|
2020-11-13 14:19:09 +00:00
|
|
|
if err != nil {
|
2020-11-13 20:29:09 +00:00
|
|
|
// Send 401 because signature could not be verified
|
2021-06-06 12:39:42 +00:00
|
|
|
a.serveError(w, r, err.Error(), http.StatusUnauthorized)
|
2020-11-13 14:19:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Parse activity
|
2022-11-23 21:16:56 +00:00
|
|
|
limit := int64(10 * 1000 * 1000) // 10 MB
|
|
|
|
body, err := io.ReadAll(io.LimitReader(r.Body, limit))
|
|
|
|
if err != nil {
|
|
|
|
a.serveError(w, r, "Failed to read body", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
apItem, err := ap.UnmarshalJSON(body)
|
2020-10-26 16:37:31 +00:00
|
|
|
if err != nil {
|
2021-06-06 12:39:42 +00:00
|
|
|
a.serveError(w, r, "Failed to decode body", http.StatusBadRequest)
|
2020-10-26 16:37:31 +00:00
|
|
|
return
|
|
|
|
}
|
2022-11-23 21:16:56 +00:00
|
|
|
// Check if it's an activity
|
|
|
|
activity, err := ap.ToActivity(apItem)
|
|
|
|
if err != nil {
|
|
|
|
a.serveError(w, r, "No activity", http.StatusBadRequest)
|
2020-11-13 14:19:09 +00:00
|
|
|
return
|
|
|
|
}
|
2022-11-23 21:16:56 +00:00
|
|
|
// Check actor
|
2022-12-20 19:30:19 +00:00
|
|
|
activityActor := activity.Actor.GetLink()
|
2022-11-23 21:16:56 +00:00
|
|
|
if activity.Actor == nil || (!activity.Actor.IsLink() && !activity.Actor.IsObject()) {
|
|
|
|
a.serveError(w, r, "Activity has no actor", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2022-12-20 19:30:19 +00:00
|
|
|
if activityActor != requestActor.GetLink() {
|
2021-06-06 12:39:42 +00:00
|
|
|
a.serveError(w, r, "Request actor isn't activity actor", http.StatusForbidden)
|
2020-11-13 14:19:09 +00:00
|
|
|
return
|
|
|
|
}
|
2022-11-23 21:16:56 +00:00
|
|
|
// Handle activity
|
|
|
|
switch activity.GetType() {
|
|
|
|
case ap.FollowType:
|
2022-12-10 11:47:36 +00:00
|
|
|
a.apAccept(blogName, blog, activity)
|
2022-11-23 21:16:56 +00:00
|
|
|
case ap.UndoType:
|
|
|
|
if activity.Object.IsObject() {
|
|
|
|
objectActivity, err := ap.ToActivity(activity.Object)
|
2022-12-20 19:30:19 +00:00
|
|
|
if err == nil && objectActivity.GetType() == ap.FollowType && objectActivity.Actor.GetLink() == activityActor {
|
2022-11-23 21:16:56 +00:00
|
|
|
_ = a.db.apRemoveFollower(blogName, activityActor.String())
|
2020-10-26 16:37:31 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-23 21:16:56 +00:00
|
|
|
case ap.CreateType, ap.UpdateType:
|
|
|
|
if activity.Object.IsObject() {
|
2022-11-25 19:36:14 +00:00
|
|
|
a.apOnCreateUpdate(blog, requestActor, activity)
|
2020-10-26 16:37:31 +00:00
|
|
|
}
|
2022-11-23 21:16:56 +00:00
|
|
|
case ap.DeleteType, ap.BlockType:
|
2022-12-20 19:30:19 +00:00
|
|
|
if activity.Object.GetLink() == activityActor {
|
2022-11-23 21:16:56 +00:00
|
|
|
_ = a.db.apRemoveFollower(blogName, activityActor.String())
|
|
|
|
} else {
|
|
|
|
// Check if comment exists
|
2022-12-20 19:30:19 +00:00
|
|
|
exists, commentId, err := a.db.commentIdByOriginal(activity.Object.GetLink().String())
|
2022-11-23 21:16:56 +00:00
|
|
|
if err == nil && exists {
|
|
|
|
_ = a.db.deleteComment(commentId)
|
2022-12-20 19:30:19 +00:00
|
|
|
_ = a.db.deleteWebmentionUUrl(activity.Object.GetLink().String())
|
2022-11-23 21:16:56 +00:00
|
|
|
}
|
2020-10-26 16:37:31 +00:00
|
|
|
}
|
2022-11-23 21:16:56 +00:00
|
|
|
case ap.AnnounceType:
|
2022-12-20 19:30:19 +00:00
|
|
|
a.sendNotification(fmt.Sprintf("%s announced %s", activityActor, activity.Object.GetLink()))
|
2022-11-23 21:16:56 +00:00
|
|
|
case ap.LikeType:
|
2022-12-20 19:30:19 +00:00
|
|
|
a.sendNotification(fmt.Sprintf("%s liked %s", activityActor, activity.Object.GetLink()))
|
2020-10-26 16:37:31 +00:00
|
|
|
}
|
2020-11-13 20:29:09 +00:00
|
|
|
// Return 200
|
|
|
|
w.WriteHeader(http.StatusOK)
|
2020-11-13 14:19:09 +00:00
|
|
|
}
|
2020-10-26 16:37:31 +00:00
|
|
|
|
2022-11-25 19:36:14 +00:00
|
|
|
func (a *goBlog) apOnCreateUpdate(blog *configBlog, requestActor *ap.Actor, activity *ap.Activity) {
|
|
|
|
object, err := ap.ToObject(activity.Object)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if object.GetType() != ap.NoteType && object.GetType() != ap.ArticleType {
|
|
|
|
// ignore other objects for now
|
|
|
|
return
|
|
|
|
}
|
|
|
|
visible := true
|
|
|
|
if !object.To.Contains(ap.PublicNS) && !object.CC.Contains(ap.PublicNS) {
|
|
|
|
visible = false
|
|
|
|
}
|
2022-12-20 19:30:19 +00:00
|
|
|
if inReplyTo := object.InReplyTo; inReplyTo != nil {
|
|
|
|
if replyTarget := inReplyTo.GetLink().String(); visible && 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()
|
|
|
|
_, _, _ = a.createComment(blog, replyTarget, content, name, website, original)
|
|
|
|
return
|
2022-11-25 19:36:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Might be a private reply or mention etc.
|
|
|
|
// TODO: handle them
|
|
|
|
}
|
|
|
|
|
2022-12-10 11:47:36 +00:00
|
|
|
func (a *goBlog) apVerifySignature(r *http.Request, blog string) (*ap.Actor, error) {
|
2020-11-13 14:19:09 +00:00
|
|
|
verifier, err := httpsig.NewVerifier(r)
|
|
|
|
if err != nil {
|
|
|
|
// Error with signature header etc.
|
2022-12-10 11:47:36 +00:00
|
|
|
return nil, err
|
2020-11-13 14:19:09 +00:00
|
|
|
}
|
2022-12-10 11:47:36 +00:00
|
|
|
actor, err := a.apGetRemoteActor(ap.IRI(verifier.KeyId()), blog)
|
|
|
|
if err != nil || actor == nil {
|
2020-11-13 14:19:09 +00:00
|
|
|
// Actor not found or something else bad
|
2022-12-14 22:29:19 +00:00
|
|
|
return nil, errors.New("failed to get actor")
|
2020-11-13 14:19:09 +00:00
|
|
|
}
|
2022-11-23 21:16:56 +00:00
|
|
|
if actor.PublicKey.PublicKeyPem == "" {
|
2022-12-10 11:47:36 +00:00
|
|
|
return nil, errors.New("actor has no public key")
|
2020-11-13 14:19:09 +00:00
|
|
|
}
|
|
|
|
block, _ := pem.Decode([]byte(actor.PublicKey.PublicKeyPem))
|
|
|
|
if block == nil {
|
2022-12-10 11:47:36 +00:00
|
|
|
return nil, errors.New("public key invalid")
|
2020-11-13 14:19:09 +00:00
|
|
|
}
|
|
|
|
pubKey, err := x509.ParsePKIXPublicKey(block.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
// Unable to parse public key
|
2022-12-10 11:47:36 +00:00
|
|
|
return nil, err
|
2020-11-13 14:19:09 +00:00
|
|
|
}
|
2022-12-10 11:47:36 +00:00
|
|
|
return actor, verifier.Verify(pubKey, httpsig.RSA_SHA256)
|
2020-10-26 16:37:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleWellKnownHostMeta(w http.ResponseWriter, r *http.Request) {
|
2021-06-18 12:32:03 +00:00
|
|
|
w.Header().Set(contentType, "application/xrd+xml"+contenttype.CharsetUtf8Suffix)
|
2022-02-12 21:29:45 +00:00
|
|
|
_, _ = io.WriteString(w, xml.Header)
|
|
|
|
_, _ = io.WriteString(w, `<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0"><Link rel="lrdd" type="application/xrd+xml" template="https://`+r.Host+`/.well-known/webfinger?resource={uri}"/></XRD>`)
|
2020-10-26 16:37:31 +00:00
|
|
|
}
|
|
|
|
|
2022-11-25 19:36:14 +00:00
|
|
|
func (a *goBlog) apGetFollowersCollectionId(blogName string, blog *configBlog) ap.IRI {
|
|
|
|
return ap.IRI(a.apIri(blog) + "/activitypub/followers/" + blogName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *goBlog) apShowFollowers(w http.ResponseWriter, r *http.Request) {
|
|
|
|
blogName := chi.URLParam(r, "blog")
|
|
|
|
blog, ok := a.cfg.Blogs[blogName]
|
|
|
|
if !ok || blog == nil {
|
|
|
|
a.serveError(w, r, "Blog not found", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
followers, err := a.db.apGetAllFollowers(blogName)
|
|
|
|
if err != nil {
|
|
|
|
a.serveError(w, r, "Failed to get followers", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if asRequest, ok := r.Context().Value(asRequestKey).(bool); ok && asRequest {
|
|
|
|
followersCollection := ap.CollectionNew(a.apGetFollowersCollectionId(blogName, blog))
|
|
|
|
for _, follower := range followers {
|
|
|
|
followersCollection.Items.Append(ap.IRI(follower.follower))
|
|
|
|
}
|
|
|
|
followersCollection.TotalItems = uint(len(followers))
|
|
|
|
a.serveAPItem(followersCollection, w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
a.render(w, r, a.renderActivityPubFollowers, &renderData{
|
|
|
|
BlogString: blogName,
|
|
|
|
Data: &activityPubFollowersRenderData{
|
|
|
|
apUser: fmt.Sprintf("@%s@%s", blogName, a.cfg.Server.publicHostname),
|
|
|
|
followers: followers,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-12-10 11:47:36 +00:00
|
|
|
func (a *goBlog) apGetRemoteActor(iri ap.IRI, blog string) (*ap.Actor, error) {
|
|
|
|
return a.apHttpClients[blog].Actor(context.Background(), iri)
|
2020-10-26 16:37:31 +00:00
|
|
|
}
|
|
|
|
|
2021-07-30 13:43:13 +00:00
|
|
|
func (db *database) apGetAllInboxes(blog string) (inboxes []string, err error) {
|
2022-08-09 15:25:22 +00:00
|
|
|
rows, err := db.Query("select distinct inbox from activitypub_followers where blog = @blog", sql.Named("blog", blog))
|
2020-10-26 16:37:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-30 13:43:13 +00:00
|
|
|
var inbox string
|
2020-10-26 16:37:31 +00:00
|
|
|
for rows.Next() {
|
2020-11-25 10:29:36 +00:00
|
|
|
err = rows.Scan(&inbox)
|
2020-10-26 16:37:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-11-25 10:29:36 +00:00
|
|
|
inboxes = append(inboxes, inbox)
|
2020-10-26 16:37:31 +00:00
|
|
|
}
|
2020-11-25 10:29:36 +00:00
|
|
|
return inboxes, nil
|
2020-10-26 16:37:31 +00:00
|
|
|
}
|
|
|
|
|
2022-11-08 19:13:20 +00:00
|
|
|
type apFollower struct {
|
2022-12-07 15:43:13 +00:00
|
|
|
follower, inbox, username string
|
2022-11-08 19:13:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db *database) apGetAllFollowers(blog string) (followers []*apFollower, err error) {
|
2022-12-07 15:43:13 +00:00
|
|
|
rows, err := db.Query("select follower, inbox, username from activitypub_followers where blog = @blog", sql.Named("blog", blog))
|
2022-11-08 19:13:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-12-07 15:43:13 +00:00
|
|
|
var follower, inbox, username string
|
2022-11-08 19:13:20 +00:00
|
|
|
for rows.Next() {
|
2022-12-07 15:43:13 +00:00
|
|
|
err = rows.Scan(&follower, &inbox, &username)
|
2022-11-08 19:13:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-12-07 15:43:13 +00:00
|
|
|
followers = append(followers, &apFollower{follower: follower, inbox: inbox, username: username})
|
2022-11-08 19:13:20 +00:00
|
|
|
}
|
|
|
|
return followers, nil
|
|
|
|
}
|
|
|
|
|
2022-12-07 15:43:13 +00:00
|
|
|
func (db *database) apAddFollower(blog, follower, inbox, username string) error {
|
|
|
|
_, err := db.Exec(
|
|
|
|
"insert or replace into activitypub_followers (blog, follower, inbox, username) values (@blog, @follower, @inbox, @username)",
|
|
|
|
sql.Named("blog", blog), sql.Named("follower", follower), sql.Named("inbox", inbox), sql.Named("username", username),
|
|
|
|
)
|
2020-11-09 15:40:12 +00:00
|
|
|
return err
|
2020-10-26 16:37:31 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (db *database) apRemoveFollower(blog, follower string) error {
|
2022-08-09 15:25:22 +00:00
|
|
|
_, err := db.Exec("delete from activitypub_followers where blog = @blog and follower = @follower", sql.Named("blog", blog), sql.Named("follower", follower))
|
2020-11-09 15:40:12 +00:00
|
|
|
return err
|
2020-10-26 16:37:31 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (db *database) apRemoveInbox(inbox string) error {
|
2022-08-09 15:25:22 +00:00
|
|
|
_, err := db.Exec("delete from activitypub_followers where inbox = @inbox", sql.Named("inbox", inbox))
|
2020-11-25 10:29:36 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (a *goBlog) apPost(p *post) {
|
2022-12-14 15:03:54 +00:00
|
|
|
blogConfig := a.getBlogFromPost(p)
|
|
|
|
c := ap.CreateNew(a.activityPubId(p), a.toAPNote(p))
|
|
|
|
c.Actor = a.apAPIri(blogConfig)
|
|
|
|
c.Published = time.Now()
|
|
|
|
a.apSendToAllFollowers(p.Blog, c)
|
2020-10-26 16:37:31 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (a *goBlog) apUpdate(p *post) {
|
2022-12-14 15:03:54 +00:00
|
|
|
blogConfig := a.getBlogFromPost(p)
|
|
|
|
u := ap.UpdateNew(a.activityPubId(p), a.toAPNote(p))
|
|
|
|
u.Actor = a.apAPIri(blogConfig)
|
|
|
|
u.Published = time.Now()
|
|
|
|
a.apSendToAllFollowers(p.Blog, u)
|
2020-10-26 16:37:31 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (a *goBlog) apDelete(p *post) {
|
2022-12-14 15:03:54 +00:00
|
|
|
blogConfig := a.getBlogFromPost(p)
|
|
|
|
d := ap.DeleteNew(a.apNewID(blogConfig), a.activityPubId(p))
|
|
|
|
d.Actor = a.apAPIri(blogConfig)
|
|
|
|
a.apSendToAllFollowers(p.Blog, d)
|
2022-01-03 12:55:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *goBlog) apUndelete(p *post) {
|
2022-03-31 12:55:36 +00:00
|
|
|
// The optimal way to do this would be to send a "Undo Delete" activity,
|
|
|
|
// but that doesn't work with Mastodon yet.
|
|
|
|
// see:
|
|
|
|
// https://socialhub.activitypub.rocks/t/soft-deletes-and-restoring-deleted-posts/2318
|
|
|
|
// https://github.com/mastodon/mastodon/issues/17553
|
|
|
|
|
|
|
|
// Update "activityPubVersion" parameter to current timestamp in nanoseconds
|
|
|
|
p.Parameters[activityPubVersionParam] = []string{fmt.Sprintf("%d", utcNowNanos())}
|
2022-04-04 11:07:36 +00:00
|
|
|
_ = a.db.replacePostParam(p.Path, activityPubVersionParam, p.Parameters[activityPubVersionParam])
|
2022-03-31 12:55:36 +00:00
|
|
|
// Post as new post
|
|
|
|
a.apPost(p)
|
2020-10-26 16:37:31 +00:00
|
|
|
}
|
|
|
|
|
2022-12-10 11:47:36 +00:00
|
|
|
func (a *goBlog) apAccept(blogName string, blog *configBlog, follow *ap.Activity) {
|
2022-12-20 19:30:19 +00:00
|
|
|
newFollower := follow.Actor.GetLink()
|
2022-12-07 15:43:13 +00:00
|
|
|
log.Println("New follow request from follower id:", newFollower.String())
|
2022-11-23 21:16:56 +00:00
|
|
|
// Get remote actor
|
2022-12-10 11:47:36 +00:00
|
|
|
follower, err := a.apGetRemoteActor(newFollower, blogName)
|
|
|
|
if err != nil || follower == nil {
|
2020-10-26 16:37:31 +00:00
|
|
|
// Couldn't retrieve remote actor info
|
|
|
|
log.Println("Failed to retrieve remote actor info:", newFollower)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Add or update follower
|
2022-12-20 19:30:19 +00:00
|
|
|
inbox := follower.Inbox.GetLink()
|
|
|
|
if endpoints := follower.Endpoints; endpoints != nil && endpoints.SharedInbox != nil && endpoints.SharedInbox.GetLink() != "" {
|
|
|
|
inbox = endpoints.SharedInbox.GetLink()
|
2022-11-23 21:16:56 +00:00
|
|
|
}
|
|
|
|
if inbox == "" {
|
|
|
|
return
|
2020-11-25 10:29:36 +00:00
|
|
|
}
|
2022-12-07 15:43:13 +00:00
|
|
|
username := apUsername(follower)
|
2022-12-20 19:30:19 +00:00
|
|
|
if err = a.db.apAddFollower(blogName, follower.GetLink().String(), inbox.String(), username); err != nil {
|
2021-02-08 17:51:07 +00:00
|
|
|
return
|
|
|
|
}
|
2021-11-18 16:21:50 +00:00
|
|
|
// Send accept response to the new follower
|
2022-11-23 21:16:56 +00:00
|
|
|
accept := ap.AcceptNew(a.apNewID(blog), follow)
|
2022-11-27 20:53:34 +00:00
|
|
|
accept.To.Append(newFollower)
|
2022-11-23 21:16:56 +00:00
|
|
|
accept.Actor = a.apAPIri(blog)
|
|
|
|
_ = a.apQueueSendSigned(a.apIri(blog), inbox.String(), accept)
|
2022-11-27 20:53:34 +00:00
|
|
|
// Notification
|
2022-12-20 19:30:19 +00:00
|
|
|
a.sendNotification(fmt.Sprintf("%s (%s) started following %s", username, follower.GetLink().String(), a.apIri(blog)))
|
2020-10-26 16:37:31 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 16:18:39 +00:00
|
|
|
func (a *goBlog) apSendProfileUpdates() {
|
|
|
|
for blog, config := range a.cfg.Blogs {
|
2022-11-23 21:16:56 +00:00
|
|
|
person := a.toApPerson(blog)
|
|
|
|
update := ap.UpdateNew(a.apNewID(config), person)
|
|
|
|
update.Actor = a.apAPIri(config)
|
|
|
|
update.Published = time.Now()
|
|
|
|
a.apSendToAllFollowers(blog, update)
|
2022-04-21 16:18:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-23 21:16:56 +00:00
|
|
|
func (a *goBlog) apSendToAllFollowers(blog string, activity *ap.Activity) {
|
2021-06-06 12:39:42 +00:00
|
|
|
inboxes, err := a.db.apGetAllInboxes(blog)
|
2020-10-26 16:37:31 +00:00
|
|
|
if err != nil {
|
2020-11-25 10:29:36 +00:00
|
|
|
log.Println("Failed to retrieve inboxes:", err.Error())
|
2020-10-26 16:37:31 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-31 12:55:36 +00:00
|
|
|
a.apSendTo(a.apIri(a.cfg.Blogs[blog]), activity, inboxes)
|
2020-10-26 16:37:31 +00:00
|
|
|
}
|
|
|
|
|
2022-11-23 21:16:56 +00:00
|
|
|
func (a *goBlog) apSendTo(blogIri string, activity *ap.Activity, inboxes []string) {
|
2020-11-25 10:29:36 +00:00
|
|
|
for _, i := range inboxes {
|
2020-10-26 16:37:31 +00:00
|
|
|
go func(inbox string) {
|
2022-03-31 12:55:36 +00:00
|
|
|
_ = a.apQueueSendSigned(blogIri, inbox, activity)
|
2020-10-26 16:37:31 +00:00
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-23 21:16:56 +00:00
|
|
|
func (a *goBlog) apNewID(blog *configBlog) ap.ID {
|
|
|
|
return ap.ID(a.apIri(blog) + "#" + uuid.NewString())
|
2020-10-26 16:37:31 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (a *goBlog) apIri(b *configBlog) string {
|
2021-06-11 06:24:41 +00:00
|
|
|
return a.getFullAddress(b.getRelativePath(""))
|
2020-10-26 16:37:31 +00:00
|
|
|
}
|
|
|
|
|
2022-11-23 21:16:56 +00:00
|
|
|
func (a *goBlog) apAPIri(b *configBlog) ap.IRI {
|
|
|
|
return ap.IRI(a.apIri(b))
|
|
|
|
}
|
|
|
|
|
2020-10-26 16:37:31 +00:00
|
|
|
func apRequestIsSuccess(code int) bool {
|
|
|
|
return code == http.StatusOK || code == http.StatusCreated || code == http.StatusAccepted || code == http.StatusNoContent
|
|
|
|
}
|
2021-07-29 13:31:49 +00:00
|
|
|
|
|
|
|
// Load or generate key for ActivityPub communication
|
|
|
|
func (a *goBlog) loadActivityPubPrivateKey() error {
|
|
|
|
// Check if already loaded
|
|
|
|
if a.apPrivateKey != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Check if already generated
|
|
|
|
if keyData, err := a.db.retrievePersistentCache("activitypub_key"); err == nil && keyData != nil {
|
|
|
|
privateKeyDecoded, _ := pem.Decode(keyData)
|
|
|
|
if privateKeyDecoded == nil {
|
|
|
|
log.Println("failed to decode cached private key")
|
|
|
|
// continue
|
|
|
|
} else {
|
|
|
|
key, err := x509.ParsePKCS1PrivateKey(privateKeyDecoded.Bytes)
|
2022-04-21 16:18:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pubKeyBytes, err := x509.MarshalPKIXPublicKey(&key.PublicKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-07-29 13:31:49 +00:00
|
|
|
}
|
2022-04-21 16:18:39 +00:00
|
|
|
a.apPrivateKey = key
|
|
|
|
a.apPubKeyBytes = pubKeyBytes
|
|
|
|
return nil
|
2021-07-29 13:31:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Generate and cache key
|
2022-04-21 16:18:39 +00:00
|
|
|
key, err := rsa.GenerateKey(rand.Reader, 2048)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pubKeyBytes, err := x509.MarshalPKIXPublicKey(&key.PublicKey)
|
2021-07-29 13:31:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-21 16:18:39 +00:00
|
|
|
a.apPrivateKey = key
|
|
|
|
a.apPubKeyBytes = pubKeyBytes
|
2022-02-22 15:52:03 +00:00
|
|
|
return a.db.cachePersistently(
|
|
|
|
"activitypub_key",
|
|
|
|
pem.EncodeToMemory(&pem.Block{
|
|
|
|
Type: "PRIVATE KEY",
|
|
|
|
Bytes: x509.MarshalPKCS1PrivateKey(a.apPrivateKey),
|
|
|
|
}),
|
|
|
|
)
|
2021-07-29 13:31:49 +00:00
|
|
|
}
|
2022-12-10 11:47:36 +00:00
|
|
|
|
|
|
|
func (a *goBlog) signRequest(r *http.Request, blogIri string) error {
|
|
|
|
if date := r.Header.Get("Date"); date == "" {
|
2022-12-11 14:49:19 +00:00
|
|
|
r.Header.Set("Date", time.Now().UTC().Format(http.TimeFormat))
|
2022-12-10 11:47:36 +00:00
|
|
|
}
|
|
|
|
if host := r.Header.Get("Host"); host == "" {
|
|
|
|
r.Header.Set("Host", r.URL.Host)
|
|
|
|
}
|
2022-12-14 22:24:06 +00:00
|
|
|
bodyBuf := bytes.NewBufferString("")
|
2022-12-10 11:47:36 +00:00
|
|
|
if r.Body != nil {
|
2022-12-14 22:24:06 +00:00
|
|
|
if _, err := io.Copy(bodyBuf, r.Body); err == nil {
|
|
|
|
r.Body = io.NopCloser(bodyBuf)
|
2022-12-10 11:47:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
a.apSignMutex.Lock()
|
|
|
|
defer a.apSignMutex.Unlock()
|
|
|
|
return a.apSigner.SignRequest(a.apPrivateKey, blogIri+"#main-key", r, bodyBuf.Bytes())
|
|
|
|
}
|