2021-08-10 13:32:39 +02:00
|
|
|
/*
|
|
|
|
GoToSocial
|
|
|
|
Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org
|
|
|
|
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package dereferencing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
2021-08-25 15:34:33 +02:00
|
|
|
"strings"
|
2021-08-10 13:32:39 +02:00
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
2021-11-13 17:29:43 +01:00
|
|
|
"github.com/superseriousbusiness/activity/streams"
|
|
|
|
"github.com/superseriousbusiness/activity/streams/vocab"
|
2021-08-10 13:32:39 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/ap"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/id"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/transport"
|
|
|
|
)
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
func instanceAccount(account *gtsmodel.Account) bool {
|
|
|
|
return strings.EqualFold(account.Username, account.Domain) ||
|
|
|
|
account.FollowersURI == "" ||
|
|
|
|
account.FollowingURI == "" ||
|
|
|
|
(account.Username == "internal.fetch" && strings.Contains(account.Note, "internal service actor"))
|
|
|
|
}
|
|
|
|
|
2021-08-10 13:32:39 +02:00
|
|
|
// EnrichRemoteAccount takes an account that's already been inserted into the database in a minimal form,
|
|
|
|
// and populates it with additional fields, media, etc.
|
|
|
|
//
|
|
|
|
// EnrichRemoteAccount is mostly useful for calling after an account has been initially created by
|
|
|
|
// the federatingDB's Create function, or during the federated authorization flow.
|
2021-08-25 15:34:33 +02:00
|
|
|
func (d *deref) EnrichRemoteAccount(ctx context.Context, username string, account *gtsmodel.Account) (*gtsmodel.Account, error) {
|
|
|
|
// if we're dealing with an instance account, we don't need to update anything
|
|
|
|
if instanceAccount(account) {
|
|
|
|
return account, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := d.PopulateAccountFields(ctx, account, username, false); err != nil {
|
2021-08-10 13:32:39 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-01 11:08:21 +02:00
|
|
|
updated, err := d.db.UpdateAccount(ctx, account)
|
2021-08-25 15:34:33 +02:00
|
|
|
if err != nil {
|
2021-10-11 14:37:33 +02:00
|
|
|
logrus.Errorf("EnrichRemoteAccount: error updating account: %s", err)
|
2021-09-01 11:08:21 +02:00
|
|
|
return account, nil
|
2021-08-10 13:32:39 +02:00
|
|
|
}
|
|
|
|
|
2021-09-01 11:08:21 +02:00
|
|
|
return updated, nil
|
2021-08-10 13:32:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetRemoteAccount completely dereferences a remote account, converts it to a GtS model account,
|
|
|
|
// puts it in the database, and returns it to a caller. The boolean indicates whether the account is new
|
|
|
|
// to us or not. If we haven't seen the account before, bool will be true. If we have seen the account before,
|
|
|
|
// it will be false.
|
|
|
|
//
|
|
|
|
// Refresh indicates whether--if the account exists in our db already--it should be refreshed by calling
|
|
|
|
// the remote instance again.
|
|
|
|
//
|
|
|
|
// SIDE EFFECTS: remote account will be stored in the database, or updated if it already exists (and refresh is true).
|
2021-08-25 15:34:33 +02:00
|
|
|
func (d *deref) GetRemoteAccount(ctx context.Context, username string, remoteAccountID *url.URL, refresh bool) (*gtsmodel.Account, bool, error) {
|
2021-08-10 13:32:39 +02:00
|
|
|
new := true
|
|
|
|
|
|
|
|
// check if we already have the account in our db
|
2021-08-25 15:34:33 +02:00
|
|
|
maybeAccount, err := d.db.GetAccountByURI(ctx, remoteAccountID.String())
|
2021-08-20 12:26:56 +02:00
|
|
|
if err == nil {
|
2021-08-10 13:32:39 +02:00
|
|
|
// we've seen this account before so it's not new
|
|
|
|
new = false
|
|
|
|
if !refresh {
|
2021-08-10 16:56:59 +02:00
|
|
|
// we're not being asked to refresh, but just in case we don't have the avatar/header cached yet....
|
2021-08-25 15:34:33 +02:00
|
|
|
maybeAccount, err = d.EnrichRemoteAccount(ctx, username, maybeAccount)
|
2021-08-10 16:56:59 +02:00
|
|
|
return maybeAccount, new, err
|
2021-08-10 13:32:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
accountable, err := d.dereferenceAccountable(ctx, username, remoteAccountID)
|
2021-08-10 13:32:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, new, fmt.Errorf("FullyDereferenceAccount: error dereferencing accountable: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
gtsAccount, err := d.typeConverter.ASRepresentationToAccount(ctx, accountable, refresh)
|
2021-08-10 13:32:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, new, fmt.Errorf("FullyDereferenceAccount: error converting accountable to account: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if new {
|
|
|
|
// generate a new id since we haven't seen this account before, and do a put
|
|
|
|
ulid, err := id.NewRandomULID()
|
|
|
|
if err != nil {
|
|
|
|
return nil, new, fmt.Errorf("FullyDereferenceAccount: error generating new id for account: %s", err)
|
|
|
|
}
|
|
|
|
gtsAccount.ID = ulid
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := d.PopulateAccountFields(ctx, gtsAccount, username, refresh); err != nil {
|
2021-08-10 13:32:39 +02:00
|
|
|
return nil, new, fmt.Errorf("FullyDereferenceAccount: error populating further account fields: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := d.db.Put(ctx, gtsAccount); err != nil {
|
2021-08-10 13:32:39 +02:00
|
|
|
return nil, new, fmt.Errorf("FullyDereferenceAccount: error putting new account: %s", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// take the id we already have and do an update
|
|
|
|
gtsAccount.ID = maybeAccount.ID
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := d.PopulateAccountFields(ctx, gtsAccount, username, refresh); err != nil {
|
2021-08-10 13:32:39 +02:00
|
|
|
return nil, new, fmt.Errorf("FullyDereferenceAccount: error populating further account fields: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
gtsAccount, err = d.db.UpdateAccount(ctx, gtsAccount)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, fmt.Errorf("EnrichRemoteAccount: error updating account: %s", err)
|
2021-08-10 13:32:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return gtsAccount, new, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// dereferenceAccountable calls remoteAccountID with a GET request, and tries to parse whatever
|
|
|
|
// it finds as something that an account model can be constructed out of.
|
|
|
|
//
|
|
|
|
// Will work for Person, Application, or Service models.
|
2021-08-25 15:34:33 +02:00
|
|
|
func (d *deref) dereferenceAccountable(ctx context.Context, username string, remoteAccountID *url.URL) (ap.Accountable, error) {
|
2021-08-10 13:32:39 +02:00
|
|
|
d.startHandshake(username, remoteAccountID)
|
|
|
|
defer d.stopHandshake(username, remoteAccountID)
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
if blocked, err := d.db.IsDomainBlocked(ctx, remoteAccountID.Host); blocked || err != nil {
|
2021-08-10 13:32:39 +02:00
|
|
|
return nil, fmt.Errorf("DereferenceAccountable: domain %s is blocked", remoteAccountID.Host)
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
transport, err := d.transportController.NewTransportForUsername(ctx, username)
|
2021-08-10 13:32:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("DereferenceAccountable: transport err: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-10-04 15:24:19 +02:00
|
|
|
b, err := transport.Dereference(ctx, remoteAccountID)
|
2021-08-10 13:32:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("DereferenceAccountable: error deferencing %s: %s", remoteAccountID.String(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m := make(map[string]interface{})
|
|
|
|
if err := json.Unmarshal(b, &m); err != nil {
|
|
|
|
return nil, fmt.Errorf("DereferenceAccountable: error unmarshalling bytes into json: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-10-04 15:24:19 +02:00
|
|
|
t, err := streams.ToType(ctx, m)
|
2021-08-10 13:32:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("DereferenceAccountable: error resolving json into ap vocab type: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch t.GetTypeName() {
|
2021-09-03 10:30:40 +02:00
|
|
|
case ap.ActorApplication:
|
2021-08-10 13:32:39 +02:00
|
|
|
p, ok := t.(vocab.ActivityStreamsApplication)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("DereferenceAccountable: error resolving type as activitystreams application")
|
|
|
|
}
|
|
|
|
return p, nil
|
2021-09-30 12:27:42 +02:00
|
|
|
case ap.ActorGroup:
|
|
|
|
p, ok := t.(vocab.ActivityStreamsGroup)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("DereferenceAccountable: error resolving type as activitystreams group")
|
|
|
|
}
|
|
|
|
return p, nil
|
|
|
|
case ap.ActorOrganization:
|
|
|
|
p, ok := t.(vocab.ActivityStreamsOrganization)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("DereferenceAccountable: error resolving type as activitystreams organization")
|
|
|
|
}
|
|
|
|
return p, nil
|
|
|
|
case ap.ActorPerson:
|
|
|
|
p, ok := t.(vocab.ActivityStreamsPerson)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("DereferenceAccountable: error resolving type as activitystreams person")
|
|
|
|
}
|
|
|
|
return p, nil
|
2021-09-03 10:30:40 +02:00
|
|
|
case ap.ActorService:
|
2021-08-10 13:32:39 +02:00
|
|
|
p, ok := t.(vocab.ActivityStreamsService)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("DereferenceAccountable: error resolving type as activitystreams service")
|
|
|
|
}
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("DereferenceAccountable: type name %s not supported", t.GetTypeName())
|
|
|
|
}
|
|
|
|
|
2021-08-10 16:56:59 +02:00
|
|
|
// PopulateAccountFields populates any fields on the given account that weren't populated by the initial
|
2021-08-10 13:32:39 +02:00
|
|
|
// dereferencing. This includes things like header and avatar etc.
|
2021-08-25 15:34:33 +02:00
|
|
|
func (d *deref) PopulateAccountFields(ctx context.Context, account *gtsmodel.Account, requestingUsername string, refresh bool) error {
|
2021-10-11 14:37:33 +02:00
|
|
|
l := logrus.WithFields(logrus.Fields{
|
2021-08-10 13:32:39 +02:00
|
|
|
"func": "PopulateAccountFields",
|
|
|
|
"requestingUsername": requestingUsername,
|
|
|
|
})
|
|
|
|
|
|
|
|
accountURI, err := url.Parse(account.URI)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("PopulateAccountFields: couldn't parse account URI %s: %s", account.URI, err)
|
|
|
|
}
|
2021-08-25 15:34:33 +02:00
|
|
|
if blocked, err := d.db.IsDomainBlocked(ctx, accountURI.Host); blocked || err != nil {
|
2021-08-10 13:32:39 +02:00
|
|
|
return fmt.Errorf("PopulateAccountFields: domain %s is blocked", accountURI.Host)
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
t, err := d.transportController.NewTransportForUsername(ctx, requestingUsername)
|
2021-08-10 13:32:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("PopulateAccountFields: error getting transport for user: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetch the header and avatar
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := d.fetchHeaderAndAviForAccount(ctx, account, t, refresh); err != nil {
|
2021-08-10 13:32:39 +02:00
|
|
|
// if this doesn't work, just skip it -- we can do it later
|
|
|
|
l.Debugf("error fetching header/avi for account: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetchHeaderAndAviForAccount fetches the header and avatar for a remote account, using a transport
|
|
|
|
// on behalf of requestingUsername.
|
|
|
|
//
|
|
|
|
// targetAccount's AvatarMediaAttachmentID and HeaderMediaAttachmentID will be updated as necessary.
|
|
|
|
//
|
|
|
|
// SIDE EFFECTS: remote header and avatar will be stored in local storage.
|
2021-08-25 15:34:33 +02:00
|
|
|
func (d *deref) fetchHeaderAndAviForAccount(ctx context.Context, targetAccount *gtsmodel.Account, t transport.Transport, refresh bool) error {
|
2021-08-10 13:32:39 +02:00
|
|
|
accountURI, err := url.Parse(targetAccount.URI)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("fetchHeaderAndAviForAccount: couldn't parse account URI %s: %s", targetAccount.URI, err)
|
|
|
|
}
|
2021-08-25 15:34:33 +02:00
|
|
|
if blocked, err := d.db.IsDomainBlocked(ctx, accountURI.Host); blocked || err != nil {
|
2021-08-10 13:32:39 +02:00
|
|
|
return fmt.Errorf("fetchHeaderAndAviForAccount: domain %s is blocked", accountURI.Host)
|
|
|
|
}
|
|
|
|
|
|
|
|
if targetAccount.AvatarRemoteURL != "" && (targetAccount.AvatarMediaAttachmentID == "" || refresh) {
|
2021-08-25 15:34:33 +02:00
|
|
|
a, err := d.mediaHandler.ProcessRemoteHeaderOrAvatar(ctx, t, >smodel.MediaAttachment{
|
2021-08-10 13:32:39 +02:00
|
|
|
RemoteURL: targetAccount.AvatarRemoteURL,
|
|
|
|
Avatar: true,
|
|
|
|
}, targetAccount.ID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error processing avatar for user: %s", err)
|
|
|
|
}
|
|
|
|
targetAccount.AvatarMediaAttachmentID = a.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
if targetAccount.HeaderRemoteURL != "" && (targetAccount.HeaderMediaAttachmentID == "" || refresh) {
|
2021-08-25 15:34:33 +02:00
|
|
|
a, err := d.mediaHandler.ProcessRemoteHeaderOrAvatar(ctx, t, >smodel.MediaAttachment{
|
2021-08-10 13:32:39 +02:00
|
|
|
RemoteURL: targetAccount.HeaderRemoteURL,
|
|
|
|
Header: true,
|
|
|
|
}, targetAccount.ID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error processing header for user: %s", err)
|
|
|
|
}
|
|
|
|
targetAccount.HeaderMediaAttachmentID = a.ID
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|