Skip to content
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

fix: Update Conversation List when new conversation is streamed #1333

Merged
merged 1 commit into from
Dec 10, 2024

Conversation

alexrisch
Copy link
Contributor

@alexrisch alexrisch commented Dec 10, 2024

Add new conversation to list when streamed
Fix content type errors

Summary by CodeRabbit

  • New Features

    • Enhanced error handling for fetching spam scores and conversation updates.
    • Added functions for updating group image, name, and description in conversation lists.
    • Introduced a new method for adding conversations to the conversation list.
  • Bug Fixes

    • Improved robustness of content type handling to prevent errors with undefined values.
  • Documentation

    • Updated method signatures for clarity on parameter types.

Add new conversation to list when streamed
Fix content type errors
@alexrisch alexrisch requested a review from a team as a code owner December 10, 2024 00:13
Copy link
Contributor

coderabbitai bot commented Dec 10, 2024

Walkthrough

The changes in this pull request involve modifications across several files, primarily enhancing error handling and control flow in functions related to conversation data management. The useV3RequestItems hook has improved spam score calculation and error handling, while the conversation list query functions now check for existing data before proceeding with updates. Additionally, the getMessageContentType function has been updated to handle undefined parameters more robustly. A new method for adding conversations to the list has also been introduced.

Changes

File Path Change Summary
features/conversation-requests-list/useV3RequestItems.tsx - Added logger utility import.
- Improved error handling in fetchGroups function, refining spam score calculation and content handling.
queries/useV3ConversationListQuery.ts - Updated updateConversationDataToConversationListQuery to check for previousConversationsData before updates.
- Added functions for updating group image, name, and description.
- Retained mapping logic for updates.
utils/xmtpRN/contentTypes/index.ts - Updated getMessageContentType function parameter type from string to `string
utils/xmtpRN/conversations.ts - Added import for addConversationToConversationListQuery.
- Replaced commented-out line with a call to addConversationToConversationListQuery in streamConversations function.

Possibly related PRs

  • fix: Sync Account on Add #1104: The changes in the main PR regarding the useV3RequestItems hook's error handling and spam score calculation may relate to the updates in updateConversationDataToConversationListQuery, which also enhances error handling when managing conversation data.
  • fix: Frames Renders on initial Send #1113: The modifications in the main PR to improve error handling in the fetchGroups function could be connected to the changes in the Chat.tsx component, which also focuses on improving the handling of conversation data.
  • fix: Group Admin Table  #1250: The updates in the main PR to the useV3RequestItems hook may relate to the changes in the group metadata handling in useGroupMetadataToConversationListQuery, as both involve managing conversation-related data.
  • Mark conversation as read when entering chat #1311: The main PR's focus on marking conversations as read when accessed aligns with the changes in the V3Conversation.tsx component, which also enhances the handling of message read statuses.
  • fix: Update List Group Metadata #1313: The updates in the main PR to improve the handling of group metadata updates may relate to the changes in handleGroupUpdatedMessage, which also focuses on managing group metadata effectively.

Suggested labels

2.0.7, 3.0.0

Suggested reviewers

  • thierryskoda

Poem

🐇 In the meadow, where data flows,
Enhancements bloom like springtime's rose.
With error checks and scores refined,
Conversations thrive, all well aligned.
A hop, a skip, through code we prance,
Celebrating changes, let’s take a chance! 🌼


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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.

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. (Experiment)
  • @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
Contributor

Performance Comparison Report

  • Current: 2df41a4 - 2024-12-10 00:18:11Z
  • Baseline: release/3.0.0 (02a23bd) - 2024-12-10 00:16:51Z

Significant Changes To Duration

There are no entries

Meaningless Changes To Duration

Show entries
Name Type Duration Count
Avatar Image 10 runs render 1.2 ms → 1.0 ms (-0.2 ms, -16.7%) 🟢 1 → 1
Avatar Image 50 runs render 1.1 ms → 1.0 ms (-0.0 ms, -1.9%) 1 → 1
Empty Avatar 10 runs render 0.9 ms → 1.0 ms (+0.1 ms, +11.1%) 1 → 1
Empty Avatar 50 runs render 0.8 ms → 0.7 ms (-0.1 ms, -16.7%) 🟢 1 → 1
Text Component with color prop - 10 runs render 0.4 ms → 0.1 ms (-0.3 ms, -75.0%) 🟢 1 → 1
Text Component with default props - 10 runs render 0.5 ms → 0.2 ms (-0.3 ms, -60.0%) 🟢 1 → 1
Text Component with translation key - 10 runs render 0.3 ms → 0.2 ms (-0.1 ms, -33.3%) 🟢 1 → 1
Text Component with weight and size - 10 runs render 0.4 ms → 0.3 ms (-0.1 ms, -25.0%) 🟢 1 → 1
Show details
Name Type Duration Count
Avatar Image 10 runs render Baseline
Mean: 1.2 ms
Stdev: 0.6 ms (52.7%)
Runs: 1 1 1 1 2 1 0 2 1 2
Warmup runs: 2

Current
Mean: 1.0 ms
Stdev: 0.5 ms (47.1%)
Runs: 0 1 1 1 1 1 1 1 2 1
Warmup runs: 2
Baseline
Mean: 1
Stdev: 0 (0.0%)
Runs: 1 1 1 1 1 1 1 1 1 1
Render issues:

Current
Mean: 1
Stdev: 0 (0.0%)
Runs: 1 1 1 1 1 1 1 1 1 1
Render issues:
Avatar Image 50 runs render Baseline
Mean: 1.1 ms
Stdev: 0.2 ms (22.6%)
Runs: 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1
Warmup runs: 1

Current
Mean: 1.0 ms
Stdev: 0.2 ms (19.0%)
Runs: 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Warmup runs: 1
Baseline
Mean: 1
Stdev: 0 (0.0%)
Runs: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Render issues:

Current
Mean: 1
Stdev: 0 (0.0%)
Runs: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Render issues:
Empty Avatar 10 runs render Baseline
Mean: 0.9 ms
Stdev: 0.3 ms (35.1%)
Runs: 1 1 0 1 1 1 1 1 1 1
Warmup runs: 3

Current
Mean: 1.0 ms
Stdev: 0.5 ms (47.1%)
Runs: 1 1 1 0 2 1 1 1 1 1
Warmup runs: 2
Baseline
Mean: 1
Stdev: 0 (0.0%)
Runs: 1 1 1 1 1 1 1 1 1 1
Render issues:

Current
Mean: 1
Stdev: 0 (0.0%)
Runs: 1 1 1 1 1 1 1 1 1 1
Render issues:
Empty Avatar 50 runs render Baseline
Mean: 0.8 ms
Stdev: 0.4 ms (44.1%)
Runs: 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 0 1 1 0 1 1 1 1 1 1 0 0
Warmup runs: 1

Current
Mean: 0.7 ms
Stdev: 0.5 ms (66.1%)
Runs: 1 1 0 1 1 1 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 1 1 0 1 0 0 0 1 1 0 1 1 1 0 1 1 1 1 0 1
Warmup runs: 1
Baseline
Mean: 1
Stdev: 0 (0.0%)
Runs: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Render issues:

Current
Mean: 1
Stdev: 0 (0.0%)
Runs: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Render issues:
Text Component with color prop - 10 runs render Baseline
Mean: 0.4 ms
Stdev: 0.5 ms (129.1%)
Runs: 0 0 0 1 1 1 0 1 0 0
Warmup runs: 1

Current
Mean: 0.1 ms
Stdev: 0.3 ms (316.2%)
Runs: 0 0 0 0 0 0 1 0 0 0
Warmup runs: 1
Baseline
Mean: 1
Stdev: 0 (0.0%)
Runs: 1 1 1 1 1 1 1 1 1 1
Render issues:

Current
Mean: 1
Stdev: 0 (0.0%)
Runs: 1 1 1 1 1 1 1 1 1 1
Render issues:
Text Component with default props - 10 runs render Baseline
Mean: 0.5 ms
Stdev: 0.5 ms (105.4%)
Runs: 0 1 1 1 1 0 1 0 0 0
Warmup runs: 0

Current
Mean: 0.2 ms
Stdev: 0.4 ms (210.8%)
Runs: 0 1 0 0 1 0 0 0 0 0
Warmup runs: 0
Baseline
Mean: 1
Stdev: 0 (0.0%)
Runs: 1 1 1 1 1 1 1 1 1 1
Render issues:

Current
Mean: 1
Stdev: 0 (0.0%)
Runs: 1 1 1 1 1 1 1 1 1 1
Render issues:
Text Component with translation key - 10 runs render Baseline
Mean: 0.3 ms
Stdev: 0.5 ms (161.0%)
Runs: 0 0 0 0 0 0 0 1 1 1
Warmup runs: 1

Current
Mean: 0.2 ms
Stdev: 0.4 ms (210.8%)
Runs: 0 1 0 0 0 1 0 0 0 0
Warmup runs: 1
Baseline
Mean: 1
Stdev: 0 (0.0%)
Runs: 1 1 1 1 1 1 1 1 1 1
Render issues:

Current
Mean: 1
Stdev: 0 (0.0%)
Runs: 1 1 1 1 1 1 1 1 1 1
Render issues:
Text Component with weight and size - 10 runs render Baseline
Mean: 0.4 ms
Stdev: 0.5 ms (129.1%)
Runs: 1 1 0 0 0 1 0 1 0 0
Warmup runs: 0

Current
Mean: 0.3 ms
Stdev: 0.5 ms (161.0%)
Runs: 1 0 0 1 0 0 0 1 0 0
Warmup runs: 0
Baseline
Mean: 1
Stdev: 0 (0.0%)
Runs: 1 1 1 1 1 1 1 1 1 1
Render issues:

Current
Mean: 1
Stdev: 0 (0.0%)
Runs: 1 1 1 1 1 1 1 1 1 1
Render issues:

Render Count Changes

There are no entries

Render Issues

There are no entries

Added Scenarios

There are no entries

Removed Scenarios

There are no entries

Generated by 🚫 dangerJS against 2697d0e

Copy link
Contributor

@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: 2

🧹 Outside diff range and nitpick comments (1)
features/conversation-requests-list/useV3RequestItems.tsx (1)

51-61: Consider caching spam scores for performance

Since spam score calculation is an expensive operation that could be called frequently, consider caching the results.

Example implementation:

+ const spamScoreCache = new Map<string, number>();
+ 
  let spamScore = 0;
  try {
    if (contentType) {
+     const cacheKey = `${content}-${contentType}`;
+     if (spamScoreCache.has(cacheKey)) {
+       spamScore = spamScoreCache.get(cacheKey)!;
+     } else {
        spamScore = await getV3SpamScore({
          message: content,
          contentType,
        });
+       spamScoreCache.set(cacheKey, spamScore);
+     }
    }
  } catch (error) {
    logger.error("Couldn't get spam score", error);
  }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 02a23bd and 2697d0e.

📒 Files selected for processing (4)
  • features/conversation-requests-list/useV3RequestItems.tsx (2 hunks)
  • queries/useV3ConversationListQuery.ts (1 hunks)
  • utils/xmtpRN/contentTypes/index.ts (1 hunks)
  • utils/xmtpRN/conversations.ts (1 hunks)
🧰 Additional context used
📓 Learnings (1)
queries/useV3ConversationListQuery.ts (1)
Learnt from: alexrisch
PR: ephemeraHQ/converse-app#1313
File: queries/useV3ConversationListQuery.ts:196-268
Timestamp: 2024-12-05T18:33:24.472Z
Learning: In the TypeScript file `queries/useV3ConversationListQuery.ts`, helper functions like `updateGroupImageToConversationListQuery`, `updateGroupNameToConversationListQuery`, and `updateGroupDescriptionToConversationListQuery` are used to reduce dependency on exact types or creating objects that get passed unnecessarily, even though they internally use `updateGroupMetadataToConversationListQuery`.
🔇 Additional comments (6)
utils/xmtpRN/contentTypes/index.ts (1)

29-32: LGTM! Robust null check added to getMessageContentType

The addition of undefined handling improves the function's robustness and helps prevent runtime errors when processing messages without content types.

Let's verify all callers handle the undefined return value appropriately:

✅ Verification successful

Undefined return value is handled appropriately in all callers

All usages of getMessageContentType have been verified and are safe:

  • In notificationContent.ts: Safely checks truthiness with !! and has early return
  • In useV3RequestItems.tsx: Explicitly checks if (contentType) before using
  • In message-utils.tsx: All comparisons with string literals (=== "text" etc.) are safe with undefined
  • In useMessageText.ts: Safe comparison with string literal
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for all usages of getMessageContentType
ast-grep --pattern 'getMessageContentType($_)' -A 3

Length of output: 4425

features/conversation-requests-list/useV3RequestItems.tsx (1)

1-1: LGTM! Improved error handling in spam score calculation

Good improvements:

  • Safe initialization of spamScore
  • Proper error handling with logging
  • Defensive check for contentType existence

Also applies to: 51-61

queries/useV3ConversationListQuery.ts (1)

170-170: LGTM! Added null check for conversation data

Good defensive programming practice to check for existence of previous conversations data.

utils/xmtpRN/conversations.ts (3)

17-17: LGTM: Import addition aligns with the new conversation list update functionality

The import is correctly placed and follows the project's import conventions.


25-25: Consider consistent conversation list updates across the codebase

Other conversation creation/management functions might need similar updates to maintain consistency with the new query-based approach. For example, createConversationByAccount has commented-out code that used the old handleNewConversation approach.


25-25: Verify query performance in streaming scenarios

The new query-based update mechanism needs verification for performance in high-frequency streaming scenarios.

✅ Verification successful

Let me gather more information about the query implementation and caching mechanism.


Let me check one more aspect regarding the query client implementation and any potential optimizations.


Based on the analysis of the codebase, I can now provide a final response regarding the query performance in streaming scenarios.

Performance optimizations are already in place for streaming scenarios

The implementation includes several performance safeguards:

  • Uses TanStack Query's built-in caching and state management
  • Implements structural sharing optimization (disabled to prevent unnecessary rerenders)
  • Has configurable stale time and garbage collection settings
  • Employs query-based persister instead of whole state persistence
  • Handles streaming cancellation properly through cancelStream() mechanism
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for potential performance bottlenecks in the conversation list query implementation

# Look for debouncing or throttling mechanisms in the query implementation
rg -A 10 "addConversationToConversationListQuery" --type ts

# Check for batch update patterns
ast-grep --pattern 'function addConversationToConversationListQuery($_) {
  $$$
}'

Length of output: 3996


Script:

#!/bin/bash
# Check the implementation of conversation list query data management
rg -A 15 "setConversationListQueryData|getConversationListQueryData" --type ts

# Look for any caching or state management patterns
ast-grep --pattern 'const $_ConversationListQuery = {
  $$$
}'

Length of output: 6318


Script:

#!/bin/bash
# Check query client configuration and optimization settings
rg -A 10 "queryClient" --type ts

# Look for any debouncing or batching mechanisms
rg -A 10 "useQuery.*conversation" --type ts

Length of output: 47571

Comment on lines +170 to 171

if (!previousConversationsData) return;
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider initializing empty conversation list instead of early return

The early return when no previous conversations exist could lead to missed updates. Consider initializing with an empty array instead.

- if (!previousConversationsData) return;
+ if (!previousConversationsData) {
+   setConversationListQueryData(account, [conversation]);
+   return;
+ }

This ensures that updates are not lost when the conversation list is initially empty.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (!previousConversationsData) return;
if (!previousConversationsData) {
setConversationListQueryData(account, [conversation]);
return;
}

Comment on lines +24 to +25

addConversationToConversationListQuery(account, conversation);
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add error handling for the query update

The conversation list update should be wrapped in a try-catch block to handle potential failures gracefully. Also, consider removing the empty line for better readability.

  logger.info("[XMTPRN Conversations] GOT A NEW CONVO");
-
-    addConversationToConversationListQuery(account, conversation);
+  try {
+    await addConversationToConversationListQuery(account, conversation);
+  } catch (error) {
+    logger.error("[XMTPRN Conversations] Failed to update conversation list", error);
+  }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
addConversationToConversationListQuery(account, conversation);
logger.info("[XMTPRN Conversations] GOT A NEW CONVO");
try {
await addConversationToConversationListQuery(account, conversation);
} catch (error) {
logger.error("[XMTPRN Conversations] Failed to update conversation list", error);
}

Comment on lines +30 to +32
if (!contentType) {
return undefined;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I wonder if we should put | undefined here 🤔

And instead handle the undefined before calling the function.

Curious on your thoughts. It's just that I feel it's counterintuitive to call this function with a contentType that is undefined haha.

Love the | undefined instead of ?: string tho! At least it shows it wants an argument

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