Skip to content

Conversation

@nolramaf
Copy link
Contributor

@nolramaf nolramaf commented Sep 1, 2025

  • Introduce SAVE_VIDEO in S3 config
  • Added check for video messages before attempting S3 upload
  • Prevents unnecessary uploads when SAVE_VIDEO is disabled

Summary by Sourcery

Introduce a new SAVE_VIDEO configuration flag to prevent unnecessary S3 uploads for video messages when disabled, and enforce this check across message handling services and utilities.

New Features:

  • Add SAVE_VIDEO boolean to S3 configuration settings

Enhancements:

  • Skip S3 upload for video messages in Baileys and Business WhatsApp services when SAVE_VIDEO is false
  • Adjust getConversationMessage utility to exclude video media URLs if SAVE_VIDEO is disabled

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Sep 1, 2025

Reviewer's Guide

This PR introduces a new SAVE_VIDEO flag to the S3 configuration and updates message handling and upload paths to detect and skip video uploads when this flag is disabled, along with minor formatting cleanups.

Sequence diagram for video upload validation before S3 upload

sequenceDiagram
  participant Service as BaileysStartupService/BusinessStartupService
  participant Config as ConfigService
  participant S3 as S3
  participant S3Bucket as S3 Bucket

  Service->>Config: get<S3>("S3")
  Config-->>Service: S3 config (ENABLE, SAVE_VIDEO)
  Service->>Service: Check if message is video
  alt SAVE_VIDEO is false and message is video
    Service->>Service: Throw error 'Video upload is disabled.'
  else SAVE_VIDEO is true or not video
    Service->>S3Bucket: Upload media
  end
Loading

Class diagram for updated S3 configuration and message handling

classDiagram
  class ConfigService {
    +get<S3>(key: string): S3
    ...
  }
  class S3 {
    ENABLE: boolean
    SAVE_VIDEO: boolean
    ...
  }
  ConfigService --> S3

  class BaileysStartupService {
    +handleReceivedMessage(received)
    +handleSentMessage(messageSent)
    ...
  }
  BaileysStartupService --> ConfigService

  class BusinessStartupService {
    +uploadMedia(result)
    ...
  }
  BusinessStartupService --> ConfigService

  class getTypeMessage {
    +getTypeMessage(msg)
  }
  getTypeMessage --> ConfigService
Loading

File-Level Changes

Change Details Files
Add SAVE_VIDEO flag to S3 configuration
  • Extended S3 config type definition with SAVE_VIDEO
  • Loaded SAVE_VIDEO value from environment variables
src/config/env.config.ts
Guard video uploads based on SAVE_VIDEO
  • Detected videoMessage in incoming and outgoing handlers
  • Threw an error to abort S3 upload when SAVE_VIDEO is false
src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
src/api/integrations/channel/meta/whatsapp.business.service.ts
Respect SAVE_VIDEO in conversation message extraction
  • Wrapped mediaUrl assignment with SAVE_VIDEO check and video presence detection
  • Maintained fallback to key ID when skipping video URLs
src/utils/getConversationMessage.ts
Miscellaneous formatting and cleanup
  • Aligned qrcode log indentation
  • Reformatted business API URL string interpolation
  • Tidied up array mapping parentheses
src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
src/api/integrations/channel/meta/whatsapp.business.service.ts

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

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes - here's some feedback:

  • The if (isVideo && !SAVE_VIDEO) block is repeated in multiple places; consider extracting that logic into a shared helper or middleware to DRY up the upload flow.
  • The conditional logic in getConversationMessage around SAVE_VIDEO and media types is becoming complex—refactor or simplify it into smaller functions for readability and easier maintenance.
  • Instead of throwing a generic Error when video uploads are disabled, use a custom error class or error code so downstream logic can handle this case more explicitly.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The `if (isVideo && !SAVE_VIDEO)` block is repeated in multiple places; consider extracting that logic into a shared helper or middleware to DRY up the upload flow.
- The conditional logic in getConversationMessage around SAVE_VIDEO and media types is becoming complex—refactor or simplify it into smaller functions for readability and easier maintenance.
- Instead of throwing a generic Error when video uploads are disabled, use a custom error class or error code so downstream logic can handle this case more explicitly.

## Individual Comments

### Comment 1
<location> `src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts:1263` </location>
<code_context>
             if (isMedia) {
               if (this.configService.get<S3>('S3').ENABLE) {
                 try {
+                  if (isVideo && !this.configService.get<S3>('S3').SAVE_VIDEO) {
+                    throw new Error('Video upload is disabled.');
+                  }
+
</code_context>

<issue_to_address>
Throwing an error for disabled video upload may disrupt flow.

Instead of raising an exception, consider skipping video uploads and logging a warning to prevent service interruption for other media types.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
                  if (isVideo && !this.configService.get<S3>('S3').SAVE_VIDEO) {
                    throw new Error('Video upload is disabled.');
                  }
=======
                  if (isVideo && !this.configService.get<S3>('S3').SAVE_VIDEO) {
                    this.logger.warn('Video upload is disabled. Skipping video upload.');
                    // Skip video upload by returning early from this block
                    return;
                  }
>>>>>>> REPLACE

</suggested_fix>

### Comment 2
<location> `src/api/integrations/channel/meta/whatsapp.business.service.ts:462` </location>
<code_context>
                   mediaType = 'video';
                 }

+                if (mediaType == 'video' && !this.configService.get<S3>('S3').SAVE_VIDEO) {
+                  throw new Error('Video upload is disabled.');
+                }
+
</code_context>

<issue_to_address>
Error thrown for disabled video upload may affect user experience.

Instead of raising an exception, consider returning a user-friendly message or logging the event to avoid disrupting the user experience.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
                if (mediaType == 'video' && !this.configService.get<S3>('S3').SAVE_VIDEO) {
                  throw new Error('Video upload is disabled.');
                }
=======
                if (mediaType == 'video' && !this.configService.get<S3>('S3').SAVE_VIDEO) {
                  this.logger?.info?.('Video upload attempted but is disabled by configuration.');
                  return {
                    success: false,
                    message: 'Video upload is currently disabled. Please contact support if you need this feature enabled.',
                  };
                }
>>>>>>> REPLACE

</suggested_fix>

### Comment 3
<location> `src/config/env.config.ts:285` </location>
<code_context>
   USE_SSL?: boolean;
   REGION?: string;
   SKIP_POLICY?: boolean;
+  SAVE_VIDEO: boolean;
 };

</code_context>

<issue_to_address>
SAVE_VIDEO type should be optional for consistency.

Marking SAVE_VIDEO as optional aligns with the other S3 config properties and helps prevent issues with undefined values.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
  SAVE_VIDEO: boolean;
=======
  SAVE_VIDEO?: boolean;
>>>>>>> REPLACE

</suggested_fix>

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.

nolramaf and others added 3 commits September 1, 2025 19:49
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
@DavidsonGomes DavidsonGomes changed the base branch from main to develop September 2, 2025 13:02
@DavidsonGomes
Copy link
Collaborator

Please fix the lint with npm run lint

@DavidsonGomes DavidsonGomes merged commit 7ba8787 into EvolutionAPI:develop Sep 17, 2025
1 check failed
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