Skip to content

chore: remove cycle dependency of EventService and StateService#406

Open
sampaiodiego wants to merge 4 commits into
mainfrom
refactor/385-break-event-state-cycle
Open

chore: remove cycle dependency of EventService and StateService#406
sampaiodiego wants to merge 4 commits into
mainfrom
refactor/385-break-event-state-cycle

Conversation

@sampaiodiego

@sampaiodiego sampaiodiego commented Jul 22, 2026

Copy link
Copy Markdown
Member

Closes #385

Review in cubic

Summary by CodeRabbit

  • New Features

    • Added event notifications for supported federation room events, including messages, membership changes, reactions, redactions, room settings, and encryption.
    • Added role and permission change notifications when room power levels are updated.
  • Improvements

    • Streamlined event authorization, persistence, and notification into a unified flow.
    • Improved event handling during room joins and initial state processing.

sampaiodiego and others added 4 commits July 16, 2026 20:44
EventService.notify() now delegates to the new concrete service. No
behavior change; first step toward removing the EventService/StateService
circular dependency.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
StateService no longer depends on EventService — its only reverse call
was notify() in processInitialState(), which now receives the notifier
as a parameter (a constructor injection would recreate the cycle, since
EventNotifierService needs StateService for power-level diffs).

Both @Inject(delay(() => require(...))) hacks are gone; EventService now
injects StateService plainly.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…path (#385)

Encapsulates the handlePdu -> notify sequencing that StagingAreaService
previously hand-rolled across two services. EventService.notify() had no
remaining callers and is removed.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…tService (#385)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

Introduces EventNotifierService, moves federation event dispatching out of EventService, and updates persistence and initial-state flows to use the notifier directly. Power-level events now generate role notifications based on prior and current room state.

Changes

Federation event pipeline

Layer / File(s) Summary
Centralized event notification
packages/federation-sdk/src/services/event-notifier.service.ts
Adds event-type notification dispatching, power-level role updates, and unknown-event warnings.
Authorize-and-persist orchestration
packages/federation-sdk/src/services/event.service.ts, packages/federation-sdk/src/services/staging-area.service.ts
Replaces EventService.notify with authorizeAndPersist, which handles the PDU before delegating notification to EventNotifierService.
Initial-state notification wiring
packages/federation-sdk/src/services/state.service.ts, packages/federation-sdk/src/services/room.service.ts, packages/federation-sdk/src/services/*service.spec.ts
Passes the notifier into initial-state processing, removes the StateServiceEventService dependency, and updates related tests and join handling.

Estimated code review effort: 4 (Complex) | ~45 minutes

Possibly related issues

Possibly related PRs

Suggested labels: type: chore

Suggested reviewers: ggazzo

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: breaking the EventService/StateService circular dependency.

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

Comment @coderabbitai help to get the list of available commands.

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 8.55856% with 203 lines in your changes missing coverage. Please review.
✅ Project coverage is 51.81%. Comparing base (cb41e79) to head (89012c6).

Files with missing lines Patch % Lines
...eration-sdk/src/services/event-notifier.service.ts 4.85% 196 Missing ⚠️
...kages/federation-sdk/src/services/event.service.ts 50.00% 4 Missing ⚠️
...kages/federation-sdk/src/services/state.service.ts 50.00% 2 Missing ⚠️
...ederation-sdk/src/services/staging-area.service.ts 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #406      +/-   ##
==========================================
+ Coverage   51.78%   51.81%   +0.03%     
==========================================
  Files         113      114       +1     
  Lines       12689    12704      +15     
==========================================
+ Hits         6571     6583      +12     
- Misses       6118     6121       +3     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/federation-sdk/src/services/event-notifier.service.ts`:
- Around line 158-202: Update the power-level notification logic around
setOrUnsetPowerLevels to also identify users present in oldPowerLevels but
absent from changedUserPowers. Add those removed user IDs to the notified set
and emit homeserver.matrix.room.role for each with role user, ensuring the
existing changed-user loop does not emit duplicates.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 4e84ddc1-ec98-4a2e-b5e6-63d84570075d

📥 Commits

Reviewing files that changed from the base of the PR and between cb41e79 and 89012c6.

📒 Files selected for processing (7)
  • packages/federation-sdk/src/services/event-notifier.service.ts
  • packages/federation-sdk/src/services/event.service.ts
  • packages/federation-sdk/src/services/invite.service.spec.ts
  • packages/federation-sdk/src/services/room.service.ts
  • packages/federation-sdk/src/services/staging-area.service.ts
  • packages/federation-sdk/src/services/state.service.spec.ts
  • packages/federation-sdk/src/services/state.service.ts
📜 Review details
⏰ Context from checks skipped due to timeout. (2)
  • GitHub Check: cubic · AI code reviewer
  • GitHub Check: Code Quality Checks(lint, test, tsc)
🔇 Additional comments (8)
packages/federation-sdk/src/services/event-notifier.service.ts (1)

23-104: LGTM!

packages/federation-sdk/src/services/event.service.ts (1)

55-66: LGTM!

Also applies to: 817-822

packages/federation-sdk/src/services/staging-area.service.ts (1)

104-107: LGTM!

packages/federation-sdk/src/services/state.service.ts (2)

353-355: LGTM!

Also applies to: 463-463


76-82: 🩺 Stability & Availability

No this.eventService references remain in StateService.

packages/federation-sdk/src/services/room.service.ts (1)

35-35: LGTM!

Also applies to: 67-67, 812-812

packages/federation-sdk/src/services/invite.service.spec.ts (1)

67-71: LGTM!

Also applies to: 210-212, 238-238, 291-291, 379-387

packages/federation-sdk/src/services/state.service.spec.ts (1)

132-136: LGTM!

Also applies to: 2126-2126, 2144-2144, 2156-2156

Comment thread packages/federation-sdk/src/services/event-notifier.service.ts

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

2 issues found across 7 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="packages/federation-sdk/src/services/event-notifier.service.ts">

<violation number="1" location="packages/federation-sdk/src/services/event-notifier.service.ts:138">
P2: A power-level event without `users` but with `users_default: 50` reports every previously explicit user as `user` instead of `moderator`. Derive the emitted role from the new event's `users_default` value.</violation>

<violation number="2" location="packages/federation-sdk/src/services/event-notifier.service.ts:162">
P1: Removing a user from `content.users` emits no `homeserver.matrix.room.role` update, leaving downstream clients with that user's former role. Include old-only keys in the diff and emit their new effective default role.</violation>
</file>

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

const usersInOldPowerLevelEvent = Object.keys(oldPowerLevels);
const usersInNewPowerLevelEvent = Object.keys(changedUserPowers);

const setOrUnsetPowerLevels = new Set(usersInNewPowerLevelEvent).difference(new Set(usersInOldPowerLevelEvent));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1: Removing a user from content.users emits no homeserver.matrix.room.role update, leaving downstream clients with that user's former role. Include old-only keys in the diff and emit their new effective default role.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At packages/federation-sdk/src/services/event-notifier.service.ts, line 162:

<comment>Removing a user from `content.users` emits no `homeserver.matrix.room.role` update, leaving downstream clients with that user's former role. Include old-only keys in the diff and emit their new effective default role.</comment>

<file context>
@@ -0,0 +1,212 @@
+					const usersInOldPowerLevelEvent = Object.keys(oldPowerLevels);
+					const usersInNewPowerLevelEvent = Object.keys(changedUserPowers);
+
+					const setOrUnsetPowerLevels = new Set(usersInNewPowerLevelEvent).difference(new Set(usersInOldPowerLevelEvent));
+
+					this.logger.debug(
</file context>

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

won't fix. this is a pre-existing bug that will be handled in another PR

sender_id: event.event.sender,
user_id: userId,
room_id: roomId,
role: 'user', // since new power level reset all powers

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2: A power-level event without users but with users_default: 50 reports every previously explicit user as user instead of moderator. Derive the emitted role from the new event's users_default value.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At packages/federation-sdk/src/services/event-notifier.service.ts, line 138:

<comment>A power-level event without `users` but with `users_default: 50` reports every previously explicit user as `user` instead of `moderator`. Derive the emitted role from the new event's `users_default` value.</comment>

<file context>
@@ -0,0 +1,212 @@
+							sender_id: event.event.sender,
+							user_id: userId,
+							room_id: roomId,
+							role: 'user', // since new power level reset all powers
+						});
+					}
</file context>

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

won't fix, this is a pre-existing bug

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RFC: Break EventService/StateService circular dependency

2 participants