2021-05-27 17:06:24 +03:00
|
|
|
package federatingdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
|
2022-07-19 11:47:55 +03:00
|
|
|
"codeberg.org/gruf/go-kv"
|
2021-11-13 19:29:43 +03:00
|
|
|
"github.com/superseriousbusiness/activity/streams/vocab"
|
2021-05-27 17:06:24 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
2022-07-19 11:47:55 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/log"
|
2021-05-27 17:06:24 +03: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 16:34:33 +03:00
|
|
|
func (f *federatingDB) Followers(ctx context.Context, actorIRI *url.URL) (followers vocab.ActivityStreamsCollection, err error) {
|
2022-07-19 11:47:55 +03:00
|
|
|
l := log.WithFields(kv.Fields{
|
|
|
|
{"id", actorIRI},
|
|
|
|
}...)
|
2021-10-04 16:24:19 +03:00
|
|
|
l.Debug("entering Followers")
|
2021-05-27 17:06:24 +03:00
|
|
|
|
2021-10-04 16:24:19 +03:00
|
|
|
acct, err := f.getAccountForIRI(ctx, actorIRI)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-05-27 17:06:24 +03:00
|
|
|
}
|
|
|
|
|
2021-08-25 16:34:33 +03:00
|
|
|
acctFollowers, err := f.db.GetAccountFollowedBy(ctx, acct.ID, false)
|
2021-08-20 13:26:56 +03:00
|
|
|
if err != nil {
|
2021-10-04 16:24:19 +03:00
|
|
|
return nil, fmt.Errorf("Followers: db error getting followers for account id %s: %s", acct.ID, err)
|
2021-05-27 17:06:24 +03:00
|
|
|
}
|
|
|
|
|
2021-10-04 16:24:19 +03:00
|
|
|
iris := []*url.URL{}
|
2021-05-27 17:06:24 +03:00
|
|
|
for _, follow := range acctFollowers {
|
2021-08-25 16:34:33 +03:00
|
|
|
if follow.Account == nil {
|
2021-10-04 16:24:19 +03:00
|
|
|
a, err := f.db.GetAccountByID(ctx, follow.AccountID)
|
2021-08-25 16:34:33 +03:00
|
|
|
if err != nil {
|
2021-10-04 16:24:19 +03:00
|
|
|
errWrapped := fmt.Errorf("Followers: db error getting account id %s: %s", follow.AccountID, err)
|
2021-09-28 17:19:13 +03:00
|
|
|
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 16:34:33 +03:00
|
|
|
}
|
2021-10-04 16:24:19 +03:00
|
|
|
follow.Account = a
|
2021-05-27 17:06:24 +03:00
|
|
|
}
|
2021-10-04 16:24:19 +03:00
|
|
|
u, err := url.Parse(follow.Account.URI)
|
2021-05-27 17:06:24 +03:00
|
|
|
if err != nil {
|
2021-10-04 16:24:19 +03:00
|
|
|
return nil, err
|
2021-05-27 17:06:24 +03:00
|
|
|
}
|
2021-10-04 16:24:19 +03:00
|
|
|
iris = append(iris, u)
|
2021-05-27 17:06:24 +03:00
|
|
|
}
|
2021-10-04 16:24:19 +03:00
|
|
|
|
|
|
|
return f.collectIRIs(ctx, iris)
|
2021-05-27 17:06:24 +03:00
|
|
|
}
|