summaryrefslogtreecommitdiff
path: root/internal/ap
diff options
context:
space:
mode:
authorLibravatar tobi <31960611+tsmethurst@users.noreply.github.com>2021-10-06 18:18:02 +0200
committerLibravatar GitHub <noreply@github.com>2021-10-06 18:18:02 +0200
commit3dc7644ae6bdbfbae2e126896937e322247b5b33 (patch)
tree45487a241b439ce590863f68ea479d8dfd92ba28 /internal/ap
parentfix logs not working properly (#264) (diff)
downloadgotosocial-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/ap')
-rw-r--r--internal/ap/extract.go66
-rw-r--r--internal/ap/extract_test.go101
-rw-r--r--internal/ap/extractvisibility_test.go71
-rw-r--r--internal/ap/interfaces.go6
4 files changed, 243 insertions, 1 deletions
diff --git a/internal/ap/extract.go b/internal/ap/extract.go
index 9ad148bcb..8a1d99ce9 100644
--- a/internal/ap/extract.go
+++ b/internal/ap/extract.go
@@ -253,7 +253,7 @@ func ExtractSummary(i WithSummary) (string, error) {
}
}
- return "", errors.New("could not extract summary")
+ return "", nil
}
// ExtractDiscoverable extracts the Discoverable boolean of an interface.
@@ -610,3 +610,67 @@ func ExtractObject(i WithObject) (*url.URL, error) {
}
return nil, errors.New("no iri found for object prop")
}
+
+// ExtractVisibility extracts the gtsmodel.Visibility of a given addressable with a To and CC property.
+//
+// ActorFollowersURI is needed to check whether the visibility is FollowersOnly or not. The passed-in value
+// should just be the string value representation of the followers URI of the actor who created the activity,
+// eg https://example.org/users/whoever/followers.
+func ExtractVisibility(addressable Addressable, actorFollowersURI string) (gtsmodel.Visibility, error) {
+ to, err := ExtractTos(addressable)
+ if err != nil {
+ return "", fmt.Errorf("deriveVisibility: error extracting TO values: %s", err)
+ }
+
+ cc, err := ExtractCCs(addressable)
+ if err != nil {
+ return "", fmt.Errorf("deriveVisibility: error extracting CC values: %s", err)
+ }
+
+ if len(to) == 0 && len(cc) == 0 {
+ return "", errors.New("deriveVisibility: message wasn't TO or CC anyone")
+ }
+
+ // for visibility derivation, we start by assuming most restrictive, and work our way to least restrictive
+ visibility := gtsmodel.VisibilityDirect
+
+ // if it's got followers in TO and it's not also CC'ed to public, it's followers only
+ if isFollowers(to, actorFollowersURI) {
+ visibility = gtsmodel.VisibilityFollowersOnly
+ }
+
+ // if it's CC'ed to public, it's unlocked
+ // mentioned SPECIFIC ACCOUNTS also get added to CC'es if it's not a direct message
+ if isPublic(cc) {
+ visibility = gtsmodel.VisibilityUnlocked
+ }
+
+ // if it's To public, it's just straight up public
+ if isPublic(to) {
+ visibility = gtsmodel.VisibilityPublic
+ }
+
+ return visibility, nil
+}
+
+// isPublic checks if at least one entry in the given uris slice equals
+// the activitystreams public uri.
+func isPublic(uris []*url.URL) bool {
+ for _, entry := range uris {
+ if strings.EqualFold(entry.String(), pub.PublicActivityPubIRI) {
+ return true
+ }
+ }
+ return false
+}
+
+// isFollowers checks if at least one entry in the given uris slice equals
+// the given followersURI.
+func isFollowers(uris []*url.URL, followersURI string) bool {
+ for _, entry := range uris {
+ if strings.EqualFold(entry.String(), followersURI) {
+ return true
+ }
+ }
+ return false
+}
diff --git a/internal/ap/extract_test.go b/internal/ap/extract_test.go
index 8753e8c24..1b5c1f11f 100644
--- a/internal/ap/extract_test.go
+++ b/internal/ap/extract_test.go
@@ -19,9 +19,14 @@
package ap_test
import (
+ "context"
+ "encoding/json"
+
+ "github.com/go-fed/activity/pub"
"github.com/go-fed/activity/streams"
"github.com/go-fed/activity/streams/vocab"
"github.com/stretchr/testify/suite"
+ "github.com/superseriousbusiness/gotosocial/internal/ap"
"github.com/superseriousbusiness/gotosocial/testrig"
)
@@ -86,15 +91,111 @@ func noteWithMentions1() vocab.ActivityStreamsNote {
return note
}
+func addressable1() ap.Addressable {
+ // make a note addressed to public with followers in cc
+ note := streams.NewActivityStreamsNote()
+
+ toProp := streams.NewActivityStreamsToProperty()
+ toProp.AppendIRI(testrig.URLMustParse(pub.PublicActivityPubIRI))
+
+ note.SetActivityStreamsTo(toProp)
+
+ ccProp := streams.NewActivityStreamsCcProperty()
+ ccProp.AppendIRI(testrig.URLMustParse("http://localhost:8080/users/the_mighty_zork/followers"))
+
+ note.SetActivityStreamsCc(ccProp)
+
+ return note
+}
+
+func addressable2() ap.Addressable {
+ // make a note addressed to followers with public in cc
+ note := streams.NewActivityStreamsNote()
+
+ toProp := streams.NewActivityStreamsToProperty()
+ toProp.AppendIRI(testrig.URLMustParse("http://localhost:8080/users/the_mighty_zork/followers"))
+
+ note.SetActivityStreamsTo(toProp)
+
+ ccProp := streams.NewActivityStreamsCcProperty()
+ ccProp.AppendIRI(testrig.URLMustParse(pub.PublicActivityPubIRI))
+
+ note.SetActivityStreamsCc(ccProp)
+
+ return note
+}
+
+func addressable3() ap.Addressable {
+ // make a note addressed to followers
+ note := streams.NewActivityStreamsNote()
+
+ toProp := streams.NewActivityStreamsToProperty()
+ toProp.AppendIRI(testrig.URLMustParse("http://localhost:8080/users/the_mighty_zork/followers"))
+
+ note.SetActivityStreamsTo(toProp)
+
+ return note
+}
+
+func addressable4() vocab.ActivityStreamsAnnounce {
+ // https://github.com/superseriousbusiness/gotosocial/issues/267
+ announceJson := []byte(`
+{
+ "@context": "https://www.w3.org/ns/activitystreams",
+ "actor": "https://example.org/users/someone",
+ "cc": "https://another.instance/users/someone_else",
+ "id": "https://example.org/users/someone/statuses/107043888547829808/activity",
+ "object": "https://another.instance/users/someone_else/statuses/107026674805188668",
+ "published": "2021-10-04T15:08:35Z",
+ "to": "https://example.org/users/someone/followers",
+ "type": "Announce"
+}`)
+
+ var jsonAsMap map[string]interface{}
+ err := json.Unmarshal(announceJson, &jsonAsMap)
+ if err != nil {
+ panic(err)
+ }
+
+ t, err := streams.ToType(context.Background(), jsonAsMap)
+ if err != nil {
+ panic(err)
+ }
+
+ return t.(vocab.ActivityStreamsAnnounce)
+}
+
+func addressable5() ap.Addressable {
+ // make a note addressed to one person (direct message)
+ note := streams.NewActivityStreamsNote()
+
+ toProp := streams.NewActivityStreamsToProperty()
+ toProp.AppendIRI(testrig.URLMustParse("http://localhost:8080/users/1_happy_turtle"))
+
+ note.SetActivityStreamsTo(toProp)
+
+ return note
+}
+
type ExtractTestSuite struct {
suite.Suite
document1 vocab.ActivityStreamsDocument
attachment1 vocab.ActivityStreamsAttachmentProperty
noteWithMentions1 vocab.ActivityStreamsNote
+ addressable1 ap.Addressable
+ addressable2 ap.Addressable
+ addressable3 ap.Addressable
+ addressable4 vocab.ActivityStreamsAnnounce
+ addressable5 ap.Addressable
}
func (suite *ExtractTestSuite) SetupTest() {
suite.document1 = document1()
suite.attachment1 = attachment1()
suite.noteWithMentions1 = noteWithMentions1()
+ suite.addressable1 = addressable1()
+ suite.addressable2 = addressable2()
+ suite.addressable3 = addressable3()
+ suite.addressable4 = addressable4()
+ suite.addressable5 = addressable5()
}
diff --git a/internal/ap/extractvisibility_test.go b/internal/ap/extractvisibility_test.go
new file mode 100644
index 000000000..96e0d4c70
--- /dev/null
+++ b/internal/ap/extractvisibility_test.go
@@ -0,0 +1,71 @@
+/*
+ GoToSocial
+ Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+package ap_test
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/suite"
+ "github.com/superseriousbusiness/gotosocial/internal/ap"
+ "github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
+)
+
+type ExtractVisibilityTestSuite struct {
+ ExtractTestSuite
+}
+
+func (suite *ExtractVisibilityTestSuite) TestExtractVisibilityPublic() {
+ a := suite.addressable1
+ visibility, err := ap.ExtractVisibility(a, "http://localhost:8080/users/the_mighty_zork/followers")
+ suite.NoError(err)
+ suite.Equal(visibility, gtsmodel.VisibilityPublic)
+}
+
+func (suite *ExtractVisibilityTestSuite) TestExtractVisibilityUnlocked() {
+ a := suite.addressable2
+ visibility, err := ap.ExtractVisibility(a, "http://localhost:8080/users/the_mighty_zork/followers")
+ suite.NoError(err)
+ suite.Equal(visibility, gtsmodel.VisibilityUnlocked)
+}
+
+func (suite *ExtractVisibilityTestSuite) TestExtractVisibilityFollowersOnly() {
+ a := suite.addressable3
+ visibility, err := ap.ExtractVisibility(a, "http://localhost:8080/users/the_mighty_zork/followers")
+ suite.NoError(err)
+ suite.Equal(visibility, gtsmodel.VisibilityFollowersOnly)
+}
+
+func (suite *ExtractVisibilityTestSuite) TestExtractVisibilityFollowersOnlyAnnounce() {
+ // https://github.com/superseriousbusiness/gotosocial/issues/267
+ a := suite.addressable4
+ visibility, err := ap.ExtractVisibility(a, "https://example.org/users/someone/followers")
+ suite.NoError(err)
+ suite.Equal(visibility, gtsmodel.VisibilityFollowersOnly)
+}
+
+func (suite *ExtractVisibilityTestSuite) TestExtractVisibilityDirect() {
+ a := suite.addressable5
+ visibility, err := ap.ExtractVisibility(a, "http://localhost:8080/users/the_mighty_zork/followers")
+ suite.NoError(err)
+ suite.Equal(visibility, gtsmodel.VisibilityDirect)
+}
+
+func TestExtractVisibilityTestSuite(t *testing.T) {
+ suite.Run(t, &ExtractVisibilityTestSuite{})
+}
diff --git a/internal/ap/interfaces.go b/internal/ap/interfaces.go
index a20f39bd2..da7e001db 100644
--- a/internal/ap/interfaces.go
+++ b/internal/ap/interfaces.go
@@ -133,6 +133,12 @@ type Announceable interface {
WithCC
}
+// Addressable represents the minimum interface for an addressed activity.
+type Addressable interface {
+ WithTo
+ WithCC
+}
+
// CollectionPageable represents the minimum interface for an activitystreams 'CollectionPage' object.
type CollectionPageable interface {
WithJSONLDId