From 621e59fd424ebf1caaab2e14abb147d3b5544371 Mon Sep 17 00:00:00 2001 From: tsmethurst Date: Wed, 28 Apr 2021 20:43:26 +0200 Subject: [PATCH] good! --- internal/typeutils/internaltoas.go | 62 ++++++++++++++++++++--- internal/typeutils/internaltoas_test.go | 65 +++++++++++++++++++++++++ 2 files changed, 121 insertions(+), 6 deletions(-) diff --git a/internal/typeutils/internaltoas.go b/internal/typeutils/internaltoas.go index 7db337c..bb1531e 100644 --- a/internal/typeutils/internaltoas.go +++ b/internal/typeutils/internaltoas.go @@ -22,7 +22,6 @@ import ( "crypto/x509" "encoding/pem" "net/url" - "strings" "github.com/go-fed/activity/streams" "github.com/go-fed/activity/streams/vocab" @@ -129,8 +128,9 @@ func (c *converter) AccountToAS(a *gtsmodel.Account) (vocab.ActivityStreamsPerso } urlProp := streams.NewActivityStreamsUrlProperty() urlProp.AppendIRI(profileURL) + person.SetActivityStreamsUrl(urlProp) - // manuallyApproveFollowers + // manuallyApprovesFollowers // Will be shown as a locked account. // TODO: NOT IMPLEMENTED **YET** -- this needs to be added as an activitypub extension to https://github.com/go-fed/activity, see https://github.com/go-fed/activity/tree/master/astool @@ -141,7 +141,7 @@ func (c *converter) AccountToAS(a *gtsmodel.Account) (vocab.ActivityStreamsPerso person.SetTootDiscoverable(discoverableProp) // devices - // NOT IMPLEMENTED + // NOT IMPLEMENTED, probably won't implement // alsoKnownAs // Required for Move activity. @@ -177,9 +177,9 @@ func (c *converter) AccountToAS(a *gtsmodel.Account) (vocab.ActivityStreamsPerso Type: "PUBLIC KEY", Bytes: encodedPublicKey, }) - publicKeyString := strings.ReplaceAll(string(publicKeyBytes), "\n", "\\n") // replace all the newlines with backslash n publicKeyPEMProp := streams.NewW3IDSecurityV1PublicKeyPemProperty() - publicKeyPEMProp.Set(publicKeyString) + publicKeyPEMProp.Set(string(publicKeyBytes)) + publicKey.SetW3IDSecurityV1PublicKeyPem(publicKeyPEMProp) // append the public key to the public key property publicKeyProp.AppendW3IDSecurityV1PublicKey(publicKey) @@ -188,19 +188,69 @@ func (c *converter) AccountToAS(a *gtsmodel.Account) (vocab.ActivityStreamsPerso person.SetW3IDSecurityV1PublicKey(publicKeyProp) // tag - // Any tags used in the summary of this profile + // TODO: Any tags used in the summary of this profile // attachment // Used for profile fields. + // TODO: The PropertyValue type has to be added: https://schema.org/PropertyValue // endpoints // NOT IMPLEMENTED -- this is for shared inbox which we don't use // icon // Used as profile avatar. + if a.AvatarMediaAttachmentID != "" { + iconProperty := streams.NewActivityStreamsIconProperty() + + iconImage := streams.NewActivityStreamsImage() + + avatar := >smodel.MediaAttachment{} + if err := c.db.GetByID(a.AvatarMediaAttachmentID, avatar); err != nil { + return nil, err + } + + mediaType := streams.NewActivityStreamsMediaTypeProperty() + mediaType.Set(avatar.File.ContentType) + iconImage.SetActivityStreamsMediaType(mediaType) + + avatarURLProperty := streams.NewActivityStreamsUrlProperty() + avatarURL, err := url.Parse(avatar.URL) + if err != nil { + return nil, err + } + avatarURLProperty.AppendIRI(avatarURL) + iconImage.SetActivityStreamsUrl(avatarURLProperty) + + iconProperty.AppendActivityStreamsImage(iconImage) + person.SetActivityStreamsIcon(iconProperty) + } // image // Used as profile header. + if a.HeaderMediaAttachmentID != "" { + iconProperty := streams.NewActivityStreamsIconProperty() + + iconImage := streams.NewActivityStreamsImage() + + header := >smodel.MediaAttachment{} + if err := c.db.GetByID(a.HeaderMediaAttachmentID, header); err != nil { + return nil, err + } + + mediaType := streams.NewActivityStreamsMediaTypeProperty() + mediaType.Set(header.File.ContentType) + iconImage.SetActivityStreamsMediaType(mediaType) + + headerURLProperty := streams.NewActivityStreamsUrlProperty() + headerURL, err := url.Parse(header.URL) + if err != nil { + return nil, err + } + headerURLProperty.AppendIRI(headerURL) + iconImage.SetActivityStreamsUrl(headerURLProperty) + + iconProperty.AppendActivityStreamsImage(iconImage) + } return person, nil } diff --git a/internal/typeutils/internaltoas_test.go b/internal/typeutils/internaltoas_test.go index e20e693..d606ad9 100644 --- a/internal/typeutils/internaltoas_test.go +++ b/internal/typeutils/internaltoas_test.go @@ -17,3 +17,68 @@ */ package typeutils_test + +import ( + "encoding/json" + "fmt" + "testing" + + "github.com/sirupsen/logrus" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/suite" + + "github.com/superseriousbusiness/gotosocial/internal/config" + "github.com/superseriousbusiness/gotosocial/internal/db" + "github.com/superseriousbusiness/gotosocial/internal/db/gtsmodel" + "github.com/superseriousbusiness/gotosocial/internal/typeutils" + "github.com/superseriousbusiness/gotosocial/testrig" +) + +type InternalToASTestSuite struct { + suite.Suite + config *config.Config + db db.DB + log *logrus.Logger + accounts map[string]*gtsmodel.Account + + typeconverter typeutils.TypeConverter +} + +// SetupSuite sets some variables on the suite that we can use as consts (more or less) throughout +func (suite *InternalToASTestSuite) SetupSuite() { + // setup standard items + suite.config = testrig.NewTestConfig() + suite.db = testrig.NewTestDB() + suite.log = testrig.NewTestLog() + suite.accounts = testrig.NewTestAccounts() + suite.typeconverter = typeutils.NewConverter(suite.config, suite.db) +} + +func (suite *InternalToASTestSuite) SetupTest() { + testrig.StandardDBSetup(suite.db) +} + +// TearDownTest drops tables to make sure there's no data in the db +func (suite *InternalToASTestSuite) TearDownTest() { + testrig.StandardDBTeardown(suite.db) +} + +func (suite *InternalToASTestSuite) TestPostAccountToAS() { + testAccount := suite.accounts["local_account_1"] // take zork for this test + + asPerson, err := suite.typeconverter.AccountToAS(testAccount) + assert.NoError(suite.T(), err) + + ser, err := asPerson.Serialize() + assert.NoError(suite.T(), err) + + bytes, err := json.Marshal(ser) + assert.NoError(suite.T(), err) + + fmt.Println(string(bytes)) + // TODO: write assertions here, rn we're just eyeballing the output +} + +func TestInternalToASTestSuite(t *testing.T) { + suite.Run(t, new(InternalToASTestSuite)) +}