[feature] Enable federation in/out of profile PropertyValue fields (#1722)

Co-authored-by: kim <grufwub@gmail.com>
Co-authored-by: kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com>
This commit is contained in:
tobi 2023-05-09 12:16:10 +02:00 committed by GitHub
commit 0e29f1f5bb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
180 changed files with 9278 additions and 1550 deletions

View file

@ -96,7 +96,7 @@ func (p *Processor) getFor(ctx context.Context, requestingAccount *gtsmodel.Acco
}
a, err := p.federator.GetAccountByURI(
gtscontext.SetFastFail(ctx), requestingAccount.Username, targetAccountURI, true,
gtscontext.SetFastFail(ctx), requestingAccount.Username, targetAccountURI,
)
if err == nil {
targetAccount = a

View file

@ -36,6 +36,14 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/validate"
)
func (p *Processor) selectNoteFormatter(contentType string) text.FormatFunc {
if contentType == "text/markdown" {
return p.formatter.FromMarkdown
}
return p.formatter.FromPlain
}
// Update processes the update of an account with the given form.
func (p *Processor) Update(ctx context.Context, account *gtsmodel.Account, form *apimodel.UpdateCredentialsRequest) (*apimodel.Account, gtserror.WithCode) {
if form.Discoverable != nil {
@ -46,56 +54,144 @@ func (p *Processor) Update(ctx context.Context, account *gtsmodel.Account, form
account.Bot = form.Bot
}
reparseEmojis := false
// Via the process of updating the account,
// it is possible that the emojis used by
// that account in note/display name/fields
// may change; we need to keep track of this.
var emojisChanged bool
if form.DisplayName != nil {
if err := validate.DisplayName(*form.DisplayName); err != nil {
return nil, gtserror.NewErrorBadRequest(err)
displayName := *form.DisplayName
if err := validate.DisplayName(displayName); err != nil {
return nil, gtserror.NewErrorBadRequest(err, err.Error())
}
account.DisplayName = text.SanitizePlaintext(*form.DisplayName)
reparseEmojis = true
// Parse new display name (always from plaintext).
account.DisplayName = text.SanitizePlaintext(displayName)
// If display name has changed, account emojis may have also changed.
emojisChanged = true
}
if form.Note != nil {
if err := validate.Note(*form.Note); err != nil {
return nil, gtserror.NewErrorBadRequest(err)
note := *form.Note
if err := validate.Note(note); err != nil {
return nil, gtserror.NewErrorBadRequest(err, err.Error())
}
// Set the raw note before processing
account.NoteRaw = *form.Note
reparseEmojis = true
// Store raw version of the note for now,
// we'll process the proper version later.
account.NoteRaw = note
// If note has changed, account emojis may have also changed.
emojisChanged = true
}
if reparseEmojis {
// If either DisplayName or Note changed, reparse both, because we
// can't otherwise tell which one each emoji belongs to.
// Deduplicate emojis between the two fields.
if form.FieldsAttributes != nil {
var (
fieldsAttributes = *form.FieldsAttributes
fieldsLen = len(fieldsAttributes)
fieldsRaw = make([]*gtsmodel.Field, 0, fieldsLen)
)
for _, updateField := range fieldsAttributes {
if updateField.Name == nil || updateField.Value == nil {
continue
}
var (
name string = *updateField.Name
value string = *updateField.Value
)
if name == "" || value == "" {
continue
}
// Sanitize raw field values.
fieldRaw := &gtsmodel.Field{
Name: text.SanitizePlaintext(name),
Value: text.SanitizePlaintext(value),
}
fieldsRaw = append(fieldsRaw, fieldRaw)
}
// Check length of parsed raw fields.
if err := validate.ProfileFields(fieldsRaw); err != nil {
return nil, gtserror.NewErrorBadRequest(err, err.Error())
}
// OK, new raw fields are valid.
account.FieldsRaw = fieldsRaw
account.Fields = make([]*gtsmodel.Field, 0, fieldsLen) // process these in a sec
// If fields have changed, account emojis may also have changed.
emojisChanged = true
}
if emojisChanged {
// Use map to deduplicate emojis by their ID.
emojis := make(map[string]*gtsmodel.Emoji)
formatResult := p.formatter.FromPlainEmojiOnly(ctx, p.parseMention, account.ID, "", account.DisplayName)
for _, emoji := range formatResult.Emojis {
// Retrieve display name emojis.
for _, emoji := range p.formatter.FromPlainEmojiOnly(
ctx,
p.parseMention,
account.ID,
"",
account.DisplayName,
).Emojis {
emojis[emoji.ID] = emoji
}
// Process note to generate a valid HTML representation
var f text.FormatFunc
if account.StatusContentType == "text/markdown" {
f = p.formatter.FromMarkdown
} else {
f = p.formatter.FromPlain
}
formatted := f(ctx, p.parseMention, account.ID, "", account.NoteRaw)
// Format + set note according to user prefs.
f := p.selectNoteFormatter(account.StatusContentType)
formatNoteResult := f(ctx, p.parseMention, account.ID, "", account.NoteRaw)
account.Note = formatNoteResult.HTML
// Set updated HTML-ified note
account.Note = formatted.HTML
for _, emoji := range formatted.Emojis {
// Retrieve note emojis.
for _, emoji := range formatNoteResult.Emojis {
emojis[emoji.ID] = emoji
}
account.Emojis = []*gtsmodel.Emoji{}
account.EmojiIDs = []string{}
for eid, emoji := range emojis {
// Process the raw fields we stored earlier.
for _, fieldRaw := range account.FieldsRaw {
field := &gtsmodel.Field{}
// Name stays plain, but we still need to
// see if there are any emojis set in it.
field.Name = fieldRaw.Name
for _, emoji := range p.formatter.FromPlainEmojiOnly(
ctx,
p.parseMention,
account.ID,
"",
fieldRaw.Name,
).Emojis {
emojis[emoji.ID] = emoji
}
// Value can be HTML, but we don't want
// to wrap the result in <p> tags.
fieldFormatValueResult := p.formatter.FromPlainNoParagraph(ctx, p.parseMention, account.ID, "", fieldRaw.Value)
field.Value = fieldFormatValueResult.HTML
// Retrieve field emojis.
for _, emoji := range fieldFormatValueResult.Emojis {
emojis[emoji.ID] = emoji
}
// We're done, append the shiny new field.
account.Fields = append(account.Fields, field)
}
emojisCount := len(emojis)
account.Emojis = make([]*gtsmodel.Emoji, 0, emojisCount)
account.EmojiIDs = make([]string, 0, emojisCount)
for id, emoji := range emojis {
account.Emojis = append(account.Emojis, emoji)
account.EmojiIDs = append(account.EmojiIDs, eid)
account.EmojiIDs = append(account.EmojiIDs, id)
}
}
@ -164,26 +260,6 @@ func (p *Processor) Update(ctx context.Context, account *gtsmodel.Account, form
account.EnableRSS = form.EnableRSS
}
if form.FieldsAttributes != nil && len(*form.FieldsAttributes) != 0 {
if err := validate.ProfileFieldsCount(*form.FieldsAttributes); err != nil {
return nil, gtserror.NewErrorBadRequest(err)
}
account.Fields = make([]gtsmodel.Field, 0) // reset fields
for _, f := range *form.FieldsAttributes {
if f.Name != nil && f.Value != nil {
if *f.Name != "" && *f.Value != "" {
field := gtsmodel.Field{}
field.Name = validate.ProfileField(f.Name)
field.Value = validate.ProfileField(f.Value)
account.Fields = append(account.Fields, field)
}
}
}
}
err := p.state.DB.UpdateAccount(ctx, account)
if err != nil {
return nil, gtserror.NewErrorInternalError(fmt.Errorf("could not update account %s: %s", account.ID, err))

View file

@ -148,6 +148,86 @@ func (suite *AccountUpdateTestSuite) TestAccountUpdateWithMarkdownNote() {
suite.Equal(expectedNote, dbAccount.Note)
}
func (suite *AccountUpdateTestSuite) TestAccountUpdateWithFields() {
testAccount := suite.testAccounts["local_account_1"]
updateFields := []apimodel.UpdateField{
{
Name: func() *string { s := "favourite emoji"; return &s }(),
Value: func() *string { s := ":rainbow:"; return &s }(),
},
{
Name: func() *string { s := "my website"; return &s }(),
Value: func() *string { s := "https://example.org"; return &s }(),
},
}
form := &apimodel.UpdateCredentialsRequest{
FieldsAttributes: &updateFields,
}
// should get no error from the update function, and an api model account returned
apiAccount, errWithCode := suite.accountProcessor.Update(context.Background(), testAccount, form)
// reset test account to avoid breaking other tests
testAccount.StatusContentType = "text/plain"
suite.NoError(errWithCode)
suite.NotNil(apiAccount)
suite.EqualValues([]apimodel.Field{
{
Name: "favourite emoji",
Value: ":rainbow:",
VerifiedAt: (*string)(nil),
},
{
Name: "my website",
Value: "<a href=\"https://example.org\" rel=\"nofollow noreferrer noopener\" target=\"_blank\">https://example.org</a>",
VerifiedAt: (*string)(nil),
},
}, apiAccount.Fields)
suite.EqualValues([]apimodel.Field{
{
Name: "favourite emoji",
Value: ":rainbow:",
VerifiedAt: (*string)(nil),
},
{
Name: "my website",
Value: "https://example.org",
VerifiedAt: (*string)(nil),
},
}, apiAccount.Source.Fields)
suite.EqualValues([]apimodel.Emoji{
{
Shortcode: "rainbow",
URL: "http://localhost:8080/fileserver/01AY6P665V14JJR0AFVRT7311Y/emoji/original/01F8MH9H8E4VG3KDYJR9EGPXCQ.png",
StaticURL: "http://localhost:8080/fileserver/01AY6P665V14JJR0AFVRT7311Y/emoji/static/01F8MH9H8E4VG3KDYJR9EGPXCQ.png",
VisibleInPicker: true,
Category: "reactions",
},
}, apiAccount.Emojis)
// we should have an update in the client api channel
msg := <-suite.fromClientAPIChan
suite.Equal(ap.ActivityUpdate, msg.APActivityType)
suite.Equal(ap.ObjectProfile, msg.APObjectType)
suite.NotNil(msg.OriginAccount)
suite.Equal(testAccount.ID, msg.OriginAccount.ID)
suite.Nil(msg.TargetAccount)
// fields should be updated in the database as well
dbAccount, err := suite.db.GetAccountByID(context.Background(), testAccount.ID)
suite.NoError(err)
suite.Equal("favourite emoji", dbAccount.Fields[0].Name)
suite.Equal(":rainbow:", dbAccount.Fields[0].Value)
suite.Equal("my website", dbAccount.Fields[1].Name)
suite.Equal("<a href=\"https://example.org\" rel=\"nofollow noreferrer noopener\" target=\"_blank\">https://example.org</a>", dbAccount.Fields[1].Value)
suite.Equal("favourite emoji", dbAccount.FieldsRaw[0].Name)
suite.Equal(":rainbow:", dbAccount.FieldsRaw[0].Value)
suite.Equal("my website", dbAccount.FieldsRaw[1].Name)
suite.Equal("https://example.org", dbAccount.FieldsRaw[1].Value)
}
func TestAccountUpdateTestSuite(t *testing.T) {
suite.Run(t, new(AccountUpdateTestSuite))
}

View file

@ -24,7 +24,6 @@ import (
"net/http"
"net/url"
"github.com/superseriousbusiness/activity/streams"
"github.com/superseriousbusiness/gotosocial/internal/ap"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
@ -74,7 +73,7 @@ func (p *Processor) OutboxGet(ctx context.Context, requestedUsername string, pag
return nil, gtserror.NewErrorInternalError(err)
}
data, err = streams.Serialize(collection)
data, err = ap.Serialize(collection)
if err != nil {
return nil, gtserror.NewErrorInternalError(err)
}
@ -93,7 +92,7 @@ func (p *Processor) OutboxGet(ctx context.Context, requestedUsername string, pag
if err != nil {
return nil, gtserror.NewErrorInternalError(err)
}
data, err = streams.Serialize(outboxPage)
data, err = ap.Serialize(outboxPage)
if err != nil {
return nil, gtserror.NewErrorInternalError(err)
}
@ -119,7 +118,7 @@ func (p *Processor) FollowersGet(ctx context.Context, requestedUsername string)
return nil, gtserror.NewErrorInternalError(fmt.Errorf("error fetching followers for uri %s: %s", requestedAccountURI.String(), err))
}
data, err := streams.Serialize(requestedFollowers)
data, err := ap.Serialize(requestedFollowers)
if err != nil {
return nil, gtserror.NewErrorInternalError(err)
}
@ -145,7 +144,7 @@ func (p *Processor) FollowingGet(ctx context.Context, requestedUsername string)
return nil, gtserror.NewErrorInternalError(fmt.Errorf("error fetching following for uri %s: %s", requestedAccountURI.String(), err))
}
data, err := streams.Serialize(requestedFollowing)
data, err := ap.Serialize(requestedFollowing)
if err != nil {
return nil, gtserror.NewErrorInternalError(err)
}
@ -173,7 +172,7 @@ func (p *Processor) FeaturedCollectionGet(ctx context.Context, requestedUsername
return nil, gtserror.NewErrorInternalError(err)
}
data, err := ap.SerializeOrderedCollection(collection)
data, err := ap.Serialize(collection)
if err != nil {
return nil, gtserror.NewErrorInternalError(err)
}

View file

@ -40,7 +40,7 @@ func (p *Processor) authenticate(ctx context.Context, requestedUsername string)
return
}
if requestingAccount, err = p.federator.GetAccountByURI(gtscontext.SetFastFail(ctx), requestedUsername, requestingAccountURI, false); err != nil {
if requestingAccount, err = p.federator.GetAccountByURI(gtscontext.SetFastFail(ctx), requestedUsername, requestingAccountURI); err != nil {
errWithCode = gtserror.NewErrorUnauthorized(err)
return
}

View file

@ -21,7 +21,7 @@ import (
"context"
"fmt"
"github.com/superseriousbusiness/activity/streams"
"github.com/superseriousbusiness/gotosocial/internal/ap"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
)
@ -49,7 +49,7 @@ func (p *Processor) EmojiGet(ctx context.Context, requestedEmojiID string) (inte
return nil, gtserror.NewErrorInternalError(fmt.Errorf("error converting gtsmodel emoji with id %s to ap emoji: %s", requestedEmojiID, err))
}
data, err := streams.Serialize(apEmoji)
data, err := ap.Serialize(apEmoji)
if err != nil {
return nil, gtserror.NewErrorInternalError(err)
}

View file

@ -22,7 +22,7 @@ import (
"fmt"
"net/url"
"github.com/superseriousbusiness/activity/streams"
"github.com/superseriousbusiness/gotosocial/internal/ap"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
)
@ -57,7 +57,7 @@ func (p *Processor) StatusGet(ctx context.Context, requestedUsername string, req
return nil, gtserror.NewErrorInternalError(err)
}
data, err := streams.Serialize(asStatus)
data, err := ap.Serialize(asStatus)
if err != nil {
return nil, gtserror.NewErrorInternalError(err)
}
@ -105,7 +105,7 @@ func (p *Processor) StatusRepliesGet(ctx context.Context, requestedUsername stri
return nil, gtserror.NewErrorInternalError(err)
}
data, err = streams.Serialize(collection)
data, err = ap.Serialize(collection)
if err != nil {
return nil, gtserror.NewErrorInternalError(err)
}
@ -117,7 +117,7 @@ func (p *Processor) StatusRepliesGet(ctx context.Context, requestedUsername stri
return nil, gtserror.NewErrorInternalError(err)
}
// but only return the first page
data, err = streams.Serialize(collection.GetActivityStreamsFirst().GetActivityStreamsCollectionPage())
data, err = ap.Serialize(collection.GetActivityStreamsFirst().GetActivityStreamsCollectionPage())
if err != nil {
return nil, gtserror.NewErrorInternalError(err)
}
@ -166,7 +166,7 @@ func (p *Processor) StatusRepliesGet(ctx context.Context, requestedUsername stri
if err != nil {
return nil, gtserror.NewErrorInternalError(err)
}
data, err = streams.Serialize(repliesPage)
data, err = ap.Serialize(repliesPage)
if err != nil {
return nil, gtserror.NewErrorInternalError(err)
}

View file

@ -22,8 +22,8 @@ import (
"fmt"
"net/url"
"github.com/superseriousbusiness/activity/streams"
"github.com/superseriousbusiness/activity/streams/vocab"
"github.com/superseriousbusiness/gotosocial/internal/ap"
"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/uris"
@ -56,7 +56,7 @@ func (p *Processor) UserGet(ctx context.Context, requestedUsername string, reque
// if we're not already handshaking/dereferencing a remote account, dereference it now
if !p.federator.Handshaking(requestedUsername, requestingAccountURI) {
requestingAccount, err := p.federator.GetAccountByURI(
gtscontext.SetFastFail(ctx), requestedUsername, requestingAccountURI, false,
gtscontext.SetFastFail(ctx), requestedUsername, requestingAccountURI,
)
if err != nil {
return nil, gtserror.NewErrorUnauthorized(err)
@ -78,7 +78,7 @@ func (p *Processor) UserGet(ctx context.Context, requestedUsername string, reque
}
}
data, err := streams.Serialize(requestedPerson)
data, err := ap.Serialize(requestedPerson)
if err != nil {
return nil, gtserror.NewErrorInternalError(err)
}

View file

@ -145,7 +145,7 @@ func (p *Processor) processCreateStatusFromFederator(ctx context.Context, federa
status.Account = a
}
// do a BLOCKING get of the remote account to make sure the avi and header are cached
// Get the remote account to make sure the avi and header are cached.
if status.Account.Domain != "" {
remoteAccountID, err := url.Parse(status.Account.URI)
if err != nil {
@ -155,7 +155,6 @@ func (p *Processor) processCreateStatusFromFederator(ctx context.Context, federa
a, err := p.federator.GetAccountByURI(ctx,
federatorMsg.ReceivingAccount.Username,
remoteAccountID,
true,
)
if err != nil {
return err
@ -187,7 +186,7 @@ func (p *Processor) processCreateFaveFromFederator(ctx context.Context, federato
incomingFave.Account = a
}
// do a BLOCKING get of the remote account to make sure the avi and header are cached
// Get the remote account to make sure the avi and header are cached.
if incomingFave.Account.Domain != "" {
remoteAccountID, err := url.Parse(incomingFave.Account.URI)
if err != nil {
@ -197,7 +196,6 @@ func (p *Processor) processCreateFaveFromFederator(ctx context.Context, federato
a, err := p.federator.GetAccountByURI(ctx,
federatorMsg.ReceivingAccount.Username,
remoteAccountID,
true,
)
if err != nil {
return err
@ -229,7 +227,7 @@ func (p *Processor) processCreateFollowRequestFromFederator(ctx context.Context,
followRequest.Account = a
}
// do a BLOCKING get of the remote account to make sure the avi and header are cached
// Get the remote account to make sure the avi and header are cached.
if followRequest.Account.Domain != "" {
remoteAccountID, err := url.Parse(followRequest.Account.URI)
if err != nil {
@ -239,7 +237,6 @@ func (p *Processor) processCreateFollowRequestFromFederator(ctx context.Context,
a, err := p.federator.GetAccountByURI(ctx,
federatorMsg.ReceivingAccount.Username,
remoteAccountID,
true,
)
if err != nil {
return err
@ -290,7 +287,7 @@ func (p *Processor) processCreateAnnounceFromFederator(ctx context.Context, fede
incomingAnnounce.Account = a
}
// do a BLOCKING get of the remote account to make sure the avi and header are cached
// Get the remote account to make sure the avi and header are cached.
if incomingAnnounce.Account.Domain != "" {
remoteAccountID, err := url.Parse(incomingAnnounce.Account.URI)
if err != nil {
@ -300,7 +297,6 @@ func (p *Processor) processCreateAnnounceFromFederator(ctx context.Context, fede
a, err := p.federator.GetAccountByURI(ctx,
federatorMsg.ReceivingAccount.Username,
remoteAccountID,
true,
)
if err != nil {
return err
@ -370,16 +366,28 @@ func (p *Processor) processCreateFlagFromFederator(ctx context.Context, federato
func (p *Processor) processUpdateAccountFromFederator(ctx context.Context, federatorMsg messages.FromFederator) error {
incomingAccount, ok := federatorMsg.GTSModel.(*gtsmodel.Account)
if !ok {
return errors.New("profile was not parseable as *gtsmodel.Account")
return errors.New("*gtsmodel.Account was not parseable on update account message")
}
// Call UpdateAccount with force to reflect that
// we want to fetch new bio, avatar, header, etc.
if _, err := p.federator.UpdateAccount(ctx,
// Because this was an Update, the new AP Object should be set on the message.
incomingAccountable, ok := federatorMsg.APObjectModel.(ap.Accountable)
if !ok {
return errors.New("Accountable was not parseable on update account message")
}
// Call RefreshAccount to fetch up-to-date bio, avatar, header, etc.
updatedAccount, err := p.federator.RefreshAccount(
ctx,
federatorMsg.ReceivingAccount.Username,
incomingAccountable,
incomingAccount,
true,
); err != nil {
)
if err != nil {
return fmt.Errorf("error enriching updated account from federator: %s", err)
}
// RefreshAccount doesn't make DB update calls, so do that here.
if err := p.state.DB.UpdateAccount(ctx, updatedAccount); err != nil {
return fmt.Errorf("error enriching updated account from federator: %s", err)
}

View file

@ -270,7 +270,7 @@ func (p *Processor) searchAccountByURI(ctx context.Context, authed *oauth.Auth,
return p.federator.GetAccountByURI(
gtscontext.SetFastFail(ctx),
authed.Account.Username,
uri, false,
uri,
)
}
@ -297,6 +297,6 @@ func (p *Processor) searchAccountByUsernameDomain(ctx context.Context, authed *o
return p.federator.GetAccountByUsernameDomain(
gtscontext.SetFastFail(ctx),
authed.Account.Username,
username, domain, false,
username, domain,
)
}

View file

@ -62,7 +62,6 @@ func GetParseMentionFunc(dbConn db.DB, federator federation.Federator) gtsmodel.
requestingUsername,
username,
domain,
false,
)
if err != nil {
return nil, fmt.Errorf("parseMentionFunc: error fetching account: %s", err)