Skip to content

fix(sticker): send animated WebP stickers as-is (skip static re-encode) - #151

Open
nicolasnovis wants to merge 1 commit into
evolution-foundation:mainfrom
nicolasnovis:pr/animated-sticker
Open

fix(sticker): send animated WebP stickers as-is (skip static re-encode)#151
nicolasnovis wants to merge 1 commit into
evolution-foundation:mainfrom
nicolasnovis:pr/animated-sticker

Conversation

@nicolasnovis

@nicolasnovis nicolasnovis commented Jul 31, 2026

Copy link
Copy Markdown

SendSticker always runs convertToWebP, which decodes the image via webpDecodeRGBA and fails on animated WebP (webpDecodeRGBA: failed), so sending an animated sticker errors out.

This uploads the sticker untouched when it is already WebP (skipping the lossy re-encode), detects animation (VP8X animation flag / ANIM chunk) and sets IsAnimated accordingly, and pins the mimetype to image/webp. Static and animated WebP stickers both send correctly; non-WebP inputs still go through convertToWebP as before.

Tested in production against WhatsApp with both static and animated .webp stickers.

Summary by Sourcery

Handle sending WebP stickers by uploading existing WebP data untouched, only re-encoding non-WebP sources to WebP, and correctly marking animated stickers.

Bug Fixes:

  • Prevent failures when sending animated WebP stickers by avoiding re-encoding through a decoder that cannot handle animation.

Enhancements:

  • Detect WebP format and animation when fetching sticker data from a URL and set the sticker mimetype and IsAnimated flag appropriately.

SendSticker always ran convertToWebP (image.Decode + static webp.Encode), which
fails on animated WebP (webpDecodeRGBA: failed) and would flatten it to one frame
anyway. Now: download the sticker, and if it's already WebP (RIFF/WEBP magic),
upload it UNTOUCHED and set StickerMessage.IsAnimated when the VP8X animation flag
(or ANIM chunk) is present; only re-encode non-WebP sources. Static WebP stickers
also stop losing quality. No route/struct/JSON-contract change.
@sourcery-ai

sourcery-ai Bot commented Jul 31, 2026

Copy link
Copy Markdown

Reviewer's Guide

Refactors sticker sending to avoid re-encoding existing WebP data, properly handle animated WebP stickers, and simplify the upload path, while explicitly setting WebP mimetype and animation flag.

Sequence diagram for updated SendSticker WebP handling

sequenceDiagram
  actor User
  participant sendService
  participant HTTPServer
  participant WhatsAppClient as whatsmeowClient
  participant waE2E as waE2EMessage

  User->>sendService: SendSticker(data, instance)
  sendService->>sendService: ensureClientConnected(instance.Id)
  sendService->>HTTPServer: http.Get(data.Sticker)
  HTTPServer-->>sendService: *resp.Body*
  sendService->>sendService: io.ReadAll(resp.Body)
  alt isWebP(raw)
    sendService->>sendService: isWebP(raw)
    sendService->>sendService: isAnimatedWebP(raw)
    note over sendService: filedata = raw
  else nonWebP
    sendService->>sendService: convertBytesToWebP(raw)
    note over sendService: filedata = converted WebP
  end
  sendService->>WhatsAppClient: Upload(context.Background(), filedata, whatsmeow.MediaImage)
  WhatsAppClient-->>sendService: UploadResponse
  sendService->>waE2E: new StickerMessage
  note over waE2E: Mimetype = image/webp
  alt isAnimated == true
    note over waE2E: IsAnimated = true
  end
  sendService->>sendService: SendMessage(instance, Message, "StickerMessage", SendDataStruct)
  sendService-->>User: MessageSendStruct
Loading

File-Level Changes

Change Details Files
Add in-memory image-to-WebP conversion and WebP/animated-WebP detection helpers to separate concerns and enable conditional re-encoding.
  • Introduce convertBytesToWebP to decode non-WebP image bytes and encode them as WebP with configured quality settings.
  • Add isWebP helper that checks RIFF/WEBP magic bytes to identify WebP inputs.
  • Add isAnimatedWebP helper that detects animation via VP8X animation flag and ANIM chunk presence, using isWebP as a guard.
pkg/sendMessage/service/send_service.go
Rework SendSticker to always fetch the sticker bytes, only re-encode when input is not WebP, upload the resulting WebP, and set mimetype and animation metadata explicitly.
  • Replace the previous URL-conditional convertToWebP path with an early URL validation check and an HTTP GET to fetch sticker bytes unconditionally for valid URLs.
  • Use isWebP to bypass re-encoding for existing WebP stickers and isAnimatedWebP to determine if the sticker is animated, storing the result in a local isAnimated flag.
  • Fallback to convertBytesToWebP for non-WebP inputs, propagating errors when conversion fails.
  • Upload the prepared filedata via client.Upload and handle errors, storing the response in uploaded.
  • Build a StickerMessage struct with fixed mimetype "image/webp" and set IsAnimated=true when appropriate before sending the message via SendMessage.
pkg/sendMessage/service/send_service.go

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • Consider using an HTTP client with timeouts and/or the existing request context instead of bare http.Get, so sticker fetches can’t hang indefinitely and can be cancelled along with the send operation.
  • The WebP detection helpers currently convert header slices to strings; you could avoid unnecessary allocations and be more explicit by using byte comparisons (e.g. bytes.Equal(data[0:4], []byte("RIFF"))) for magic and chunk IDs.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider using an HTTP client with timeouts and/or the existing request context instead of bare `http.Get`, so sticker fetches can’t hang indefinitely and can be cancelled along with the send operation.
- The WebP detection helpers currently convert header slices to strings; you could avoid unnecessary allocations and be more explicit by using byte comparisons (e.g. `bytes.Equal(data[0:4], []byte("RIFF"))`) for magic and chunk IDs.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant