Skip to content

fix: normalize audio/opus content type to audio/ogg for WhatsApp attachments#223

Merged
gabrieljablonski merged 1 commit intomainfrom
gabrieljablonski/fix-whatsapp-audio-message
Feb 25, 2026
Merged

fix: normalize audio/opus content type to audio/ogg for WhatsApp attachments#223
gabrieljablonski merged 1 commit intomainfrom
gabrieljablonski/fix-whatsapp-audio-message

Conversation

@gabrieljablonski
Copy link
Member

@gabrieljablonski gabrieljablonski commented Feb 25, 2026

Pull Request Template

Description

Please include a summary of the change and issue(s) fixed. Also, mention relevant motivation, context, and any dependencies that this change requires.
Fixes # (issue)

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality not to work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration.

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented on my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

This change is Reviewable

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Fixed incorrect audio file type detection for OGG audio files. Files with .ogg extension are now properly identified and processed with the correct audio format, improving compatibility with audio attachments and messaging integrations.
  • Tests

    • Added test coverage for audio file type normalization to ensure proper handling of OGG audio files.

@gabrieljablonski gabrieljablonski self-assigned this Feb 25, 2026
Copilot AI review requested due to automatic review settings February 25, 2026 00:53
@coderabbitai
Copy link

coderabbitai bot commented Feb 25, 2026

📝 Walkthrough

Walkthrough

The pull request consolidates audio MIME type normalization logic from the WhatsApp service into the Attachment model and ActiveStorage initialization layer. A new helper method normalizes audio/opus to audio/ogg content types, invoked both when accessing download URLs and globally via an ActiveStorage patch, with the WhatsApp-specific normalization removed.

Changes

Cohort / File(s) Summary
Attachment Model Enhancement
app/models/attachment.rb, spec/models/attachment_spec.rb
Added normalize_opus_blob_content_type! helper method to lazily update blob content types for OGG files, invoked before generating download URLs. Test verifies normalization occurs when retrieving attachment URLs.
WhatsApp Service Cleanup
app/services/whatsapp/providers/whatsapp_cloud_service.rb, spec/services/whatsapp/providers/whatsapp_cloud_service_spec.rb
Removed normalize_opus_content_type method and its invocation from send_attachment_message. Test updated to verify normalization still occurs via the centralized Attachment layer rather than service-specific logic.
ActiveStorage Initialization
config/initializers/active_storage_opus_fix.rb
New initializer that patches ActiveStorage::Blob to override identify_content_type, normalizing audio/opus to audio/ogg when filenames end with .ogg extension, providing global normalization coverage.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

🐰 From service methods, the logic now springs,
Consolidated clean in model and Blob things,
OGG files normalize with grace,
ActiveStorage patches embrace,
A rabbit hops happily through cleaner code lanes! 🎵

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main objective: normalizing audio/opus content type to audio/ogg for WhatsApp attachments, which is reflected across all changed files.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch gabrieljablonski/fix-whatsapp-audio-message

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@gabrieljablonski
Copy link
Member Author

@coderabbitai review

@coderabbitai
Copy link

coderabbitai bot commented Feb 25, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes an issue where the Marcel gem detects OGG files with Opus codec as audio/opus instead of audio/ogg, causing problems with WhatsApp Cloud API which expects audio/ogg for OGG container files. The fix implements a two-pronged approach: an ActiveStorage initializer to prevent new files from being persisted with the wrong content type, and lazy normalization in the Attachment model to fix existing blobs.

Changes:

  • Added an ActiveStorage initializer to normalize audio/opus to audio/ogg during content type identification for .ogg files
  • Moved content type normalization from WhatsApp service to the Attachment model's download_url method for broader applicability
  • Removed the service-specific normalization method from WhatsApp Cloud service

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
config/initializers/active_storage_opus_fix.rb New initializer that prepends a module to ActiveStorage::Blob to normalize audio/opus → audio/ogg during content type identification for .ogg files
app/models/attachment.rb Added normalize_opus_blob_content_type! method and integrated it into download_url to lazily normalize existing blobs
app/services/whatsapp/providers/whatsapp_cloud_service.rb Removed the normalize_opus_content_type method as normalization now happens in the Attachment model
spec/models/attachment_spec.rb Added test to verify audio/opus normalization to audio/ogg when accessing download_url
spec/services/whatsapp/providers/whatsapp_cloud_service_spec.rb Added test to verify normalization works correctly with WhatsApp voice messages

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@app/models/attachment.rb`:
- Around line 191-199: The check in normalize_opus_blob_content_type! currently
uses blob.filename.to_s.end_with?('.ogg') which is case-sensitive and will miss
CAPITALIZED extensions; change the conditional to perform a case-insensitive
extension check (for example compare a downcased filename or use
case-insensitive extname comparison) so that filenames like "sample.OGG" are
detected and blob.update_column(:content_type, 'audio/ogg') still runs for those
cases.

ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bce4e9b and d2ba841.

📒 Files selected for processing (5)
  • app/models/attachment.rb
  • app/services/whatsapp/providers/whatsapp_cloud_service.rb
  • config/initializers/active_storage_opus_fix.rb
  • spec/models/attachment_spec.rb
  • spec/services/whatsapp/providers/whatsapp_cloud_service_spec.rb
💤 Files with no reviewable changes (1)
  • app/services/whatsapp/providers/whatsapp_cloud_service.rb

@gabrieljablonski gabrieljablonski merged commit 72354a4 into main Feb 25, 2026
6 checks passed
@gabrieljablonski gabrieljablonski deleted the gabrieljablonski/fix-whatsapp-audio-message branch February 25, 2026 01:01
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.

2 participants