8000 feat: add flag to edited messages and content history tracking by CayoPOliveira · Pull Request #35 · fazer-ai/chatwoot · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: add flag to edited messages and content history tracking #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 6, 2025

Conversation

CayoPOliveira
Copy link
Collaborator
@CayoPOliveira CayoPOliveira commented Apr 30, 2025

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

  • New Features
    • Messages now indicate if they have been edited and display a history of previous content versions.
  • Bug Fixes
    • Improved handling of edited messages to ensure content updates and edit history are accurately maintained.
  • Tests
    • Enhanced test coverage for message editing, verifying the edited status and preservation of previous content.

Copy link
coderabbitai bot commented Apr 30, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

This change adds is_edited and previous_content attributes to the content_attributes JSON store in the Message model for tracking message edits and the content before editing. The WhatsApp incoming message service method is renamed and refactored to update message content, set is_edited to true, and save the previous content. Tests are enhanced to verify these new behaviors.

Changes

File(s) Change Summary
app/models/message.rb Added :is_edited and :previous_content accessors to the content_attributes JSON store with descriptive comments.
app/services/whatsapp/incoming_message_baileys_service.rb Renamed update_message_content to handle_edited_content; refactored logic to update message content, set is_edited, and save previous_content.
spec/services/whatsapp/incoming_message_baileys_service_spec.rb Extended existing test to verify that is_edited is set to true and previous_content stores the original content after update.

Possibly related PRs

Poem

A message once sent, now changed with care,
The rabbit notes edits with flair.
Flags raised high, and content saved,
So no word is lost or waived.
Hopping through code with joy and glee,
Keeping messages true as can be! 🐇✨

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 RuboCop (1.73)
spec/services/whatsapp/incoming_message_baileys_service_spec.rb

Error: RSpec/FactoryBot/* has been extracted to the rubocop-factory_bot gem.
(obsolete configuration found in .rubocop.yml, please update it)


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5f6889d and 2825570.

📒 Files selected for processing (1)
  • spec/services/whatsapp/incoming_message_baileys_service_spec.rb (1 hunks)
✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
@Copilot 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 introduces a flag to indicate when a message has been edited and adds tracking for the message’s content history. Key changes include:

  • Modifications to the WhatsApp incoming message service to update and store the edit state and content history.
  • Updates to the tests to verify the new edited message functionality.
  • Changes to the message model to support the new attributes in the content_attributes store.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
spec/services/whatsapp/incoming_message_baileys_service_spec.rb Added test cases to validate edited message content updates and history.
app/services/whatsapp/incoming_message_baileys_service.rb Updated service logic to set is_edited, manage content history and update content.
app/models/message.rb Extended the store attributes to include is_edited and content_history.

message = @raw_message.dig(:update, :message, :editedMessage, :message)
if message.blank?
@raw_message = @raw_message.dig(:update, :message, :editedMessage)
if @raw_message[:message].blank?
Copy link
Preview
Copilot AI Apr 30, 2025

Choose a reason for hiding this comment

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

The code assumes @raw_message is not nil before checking @raw_message[:message]. This might lead to a NoMethodError if the dig returns nil; add an explicit nil check for @raw_message.

Suggested change
if @raw_message[:message].blank?
if @raw_message.nil? || @raw_message[:message].blank?

Copilot uses AI. Check for mistakes.

Comment on lines 322 to 323
@raw_message = @raw_message.dig(:update, :message, :editedMessage)
if @raw_message[:message].blank?
Copy link
Preview
Copilot AI Apr 30, 2025

Choose a reason for hiding this comment

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

[nitpick] Reassigning @raw_message to a nested value can obscure the original data and impact readability. Consider using a local variable to store the nested message data instead.

Suggested change
@raw_message = @raw_message.dig(:update, :message, :editedMessage)
if @raw_message[:message].blank?
edited_message = @raw_message.dig(:update, :message, :editedMessage)
if edited_message[:message].blank?

Copilot uses AI. Check for mistakes.

Base automatically changed from fix/ignore-baileys-protocolMessage to fazer-ai/main May 1, 2025 00:33
Copy link
@gabrieljablonski gabrieljablonski left a comment

Choose a reason for hiding this comment

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

Reviewed 3 of 3 files at r1, all commit messages.
Reviewable status: all files reviewed, 8 unresolved discussions (waiting on @CayoPOliveira)


app/services/whatsapp/incoming_message_baileys_service.rb line 321 at r1 (raw file):

  end

  def update_message_content

this name seems clearer, WDYT

Suggestion:

  def handle_edited_message

app/services/whatsapp/incoming_message_baileys_service.rb line 338 at r1 (raw file):

    end
    @message.update!(content: content)
  end

Please review the whole logic for this function, i believe it's unnecessarily complex.


app/models/message.rb line 111 at r1 (raw file):

                                         :external_created_at, :story_sender, :story_id, :external_error,
                                         :translations, :in_reply_to_external_id, :is_unsupported,
                                         :is_reaction, :is_edited, :content_history], coder: JSON

Don't forget to refactor names.

Suggestion:

  # [:is_edited] : Used to indicated edited message and edit history

  store :content_attributes, accessors: [:submitted_email, :items, :submitted_values, :email, :in_reply_to, :deleted,
                                         :external_created_at, :story_sender, :story_id, :external_error,
                                         :translations, :in_reply_to_external_id, :is_unsupported,
                                         :is_reaction, :is_edited, :edit_history], coder: JSON

spec/services/whatsapp/incoming_message_baileys_service_spec.rb line 731 at r1 (raw file):

          expect(message.content_history).to be_nil

          described_class.new(inbox: inbox, params: params).perform

Not needed.

Suggestion:

          }

          described_class.new(inbox: inbox, params: params).perform

spec/services/whatsapp/incoming_message_baileys_service_spec.rb line 738 at r1 (raw file):

        end

        it 'updates the message content for an already edited message' do

NIT

Suggestion:

        it 'updates the message content and the edit history' do

spec/services/whatsapp/incoming_message_baileys_service_spec.rb line 753 at r1 (raw file):

          }

          expect(message.content_attributes).to eq({ 'is_edited' => true, 'content_history' => ['Old message content'] })

Why check this?

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: 0

♻️ Duplicate comments (1)
app/services/whatsapp/incoming_message_baileys_service.rb (1)

322-323: ⚠️ Potential issue

Add null check before accessing nested structures

The code directly reassigns @raw_message to a deeply nested value without first checking if the intermediate objects exist. This could lead to a NoMethodError if any part of the path is nil.

-    @raw_message = @raw_message.dig(:update, :message, :editedMessage)
-    if @raw_message[:message].blank?
+    edited_message = @raw_message.dig(:update, :message, :editedMessage)
+    if edited_message.nil? || edited_message[:message].blank?
🧹 Nitpick comments (1)
app/services/whatsapp/incoming_message_baileys_service.rb (1)

331-337: Well-structured message edit history tracking

The implementation correctly sets the edit flag and manages the content history array, including handling both initial edits and subsequent edits elegantly.

However, consider adding a maximum limit to the history array to prevent unlimited growth over time.

 @message.is_edited = true
 if @message.content_history.blank?
   @message.content_history = [@message.content]
 else
+  # Prevent unlimited growth of history array
+  @message.content_history = @message.content_history.last(9) if @message.content_history.size >= 10
   @message.content_history.push(@message.content)
 end
 @message.update!(content: content)
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a52a4cf and 7fde42d.

📒 Files selected for processing (3)
  • app/models/message.rb (1 hunks)
  • app/services/whatsapp/incoming_message_baileys_service.rb (1 hunks)
  • spec/services/whatsapp/incoming_message_baileys_service_spec.rb (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
app/services/whatsapp/incoming_message_baileys_service.rb (1)
app/models/message.rb (1)
  • content (189-194)
🔇 Additional comments (5)
app/models/message.rb (2)

105-107: Good documentation of the new attributes

The comments clearly explain the purpose of the new is_edited and content_history attributes.


110-111: LGTM: Properly extending the content_attributes store

The new attributes are correctly added to the existing content_attributes JSON store, allowing them to be accessed and modified like other message attributes.

app/services/whatsapp/incoming_message_baileys_service.rb (1)

328-329: LGTM: Good defensive check for content

Properly validating that content exists before proceeding with the update.

spec/services/whatsapp/incoming_message_baileys_service_spec.rb (2)

728-736: LGTM: Good test coverage for initial edit behavior

The test case properly verifies that the message is marked as edited and the original content is stored in the history when a message is edited for the first time.


738-759: LGTM: Comprehensive test for multiple edits

Good test case that verifies the correct behavior when a message that has already been edited is edited again, ensuring the content history accumulates properly.

Copy link
Collaborator Author
@CayoPOliveira CayoPOliveira left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 3 files at r1, 2 of 2 files at r3, all commit messages.
Reviewable status: all files reviewed, 4 unresolved discussions (waiting on @gabrieljablonski)


app/models/message.rb line 111 at r1 (raw file):

Previously, gabrieljablonski (Gabriel Jablonski) wrote…

Don't forget to refactor names.

Done.


app/services/whatsapp/incoming_message_baileys_service.rb line 321 at r1 (raw file):

Previously, gabrieljablonski (Gabriel Jablonski) wrote…

this name seems clearer, WDYT

Done

Ok


app/services/whatsapp/incoming_message_baileys_service.rb line 338 at r1 (raw file):

Previously, gabrieljablonski (Gabriel Jablonski) wrote…

Please review the whole logic for this function, i believe it's unnecessarily complex.

Done.


spec/services/whatsapp/incoming_message_baileys_service_spec.rb line 731 at r1 (raw file):

Previously, gabrieljablonski (Gabriel Jablonski) wrote…

Not needed.

Done.


spec/services/whatsapp/incoming_message_baileys_service_spec.rb line 753 at r1 (raw file):

Previously, gabrieljablonski (Gabriel Jablonski) wrote…

Why check this?

LGTM Is for test the message.update was done, but I can take away

Copy link
Collaborator Author
@CayoPOliveira CayoPOliveira left a comment

Choose a reason for hiding this comment

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

-(questions/changes requested) +(review needed)

Reviewable status: 0 of 3 files reviewed, 4 unresolved discussions (waiting on @gabrieljablonski)

Copy link
@gabrieljablonski gabrieljablonski left a comment

Choose a reason for hiding this comment

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

Reviewed 2 of 3 files at r4, 3 of 3 files at r5, 1 of 1 files at r6, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @CayoPOliveira)

@CayoPOliveira CayoPOliveira merged commit 8d5a6bc into fazer-ai/main May 6, 2025
0 of 2 checks passed
@CayoPOliveira CayoPOliveira deleted the feat/mark-edited-messages branch May 6, 2025 19:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved for merge enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0