2021-05-27 14:06:24 +00: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"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
2021-05-29 17:36:54 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/util"
|
2021-05-27 14:06:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Followers obtains the Followers 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 13:34:33 +00:00
|
|
|
func (f *federatingDB) Followers(ctx context.Context, actorIRI *url.URL) (followers vocab.ActivityStreamsCollection, err error) {
|
2021-05-27 14:06:24 +00:00
|
|
|
l := f.log.WithFields(
|
|
|
|
logrus.Fields{
|
|
|
|
"func": "Followers",
|
|
|
|
"actorIRI": actorIRI.String(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
l.Debugf("entering FOLLOWERS function with actorIRI %s", actorIRI.String())
|
|
|
|
|
|
|
|
acct := >smodel.Account{}
|
2021-05-29 17:36:54 +00:00
|
|
|
|
|
|
|
if util.IsUserPath(actorIRI) {
|
2021-08-25 13:34:33 +00:00
|
|
|
acct, err = f.db.GetAccountByURI(ctx, actorIRI.String())
|
2021-08-20 10:26:56 +00:00
|
|
|
if err != nil {
|
2021-07-27 08:45:22 +00:00
|
|
|
return nil, fmt.Errorf("FOLLOWERS: db error getting account with uri %s: %s", actorIRI.String(), err)
|
2021-05-29 17:36:54 +00:00
|
|
|
}
|
|
|
|
} else if util.IsFollowersPath(actorIRI) {
|
2021-08-25 13:34:33 +00:00
|
|
|
if err := f.db.GetWhere(ctx, []db.Where{{Key: "followers_uri", Value: actorIRI.String()}}, acct); err != nil {
|
2021-07-27 08:45:22 +00:00
|
|
|
return nil, fmt.Errorf("FOLLOWERS: db error getting account with followers uri %s: %s", actorIRI.String(), err)
|
2021-05-29 17:36:54 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-07-27 08:45:22 +00:00
|
|
|
return nil, fmt.Errorf("FOLLOWERS: could not parse actor IRI %s as users or followers path", actorIRI.String())
|
2021-05-27 14:06:24 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
acctFollowers, err := f.db.GetAccountFollowedBy(ctx, acct.ID, false)
|
2021-08-20 10:26:56 +00:00
|
|
|
if err != nil {
|
2021-07-27 08:45:22 +00:00
|
|
|
return nil, fmt.Errorf("FOLLOWERS: db error getting followers for account id %s: %s", acct.ID, err)
|
2021-05-27 14:06:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
followers = streams.NewActivityStreamsCollection()
|
|
|
|
items := streams.NewActivityStreamsItemsProperty()
|
|
|
|
for _, follow := range acctFollowers {
|
2021-08-25 13:34:33 +00:00
|
|
|
if follow.Account == nil {
|
|
|
|
followAccount, err := f.db.GetAccountByID(ctx, follow.AccountID)
|
|
|
|
if err != nil {
|
2021-09-28 14:19:13 +00:00
|
|
|
errWrapped := fmt.Errorf("FOLLOWERS: 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 13:34:33 +00:00
|
|
|
}
|
|
|
|
follow.Account = followAccount
|
2021-05-27 14:06:24 +00:00
|
|
|
}
|
2021-08-25 13:34:33 +00:00
|
|
|
|
|
|
|
uri, err := url.Parse(follow.Account.URI)
|
2021-05-27 14:06:24 +00:00
|
|
|
if err != nil {
|
2021-08-25 13:34:33 +00:00
|
|
|
return nil, fmt.Errorf("FOLLOWERS: error parsing %s as url: %s", follow.Account.URI, err)
|
2021-05-27 14:06:24 +00:00
|
|
|
}
|
|
|
|
items.AppendIRI(uri)
|
|
|
|
}
|
|
|
|
followers.SetActivityStreamsItems(items)
|
|
|
|
return
|
|
|
|
}
|