gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

accept.go (4177B)


      1 // GoToSocial
      2 // Copyright (C) GoToSocial Authors admin@gotosocial.org
      3 // SPDX-License-Identifier: AGPL-3.0-or-later
      4 //
      5 // This program is free software: you can redistribute it and/or modify
      6 // it under the terms of the GNU Affero General Public License as published by
      7 // the Free Software Foundation, either version 3 of the License, or
      8 // (at your option) any later version.
      9 //
     10 // This program is distributed in the hope that it will be useful,
     11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 // GNU Affero General Public License for more details.
     14 //
     15 // You should have received a copy of the GNU Affero General Public License
     16 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17 
     18 package federatingdb
     19 
     20 import (
     21 	"context"
     22 	"errors"
     23 	"fmt"
     24 
     25 	"codeberg.org/gruf/go-logger/v2/level"
     26 	"github.com/superseriousbusiness/activity/streams/vocab"
     27 	"github.com/superseriousbusiness/gotosocial/internal/ap"
     28 	"github.com/superseriousbusiness/gotosocial/internal/log"
     29 	"github.com/superseriousbusiness/gotosocial/internal/messages"
     30 	"github.com/superseriousbusiness/gotosocial/internal/uris"
     31 )
     32 
     33 func (f *federatingDB) Accept(ctx context.Context, accept vocab.ActivityStreamsAccept) error {
     34 	if log.Level() >= level.DEBUG {
     35 		i, err := marshalItem(accept)
     36 		if err != nil {
     37 			return err
     38 		}
     39 		l := log.WithContext(ctx).
     40 			WithField("accept", i)
     41 		l.Debug("entering Accept")
     42 	}
     43 
     44 	receivingAccount, _, internal := extractFromCtx(ctx)
     45 	if internal {
     46 		return nil // Already processed.
     47 	}
     48 
     49 	acceptObject := accept.GetActivityStreamsObject()
     50 	if acceptObject == nil {
     51 		return errors.New("ACCEPT: no object set on vocab.ActivityStreamsAccept")
     52 	}
     53 
     54 	for iter := acceptObject.Begin(); iter != acceptObject.End(); iter = iter.Next() {
     55 		// check if the object is an IRI
     56 		if iter.IsIRI() {
     57 			// we have just the URI of whatever is being accepted, so we need to find out what it is
     58 			acceptedObjectIRI := iter.GetIRI()
     59 			if uris.IsFollowPath(acceptedObjectIRI) {
     60 				// ACCEPT FOLLOW
     61 				followReq, err := f.state.DB.GetFollowRequestByURI(ctx, acceptedObjectIRI.String())
     62 				if err != nil {
     63 					return fmt.Errorf("ACCEPT: couldn't get follow request with id %s from the database: %s", acceptedObjectIRI.String(), err)
     64 				}
     65 
     66 				// make sure the addressee of the original follow is the same as whatever inbox this landed in
     67 				if followReq.AccountID != receivingAccount.ID {
     68 					return errors.New("ACCEPT: follow object account and inbox account were not the same")
     69 				}
     70 				follow, err := f.state.DB.AcceptFollowRequest(ctx, followReq.AccountID, followReq.TargetAccountID)
     71 				if err != nil {
     72 					return err
     73 				}
     74 
     75 				f.state.Workers.EnqueueFederator(ctx, messages.FromFederator{
     76 					APObjectType:     ap.ActivityFollow,
     77 					APActivityType:   ap.ActivityAccept,
     78 					GTSModel:         follow,
     79 					ReceivingAccount: receivingAccount,
     80 				})
     81 
     82 				return nil
     83 			}
     84 		}
     85 
     86 		// check if iter is an AP object / type
     87 		if iter.GetType() == nil {
     88 			continue
     89 		}
     90 		if iter.GetType().GetTypeName() == ap.ActivityFollow {
     91 			// ACCEPT FOLLOW
     92 			asFollow, ok := iter.GetType().(vocab.ActivityStreamsFollow)
     93 			if !ok {
     94 				return errors.New("ACCEPT: couldn't parse follow into vocab.ActivityStreamsFollow")
     95 			}
     96 			// convert the follow to something we can understand
     97 			gtsFollow, err := f.typeConverter.ASFollowToFollow(ctx, asFollow)
     98 			if err != nil {
     99 				return fmt.Errorf("ACCEPT: error converting asfollow to gtsfollow: %s", err)
    100 			}
    101 			// make sure the addressee of the original follow is the same as whatever inbox this landed in
    102 			if gtsFollow.AccountID != receivingAccount.ID {
    103 				return errors.New("ACCEPT: follow object account and inbox account were not the same")
    104 			}
    105 			follow, err := f.state.DB.AcceptFollowRequest(ctx, gtsFollow.AccountID, gtsFollow.TargetAccountID)
    106 			if err != nil {
    107 				return err
    108 			}
    109 
    110 			f.state.Workers.EnqueueFederator(ctx, messages.FromFederator{
    111 				APObjectType:     ap.ActivityFollow,
    112 				APActivityType:   ap.ActivityAccept,
    113 				GTSModel:         follow,
    114 				ReceivingAccount: receivingAccount,
    115 			})
    116 
    117 			return nil
    118 		}
    119 	}
    120 
    121 	return nil
    122 }