Skip to content

Conversation

@allouis
Copy link
Collaborator

@allouis allouis commented Jun 9, 2025

ref https://linear.app/ghost/issue/PROD-1986

We don't want to emit an AccountUpdatedEvent for every time an entity is saved, we only want to do it when we have actually updated the data. This may also have knock-on effects where an account was updated, but in the context of another accounts request, and the event handler was attempting to call sendActivity but the keys used were from the wrong context, so it fails.

ref https://linear.app/ghost/issue/PROD-1986

We don't want to emit an AccountUpdatedEvent for every time an entity is saved,
we only want to do it when we have actually updated the data. This may also
have knock-on effects where an account was updated, but in the context of
another accounts request, and the event handler was attempting to call
`sendActivity` but the keys used were from the wrong context, so it fails.
@allouis allouis requested a review from mike182uk June 9, 2025 10:33
@coderabbitai
Copy link

coderabbitai bot commented Jun 9, 2025

"""

Walkthrough

This change introduces an instance method getName() to the AccountUpdatedEvent class, mirroring its existing static counterpart. In AccountEntity, the updateProfile method is modified to return a new AccountEntity with updated profile fields and to append a new AccountUpdatedEvent to its events array when profile fields differ from the current instance. The method signature is explicitly typed to return Account. In AccountRepositoryKnex, the import and emission of AccountUpdatedEvent in the save method are removed, eliminating redundant event handling. The updateAccountProfile method in AccountService is updated to always proceed with profile updates and saving, removing the previous early return when no changes were detected. Corresponding tests are added or removed to reflect these behavioral changes. Additionally, in FediverseBridge, two private helper methods are introduced to encapsulate activity sending logic, replacing direct calls to sendActivity in event handlers.

Possibly related PRs

Suggested reviewers

  • vershwal
    """

📜 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 03405f4 and 5a5968e.

📒 Files selected for processing (5)
  • src/account/account.entity.ts (2 hunks)
  • src/account/account.entity.unit.test.ts (2 hunks)
  • src/account/account.service.ts (0 hunks)
  • src/account/account.service.unit.test.ts (0 hunks)
  • src/activitypub/fediverse-bridge.ts (7 hunks)
💤 Files with no reviewable changes (2)
  • src/account/account.service.unit.test.ts
  • src/account/account.service.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/account/account.entity.ts
🧰 Additional context used
🧬 Code Graph Analysis (2)
src/account/account.entity.unit.test.ts (2)
src/account/account.entity.ts (1)
  • AccountEntity (58-231)
src/account/account-updated.event.ts (1)
  • AccountUpdatedEvent (3-17)
src/activitypub/fediverse-bridge.ts (2)
src/account/account.entity.ts (1)
  • Account (11-38)
features/support/fixtures.js (2)
  • activity (108-108)
  • createActivity (107-242)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Build, Test and Push
🔇 Additional comments (9)
src/account/account.entity.unit.test.ts (3)

9-9: LGTM!

The import for AccountUpdatedEvent is correctly added to support the new test cases.


376-402: LGTM!

This test correctly verifies that AccountUpdatedEvent is emitted when profile data changes. The test setup is consistent with existing tests and the assertion properly checks both the event count and type.


404-435: LGTM!

This test correctly verifies that no AccountUpdatedEvent is emitted when profile data remains unchanged. The test provides identical values to ensure no actual changes occur, and appropriately asserts that no events are generated.

src/activitypub/fediverse-bridge.ts (6)

52-67: LGTM!

The sendActivityToInbox helper method correctly encapsulates the logic for sending activities to a specific recipient's inbox. The method signature and implementation properly extract the common pattern used for direct inbox delivery.


69-83: LGTM!

The sendActivityToFollowers helper method correctly encapsulates the logic for sending activities to an account's followers. The preferSharedInbox: true option is appropriately included for efficient delivery.


176-176: LGTM!

The refactored call correctly uses the new sendActivityToFollowers helper method, maintaining the same functionality while improving code organization.


198-198: LGTM!

The refactored call correctly uses the new sendActivityToFollowers helper method, maintaining the same functionality while improving code organization.


219-219: LGTM!

The refactored call correctly uses the new sendActivityToFollowers helper method, maintaining the same functionality while improving code organization.


252-252: LGTM!

The refactored call correctly uses the new sendActivityToInbox helper method, maintaining the same functionality while improving code organization for direct inbox delivery.

✨ 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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

@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

🧹 Nitpick comments (1)
src/account/account-updated.event.ts (1)

8-10: Question the necessity of duplicate getName() methods.

Both static and instance methods return the same value 'account.updated'. This duplication seems unnecessary and could lead to confusion about which method to use.

Is there a specific reason for having both? Consider removing one to maintain simplicity and follow DRY principles.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 505abfc and f583da4.

📒 Files selected for processing (3)
  • src/account/account-updated.event.ts (1 hunks)
  • src/account/account.entity.ts (2 hunks)
  • src/account/account.repository.knex.ts (0 hunks)
💤 Files with no reviewable changes (1)
  • src/account/account.repository.knex.ts
🧰 Additional context used
🧠 Learnings (1)
src/account/account.entity.ts (1)
Learnt from: allouis
PR: TryGhost/ActivityPub#566
File: src/account/account.entity.ts:110-123
Timestamp: 2025-04-24T10:41:15.124Z
Learning: In the Account entity's `updateProfile` method, the current implementation using a helper function that checks for undefined values is type-safe at runtime, even though TypeScript might not perfectly infer this due to the function's return type declaration.
🧬 Code Graph Analysis (1)
src/account/account.entity.ts (1)
src/account/account-updated.event.ts (1)
  • AccountUpdatedEvent (3-17)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Build, Test and Push
🔇 Additional comments (2)
src/account/account.entity.ts (2)

7-7: LGTM: Import addition is appropriate.

The import is correctly added to support the new event emission functionality.


146-146: LGTM: Explicit return type improves code clarity.

Adding the explicit return type Account improves type safety and code documentation.

Comment on lines 163 to 169
if (
account.username !== this.username ||
account.name !== this.name ||
account.bio !== this.bio ||
account.avatarUrl !== this.avatarUrl ||
account.bannerImageUrl !== this.bannerImageUrl
) {
Copy link
Member

Choose a reason for hiding this comment

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

Hm, just a thought, we already do this check in the account service:

https://github.com/TryGhost/ActivityPub/blob/main/src/account/account.service.ts#L613-L623

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

True, I think I should remove the check there then!

@allouis allouis merged commit da3e5ac into main Jun 9, 2025
9 checks passed
@allouis allouis deleted the move-account-updated-event branch June 9, 2025 13:28
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.

3 participants