2023-03-12 16:00:57 +01:00
|
|
|
// GoToSocial
|
|
|
|
// Copyright (C) GoToSocial Authors admin@gotosocial.org
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2021-03-07 13:05:33 +01:00
|
|
|
|
|
|
|
package federation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-05-08 14:25:55 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-03-07 13:05:33 +01:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
2021-11-13 17:29:43 +01:00
|
|
|
"github.com/superseriousbusiness/activity/pub"
|
|
|
|
"github.com/superseriousbusiness/activity/streams"
|
|
|
|
"github.com/superseriousbusiness/activity/streams/vocab"
|
2021-12-20 15:19:53 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/ap"
|
2021-04-01 20:46:45 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
2023-04-28 17:45:21 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
|
2023-03-06 10:38:43 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
|
2021-05-08 14:25:55 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
2022-07-19 10:47:55 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/log"
|
2021-12-20 15:19:53 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/uris"
|
2022-05-23 11:46:50 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/util"
|
2021-03-07 13:05:33 +01:00
|
|
|
)
|
|
|
|
|
2021-04-20 18:14:23 +02:00
|
|
|
/*
|
|
|
|
GO FED FEDERATING PROTOCOL INTERFACE
|
|
|
|
FederatingProtocol contains behaviors an application needs to satisfy for the
|
|
|
|
full ActivityPub S2S implementation to be supported by this library.
|
|
|
|
It is only required if the client application wants to support the server-to-
|
|
|
|
server, or federating, protocol.
|
|
|
|
It is passed to the library as a dependency injection from the client
|
|
|
|
application.
|
|
|
|
*/
|
2021-03-07 13:05:33 +01:00
|
|
|
|
2021-04-20 18:14:23 +02:00
|
|
|
// PostInboxRequestBodyHook callback after parsing the request body for a federated request
|
|
|
|
// to the Actor's inbox.
|
|
|
|
//
|
|
|
|
// Can be used to set contextual information based on the Activity
|
|
|
|
// received.
|
|
|
|
//
|
|
|
|
// Only called if the Federated Protocol is enabled.
|
|
|
|
//
|
|
|
|
// Warning: Neither authentication nor authorization has taken place at
|
|
|
|
// this time. Doing anything beyond setting contextual information is
|
|
|
|
// strongly discouraged.
|
|
|
|
//
|
|
|
|
// If an error is returned, it is passed back to the caller of
|
|
|
|
// PostInbox. In this case, the DelegateActor implementation must not
|
|
|
|
// write a response to the ResponseWriter as is expected that the caller
|
|
|
|
// to PostInbox will do so when handling the error.
|
2021-05-08 14:25:55 +02:00
|
|
|
func (f *federator) PostInboxRequestBodyHook(ctx context.Context, r *http.Request, activity pub.Activity) (context.Context, error) {
|
2022-05-23 11:46:50 +02:00
|
|
|
// extract any other IRIs involved in this activity
|
|
|
|
otherInvolvedIRIs := []*url.URL{}
|
|
|
|
|
|
|
|
// check if the Activity itself has an 'inReplyTo'
|
|
|
|
if replyToable, ok := activity.(ap.ReplyToable); ok {
|
|
|
|
if inReplyToURI := ap.ExtractInReplyToURI(replyToable); inReplyToURI != nil {
|
|
|
|
otherInvolvedIRIs = append(otherInvolvedIRIs, inReplyToURI)
|
|
|
|
}
|
|
|
|
}
|
2021-05-08 14:25:55 +02:00
|
|
|
|
2022-05-23 11:46:50 +02:00
|
|
|
// now check if the Object of the Activity (usually a Note or something) has an 'inReplyTo'
|
|
|
|
if object := activity.GetActivityStreamsObject(); object != nil {
|
|
|
|
if replyToable, ok := object.(ap.ReplyToable); ok {
|
|
|
|
if inReplyToURI := ap.ExtractInReplyToURI(replyToable); inReplyToURI != nil {
|
|
|
|
otherInvolvedIRIs = append(otherInvolvedIRIs, inReplyToURI)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for Tos and CCs on Activity itself
|
|
|
|
if addressable, ok := activity.(ap.Addressable); ok {
|
|
|
|
if ccURIs, err := ap.ExtractCCs(addressable); err == nil {
|
|
|
|
otherInvolvedIRIs = append(otherInvolvedIRIs, ccURIs...)
|
|
|
|
}
|
|
|
|
if toURIs, err := ap.ExtractTos(addressable); err == nil {
|
|
|
|
otherInvolvedIRIs = append(otherInvolvedIRIs, toURIs...)
|
|
|
|
}
|
2021-05-08 14:25:55 +02:00
|
|
|
}
|
2022-05-23 11:46:50 +02:00
|
|
|
|
|
|
|
// and on the Object itself
|
|
|
|
if object := activity.GetActivityStreamsObject(); object != nil {
|
|
|
|
if addressable, ok := object.(ap.Addressable); ok {
|
|
|
|
if ccURIs, err := ap.ExtractCCs(addressable); err == nil {
|
|
|
|
otherInvolvedIRIs = append(otherInvolvedIRIs, ccURIs...)
|
|
|
|
}
|
|
|
|
if toURIs, err := ap.ExtractTos(addressable); err == nil {
|
|
|
|
otherInvolvedIRIs = append(otherInvolvedIRIs, toURIs...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove any duplicate entries in the slice we put together
|
|
|
|
deduped := util.UniqueURIs(otherInvolvedIRIs)
|
|
|
|
|
|
|
|
// clean any instances of the public URI since we don't care about that in this context
|
|
|
|
cleaned := []*url.URL{}
|
|
|
|
for _, u := range deduped {
|
|
|
|
if !pub.IsPublic(u.String()) {
|
|
|
|
cleaned = append(cleaned, u)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
withOtherInvolvedIRIs := context.WithValue(ctx, ap.ContextOtherInvolvedIRIs, cleaned)
|
|
|
|
return withOtherInvolvedIRIs, nil
|
2021-03-07 13:05:33 +01:00
|
|
|
}
|
|
|
|
|
2021-04-20 18:14:23 +02:00
|
|
|
// AuthenticatePostInbox delegates the authentication of a POST to an
|
|
|
|
// inbox.
|
|
|
|
//
|
|
|
|
// If an error is returned, it is passed back to the caller of
|
|
|
|
// PostInbox. In this case, the implementation must not write a
|
|
|
|
// response to the ResponseWriter as is expected that the client will
|
|
|
|
// do so when handling the error. The 'authenticated' is ignored.
|
|
|
|
//
|
|
|
|
// If no error is returned, but authentication or authorization fails,
|
|
|
|
// then authenticated must be false and error nil. It is expected that
|
|
|
|
// the implementation handles writing to the ResponseWriter in this
|
|
|
|
// case.
|
|
|
|
//
|
|
|
|
// Finally, if the authentication and authorization succeeds, then
|
|
|
|
// authenticated must be true and error nil. The request will continue
|
|
|
|
// to be processed.
|
2021-05-08 14:25:55 +02:00
|
|
|
func (f *federator) AuthenticatePostInbox(ctx context.Context, w http.ResponseWriter, r *http.Request) (context.Context, bool, error) {
|
2023-04-06 13:19:55 +02:00
|
|
|
log.Tracef(ctx, "received request to authenticate inbox %s", r.URL.String())
|
2021-05-08 14:25:55 +02:00
|
|
|
|
2023-04-06 13:19:55 +02:00
|
|
|
// Ensure this is an inbox path, and fetch the inbox owner
|
|
|
|
// account by parsing username from `/users/{username}/inbox`.
|
2021-12-20 15:19:53 +01:00
|
|
|
username, err := uris.ParseInboxPath(r.URL)
|
2021-05-15 11:58:11 +02:00
|
|
|
if err != nil {
|
2023-04-06 13:19:55 +02:00
|
|
|
err = fmt.Errorf("AuthenticatePostInbox: could not parse %s as inbox path: %w", r.URL.String(), err)
|
|
|
|
return nil, false, err
|
2021-05-15 11:58:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if username == "" {
|
2023-04-06 13:19:55 +02:00
|
|
|
err = errors.New("AuthenticatePostInbox: inbox username was empty")
|
|
|
|
return nil, false, err
|
2021-05-15 11:58:11 +02:00
|
|
|
}
|
|
|
|
|
2022-09-02 11:56:33 +02:00
|
|
|
receivingAccount, err := f.db.GetAccountByUsernameDomain(ctx, username, "")
|
2021-08-20 12:26:56 +02:00
|
|
|
if err != nil {
|
2023-04-06 13:19:55 +02:00
|
|
|
err = fmt.Errorf("AuthenticatePostInbox: could not fetch receiving account %s: %w", username, err)
|
|
|
|
return nil, false, err
|
2021-05-08 14:25:55 +02:00
|
|
|
}
|
|
|
|
|
2023-04-06 13:19:55 +02:00
|
|
|
// Check who's delivering by inspecting the http signature.
|
2022-04-26 18:10:11 +02:00
|
|
|
publicKeyOwnerURI, errWithCode := f.AuthenticateFederatedRequest(ctx, receivingAccount.Username)
|
|
|
|
if errWithCode != nil {
|
|
|
|
switch errWithCode.Code() {
|
|
|
|
case http.StatusUnauthorized, http.StatusForbidden, http.StatusBadRequest:
|
2023-04-06 13:19:55 +02:00
|
|
|
// If codes 400, 401, or 403, obey the go-fed
|
|
|
|
// interface by writing the header and bailing.
|
2022-04-26 18:10:11 +02:00
|
|
|
w.WriteHeader(errWithCode.Code())
|
|
|
|
return ctx, false, nil
|
2022-11-11 12:18:38 +01:00
|
|
|
case http.StatusGone:
|
2023-04-06 13:19:55 +02:00
|
|
|
// If the requesting account's key has gone
|
|
|
|
// (410) then likely inbox post was a delete.
|
|
|
|
//
|
|
|
|
// We can just write 202 and leave: we didn't
|
|
|
|
// know about the account anyway, so we can't
|
|
|
|
// do any further processing.
|
2022-11-11 12:18:38 +01:00
|
|
|
w.WriteHeader(http.StatusAccepted)
|
|
|
|
return ctx, false, nil
|
2022-04-26 18:10:11 +02:00
|
|
|
default:
|
2023-04-06 13:19:55 +02:00
|
|
|
// Proper error.
|
2022-04-26 18:10:11 +02:00
|
|
|
return ctx, false, err
|
|
|
|
}
|
2021-05-08 14:25:55 +02:00
|
|
|
}
|
|
|
|
|
2023-04-06 13:19:55 +02:00
|
|
|
// Authentication has passed, check if we need to create a
|
|
|
|
// new instance entry for the Host of the requesting account.
|
|
|
|
if _, err := f.db.GetInstance(ctx, publicKeyOwnerURI.Host); err != nil {
|
|
|
|
if !errors.Is(err, db.ErrNoEntries) {
|
|
|
|
// There's been an actual error.
|
|
|
|
err = fmt.Errorf("AuthenticatePostInbox: error getting instance %s: %w", publicKeyOwnerURI.Host, err)
|
|
|
|
return ctx, false, err
|
2021-06-27 16:52:18 +02:00
|
|
|
}
|
|
|
|
|
2023-04-28 17:45:21 +02:00
|
|
|
// we don't have an entry for this instance yet so dereference it
|
|
|
|
instance, err := f.GetRemoteInstance(gtscontext.SetFastFail(ctx), username, &url.URL{
|
2021-06-27 16:52:18 +02:00
|
|
|
Scheme: publicKeyOwnerURI.Scheme,
|
|
|
|
Host: publicKeyOwnerURI.Host,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-04-06 13:19:55 +02:00
|
|
|
err = fmt.Errorf("AuthenticatePostInbox: error dereferencing instance %s: %w", publicKeyOwnerURI.Host, err)
|
|
|
|
return nil, false, err
|
2021-06-27 16:52:18 +02:00
|
|
|
}
|
|
|
|
|
2023-04-06 13:19:55 +02:00
|
|
|
if err := f.db.Put(ctx, instance); err != nil {
|
|
|
|
err = fmt.Errorf("AuthenticatePostInbox: error inserting instance entry for %s: %w", publicKeyOwnerURI.Host, err)
|
|
|
|
return nil, false, err
|
2021-06-27 16:52:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-06 13:19:55 +02:00
|
|
|
// We know the public key owner URI now, so we can
|
|
|
|
// dereference the remote account (or just get it
|
|
|
|
// from the db if we already have it).
|
2023-05-12 11:15:54 +02:00
|
|
|
requestingAccount, _, err := f.GetAccountByURI(gtscontext.SetFastFail(ctx), username, publicKeyOwnerURI)
|
2021-08-10 13:32:39 +02:00
|
|
|
if err != nil {
|
2023-03-06 10:38:43 +01:00
|
|
|
if gtserror.StatusCode(err) == http.StatusGone {
|
2023-02-15 20:41:16 +01:00
|
|
|
// This is the same case as the http.StatusGone check above.
|
|
|
|
// It can happen here and not there because there's a race
|
|
|
|
// where the sending server starts sending account deletion
|
|
|
|
// notifications out, we start processing, the request above
|
|
|
|
// succeeds, and *then* the profile is removed and starts
|
|
|
|
// returning 410 Gone, at which point _this_ request fails.
|
|
|
|
w.WriteHeader(http.StatusAccepted)
|
|
|
|
return ctx, false, nil
|
|
|
|
}
|
2023-04-06 13:19:55 +02:00
|
|
|
err = fmt.Errorf("AuthenticatePostInbox: couldn't get requesting account %s: %w", publicKeyOwnerURI, err)
|
|
|
|
return nil, false, err
|
2021-05-08 14:25:55 +02:00
|
|
|
}
|
|
|
|
|
2023-04-06 13:19:55 +02:00
|
|
|
// We have everything we need now, set the requesting
|
|
|
|
// and receiving accounts on the context for later use.
|
2021-12-20 15:19:53 +01:00
|
|
|
withRequesting := context.WithValue(ctx, ap.ContextRequestingAccount, requestingAccount)
|
|
|
|
withReceiving := context.WithValue(withRequesting, ap.ContextReceivingAccount, receivingAccount)
|
2021-10-10 12:39:25 +02:00
|
|
|
return withReceiving, true, nil
|
2021-03-07 13:05:33 +01:00
|
|
|
}
|
|
|
|
|
2021-04-20 18:14:23 +02:00
|
|
|
// Blocked should determine whether to permit a set of actors given by
|
|
|
|
// their ids are able to interact with this particular end user due to
|
|
|
|
// being blocked or other application-specific logic.
|
|
|
|
//
|
|
|
|
// If an error is returned, it is passed back to the caller of
|
|
|
|
// PostInbox.
|
|
|
|
//
|
|
|
|
// If no error is returned, but authentication or authorization fails,
|
|
|
|
// then blocked must be true and error nil. An http.StatusForbidden
|
|
|
|
// will be written in the wresponse.
|
|
|
|
//
|
|
|
|
// Finally, if the authentication and authorization succeeds, then
|
|
|
|
// blocked must be false and error nil. The request will continue
|
|
|
|
// to be processed.
|
2021-05-08 14:25:55 +02:00
|
|
|
func (f *federator) Blocked(ctx context.Context, actorIRIs []*url.URL) (bool, error) {
|
2023-02-17 12:02:29 +01:00
|
|
|
log.Tracef(ctx, "entering BLOCKED function with IRI list: %+v", actorIRIs)
|
2021-05-15 11:58:11 +02:00
|
|
|
|
2022-05-23 11:46:50 +02:00
|
|
|
// check domain blocks first for the given actor IRIs
|
|
|
|
blocked, err := f.db.AreURIsBlocked(ctx, actorIRIs)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("error checking domain blocks of actorIRIs: %s", err)
|
|
|
|
}
|
|
|
|
if blocked {
|
|
|
|
return blocked, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// check domain blocks for any other involved IRIs
|
|
|
|
otherInvolvedIRIsI := ctx.Value(ap.ContextOtherInvolvedIRIs)
|
|
|
|
otherInvolvedIRIs, ok := otherInvolvedIRIsI.([]*url.URL)
|
|
|
|
if !ok {
|
2023-02-17 12:02:29 +01:00
|
|
|
log.Error(ctx, "other involved IRIs not set on request context")
|
2022-05-23 11:46:50 +02:00
|
|
|
return false, errors.New("other involved IRIs not set on request context, so couldn't determine blocks")
|
|
|
|
}
|
|
|
|
blocked, err = f.db.AreURIsBlocked(ctx, otherInvolvedIRIs)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("error checking domain blocks of otherInvolvedIRIs: %s", err)
|
|
|
|
}
|
|
|
|
if blocked {
|
|
|
|
return blocked, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// now check for user-level block from receiving against requesting account
|
2021-12-20 15:19:53 +01:00
|
|
|
receivingAccountI := ctx.Value(ap.ContextReceivingAccount)
|
2021-10-10 12:39:25 +02:00
|
|
|
receivingAccount, ok := receivingAccountI.(*gtsmodel.Account)
|
2021-05-15 11:58:11 +02:00
|
|
|
if !ok {
|
2023-02-17 12:02:29 +01:00
|
|
|
log.Error(ctx, "receiving account not set on request context")
|
2021-10-10 12:39:25 +02:00
|
|
|
return false, errors.New("receiving account not set on request context, so couldn't determine blocks")
|
2021-05-15 11:58:11 +02:00
|
|
|
}
|
2022-05-23 11:46:50 +02:00
|
|
|
requestingAccountI := ctx.Value(ap.ContextRequestingAccount)
|
|
|
|
requestingAccount, ok := requestingAccountI.(*gtsmodel.Account)
|
|
|
|
if !ok {
|
2023-02-17 12:02:29 +01:00
|
|
|
log.Error(ctx, "requesting account not set on request context")
|
2022-05-23 11:46:50 +02:00
|
|
|
return false, errors.New("requesting account not set on request context, so couldn't determine blocks")
|
|
|
|
}
|
|
|
|
// the receiver shouldn't block the sender
|
[performance] refactoring + add fave / follow / request / visibility caching (#1607)
* refactor visibility checking, add caching for visibility
* invalidate visibility cache items on account / status deletes
* fix requester ID passed to visibility cache nil ptr
* de-interface caches, fix home / public timeline caching + visibility
* finish adding code comments for visibility filter
* fix angry goconst linter warnings
* actually finish adding filter visibility code comments for timeline functions
* move home timeline status author check to after visibility
* remove now-unused code
* add more code comments
* add TODO code comment, update printed cache start names
* update printed cache names on stop
* start adding separate follow(request) delete db functions, add specific visibility cache tests
* add relationship type caching
* fix getting local account follows / followed-bys, other small codebase improvements
* simplify invalidation using cache hooks, add more GetAccountBy___() functions
* fix boosting to return 404 if not boostable but no error (to not leak status ID)
* remove dead code
* improved placement of cache invalidation
* update license headers
* add example follow, follow-request config entries
* add example visibility cache configuration to config file
* use specific PutFollowRequest() instead of just Put()
* add tests for all GetAccountBy()
* add GetBlockBy() tests
* update block to check primitive fields
* update and finish adding Get{Account,Block,Follow,FollowRequest}By() tests
* fix copy-pasted code
* update envparsing test
* whitespace
* fix bun struct tag
* add license header to gtscontext
* fix old license header
* improved error creation to not use fmt.Errorf() when not needed
* fix various rebase conflicts, fix account test
* remove commented-out code, fix-up mention caching
* fix mention select bun statement
* ensure mention target account populated, pass in context to customrenderer logging
* remove more uncommented code, fix typeutil test
* add statusfave database model caching
* add status fave cache configuration
* add status fave cache example config
* woops, catch missed error. nice catch linter!
* add back testrig panic on nil db
* update example configuration to match defaults, slight tweak to cache configuration defaults
* update envparsing test with new defaults
* fetch followingget to use the follow target account
* use accounnt.IsLocal() instead of empty domain check
* use constants for the cache visibility type check
* use bun.In() for notification type restriction in db query
* include replies when fetching PublicTimeline() (to account for single-author threads in Visibility{}.StatusPublicTimelineable())
* use bun query building for nested select statements to ensure working with postgres
* update public timeline future status checks to match visibility filter
* same as previous, for home timeline
* update public timeline tests to dynamically check for appropriate statuses
* migrate accounts to allow unique constraint on public_key
* provide minimal account with publicKey
---------
Signed-off-by: kim <grufwub@gmail.com>
Co-authored-by: tsmethurst <tobi.smethurst@protonmail.com>
2023-03-28 15:03:14 +02:00
|
|
|
blocked, err = f.db.IsBlocked(ctx, receivingAccount.ID, requestingAccount.ID)
|
2021-08-20 12:26:56 +02:00
|
|
|
if err != nil {
|
2022-05-23 11:46:50 +02:00
|
|
|
return false, fmt.Errorf("error checking user-level blocks: %s", err)
|
2021-08-20 12:26:56 +02:00
|
|
|
}
|
|
|
|
if blocked {
|
|
|
|
return blocked, nil
|
|
|
|
}
|
|
|
|
|
2022-05-23 11:46:50 +02:00
|
|
|
// get account IDs for other involved accounts
|
|
|
|
var involvedAccountIDs []string
|
|
|
|
for _, iri := range otherInvolvedIRIs {
|
|
|
|
var involvedAccountID string
|
|
|
|
if involvedStatus, err := f.db.GetStatusByURI(ctx, iri.String()); err == nil {
|
|
|
|
involvedAccountID = involvedStatus.AccountID
|
|
|
|
} else if involvedAccount, err := f.db.GetAccountByURI(ctx, iri.String()); err == nil {
|
|
|
|
involvedAccountID = involvedAccount.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
if involvedAccountID != "" {
|
|
|
|
involvedAccountIDs = append(involvedAccountIDs, involvedAccountID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
deduped := util.UniqueStrings(involvedAccountIDs)
|
|
|
|
|
|
|
|
for _, involvedAccountID := range deduped {
|
|
|
|
// the involved account shouldn't block whoever is making this request
|
[performance] refactoring + add fave / follow / request / visibility caching (#1607)
* refactor visibility checking, add caching for visibility
* invalidate visibility cache items on account / status deletes
* fix requester ID passed to visibility cache nil ptr
* de-interface caches, fix home / public timeline caching + visibility
* finish adding code comments for visibility filter
* fix angry goconst linter warnings
* actually finish adding filter visibility code comments for timeline functions
* move home timeline status author check to after visibility
* remove now-unused code
* add more code comments
* add TODO code comment, update printed cache start names
* update printed cache names on stop
* start adding separate follow(request) delete db functions, add specific visibility cache tests
* add relationship type caching
* fix getting local account follows / followed-bys, other small codebase improvements
* simplify invalidation using cache hooks, add more GetAccountBy___() functions
* fix boosting to return 404 if not boostable but no error (to not leak status ID)
* remove dead code
* improved placement of cache invalidation
* update license headers
* add example follow, follow-request config entries
* add example visibility cache configuration to config file
* use specific PutFollowRequest() instead of just Put()
* add tests for all GetAccountBy()
* add GetBlockBy() tests
* update block to check primitive fields
* update and finish adding Get{Account,Block,Follow,FollowRequest}By() tests
* fix copy-pasted code
* update envparsing test
* whitespace
* fix bun struct tag
* add license header to gtscontext
* fix old license header
* improved error creation to not use fmt.Errorf() when not needed
* fix various rebase conflicts, fix account test
* remove commented-out code, fix-up mention caching
* fix mention select bun statement
* ensure mention target account populated, pass in context to customrenderer logging
* remove more uncommented code, fix typeutil test
* add statusfave database model caching
* add status fave cache configuration
* add status fave cache example config
* woops, catch missed error. nice catch linter!
* add back testrig panic on nil db
* update example configuration to match defaults, slight tweak to cache configuration defaults
* update envparsing test with new defaults
* fetch followingget to use the follow target account
* use accounnt.IsLocal() instead of empty domain check
* use constants for the cache visibility type check
* use bun.In() for notification type restriction in db query
* include replies when fetching PublicTimeline() (to account for single-author threads in Visibility{}.StatusPublicTimelineable())
* use bun query building for nested select statements to ensure working with postgres
* update public timeline future status checks to match visibility filter
* same as previous, for home timeline
* update public timeline tests to dynamically check for appropriate statuses
* migrate accounts to allow unique constraint on public_key
* provide minimal account with publicKey
---------
Signed-off-by: kim <grufwub@gmail.com>
Co-authored-by: tsmethurst <tobi.smethurst@protonmail.com>
2023-03-28 15:03:14 +02:00
|
|
|
blocked, err = f.db.IsBlocked(ctx, involvedAccountID, requestingAccount.ID)
|
2021-07-05 13:23:03 +02:00
|
|
|
if err != nil {
|
2022-05-23 11:46:50 +02:00
|
|
|
return false, fmt.Errorf("error checking user-level otherInvolvedIRI blocks: %s", err)
|
|
|
|
}
|
|
|
|
if blocked {
|
|
|
|
return blocked, nil
|
2021-05-15 11:58:11 +02:00
|
|
|
}
|
2021-07-11 16:22:21 +02:00
|
|
|
|
2022-05-23 11:46:50 +02:00
|
|
|
// whoever is receiving this request shouldn't block the involved account
|
[performance] refactoring + add fave / follow / request / visibility caching (#1607)
* refactor visibility checking, add caching for visibility
* invalidate visibility cache items on account / status deletes
* fix requester ID passed to visibility cache nil ptr
* de-interface caches, fix home / public timeline caching + visibility
* finish adding code comments for visibility filter
* fix angry goconst linter warnings
* actually finish adding filter visibility code comments for timeline functions
* move home timeline status author check to after visibility
* remove now-unused code
* add more code comments
* add TODO code comment, update printed cache start names
* update printed cache names on stop
* start adding separate follow(request) delete db functions, add specific visibility cache tests
* add relationship type caching
* fix getting local account follows / followed-bys, other small codebase improvements
* simplify invalidation using cache hooks, add more GetAccountBy___() functions
* fix boosting to return 404 if not boostable but no error (to not leak status ID)
* remove dead code
* improved placement of cache invalidation
* update license headers
* add example follow, follow-request config entries
* add example visibility cache configuration to config file
* use specific PutFollowRequest() instead of just Put()
* add tests for all GetAccountBy()
* add GetBlockBy() tests
* update block to check primitive fields
* update and finish adding Get{Account,Block,Follow,FollowRequest}By() tests
* fix copy-pasted code
* update envparsing test
* whitespace
* fix bun struct tag
* add license header to gtscontext
* fix old license header
* improved error creation to not use fmt.Errorf() when not needed
* fix various rebase conflicts, fix account test
* remove commented-out code, fix-up mention caching
* fix mention select bun statement
* ensure mention target account populated, pass in context to customrenderer logging
* remove more uncommented code, fix typeutil test
* add statusfave database model caching
* add status fave cache configuration
* add status fave cache example config
* woops, catch missed error. nice catch linter!
* add back testrig panic on nil db
* update example configuration to match defaults, slight tweak to cache configuration defaults
* update envparsing test with new defaults
* fetch followingget to use the follow target account
* use accounnt.IsLocal() instead of empty domain check
* use constants for the cache visibility type check
* use bun.In() for notification type restriction in db query
* include replies when fetching PublicTimeline() (to account for single-author threads in Visibility{}.StatusPublicTimelineable())
* use bun query building for nested select statements to ensure working with postgres
* update public timeline future status checks to match visibility filter
* same as previous, for home timeline
* update public timeline tests to dynamically check for appropriate statuses
* migrate accounts to allow unique constraint on public_key
* provide minimal account with publicKey
---------
Signed-off-by: kim <grufwub@gmail.com>
Co-authored-by: tsmethurst <tobi.smethurst@protonmail.com>
2023-03-28 15:03:14 +02:00
|
|
|
blocked, err = f.db.IsBlocked(ctx, receivingAccount.ID, involvedAccountID)
|
2021-08-20 12:26:56 +02:00
|
|
|
if err != nil {
|
2022-05-23 11:46:50 +02:00
|
|
|
return false, fmt.Errorf("error checking user-level otherInvolvedIRI blocks: %s", err)
|
2021-08-20 12:26:56 +02:00
|
|
|
}
|
|
|
|
if blocked {
|
2022-05-23 11:46:50 +02:00
|
|
|
return blocked, nil
|
2021-05-15 11:58:11 +02:00
|
|
|
}
|
|
|
|
}
|
2021-09-24 17:56:48 +02:00
|
|
|
|
2021-03-07 13:05:33 +01:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2021-04-20 18:14:23 +02:00
|
|
|
// FederatingCallbacks returns the application logic that handles
|
|
|
|
// ActivityStreams received from federating peers.
|
|
|
|
//
|
|
|
|
// Note that certain types of callbacks will be 'wrapped' with default
|
|
|
|
// behaviors supported natively by the library. Other callbacks
|
|
|
|
// compatible with streams.TypeResolver can be specified by 'other'.
|
|
|
|
//
|
|
|
|
// For example, setting the 'Create' field in the
|
|
|
|
// FederatingWrappedCallbacks lets an application dependency inject
|
|
|
|
// additional behaviors they want to take place, including the default
|
|
|
|
// behavior supplied by this library. This is guaranteed to be compliant
|
|
|
|
// with the ActivityPub Social protocol.
|
|
|
|
//
|
|
|
|
// To override the default behavior, instead supply the function in
|
|
|
|
// 'other', which does not guarantee the application will be compliant
|
|
|
|
// with the ActivityPub Social Protocol.
|
|
|
|
//
|
|
|
|
// Applications are not expected to handle every single ActivityStreams
|
|
|
|
// type and extension. The unhandled ones are passed to DefaultCallback.
|
2021-05-15 11:58:11 +02:00
|
|
|
func (f *federator) FederatingCallbacks(ctx context.Context) (wrapped pub.FederatingWrappedCallbacks, other []interface{}, err error) {
|
|
|
|
wrapped = pub.FederatingWrappedCallbacks{
|
2023-04-06 13:19:55 +02:00
|
|
|
// OnFollow determines what action to take for this
|
|
|
|
// particular callback if a Follow Activity is handled.
|
2021-05-27 16:06:24 +02:00
|
|
|
//
|
2023-04-06 13:19:55 +02:00
|
|
|
// For our implementation, we always want to do nothing
|
|
|
|
// because we have internal logic for handling follows.
|
2021-05-27 16:06:24 +02:00
|
|
|
OnFollow: pub.OnFollowDoNothing,
|
2021-05-15 11:58:11 +02:00
|
|
|
}
|
|
|
|
|
2023-04-06 13:19:55 +02:00
|
|
|
// Override some default behaviors to trigger our own side effects.
|
2021-05-21 15:48:26 +02:00
|
|
|
other = []interface{}{
|
|
|
|
func(ctx context.Context, undo vocab.ActivityStreamsUndo) error {
|
|
|
|
return f.FederatingDB().Undo(ctx, undo)
|
|
|
|
},
|
|
|
|
func(ctx context.Context, accept vocab.ActivityStreamsAccept) error {
|
|
|
|
return f.FederatingDB().Accept(ctx, accept)
|
|
|
|
},
|
2021-10-16 13:27:43 +02:00
|
|
|
func(ctx context.Context, reject vocab.ActivityStreamsReject) error {
|
|
|
|
return f.FederatingDB().Reject(ctx, reject)
|
|
|
|
},
|
2021-05-28 19:57:04 +02:00
|
|
|
func(ctx context.Context, announce vocab.ActivityStreamsAnnounce) error {
|
|
|
|
return f.FederatingDB().Announce(ctx, announce)
|
|
|
|
},
|
2021-05-21 15:48:26 +02:00
|
|
|
}
|
|
|
|
|
2021-05-15 11:58:11 +02:00
|
|
|
return
|
2021-03-07 13:05:33 +01:00
|
|
|
}
|
|
|
|
|
2021-04-20 18:14:23 +02:00
|
|
|
// DefaultCallback is called for types that go-fed can deserialize but
|
|
|
|
// are not handled by the application's callbacks returned in the
|
|
|
|
// Callbacks method.
|
|
|
|
//
|
|
|
|
// Applications are not expected to handle every single ActivityStreams
|
|
|
|
// type and extension, so the unhandled ones are passed to
|
|
|
|
// DefaultCallback.
|
2021-05-08 14:25:55 +02:00
|
|
|
func (f *federator) DefaultCallback(ctx context.Context, activity pub.Activity) error {
|
2023-04-06 13:19:55 +02:00
|
|
|
log.Debugf(ctx, "received unhandle-able activity type (%s) so ignoring it", activity.GetTypeName())
|
2021-03-07 13:05:33 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-20 18:14:23 +02:00
|
|
|
// MaxInboxForwardingRecursionDepth determines how deep to search within
|
|
|
|
// an activity to determine if inbox forwarding needs to occur.
|
|
|
|
//
|
|
|
|
// Zero or negative numbers indicate infinite recursion.
|
2021-05-08 14:25:55 +02:00
|
|
|
func (f *federator) MaxInboxForwardingRecursionDepth(ctx context.Context) int {
|
2021-03-07 13:05:33 +01:00
|
|
|
// TODO
|
2021-05-15 11:58:11 +02:00
|
|
|
return 4
|
2021-03-07 13:05:33 +01:00
|
|
|
}
|
|
|
|
|
2021-04-20 18:14:23 +02:00
|
|
|
// MaxDeliveryRecursionDepth determines how deep to search within
|
|
|
|
// collections owned by peers when they are targeted to receive a
|
|
|
|
// delivery.
|
|
|
|
//
|
|
|
|
// Zero or negative numbers indicate infinite recursion.
|
2021-05-08 14:25:55 +02:00
|
|
|
func (f *federator) MaxDeliveryRecursionDepth(ctx context.Context) int {
|
2021-03-07 13:05:33 +01:00
|
|
|
// TODO
|
2021-05-15 11:58:11 +02:00
|
|
|
return 4
|
2021-03-07 13:05:33 +01:00
|
|
|
}
|
|
|
|
|
2021-04-20 18:14:23 +02:00
|
|
|
// FilterForwarding allows the implementation to apply business logic
|
|
|
|
// such as blocks, spam filtering, and so on to a list of potential
|
|
|
|
// Collections and OrderedCollections of recipients when inbox
|
|
|
|
// forwarding has been triggered.
|
|
|
|
//
|
|
|
|
// The activity is provided as a reference for more intelligent
|
|
|
|
// logic to be used, but the implementation must not modify it.
|
2021-05-08 14:25:55 +02:00
|
|
|
func (f *federator) FilterForwarding(ctx context.Context, potentialRecipients []*url.URL, a pub.Activity) ([]*url.URL, error) {
|
2021-03-07 13:05:33 +01:00
|
|
|
// TODO
|
2021-05-29 19:36:54 +02:00
|
|
|
return []*url.URL{}, nil
|
2021-03-07 13:05:33 +01:00
|
|
|
}
|
|
|
|
|
2021-04-20 18:14:23 +02:00
|
|
|
// GetInbox returns the OrderedCollection inbox of the actor for this
|
|
|
|
// context. It is up to the implementation to provide the correct
|
|
|
|
// collection for the kind of authorization given in the request.
|
|
|
|
//
|
|
|
|
// AuthenticateGetInbox will be called prior to this.
|
|
|
|
//
|
|
|
|
// Always called, regardless whether the Federated Protocol or Social
|
|
|
|
// API is enabled.
|
2021-05-08 14:25:55 +02:00
|
|
|
func (f *federator) GetInbox(ctx context.Context, r *http.Request) (vocab.ActivityStreamsOrderedCollectionPage, error) {
|
2021-05-15 11:58:11 +02:00
|
|
|
// IMPLEMENTATION NOTE: For GoToSocial, we serve GETS to outboxes and inboxes through
|
2021-05-08 14:25:55 +02:00
|
|
|
// the CLIENT API, not through the federation API, so we just do nothing here.
|
2021-05-29 19:36:54 +02:00
|
|
|
return streams.NewActivityStreamsOrderedCollectionPage(), nil
|
2021-03-07 13:05:33 +01:00
|
|
|
}
|