diff options
| author | 2021-05-24 18:49:48 +0200 | |
|---|---|---|
| committer | 2021-05-24 18:49:48 +0200 | |
| commit | e670c32a9147f632d06ee10c170201677ec1e12d (patch) | |
| tree | 08a49c9942529d3e7457a54b72d334ad36060976 /internal/message | |
| parent | first draft of Dockerfile (diff) | |
| download | gotosocial-e670c32a9147f632d06ee10c170201677ec1e12d.tar.xz | |
Faves (#31)
* start on federating faves
* outbound federation of likes working
Diffstat (limited to 'internal/message')
| -rw-r--r-- | internal/message/accountprocess.go | 7 | ||||
| -rw-r--r-- | internal/message/fromclientapiprocess.go | 34 | ||||
| -rw-r--r-- | internal/message/fromcommonprocess.go | 4 | ||||
| -rw-r--r-- | internal/message/statusprocess.go | 51 | 
4 files changed, 85 insertions, 11 deletions
diff --git a/internal/message/accountprocess.go b/internal/message/accountprocess.go index 424081c34..22542f0c3 100644 --- a/internal/message/accountprocess.go +++ b/internal/message/accountprocess.go @@ -22,6 +22,7 @@ import (  	"errors"  	"fmt" +	"github.com/google/uuid"  	apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"  	"github.com/superseriousbusiness/gotosocial/internal/db"  	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" @@ -417,11 +418,15 @@ func (p *processor) AccountFollowCreate(authed *oauth.Auth, form *apimodel.Accou  	}  	// make the follow request + +	newFollowID := uuid.NewString() +  	fr := >smodel.FollowRequest{ +		ID:              newFollowID,  		AccountID:       authed.Account.ID,  		TargetAccountID: form.TargetAccountID,  		ShowReblogs:     true, -		URI:             util.GenerateURIForFollow(authed.Account.Username, p.config.Protocol, p.config.Host), +		URI:             util.GenerateURIForFollow(authed.Account.Username, p.config.Protocol, p.config.Host, newFollowID),  		Notify:          false,  	}  	if form.Reblogs != nil { diff --git a/internal/message/fromclientapiprocess.go b/internal/message/fromclientapiprocess.go index e91bd6ce4..b0112152b 100644 --- a/internal/message/fromclientapiprocess.go +++ b/internal/message/fromclientapiprocess.go @@ -44,7 +44,7 @@ func (p *processor) processFromClientAPI(clientMsg gtsmodel.FromClientAPI) error  				return err  			} -			if status.VisibilityAdvanced.Federated { +			if status.VisibilityAdvanced != nil && status.VisibilityAdvanced.Federated {  				return p.federateStatus(status)  			}  			return nil @@ -60,6 +60,18 @@ func (p *processor) processFromClientAPI(clientMsg gtsmodel.FromClientAPI) error  			}  			return p.federateFollow(follow, clientMsg.OriginAccount, clientMsg.TargetAccount) +		case gtsmodel.ActivityStreamsLike: +			// CREATE LIKE/FAVE +			fave, ok := clientMsg.GTSModel.(*gtsmodel.StatusFave) +			if !ok { +				return errors.New("fave was not parseable as *gtsmodel.StatusFave") +			} + +			if err := p.notifyFave(fave); err != nil { +				return err +			} + +			return p.federateFave(fave, clientMsg.OriginAccount, clientMsg.TargetAccount)  		}  	case gtsmodel.ActivityStreamsUpdate:  		// UPDATE @@ -214,3 +226,23 @@ func (p *processor) federateAcceptFollowRequest(follow *gtsmodel.Follow, originA  	_, err = p.federator.FederatingActor().Send(context.Background(), outboxIRI, accept)  	return err  } + +func (p *processor) federateFave(fave *gtsmodel.StatusFave, originAccount *gtsmodel.Account, targetAccount *gtsmodel.Account) error { +	// if both accounts are local there's nothing to do here +	if originAccount.Domain == "" && targetAccount.Domain == "" { +		return nil +	} + +	// create the AS fave +	asFave, err := p.tc.FaveToAS(fave) +	if err != nil { +		return fmt.Errorf("federateFave: error converting fave to as format: %s", err) +	} + +	outboxIRI, err := url.Parse(originAccount.OutboxURI) +	if err != nil { +		return fmt.Errorf("federateFave: error parsing outboxURI %s: %s", originAccount.OutboxURI, err) +	} +	_, err = p.federator.FederatingActor().Send(context.Background(), outboxIRI, asFave) +	return err +} diff --git a/internal/message/fromcommonprocess.go b/internal/message/fromcommonprocess.go index 486da39af..2403a8b72 100644 --- a/internal/message/fromcommonprocess.go +++ b/internal/message/fromcommonprocess.go @@ -27,3 +27,7 @@ func (p *processor) notifyStatus(status *gtsmodel.Status) error {  func (p *processor) notifyFollow(follow *gtsmodel.Follow) error {  	return nil  } + +func (p *processor) notifyFave(fave *gtsmodel.StatusFave) error { +   return nil +} diff --git a/internal/message/statusprocess.go b/internal/message/statusprocess.go index 86a07eb4f..6786b2dab 100644 --- a/internal/message/statusprocess.go +++ b/internal/message/statusprocess.go @@ -25,6 +25,7 @@ import (  	"github.com/google/uuid"  	apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" +	"github.com/superseriousbusiness/gotosocial/internal/db"  	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"  	"github.com/superseriousbusiness/gotosocial/internal/oauth"  	"github.com/superseriousbusiness/gotosocial/internal/util" @@ -168,6 +169,14 @@ func (p *processor) StatusFave(authed *oauth.Auth, targetStatusID string) (*apim  		return nil, fmt.Errorf("error fetching related accounts for status %s: %s", targetStatusID, err)  	} +	var boostOfStatus *gtsmodel.Status +	if targetStatus.BoostOfID != "" { +		boostOfStatus = >smodel.Status{} +		if err := p.db.GetByID(targetStatus.BoostOfID, boostOfStatus); err != nil { +			return nil, fmt.Errorf("error fetching boosted status %s: %s", targetStatus.BoostOfID, err) +		} +	} +  	l.Trace("going to see if status is visible")  	visible, err := p.db.StatusVisible(targetStatus, targetAccount, authed.Account, relevantAccounts) // requestingAccount might well be nil here, but StatusVisible knows how to take care of that  	if err != nil { @@ -185,20 +194,44 @@ func (p *processor) StatusFave(authed *oauth.Auth, targetStatusID string) (*apim  		}  	} -	// it's visible! it's faveable! so let's fave the FUCK out of it -	_, err = p.db.FaveStatus(targetStatus, authed.Account.ID) -	if err != nil { -		return nil, fmt.Errorf("error faveing status: %s", err) +	// first check if the status is already faved, if so we don't need to do anything +	newFave := true +	gtsFave := >smodel.Status{} +	if err := p.db.GetWhere([]db.Where{{Key: "status_id", Value: targetStatus.ID}, {Key: "account_id", Value: authed.Account.ID}}, gtsFave); err == nil { +		// we already have a fave for this status +		newFave = false  	} -	var boostOfStatus *gtsmodel.Status -	if targetStatus.BoostOfID != "" { -		boostOfStatus = >smodel.Status{} -		if err := p.db.GetByID(targetStatus.BoostOfID, boostOfStatus); err != nil { -			return nil, fmt.Errorf("error fetching boosted status %s: %s", targetStatus.BoostOfID, err) +	if newFave { +		thisFaveID := uuid.NewString() + +		// we need to create a new fave in the database +		gtsFave := >smodel.StatusFave{ +			ID:               thisFaveID, +			AccountID:        authed.Account.ID, +			TargetAccountID:  targetAccount.ID, +			StatusID:         targetStatus.ID, +			URI:              util.GenerateURIForLike(authed.Account.Username, p.config.Protocol, p.config.Host, thisFaveID), +			GTSStatus:        targetStatus, +			GTSTargetAccount: targetAccount, +			GTSFavingAccount: authed.Account, +		} + +		if err := p.db.Put(gtsFave); err != nil { +			return nil, err +		} + +		// send the new fave through the processor channel for federation etc +		p.fromClientAPI <- gtsmodel.FromClientAPI{ +			APObjectType:   gtsmodel.ActivityStreamsLike, +			APActivityType: gtsmodel.ActivityStreamsCreate, +			GTSModel:       gtsFave, +			OriginAccount:  authed.Account, +			TargetAccount:  targetAccount,  		}  	} +	// return the mastodon representation of the target status  	mastoStatus, err := p.tc.StatusToMasto(targetStatus, targetAccount, authed.Account, relevantAccounts.BoostedAccount, relevantAccounts.ReplyToAccount, boostOfStatus)  	if err != nil {  		return nil, fmt.Errorf("error converting status %s to frontend representation: %s", targetStatus.ID, err)  | 
