gtsocial-umbx

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

update.go (3968B)


      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/config"
     29 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
     30 	"github.com/superseriousbusiness/gotosocial/internal/log"
     31 	"github.com/superseriousbusiness/gotosocial/internal/messages"
     32 )
     33 
     34 // Update sets an existing entry to the database based on the value's
     35 // id.
     36 //
     37 // Note that Activity values received from federated peers may also be
     38 // updated in the database this way if the Federating Protocol is
     39 // enabled. The client may freely decide to store only the id instead of
     40 // the entire value.
     41 //
     42 // The library makes this call only after acquiring a lock first.
     43 func (f *federatingDB) Update(ctx context.Context, asType vocab.Type) error {
     44 	l := log.Entry{}.WithContext(ctx)
     45 
     46 	if log.Level() >= level.DEBUG {
     47 		i, err := marshalItem(asType)
     48 		if err != nil {
     49 			return err
     50 		}
     51 		l = l.WithField("update", i)
     52 		l.Debug("entering Update")
     53 	}
     54 
     55 	receivingAccount, requestingAccount, internal := extractFromCtx(ctx)
     56 	if internal {
     57 		return nil // Already processed.
     58 	}
     59 
     60 	switch asType.GetTypeName() {
     61 	case ap.ActorApplication, ap.ActorGroup, ap.ActorOrganization, ap.ActorPerson, ap.ActorService:
     62 		return f.updateAccountable(ctx, receivingAccount, requestingAccount, asType)
     63 	}
     64 
     65 	return nil
     66 }
     67 
     68 func (f *federatingDB) updateAccountable(ctx context.Context, receivingAcct *gtsmodel.Account, requestingAcct *gtsmodel.Account, asType vocab.Type) error {
     69 	accountable, ok := asType.(ap.Accountable)
     70 	if !ok {
     71 		return errors.New("updateAccountable: could not convert vocab.Type to Accountable")
     72 	}
     73 
     74 	updatedAcct, err := f.typeConverter.ASRepresentationToAccount(ctx, accountable, "")
     75 	if err != nil {
     76 		return fmt.Errorf("updateAccountable: error converting to account: %w", err)
     77 	}
     78 
     79 	if updatedAcct.Domain == config.GetHost() || updatedAcct.Domain == config.GetAccountDomain() {
     80 		// No need to update local accounts; in fact, if we try
     81 		// this it will break the shit out of things so do NOT.
     82 		return nil
     83 	}
     84 
     85 	if requestingAcct.URI != updatedAcct.URI {
     86 		return fmt.Errorf("updateAccountable: update for account %s was requested by account %s, this is not valid", updatedAcct.URI, requestingAcct.URI)
     87 	}
     88 
     89 	// Set some basic fields on the updated account
     90 	// based on what we already know about the requester.
     91 	updatedAcct.CreatedAt = requestingAcct.CreatedAt
     92 	updatedAcct.ID = requestingAcct.ID
     93 	updatedAcct.Language = requestingAcct.Language
     94 	updatedAcct.AvatarMediaAttachmentID = requestingAcct.AvatarMediaAttachmentID
     95 	updatedAcct.HeaderMediaAttachmentID = requestingAcct.HeaderMediaAttachmentID
     96 
     97 	// Pass to the processor for further updating of eg., avatar/header,
     98 	// emojis, etc. The actual db insert/update will take place there.
     99 	f.state.Workers.EnqueueFederator(ctx, messages.FromFederator{
    100 		APObjectType:     ap.ObjectProfile,
    101 		APActivityType:   ap.ActivityUpdate,
    102 		GTSModel:         updatedAcct,
    103 		APObjectModel:    accountable,
    104 		ReceivingAccount: receivingAcct,
    105 	})
    106 
    107 	return nil
    108 }