[bugfix] Check media-description-min-chars on submission of new status (#960)

This commit is contained in:
tobi 2022-11-05 13:33:38 +01:00 committed by GitHub
parent fc9038d8e4
commit 15be35673e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 56 additions and 7 deletions

View file

@ -64,8 +64,8 @@ func (p *processor) Create(ctx context.Context, account *gtsmodel.Account, appli
return nil, errWithCode
}
if err := p.ProcessMediaIDs(ctx, form, account.ID, newStatus); err != nil {
return nil, gtserror.NewErrorInternalError(err)
if errWithCode := p.ProcessMediaIDs(ctx, form, account.ID, newStatus); errWithCode != nil {
return nil, errWithCode
}
if err := p.ProcessVisibility(ctx, form, account.Privacy, newStatus); err != nil {

View file

@ -24,6 +24,7 @@ import (
"github.com/stretchr/testify/suite"
"github.com/superseriousbusiness/gotosocial/internal/api/model"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
)
@ -174,6 +175,40 @@ func (suite *StatusCreateTestSuite) TestProcessStatusMarkdownWithSpoilerTextEmoj
suite.NotEmpty(apiStatus.Emojis)
}
func (suite *StatusCreateTestSuite) TestProcessMediaDescriptionTooShort() {
ctx := context.Background()
config.SetMediaDescriptionMinChars(100)
creatingAccount := suite.testAccounts["local_account_1"]
creatingApplication := suite.testApplications["application_1"]
statusCreateForm := &model.AdvancedStatusCreateForm{
StatusCreateRequest: model.StatusCreateRequest{
Status: "poopoo peepee",
MediaIDs: []string{suite.testAttachments["local_account_1_unattached_1"].ID},
Poll: nil,
InReplyToID: "",
Sensitive: false,
SpoilerText: "",
Visibility: model.VisibilityPublic,
ScheduledAt: "",
Language: "en",
Format: model.StatusFormatPlain,
},
AdvancedVisibilityFlagsForm: model.AdvancedVisibilityFlagsForm{
Federated: nil,
Boostable: nil,
Replyable: nil,
Likeable: nil,
},
}
apiStatus, err := suite.status.Create(ctx, creatingAccount, creatingApplication, statusCreateForm)
suite.EqualError(err, "ProcessMediaIDs: description too short! media description of at least 100 chararacters is required but 15 was provided for media with id 01F8MH8RMYQ6MSNY3JM2XT1CQ5")
suite.Nil(apiStatus)
}
func TestStatusCreateTestSuite(t *testing.T) {
suite.Run(t, new(StatusCreateTestSuite))
}

View file

@ -61,7 +61,7 @@ type Processor interface {
ProcessVisibility(ctx context.Context, form *apimodel.AdvancedStatusCreateForm, accountDefaultVis gtsmodel.Visibility, status *gtsmodel.Status) error
ProcessReplyToID(ctx context.Context, form *apimodel.AdvancedStatusCreateForm, thisAccountID string, status *gtsmodel.Status) gtserror.WithCode
ProcessMediaIDs(ctx context.Context, form *apimodel.AdvancedStatusCreateForm, thisAccountID string, status *gtsmodel.Status) error
ProcessMediaIDs(ctx context.Context, form *apimodel.AdvancedStatusCreateForm, thisAccountID string, status *gtsmodel.Status) gtserror.WithCode
ProcessLanguage(ctx context.Context, form *apimodel.AdvancedStatusCreateForm, accountDefaultLanguage string, status *gtsmodel.Status) error
ProcessMentions(ctx context.Context, form *apimodel.AdvancedStatusCreateForm, accountID string, status *gtsmodel.Status) error
ProcessTags(ctx context.Context, form *apimodel.AdvancedStatusCreateForm, accountID string, status *gtsmodel.Status) error

View file

@ -25,6 +25,7 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/api/model"
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
@ -157,7 +158,7 @@ func (p *processor) ProcessReplyToID(ctx context.Context, form *apimodel.Advance
return nil
}
func (p *processor) ProcessMediaIDs(ctx context.Context, form *apimodel.AdvancedStatusCreateForm, thisAccountID string, status *gtsmodel.Status) error {
func (p *processor) ProcessMediaIDs(ctx context.Context, form *apimodel.AdvancedStatusCreateForm, thisAccountID string, status *gtsmodel.Status) gtserror.WithCode {
if form.MediaIDs == nil {
return nil
}
@ -167,15 +168,28 @@ func (p *processor) ProcessMediaIDs(ctx context.Context, form *apimodel.Advanced
for _, mediaID := range form.MediaIDs {
attachment, err := p.db.GetAttachmentByID(ctx, mediaID)
if err != nil {
return fmt.Errorf("ProcessMediaIDs: invalid media type or media not found for media id %s", mediaID)
if errors.Is(err, db.ErrNoEntries) {
err = fmt.Errorf("ProcessMediaIDs: media not found for media id %s", mediaID)
return gtserror.NewErrorBadRequest(err, err.Error())
}
err = fmt.Errorf("ProcessMediaIDs: db error for media id %s", mediaID)
return gtserror.NewErrorInternalError(err)
}
if attachment.AccountID != thisAccountID {
return fmt.Errorf("ProcessMediaIDs: media with id %s does not belong to account %s", mediaID, thisAccountID)
err = fmt.Errorf("ProcessMediaIDs: media with id %s does not belong to account %s", mediaID, thisAccountID)
return gtserror.NewErrorBadRequest(err, err.Error())
}
if attachment.StatusID != "" || attachment.ScheduledStatusID != "" {
return fmt.Errorf("ProcessMediaIDs: media with id %s is already attached to a status", mediaID)
err = fmt.Errorf("ProcessMediaIDs: media with id %s is already attached to a status", mediaID)
return gtserror.NewErrorBadRequest(err, err.Error())
}
minDescriptionChars := config.GetMediaDescriptionMinChars()
if descriptionLength := len([]rune(attachment.Description)); descriptionLength < minDescriptionChars {
err = fmt.Errorf("ProcessMediaIDs: description too short! media description of at least %d chararacters is required but %d was provided for media with id %s", minDescriptionChars, descriptionLength, mediaID)
return gtserror.NewErrorBadRequest(err, err.Error())
}
attachments = append(attachments, attachment)