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

Ordering feed and profile posts by published_at #403

Merged
merged 10 commits into from
Mar 19, 2025
Merged

Conversation

vershwal
Copy link
Member

No description provided.

Copy link

coderabbitai bot commented Mar 18, 2025

Walkthrough

The changes introduce several modifications across the project. A wait condition of 1000 milliseconds is added in specific feed scenarios, affecting the execution timing of subsequent steps. In the step definitions, the published date for generated objects now uses the current date and time instead of a fixed timestamp. A new integration test case has been added to confirm that feed items are correctly sorted by their published date, verifying that the order is as expected. Additionally, the SQL queries in the feed service have been updated: the feed data retrieval now uses the published_at field for selection, pagination, and ordering rather than using feed IDs. Similar changes in the post service replace created_at with published_date for selecting dates and managing pagination and sorting.

Possibly related PRs

Suggested reviewers

  • allouis

Tip

⚡🧪 Multi-step agentic review comment chat (experimental)
  • We're introducing multi-step agentic chat in review comments. This experimental feature enhances review discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments.
    - To enable this feature, set early_access to true under in the settings.

📜 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 cd2c479 and e2d306c.

⛔ Files ignored due to path filters (1)
  • yarn.lock is excluded by !**/yarn.lock, !**/*.lock
📒 Files selected for processing (3)
  • features/profile-posts.feature (1 hunks)
  • features/step_definitions/stepdefs.js (6 hunks)
  • package.json (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • package.json
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Build, Test and Deploy
🔇 Additional comments (9)
features/step_definitions/stepdefs.js (4)

6-6: Appropriate import addition for time control.

The addition of the sinon library provides proper time manipulation capabilities for testing, enabling more deterministic test execution.

Also applies to: 21-21


295-295: Improved time handling for test objects.

Changing from a static timestamp to dynamic new Date() for the published field in both Article and Note objects aligns with the PR objective of ordering feed and profile posts by published_at.

Also applies to: 313-313


426-442: Well-implemented fake timer setup.

The clock initialization in the Before hook and cleanup in the After hook ensures proper time manipulation throughout tests without leaking between test runs.


1677-1679: Good implementation of time advancement step.

This step definition allows tests to explicitly advance time, giving precise control over the chronological ordering of events in tests, which is essential for testing time-based sorting functionality.

features/profile-posts.feature (5)

12-13: Appropriate test setup for time-based ordering.

Adding Bob as a second user to follow and advancing the fake timer between actions creates a proper time sequence necessary to test the sorting by published_at.


20-23: Good test coverage for multiple user posts.

Adding a second note with different content helps verify that all user-created posts appear in the profile, regardless of creation time.


30-32: Correctly updated test assumptions.

The scenario now properly tests that posts from "Bob" don't appear in the user's own posts collection, maintaining the boundary between followed accounts and personal content.

Also applies to: 36-36


43-47: Well-defined date-based sorting test.

The renamed scenario now accurately describes the tested functionality and the assertion confirms posts are ordered by publication date (newest first) rather than by ID.


49-59: Properly structured pagination test.

The updated pagination scenario effectively tests that:

  1. Posts are sorted by publication date
  2. Limit parameter functions correctly
  3. Pagination cursor works as expected

The time advancement and additional posts provide the necessary data variety for robust pagination testing.

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 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.
  • @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

@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

🧹 Nitpick comments (1)
src/feed/feed.service.ts (1)

152-152:

❓ Verification inconclusive

Improved feed ordering with published_at instead of ID

The changes consistently update the pagination and ordering logic to use published_at instead of feed ID, which aligns with the PR objective. This approach provides a more intuitive chronological ordering of posts.

Consider adding a secondary sorting criterion (like ID) to handle cases where multiple posts have the exact same timestamp:

-            .orderBy('feeds.published_at', 'desc')
+            .orderBy('feeds.published_at', 'desc')
+            .orderBy('feeds.id', 'desc')

Also, ensure there's an appropriate index on the feeds.published_at column to maintain query performance, especially since this is now the primary sorting field used for pagination.

Also applies to: 183-183, 186-186, 202-202


🏁 Script executed:

#!/bin/bash
# Check if an index exists on feeds.published_at
echo "Checking for index on feeds.published_at column"
rg -A 5 -B 5 "CREATE.*INDEX.*feeds.*published_at" --glob="*.sql"

Length of output: 171


Action Required: Verify Index on feeds.published_at and Add Secondary Sorting
The PR’s changes correctly update the ordering logic to use published_at, and the suggestion to add a secondary sort on feeds.id helps resolve tie situations. However, the automated search for an index on feeds.published_at did not yield any output. Please manually verify that an appropriate index exists on this column (or add one if missing) to ensure efficient pagination.

  • Feed Service Changes (applies to lines 152, 183, 186, 202):
    -            .orderBy('feeds.published_at', 'desc')
    +            .orderBy('feeds.published_at', 'desc')
    +            .orderBy('feeds.id', 'desc')
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 5a1b94b and 6b9db44.

📒 Files selected for processing (5)
  • features/feed.feature (2 hunks)
  • features/step_definitions/stepdefs.js (2 hunks)
  • src/feed/feed.service.integration.test.ts (1 hunks)
  • src/feed/feed.service.ts (3 hunks)
  • src/post/post.service.ts (6 hunks)
🧰 Additional context used
🧬 Code Definitions (1)
src/post/post.service.ts (1)
features/step_definitions/stepdefs.js (1) (1)
  • accountId (536:553)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Build, Test and Deploy
🔇 Additional comments (14)
features/feed.feature (2)

25-25: Good addition of a wait condition to ensure consistent test results.

Adding this wait ensures proper sequencing of events, allowing the system to correctly establish the timestamp order for the feed sorting test case.


38-42: Properly implemented wait conditions for the pagination test.

These wait periods ensure that each note has a distinct published timestamp, which is critical for the updated feed sorting logic based on published_at. This change aligns with the updates in the feed service SQL queries.

features/step_definitions/stepdefs.js (3)

292-292: LGTM: Updated Article published timestamp to use current date.

Changed from using a static timestamp to a dynamic one, which better reflects real-world usage and supports the new sorting by published_at.


310-310: LGTM: Updated Note published timestamp to use current date.

Same improvement as for Articles - using dynamic timestamps instead of static ones. This ensures proper chronological ordering in the feed tests.


1656-1658: Implementation looks good for the wait step.

This step definition properly implements the wait functionality needed for the feature test changes, allowing for controlled time intervals between post creations.

src/post/post.service.ts (8)

254-258: Improved formatting of CASE statement.

Consistent formatting improves readability while maintaining the same functionality.


264-268: Improved formatting of CASE statement for reposted posts.

Similar formatting improvement as the previous CASE statement, maintaining consistency across the codebase.


282-283: Good addition of a comment for the published_at field.

Clear documentation of the field's purpose for sorting, which helps other developers understand the query structure.


310-310: Switched to using published_at for date sorting.

This change aligns with the PR's objective to sort by published_at rather than creation date.


337-340: Updated repost date field for consistent date handling.

Changed to use the repost's creation time as the publication date for sorting, ensuring consistent ordering based on when content appears in the feed.


345-345: Updated ORDER BY clause to use published_date.

This is a key change that implements the core functionality of sorting feed items by their published date.


377-377: Updated pagination condition to use published_date.

This ensures pagination works correctly with the new sorting order based on published date.


394-394: Updated nextCursor to use published_date.

This change ensures that pagination continues correctly from where the previous page ended, using the published date as the reference point.

src/feed/feed.service.integration.test.ts (1)

204-276: Great addition of a test case to verify feed sorting by published_at.

This test thoroughly verifies that feed items are properly sorted by their published date. It creates a realistic scenario with:

  1. Multiple posts with different publication dates
  2. A repost with a newer date
  3. Verification of the expected ordering (newest first)

The test correctly validates that reposts are sorted based on their repost timestamp rather than the original post's publication date, which is important for user experience.

@@ -22,6 +22,7 @@ Feature: Feed
Given a "Create(Note)" Activity "Note1" by "Alice"
And "Alice" sends "Note1" to the Inbox
And "Note1" is in our Inbox
And We waited for 1000 milliseconds
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this so the published at date is later? It would be nice to not at a 3 second delay to the tests if we could use something like https://sinonjs.org/releases/latest/fake-timers/ or similar to mock the Date in the stepdefs.js file

Copy link
Member Author

Choose a reason for hiding this comment

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

I tried adding the fake timer but there's a problem with that. Any mock or fake timer will only work within the scope of the stepdefs.js file. However, in this case, the posts are created via the API. Fake timers only affect time-related functions inside the test process (i.e., within the step definitions), but not inside the API server. The API server will still use the real new Date() at the moment the request is processed.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Aha okay, I think we have a problem there then, they should use the passed published property if it exists, what do you think?

Copy link
Member Author

Choose a reason for hiding this comment

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

Talked about this is in slack. Used fake timer for feed tests. For profile tests, tweak the tests for now to use the fake timer, will discuss a solution around improving tests separately.

@vershwal vershwal merged commit 3a40500 into main Mar 19, 2025
4 checks passed
@vershwal vershwal deleted the orderByPublishedAt branch March 19, 2025 11:35
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