2021-05-27 16:06:24 +02:00
|
|
|
package federatingdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"github.com/go-fed/activity/streams"
|
|
|
|
"github.com/go-fed/activity/streams/vocab"
|
|
|
|
"github.com/sirupsen/logrus"
|
2021-09-28 16:19:13 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
2021-05-27 16:06:24 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
2021-05-29 19:36:54 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/util"
|
2021-05-27 16:06:24 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Following obtains the Following Collection for an actor with the
|
|
|
|
// given id.
|
|
|
|
//
|
|
|
|
// If modified, the library will then call Update.
|
|
|
|
//
|
|
|
|
// The library makes this call only after acquiring a lock first.
|
2021-08-25 15:34:33 +02:00
|
|
|
func (f *federatingDB) Following(ctx context.Context, actorIRI *url.URL) (following vocab.ActivityStreamsCollection, err error) {
|
2021-05-27 16:06:24 +02:00
|
|
|
l := f.log.WithFields(
|
|
|
|
logrus.Fields{
|
|
|
|
"func": "Following",
|
|
|
|
"actorIRI": actorIRI.String(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
l.Debugf("entering FOLLOWING function with actorIRI %s", actorIRI.String())
|
|
|
|
|
2021-08-20 12:26:56 +02:00
|
|
|
var acct *gtsmodel.Account
|
2021-05-29 19:36:54 +02:00
|
|
|
if util.IsUserPath(actorIRI) {
|
2021-08-20 12:26:56 +02:00
|
|
|
username, err := util.ParseUserPath(actorIRI)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("FOLLOWING: error parsing user path: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
a, err := f.db.GetLocalAccountByUsername(ctx, username)
|
2021-08-20 12:26:56 +02:00
|
|
|
if err != nil {
|
2021-07-27 10:45:22 +02:00
|
|
|
return nil, fmt.Errorf("FOLLOWING: db error getting account with uri %s: %s", actorIRI.String(), err)
|
2021-05-29 19:36:54 +02:00
|
|
|
}
|
2021-08-20 12:26:56 +02:00
|
|
|
|
|
|
|
acct = a
|
2021-05-29 19:36:54 +02:00
|
|
|
} else if util.IsFollowingPath(actorIRI) {
|
2021-08-20 12:26:56 +02:00
|
|
|
username, err := util.ParseFollowingPath(actorIRI)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("FOLLOWING: error parsing following path: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
a, err := f.db.GetLocalAccountByUsername(ctx, username)
|
2021-08-20 12:26:56 +02:00
|
|
|
if err != nil {
|
2021-07-27 10:45:22 +02:00
|
|
|
return nil, fmt.Errorf("FOLLOWING: db error getting account with following uri %s: %s", actorIRI.String(), err)
|
2021-05-29 19:36:54 +02:00
|
|
|
}
|
2021-08-20 12:26:56 +02:00
|
|
|
|
|
|
|
acct = a
|
2021-05-29 19:36:54 +02:00
|
|
|
} else {
|
2021-07-27 10:45:22 +02:00
|
|
|
return nil, fmt.Errorf("FOLLOWING: could not parse actor IRI %s as users or following path", actorIRI.String())
|
2021-05-27 16:06:24 +02:00
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
acctFollowing, err := f.db.GetAccountFollows(ctx, acct.ID)
|
2021-08-20 12:26:56 +02:00
|
|
|
if err != nil {
|
2021-07-27 10:45:22 +02:00
|
|
|
return nil, fmt.Errorf("FOLLOWING: db error getting following for account id %s: %s", acct.ID, err)
|
2021-05-27 16:06:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
following = streams.NewActivityStreamsCollection()
|
|
|
|
items := streams.NewActivityStreamsItemsProperty()
|
|
|
|
for _, follow := range acctFollowing {
|
2021-08-25 15:34:33 +02:00
|
|
|
if follow.Account == nil {
|
|
|
|
followAccount, err := f.db.GetAccountByID(ctx, follow.AccountID)
|
|
|
|
if err != nil {
|
2021-09-28 16:19:13 +02:00
|
|
|
errWrapped := fmt.Errorf("FOLLOWING: db error getting account id %s: %s", follow.AccountID, 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)
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
// proper error
|
|
|
|
return nil, errWrapped
|
|
|
|
}
|
2021-08-25 15:34:33 +02:00
|
|
|
}
|
|
|
|
follow.Account = followAccount
|
2021-05-27 16:06:24 +02:00
|
|
|
}
|
2021-08-25 15:34:33 +02:00
|
|
|
|
|
|
|
uri, err := url.Parse(follow.Account.URI)
|
2021-05-27 16:06:24 +02:00
|
|
|
if err != nil {
|
2021-08-25 15:34:33 +02:00
|
|
|
return nil, fmt.Errorf("FOLLOWING: error parsing %s as url: %s", follow.Account.URI, err)
|
2021-05-27 16:06:24 +02:00
|
|
|
}
|
|
|
|
items.AppendIRI(uri)
|
|
|
|
}
|
|
|
|
following.SetActivityStreamsItems(items)
|
|
|
|
return
|
|
|
|
}
|