diff options
author | 2024-08-13 09:01:50 +0000 | |
---|---|---|
committer | 2024-08-13 09:01:50 +0000 | |
commit | 5212a1057ed05085d4d332976510880c6a692a8e (patch) | |
tree | 33ec543f66439328983deb688d3c10ae37e91264 /internal/typeutils/astointernal.go | |
parent | [bugfix] incorrect AP serialize function used serializing worker data (#3196) (diff) | |
download | gotosocial-5212a1057ed05085d4d332976510880c6a692a8e.tar.xz |
[bugfix] relax missing preferred_username, instead using webfingered username (#3189)
* support no preferred_username, instead using webfingered username
* add tests for the new preferred_username behaviour
Diffstat (limited to 'internal/typeutils/astointernal.go')
-rw-r--r-- | internal/typeutils/astointernal.go | 36 |
1 files changed, 29 insertions, 7 deletions
diff --git a/internal/typeutils/astointernal.go b/internal/typeutils/astointernal.go index 2946c8d09..5ff60b09c 100644 --- a/internal/typeutils/astointernal.go +++ b/internal/typeutils/astointernal.go @@ -18,6 +18,7 @@ package typeutils import ( + "cmp" "context" "errors" "net/url" @@ -33,10 +34,24 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/util" ) -// ASRepresentationToAccount converts a remote account/person/application representation into a gts model account. +// ASRepresentationToAccount converts a remote account / person +// / application representation into a gts model account. // -// If accountDomain is provided then this value will be used as the account's Domain, else the AP ID host. -func (c *Converter) ASRepresentationToAccount(ctx context.Context, accountable ap.Accountable, accountDomain string) (*gtsmodel.Account, error) { +// If accountDomain is provided then this value will be +// used as the account's Domain, else the AP ID host. +// +// If accountUsername is provided then this is used as +// a fallback when no preferredUsername is provided. Else +// a lack of username will result in error return. +func (c *Converter) ASRepresentationToAccount( + ctx context.Context, + accountable ap.Accountable, + accountDomain string, + accountUsername string, +) ( + *gtsmodel.Account, + error, +) { var err error // Extract URI from accountable @@ -70,10 +85,17 @@ func (c *Converter) ASRepresentationToAccount(ctx context.Context, accountable a return nil, gtserror.SetMalformed(err) } - // Extract preferredUsername, this is a *requirement*. - acct.Username, err = ap.ExtractPreferredUsername(accountable) - if err != nil { - err := gtserror.Newf("unusable username for %s", uri) + // Set account username. + acct.Username = cmp.Or( + + // Prefer the AP model provided username. + ap.ExtractPreferredUsername(accountable), + + // Fallback username. + accountUsername, + ) + if acct.Username == "" { + err := gtserror.Newf("missing username for %s", uri) return nil, gtserror.SetMalformed(err) } |