diff options
author | 2021-10-06 18:18:02 +0200 | |
---|---|---|
committer | 2021-10-06 18:18:02 +0200 | |
commit | 3dc7644ae6bdbfbae2e126896937e322247b5b33 (patch) | |
tree | 45487a241b439ce590863f68ea479d8dfd92ba28 /internal/typeutils | |
parent | fix logs not working properly (#264) (diff) | |
download | gotosocial-3dc7644ae6bdbfbae2e126896937e322247b5b33.tar.xz |
Derive visibility fixes (#271)
* use pub public const
* don't error on no summary
* move extract visibility to separate function
* extract visibility test
* add addressable interface
Diffstat (limited to 'internal/typeutils')
-rw-r--r-- | internal/typeutils/astointernal.go | 94 | ||||
-rw-r--r-- | internal/typeutils/converter.go | 4 | ||||
-rw-r--r-- | internal/typeutils/internaltoas.go | 9 | ||||
-rw-r--r-- | internal/typeutils/wrap.go | 5 |
4 files changed, 14 insertions, 98 deletions
diff --git a/internal/typeutils/astointernal.go b/internal/typeutils/astointernal.go index 9b87e03d3..88dae938b 100644 --- a/internal/typeutils/astointernal.go +++ b/internal/typeutils/astointernal.go @@ -22,8 +22,6 @@ import ( "context" "errors" "fmt" - "net/url" - "strings" "github.com/superseriousbusiness/gotosocial/internal/ap" "github.com/superseriousbusiness/gotosocial/internal/db" @@ -245,13 +243,13 @@ func (c *converter) ASStatusToStatus(ctx context.Context, statusable ap.Statusab // if we don't know the account yet we can dereference it later attributedTo, err := ap.ExtractAttributedTo(statusable) if err != nil { - return nil, errors.New("attributedTo was empty") + return nil, errors.New("ASStatusToStatus: attributedTo was empty") } status.AccountURI = attributedTo.String() statusOwner, err := c.db.GetAccountByURI(ctx, attributedTo.String()) if err != nil { - return nil, fmt.Errorf("couldn't get status owner from db: %s", err) + return nil, fmt.Errorf("ASStatusToStatus: couldn't get status owner from db: %s", err) } status.AccountID = statusOwner.ID status.AccountURI = statusOwner.URI @@ -280,46 +278,9 @@ func (c *converter) ASStatusToStatus(ctx context.Context, statusable ap.Statusab } // visibility entry for this status - var visibility gtsmodel.Visibility - - to, err := ap.ExtractTos(statusable) + visibility, err := ap.ExtractVisibility(statusable, status.Account.FollowersURI) if err != nil { - return nil, fmt.Errorf("error extracting TO values: %s", err) - } - - cc, err := ap.ExtractCCs(statusable) - if err != nil { - return nil, fmt.Errorf("error extracting CC values: %s", err) - } - - if len(to) == 0 && len(cc) == 0 { - return nil, errors.New("message wasn't TO or CC anyone") - } - - // for visibility derivation, we start by assuming most restrictive, and work our way to least restrictive - - // if it's a DM then it's addressed to SPECIFIC ACCOUNTS and not followers or public - if len(to) != 0 && len(cc) == 0 { - visibility = gtsmodel.VisibilityDirect - } - - // if it's just got followers in TO and it's not also CC'ed to public, it's followers only - if isFollowers(to, statusOwner.FollowersURI) { - visibility = gtsmodel.VisibilityFollowersOnly - } - - // if it's CC'ed to public, it's public or unlocked - // mentioned SPECIFIC ACCOUNTS also get added to CC'es if it's not a direct message - if isPublic(cc) { - visibility = gtsmodel.VisibilityUnlocked - } - if isPublic(to) { - visibility = gtsmodel.VisibilityPublic - } - - // we should have a visibility by now - if visibility == "" { - return nil, errors.New("couldn't derive visibility") + return nil, fmt.Errorf("ASStatusToStatus: error extracting visibility: %s", err) } status.Visibility = visibility @@ -553,55 +514,12 @@ func (c *converter) ASAnnounceToStatus(ctx context.Context, announceable ap.Anno status.MentionIDs = []string{} status.EmojiIDs = []string{} - // parse the visibility from the To and CC entries - var visibility gtsmodel.Visibility - - to, err := ap.ExtractTos(announceable) + visibility, err := ap.ExtractVisibility(announceable, boostingAccount.FollowersURI) if err != nil { - return nil, isNew, fmt.Errorf("error extracting TO values: %s", err) - } - - cc, err := ap.ExtractCCs(announceable) - if err != nil { - return nil, isNew, fmt.Errorf("error extracting CC values: %s", err) - } - - if len(to) == 0 && len(cc) == 0 { - return nil, isNew, errors.New("message wasn't TO or CC anyone") - } - - // if it's CC'ed to public, it's public or unlocked - if isPublic(cc) { - visibility = gtsmodel.VisibilityUnlocked - } - if isPublic(to) { - visibility = gtsmodel.VisibilityPublic - } - - // we should have a visibility by now - if visibility == "" { - return nil, isNew, errors.New("couldn't derive visibility") + return nil, isNew, fmt.Errorf("ASAnnounceToStatus: error extracting visibility: %s", err) } status.Visibility = visibility // the rest of the fields will be taken from the target status, but it's not our job to do the dereferencing here return status, isNew, nil } - -func isPublic(tos []*url.URL) bool { - for _, entry := range tos { - if strings.EqualFold(entry.String(), "https://www.w3.org/ns/activitystreams#Public") { - return true - } - } - return false -} - -func isFollowers(ccs []*url.URL, followersURI string) bool { - for _, entry := range ccs { - if strings.EqualFold(entry.String(), followersURI) { - return true - } - } - return false -} diff --git a/internal/typeutils/converter.go b/internal/typeutils/converter.go index 4515fcbf0..d702c1ae7 100644 --- a/internal/typeutils/converter.go +++ b/internal/typeutils/converter.go @@ -32,10 +32,6 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" ) -const ( - asPublicURI = "https://www.w3.org/ns/activitystreams#Public" -) - // TypeConverter is an interface for the common action of converting between apimodule (frontend, serializable) models, // internal gts models used in the database, and activitypub models used in federation. // diff --git a/internal/typeutils/internaltoas.go b/internal/typeutils/internaltoas.go index 1689db496..73d274908 100644 --- a/internal/typeutils/internaltoas.go +++ b/internal/typeutils/internaltoas.go @@ -25,6 +25,7 @@ import ( "fmt" "net/url" + "github.com/go-fed/activity/pub" "github.com/go-fed/activity/streams" "github.com/go-fed/activity/streams/vocab" "github.com/superseriousbusiness/gotosocial/internal/gtserror" @@ -444,9 +445,9 @@ func (c *converter) StatusToAS(ctx context.Context, s *gtsmodel.Status) (vocab.A return nil, fmt.Errorf("StatusToAS: error parsing url %s: %s", s.Account.FollowersURI, err) } - publicURI, err := url.Parse(asPublicURI) + publicURI, err := url.Parse(pub.PublicActivityPubIRI) if err != nil { - return nil, fmt.Errorf("StatusToAS: error parsing url %s: %s", asPublicURI, err) + return nil, fmt.Errorf("StatusToAS: error parsing url %s: %s", pub.PublicActivityPubIRI, err) } // to and cc @@ -795,9 +796,9 @@ func (c *converter) BoostToAS(ctx context.Context, boostWrapperStatus *gtsmodel. return nil, fmt.Errorf("BoostToAS: error parsing uri %s: %s", boostedAccount.URI, err) } - publicURI, err := url.Parse(asPublicURI) + publicURI, err := url.Parse(pub.PublicActivityPubIRI) if err != nil { - return nil, fmt.Errorf("BoostToAS: error parsing uri %s: %s", asPublicURI, err) + return nil, fmt.Errorf("BoostToAS: error parsing uri %s: %s", pub.PublicActivityPubIRI, err) } ccProp := streams.NewActivityStreamsCcProperty() diff --git a/internal/typeutils/wrap.go b/internal/typeutils/wrap.go index e06da2568..d6aa11cb4 100644 --- a/internal/typeutils/wrap.go +++ b/internal/typeutils/wrap.go @@ -4,6 +4,7 @@ import ( "fmt" "net/url" + "github.com/go-fed/activity/pub" "github.com/go-fed/activity/streams" "github.com/go-fed/activity/streams/vocab" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" @@ -46,9 +47,9 @@ func (c *converter) WrapPersonInUpdate(person vocab.ActivityStreamsPerson, origi update.SetActivityStreamsObject(objectProp) // to should be public - toURI, err := url.Parse(asPublicURI) + toURI, err := url.Parse(pub.PublicActivityPubIRI) if err != nil { - return nil, fmt.Errorf("WrapPersonInUpdate: error parsing url %s: %s", asPublicURI, err) + return nil, fmt.Errorf("WrapPersonInUpdate: error parsing url %s: %s", pub.PublicActivityPubIRI, err) } toProp := streams.NewActivityStreamsToProperty() toProp.AppendIRI(toURI) |