summaryrefslogtreecommitdiff
path: root/internal/transport
diff options
context:
space:
mode:
authorLibravatar tobi <31960611+tsmethurst@users.noreply.github.com>2024-07-26 12:04:28 +0200
committerLibravatar GitHub <noreply@github.com>2024-07-26 12:04:28 +0200
commit8ab2b19a946251f258446d22f420d401f61d22f6 (patch)
tree39fb674f135fd1cfcf4de5b319913f0d0c17d11a /internal/transport
parent[docs] Add separate migration section + instructions for moving to GtS and no... (diff)
downloadgotosocial-8ab2b19a946251f258446d22f420d401f61d22f6.tar.xz
[feature] Federate interaction policies + Accepts; enforce policies (#3138)
* [feature] Federate interaction policies + Accepts; enforce policies * use Acceptable type * fix index * remove appendIRIStrs * add GetAccept federatingdb function * lock on object IRI
Diffstat (limited to 'internal/transport')
-rw-r--r--internal/transport/controller.go32
-rw-r--r--internal/transport/dereference.go23
2 files changed, 48 insertions, 7 deletions
diff --git a/internal/transport/controller.go b/internal/transport/controller.go
index 519298d8e..3a529a8f3 100644
--- a/internal/transport/controller.go
+++ b/internal/transport/controller.go
@@ -204,6 +204,38 @@ func (c *controller) dereferenceLocalUser(ctx context.Context, iri *url.URL) (*h
return rsp, nil
}
+// dereferenceLocalAccept is a shortcut to dereference an accept created
+// by an account on this instance, without making any external api/http calls.
+//
+// It is passed to new transports, and should only be invoked when the iri.Host == this host.
+func (c *controller) dereferenceLocalAccept(ctx context.Context, iri *url.URL) (*http.Response, error) {
+ accept, err := c.fedDB.GetAccept(ctx, iri)
+ if err != nil && !errors.Is(err, db.ErrNoEntries) {
+ return nil, err
+ }
+
+ if accept == nil {
+ // Return a generic 404 not found response.
+ rsp := craftResponse(iri, http.StatusNotFound)
+ return rsp, nil
+ }
+
+ i, err := ap.Serialize(accept)
+ if err != nil {
+ return nil, err
+ }
+
+ b, err := json.Marshal(i)
+ if err != nil {
+ return nil, err
+ }
+
+ // Return a response with AS data as body.
+ rsp := craftResponse(iri, http.StatusOK)
+ rsp.Body = io.NopCloser(bytes.NewReader(b))
+ return rsp, nil
+}
+
func craftResponse(url *url.URL, code int) *http.Response {
rsp := new(http.Response)
rsp.Request = new(http.Request)
diff --git a/internal/transport/dereference.go b/internal/transport/dereference.go
index 952791f70..8cc1f2103 100644
--- a/internal/transport/dereference.go
+++ b/internal/transport/dereference.go
@@ -29,17 +29,26 @@ import (
)
func (t *transport) Dereference(ctx context.Context, iri *url.URL) (*http.Response, error) {
- // if the request is to us, we can shortcut for certain URIs rather than going through
- // the normal request flow, thereby saving time and energy
+ // If the request is to us, we can shortcut for
+ // certain URIs rather than going through the normal
+ // request flow, thereby saving time and energy.
if iri.Host == config.GetHost() {
- if uris.IsFollowersPath(iri) {
- // the request is for followers of one of our accounts, which we can shortcut
+ switch {
+
+ case uris.IsFollowersPath(iri):
+ // The request is for followers of one of
+ // our accounts, which we can shortcut.
return t.controller.dereferenceLocalFollowers(ctx, iri)
- }
- if uris.IsUserPath(iri) {
- // the request is for one of our accounts, which we can shortcut
+ case uris.IsUserPath(iri):
+ // The request is for one of our
+ // accounts, which we can shortcut.
return t.controller.dereferenceLocalUser(ctx, iri)
+
+ case uris.IsAcceptsPath(iri):
+ // The request is for an Accept on
+ // our instance, which we can shortcut.
+ return t.controller.dereferenceLocalAccept(ctx, iri)
}
}