Refactor/tidy (#261)

* tidy up streaming

* cut down code duplication

* test get followers/following

* test streaming processor

* fix some test models

* add TimeMustParse

* fix uri / url typo

* make trace logging less verbose

* make logging more consistent

* disable quote on logging

* remove context.Background

* remove many extraneous mastodon references

* regenerate swagger

* don't log query on no rows result

* log latency first for easier reading
This commit is contained in:
tobi 2021-10-04 15:24:19 +02:00 committed by GitHub
commit e04b187702
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
126 changed files with 1192 additions and 955 deletions

View file

@ -5,12 +5,9 @@ import (
"fmt"
"net/url"
"github.com/go-fed/activity/streams"
"github.com/go-fed/activity/streams/vocab"
"github.com/sirupsen/logrus"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/util"
)
// Following obtains the Following Collection for an actor with the
@ -22,53 +19,28 @@ import (
func (f *federatingDB) Following(ctx context.Context, actorIRI *url.URL) (following vocab.ActivityStreamsCollection, err error) {
l := f.log.WithFields(
logrus.Fields{
"func": "Following",
"actorIRI": actorIRI.String(),
"func": "Following",
"id": actorIRI,
},
)
l.Debugf("entering FOLLOWING function with actorIRI %s", actorIRI.String())
l.Debug("entering Following")
var acct *gtsmodel.Account
if util.IsUserPath(actorIRI) {
username, err := util.ParseUserPath(actorIRI)
if err != nil {
return nil, fmt.Errorf("FOLLOWING: error parsing user path: %s", err)
}
a, err := f.db.GetLocalAccountByUsername(ctx, username)
if err != nil {
return nil, fmt.Errorf("FOLLOWING: db error getting account with uri %s: %s", actorIRI.String(), err)
}
acct = a
} else if util.IsFollowingPath(actorIRI) {
username, err := util.ParseFollowingPath(actorIRI)
if err != nil {
return nil, fmt.Errorf("FOLLOWING: error parsing following path: %s", err)
}
a, err := f.db.GetLocalAccountByUsername(ctx, username)
if err != nil {
return nil, fmt.Errorf("FOLLOWING: db error getting account with following uri %s: %s", actorIRI.String(), err)
}
acct = a
} else {
return nil, fmt.Errorf("FOLLOWING: could not parse actor IRI %s as users or following path", actorIRI.String())
acct, err := f.getAccountForIRI(ctx, actorIRI)
if err != nil {
return nil, err
}
acctFollowing, err := f.db.GetAccountFollows(ctx, acct.ID)
if err != nil {
return nil, fmt.Errorf("FOLLOWING: db error getting following for account id %s: %s", acct.ID, err)
return nil, fmt.Errorf("Following: db error getting following for account id %s: %s", acct.ID, err)
}
following = streams.NewActivityStreamsCollection()
items := streams.NewActivityStreamsItemsProperty()
iris := []*url.URL{}
for _, follow := range acctFollowing {
if follow.Account == nil {
followAccount, err := f.db.GetAccountByID(ctx, follow.AccountID)
if follow.TargetAccount == nil {
a, err := f.db.GetAccountByID(ctx, follow.TargetAccountID)
if err != nil {
errWrapped := fmt.Errorf("FOLLOWING: db error getting account id %s: %s", follow.AccountID, err)
errWrapped := fmt.Errorf("Following: db error getting account id %s: %s", follow.TargetAccountID, err)
if err == db.ErrNoEntries {
// no entry for this account id so it's probably been deleted and we haven't caught up yet
l.Error(errWrapped)
@ -78,15 +50,14 @@ func (f *federatingDB) Following(ctx context.Context, actorIRI *url.URL) (follow
return nil, errWrapped
}
}
follow.Account = followAccount
follow.TargetAccount = a
}
uri, err := url.Parse(follow.Account.URI)
u, err := url.Parse(follow.TargetAccount.URI)
if err != nil {
return nil, fmt.Errorf("FOLLOWING: error parsing %s as url: %s", follow.Account.URI, err)
return nil, err
}
items.AppendIRI(uri)
iris = append(iris, u)
}
following.SetActivityStreamsItems(items)
return
return f.collectIRIs(ctx, iris)
}