[bugfix] Fix setting bot on/off

This commit is contained in:
tobi 2025-04-11 12:04:16 +02:00
parent e032c959e1
commit e89ce8d43b
5 changed files with 88 additions and 22 deletions

View file

@ -431,7 +431,7 @@ func (t AccountActorType) String() string {
case AccountActorTypeService:
return "Service"
default:
panic("invalid notification type")
panic("invalid actor type")
}
}

View file

@ -77,8 +77,16 @@ func (p *Processor) Update(ctx context.Context, account *gtsmodel.Account, form
acctColumns = append(acctColumns, "discoverable")
}
if form.Bot != nil {
account.ActorType = gtsmodel.AccountActorTypeService
if bot := form.Bot; bot != nil {
if *bot {
// Mark account as an Application.
// See: https://www.w3.org/TR/activitystreams-vocabulary/#dfn-application
account.ActorType = gtsmodel.AccountActorTypeApplication
} else {
// Mark account as a Person.
// See: https://www.w3.org/TR/activitystreams-vocabulary/#dfn-person
account.ActorType = gtsmodel.AccountActorTypePerson
}
acctColumns = append(acctColumns, "actor_type")
}

View file

@ -26,6 +26,7 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/ap"
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/util"
)
type AccountUpdateTestSuite struct {
@ -331,6 +332,56 @@ func (suite *AccountUpdateTestSuite) TestAccountUpdateNoteNotFields() {
suite.Equal(fieldsBefore, len(dbAccount.Fields))
}
func (suite *AccountUpdateTestSuite) TestAccountUpdateBotNotBot() {
testAccount := &gtsmodel.Account{}
*testAccount = *suite.testAccounts["local_account_1"]
ctx := context.Background()
// Call update function to set bot = true.
apiAccount, errWithCode := suite.accountProcessor.Update(
ctx,
testAccount,
&apimodel.UpdateCredentialsRequest{
Bot: util.Ptr(true),
},
)
if errWithCode != nil {
suite.FailNow(errWithCode.Error())
}
// Returned profile should be updated.
suite.True(apiAccount.Bot)
// Check database model of account as well.
dbAccount, err := suite.db.GetAccountByID(ctx, testAccount.ID)
if err != nil {
suite.FailNow(err.Error())
}
suite.True(dbAccount.ActorType.IsBot())
// Call update function to set bot = false.
apiAccount, errWithCode = suite.accountProcessor.Update(
ctx,
testAccount,
&apimodel.UpdateCredentialsRequest{
Bot: util.Ptr(false),
},
)
if errWithCode != nil {
suite.FailNow(errWithCode.Error())
}
// Returned profile should be updated.
suite.False(apiAccount.Bot)
// Check database model of account as well.
dbAccount, err = suite.db.GetAccountByID(ctx, testAccount.ID)
if err != nil {
suite.FailNow(err.Error())
}
suite.False(dbAccount.ActorType.IsBot())
}
func TestAccountUpdateTestSuite(t *testing.T) {
suite.Run(t, new(AccountUpdateTestSuite))
}

View file

@ -39,20 +39,32 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/util/xslices"
)
// AccountToAS converts a gts model account
// into an activity streams person or service.
func accountableForActorType(actorType gtsmodel.AccountActorType) ap.Accountable {
switch actorType {
case gtsmodel.AccountActorTypeApplication:
return streams.NewActivityStreamsApplication()
case gtsmodel.AccountActorTypeGroup:
return streams.NewActivityStreamsGroup()
case gtsmodel.AccountActorTypeOrganization:
return streams.NewActivityStreamsOrganization()
case gtsmodel.AccountActorTypePerson:
return streams.NewActivityStreamsPerson()
case gtsmodel.AccountActorTypeService:
return streams.NewActivityStreamsService()
default:
panic("invalid actor type")
}
}
// AccountToAS converts a gts model
// account into an accountable.
func (c *Converter) AccountToAS(
ctx context.Context,
a *gtsmodel.Account,
) (ap.Accountable, error) {
// accountable is a service if this
// is a bot account, otherwise a person.
var accountable ap.Accountable
if a.ActorType.IsBot() {
accountable = streams.NewActivityStreamsService()
} else {
accountable = streams.NewActivityStreamsPerson()
}
// Use appropriate underlying
// actor type of accountable.
accountable := accountableForActorType(a.ActorType)
// id should be the activitypub URI of this user
// something like https://example.org/users/example_user
@ -389,14 +401,9 @@ func (c *Converter) AccountToASMinimal(
ctx context.Context,
a *gtsmodel.Account,
) (ap.Accountable, error) {
// accountable is a service if this
// is a bot account, otherwise a person.
var accountable ap.Accountable
if a.ActorType.IsBot() {
accountable = streams.NewActivityStreamsService()
} else {
accountable = streams.NewActivityStreamsPerson()
}
// Use appropriate underlying
// actor type of accountable.
accountable := accountableForActorType(a.ActorType)
// id should be the activitypub URI of this user
// something like https://example.org/users/example_user

View file

@ -155,7 +155,7 @@ func (suite *InternalToASTestSuite) TestAccountToASBot() {
"published": "2022-05-20T11:09:18Z",
"summary": "\u003cp\u003ehey yo this is my profile!\u003c/p\u003e",
"tag": [],
"type": "Service",
"type": "Application",
"url": "http://localhost:8080/@the_mighty_zork"
}`, string(bytes))
}