summaryrefslogtreecommitdiff
path: root/internal/processing/account
diff options
context:
space:
mode:
Diffstat (limited to 'internal/processing/account')
-rw-r--r--internal/processing/account/account.go3
-rw-r--r--internal/processing/account/update.go69
-rw-r--r--internal/processing/account/update_test.go49
3 files changed, 91 insertions, 30 deletions
diff --git a/internal/processing/account/account.go b/internal/processing/account/account.go
index 71b876d3b..831607d94 100644
--- a/internal/processing/account/account.go
+++ b/internal/processing/account/account.go
@@ -32,6 +32,7 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/media"
"github.com/superseriousbusiness/gotosocial/internal/messages"
"github.com/superseriousbusiness/gotosocial/internal/oauth"
+ "github.com/superseriousbusiness/gotosocial/internal/text"
"github.com/superseriousbusiness/gotosocial/internal/typeutils"
"github.com/superseriousbusiness/gotosocial/internal/visibility"
"github.com/superseriousbusiness/oauth2/v4"
@@ -83,6 +84,7 @@ type processor struct {
fromClientAPI chan messages.FromClientAPI
oauthServer oauth.Server
filter visibility.Filter
+ formatter text.Formatter
db db.DB
federator federation.Federator
log *logrus.Logger
@@ -97,6 +99,7 @@ func New(db db.DB, tc typeutils.TypeConverter, mediaHandler media.Handler, oauth
fromClientAPI: fromClientAPI,
oauthServer: oauthServer,
filter: visibility.NewFilter(db, log),
+ formatter: text.NewFormatter(config, db, log),
db: db,
federator: federator,
log: log,
diff --git a/internal/processing/account/update.go b/internal/processing/account/update.go
index e997a95c7..6dc288849 100644
--- a/internal/processing/account/update.go
+++ b/internal/processing/account/update.go
@@ -32,6 +32,7 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/media"
"github.com/superseriousbusiness/gotosocial/internal/messages"
"github.com/superseriousbusiness/gotosocial/internal/text"
+ "github.com/superseriousbusiness/gotosocial/internal/util"
"github.com/superseriousbusiness/gotosocial/internal/validate"
)
@@ -39,35 +40,29 @@ func (p *processor) Update(ctx context.Context, account *gtsmodel.Account, form
l := p.log.WithField("func", "AccountUpdate")
if form.Discoverable != nil {
- if err := p.db.UpdateOneByPrimaryKey(ctx, "discoverable", *form.Discoverable, account); err != nil {
- return nil, fmt.Errorf("error updating discoverable: %s", err)
- }
+ account.Discoverable = *form.Discoverable
}
if form.Bot != nil {
- if err := p.db.UpdateOneByPrimaryKey(ctx, "bot", *form.Bot, account); err != nil {
- return nil, fmt.Errorf("error updating bot: %s", err)
- }
+ account.Bot = *form.Bot
}
if form.DisplayName != nil {
if err := validate.DisplayName(*form.DisplayName); err != nil {
return nil, err
}
- displayName := text.RemoveHTML(*form.DisplayName) // no html allowed in display name
- if err := p.db.UpdateOneByPrimaryKey(ctx, "display_name", displayName, account); err != nil {
- return nil, err
- }
+ account.DisplayName = text.RemoveHTML(*form.DisplayName)
}
if form.Note != nil {
if err := validate.Note(*form.Note); err != nil {
return nil, err
}
- note := text.SanitizeHTML(*form.Note) // html OK in note but sanitize it
- if err := p.db.UpdateOneByPrimaryKey(ctx, "note", note, account); err != nil {
+ note, err := p.processNote(ctx, *form.Note, account.ID)
+ if err != nil {
return nil, err
}
+ account.Note = note
}
if form.Avatar != nil && form.Avatar.Size != 0 {
@@ -75,6 +70,8 @@ func (p *processor) Update(ctx context.Context, account *gtsmodel.Account, form
if err != nil {
return nil, err
}
+ account.AvatarMediaAttachmentID = avatarInfo.ID
+ account.AvatarMediaAttachment = avatarInfo
l.Tracef("new avatar info for account %s is %+v", account.ID, avatarInfo)
}
@@ -83,13 +80,13 @@ func (p *processor) Update(ctx context.Context, account *gtsmodel.Account, form
if err != nil {
return nil, err
}
+ account.HeaderMediaAttachmentID = headerInfo.ID
+ account.HeaderMediaAttachment = headerInfo
l.Tracef("new header info for account %s is %+v", account.ID, headerInfo)
}
if form.Locked != nil {
- if err := p.db.UpdateOneByPrimaryKey(ctx, "locked", *form.Locked, account); err != nil {
- return nil, err
- }
+ account.Locked = *form.Locked
}
if form.Source != nil {
@@ -97,31 +94,25 @@ func (p *processor) Update(ctx context.Context, account *gtsmodel.Account, form
if err := validate.Language(*form.Source.Language); err != nil {
return nil, err
}
- if err := p.db.UpdateOneByPrimaryKey(ctx, "language", *form.Source.Language, account); err != nil {
- return nil, err
- }
+ account.Language = *form.Source.Language
}
if form.Source.Sensitive != nil {
- if err := p.db.UpdateOneByPrimaryKey(ctx, "locked", *form.Locked, account); err != nil {
- return nil, err
- }
+ account.Sensitive = *form.Source.Sensitive
}
if form.Source.Privacy != nil {
if err := validate.Privacy(*form.Source.Privacy); err != nil {
return nil, err
}
- if err := p.db.UpdateOneByPrimaryKey(ctx, "privacy", *form.Source.Privacy, account); err != nil {
- return nil, err
- }
+ privacy := p.tc.MastoVisToVis(apimodel.Visibility(*form.Source.Privacy))
+ account.Privacy = privacy
}
}
- // fetch the account with all updated values set
- updatedAccount, err := p.db.GetAccountByID(ctx, account.ID)
+ updatedAccount, err := p.db.UpdateAccount(ctx, account)
if err != nil {
- return nil, fmt.Errorf("could not fetch updated account %s: %s", account.ID, err)
+ return nil, fmt.Errorf("could not update account %s: %s", account.ID, err)
}
p.fromClientAPI <- messages.FromClientAPI{
@@ -203,3 +194,27 @@ func (p *processor) UpdateHeader(ctx context.Context, header *multipart.FileHead
return headerInfo, f.Close()
}
+
+func (p *processor) processNote(ctx context.Context, note string, accountID string) (string, error) {
+ if note == "" {
+ return "", nil
+ }
+
+ tagStrings := util.DeriveHashtagsFromText(note)
+ tags, err := p.db.TagStringsToTags(ctx, tagStrings, accountID)
+ if err != nil {
+ return "", err
+ }
+
+ mentionStrings := util.DeriveMentionsFromText(note)
+ mentions, err := p.db.MentionStringsToMentions(ctx, mentionStrings, accountID, "")
+ if err != nil {
+ return "", err
+ }
+
+ // TODO: support emojis in account notes
+ // emojiStrings := util.DeriveEmojisFromText(note)
+ // emojis, err := p.db.EmojiStringsToEmojis(ctx, emojiStrings)
+
+ return p.formatter.FromPlain(ctx, note, mentions, tags), nil
+}
diff --git a/internal/processing/account/update_test.go b/internal/processing/account/update_test.go
index b18a5e42e..63370cd39 100644
--- a/internal/processing/account/update_test.go
+++ b/internal/processing/account/update_test.go
@@ -36,7 +36,7 @@ func (suite *AccountUpdateTestSuite) TestAccountUpdateSimple() {
locked := true
displayName := "new display name"
- note := ""
+ note := "#hello here i am!"
form := &apimodel.UpdateCredentialsRequest{
DisplayName: &displayName,
@@ -52,7 +52,7 @@ func (suite *AccountUpdateTestSuite) TestAccountUpdateSimple() {
// fields on the profile should be updated
suite.True(apiAccount.Locked)
suite.Equal(displayName, apiAccount.DisplayName)
- suite.Empty(apiAccount.Note)
+ suite.Equal(`<p><a href="http://localhost:8080/tags/hello" class="mention hashtag" rel="tag nofollow noreferrer noopener" target="_blank">#<span>hello</span></a> here i am!</p>`, apiAccount.Note)
// we should have an update in the client api channel
msg := <-suite.fromClientAPIChan
@@ -67,7 +67,50 @@ func (suite *AccountUpdateTestSuite) TestAccountUpdateSimple() {
suite.NoError(err)
suite.True(dbAccount.Locked)
suite.Equal(displayName, dbAccount.DisplayName)
- suite.Empty(dbAccount.Note)
+ suite.Equal(`<p><a href="http://localhost:8080/tags/hello" class="mention hashtag" rel="tag nofollow noreferrer noopener" target="_blank">#<span>hello</span></a> here i am!</p>`, dbAccount.Note)
+}
+
+func (suite *AccountUpdateTestSuite) TestAccountUpdateWithMention() {
+ testAccount := suite.testAccounts["local_account_1"]
+
+ locked := true
+ displayName := "new display name"
+ note := `#hello here i am!
+
+go check out @1happyturtle, they have a cool account!
+`
+ noteExpected := `<p><a href="http://localhost:8080/tags/hello" class="mention hashtag" rel="tag nofollow noreferrer noopener" target="_blank">#<span>hello</span></a> here i am!<br><br>go check out <span class="h-card"><a href="http://localhost:8080/@1happyturtle" class="u-url mention" rel="nofollow noreferrer noopener" target="_blank">@<span>1happyturtle</span></a></span>, they have a cool account!</p>`
+
+ form := &apimodel.UpdateCredentialsRequest{
+ DisplayName: &displayName,
+ Locked: &locked,
+ Note: &note,
+ }
+
+ // should get no error from the update function, and an api model account returned
+ apiAccount, err := suite.accountProcessor.Update(context.Background(), testAccount, form)
+ suite.NoError(err)
+ suite.NotNil(apiAccount)
+
+ // fields on the profile should be updated
+ suite.True(apiAccount.Locked)
+ suite.Equal(displayName, apiAccount.DisplayName)
+ suite.Equal(noteExpected, apiAccount.Note)
+
+ // 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.True(dbAccount.Locked)
+ suite.Equal(displayName, dbAccount.DisplayName)
+ suite.Equal(noteExpected, dbAccount.Note)
}
func TestAccountUpdateTestSuite(t *testing.T) {