Skip to content

Commit

Permalink
mediaOverflowError
Browse files Browse the repository at this point in the history
  • Loading branch information
nkonev committed Jan 25, 2025
1 parent 397a281 commit 325792d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
12 changes: 11 additions & 1 deletion chat/handlers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,15 @@ func (s *MediaUrlErr) Error() string {
return fmt.Sprintf("Media url is not allowed in %v: %v", s.where, s.url)
}

type MediaOverflowErr struct {
allowed int
given int
}

func (s *MediaOverflowErr) Error() string {
return fmt.Sprintf("Too many medias: allowed %v, given %v", s.allowed, s.given)
}

func TrimAmdSanitizeMessage(ctx context.Context, lgr *logger.Logger, policy *services.SanitizerPolicy, input string) (string, error) {
sanitizedHtml := Trim(SanitizeMessage(policy, input))

Expand Down Expand Up @@ -267,7 +276,8 @@ func TrimAmdSanitizeMessage(ctx context.Context, lgr *logger.Logger, policy *ser
}

if mediaCount > maxMediasCount {
return "", errors.New("Too many medias")
retErr = &MediaOverflowErr{maxMediasCount, mediaCount}
return "", retErr
}

doc.Find("a").Each(func(i int, s *goquery.Selection) {
Expand Down
9 changes: 9 additions & 0 deletions chat/handlers/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,10 @@ func (mc *MessageHandler) PostMessage(c echo.Context) error {
if errors.As(errOuter, &mediaError) {
return c.JSON(http.StatusBadRequest, &utils.H{"message": mediaError.Error(), "businessErrorCode": badMediaUrl})
}
var mediaOverflowError *MediaOverflowErr
if errors.As(errOuter, &mediaOverflowError) {
return c.JSON(http.StatusBadRequest, &utils.H{"message": mediaOverflowError.Error()})
}
var npe *notParticipantError
if errors.As(errOuter, &npe) {
return c.JSON(http.StatusBadRequest, &utils.H{"message": "You are not allowed to write to this chat"})
Expand Down Expand Up @@ -1095,6 +1099,11 @@ func (mc *MessageHandler) EditMessage(c echo.Context) error {
return c.JSON(http.StatusBadRequest, &utils.H{"message": mediaError.Error(), "businessErrorCode": badMediaUrl})
}

var mediaOverflowError *MediaOverflowErr
if errors.As(errOuter, &mediaOverflowError) {
return c.JSON(http.StatusBadRequest, &utils.H{"message": mediaOverflowError.Error()})
}

mc.lgr.WithTracing(c.Request().Context()).Errorf("Error during act transaction %v", errOuter)
return errOuter
}
Expand Down

0 comments on commit 325792d

Please sign in to comment.