Skip to content

Conversation

@rodrigok
Copy link
Member

@rodrigok rodrigok commented Sep 25, 2025

Proposed changes (including videos or screenshots)

Issue(s)

Steps to test or reproduce

Further comments

Summary by CodeRabbit

  • Bug Fixes

    • More reliable federation room membership sync for join/leave events.
    • Correctly distinguishes voluntary leaves from kicks, ensuring accurate removals.
    • Automatically creates and adds federated users on join when needed.
  • Refactor

    • Streamlined membership event handling into dedicated join/leave paths for clearer behavior.
    • Removed legacy invite event handling from federation event registration and related orchestration.

@rodrigok rodrigok added this to the 7.11.0 milestone Sep 25, 2025
@dionisio-bot
Copy link
Contributor

dionisio-bot bot commented Sep 25, 2025

Looks like this PR is not ready to merge, because of the following issues:

  • This PR is missing the 'stat: QA assured' label

Please fix the issues and try again

If you have any trouble, please check the PR guidelines

@changeset-bot
Copy link

changeset-bot bot commented Sep 25, 2025

⚠️ No Changeset found

Latest commit: ef55cac

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Sep 25, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

Removes the Matrix invite event registration and deletes the invite orchestration file. Refactors membership handling by splitting join and leave logic into dedicated helpers that resolve rooms/users, create federated users on join when needed, and distinguish voluntary leaves from kicks.

Changes

Cohort / File(s) Summary
Event wiring cleanup
ee/packages/federation-matrix/src/events/index.ts
Removes the import and registration call for the invite event.
Remove invite handler
ee/packages/federation-matrix/src/events/invite.ts
Deletes the invite orchestration file and the exported invite(...) function that handled accept-invite, bridged user resolution/creation, and room joining.
Membership handling refactor
ee/packages/federation-matrix/src/events/member.ts
Adds membershipJoinAction and membershipLeaveAction; handles only 'join' and 'leave' events, resolves rooms/users, creates federated users on join when absent, and distinguishes voluntary leave vs. kick when removing users. Also imports UserStatus.

Sequence Diagram(s)

sequenceDiagram
    autonumber
    participant HS as Homeserver
    participant MemberEvt as member.ts handler
    participant JoinAct as membershipJoinAction
    participant LeaveAct as membershipLeaveAction
    participant Rooms as Rooms Repo
    participant Users as Users Repo

    HS->>MemberEvt: matrix: state.member (event)
    alt membership == 'join'
        MemberEvt->>JoinAct: handle join
        JoinAct->>Rooms: find by federation.mrid
        alt room found
            JoinAct->>Users: find by federation.mui (sender)
            alt user exists
                JoinAct->>Rooms: add user to room
            else user missing
                JoinAct->>Users: create federated user
                JoinAct->>Users: fetch inserted user
                JoinAct->>Rooms: add user to room
            end
        else room missing
            Note over JoinAct: warn and return
        end
    else membership == 'leave'
        MemberEvt->>LeaveAct: handle leave
        LeaveAct->>Rooms: find by federation.mrid
        alt room found
            LeaveAct->>Users: find by federation.mui (state_key)
            alt sender == state_key
                LeaveAct->>Rooms: remove user (voluntary)
            else kicked
                LeaveAct->>Users: find kicker by federation.mui (sender)
                LeaveAct->>Rooms: remove user with byUser context
            end
        else room missing
            Note over LeaveAct: warn and return
        end
    else other membership
        Note over MemberEvt: debug: ignored
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested labels

stat: ready to merge, stat: QA assured

Suggested reviewers

  • ggazzo

Poem

I hop where invites once did hide,
Joins and leaves now split inside.
I plant a carrot, give a cheer,
New users hop, old ones clear.
Little rabbit, tidy trail—merge approved, a happy tale. 🥕🐇

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title clearly highlights the primary functionality added—support for handling remote join events in the federation module—and follows the conventional “feat(scope):” format, making it concise and informative for teammates browsing PR history.

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

Disabled knowledge base sources:

  • Jira integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between a356cfc and ef55cac.

📒 Files selected for processing (1)
  • ee/packages/federation-matrix/src/events/member.ts (1 hunks)

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 and usage tips.

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

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

Disabled knowledge base sources:

  • Jira integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 1a32eb4 and a356cfc.

📒 Files selected for processing (3)
  • ee/packages/federation-matrix/src/events/index.ts (0 hunks)
  • ee/packages/federation-matrix/src/events/invite.ts (0 hunks)
  • ee/packages/federation-matrix/src/events/member.ts (1 hunks)
💤 Files with no reviewable changes (2)
  • ee/packages/federation-matrix/src/events/invite.ts
  • ee/packages/federation-matrix/src/events/index.ts
🧰 Additional context used
🧬 Code graph analysis (1)
ee/packages/federation-matrix/src/events/member.ts (3)
ee/packages/federation-matrix/src/events/room.ts (1)
  • room (6-59)
packages/models/src/index.ts (2)
  • Rooms (202-202)
  • Users (211-211)
packages/core-services/src/index.ts (1)
  • Room (169-169)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: 📦 Build Packages
  • GitHub Check: CodeQL-Build
  • GitHub Check: CodeQL-Build

Comment on lines +56 to +59
const [, serverName] = data.sender.split(':');
if (!serverName) {
throw new Error('Invalid sender format, missing server name');
}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Handle Matrix server name with ports correctly

Splitting on : drops everything after the first separator, so IDs like @alice:server.example:8448 lose the port and we persist the wrong origin. That breaks reconciliations and future joins from homeservers that expose non-default ports or IPv6 literals. Please slice everything after the first colon instead of relying on array destructuring.

-	const [, serverName] = data.sender.split(':');
-	if (!serverName) {
-		throw new Error('Invalid sender format, missing server name');
-	}
+	const separatorIndex = data.sender.indexOf(':');
+	if (separatorIndex === -1 || separatorIndex === data.sender.length - 1) {
+		throw new Error('Invalid sender format, missing server name');
+	}
+	const serverName = data.sender.slice(separatorIndex + 1);
📝 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
const [, serverName] = data.sender.split(':');
if (!serverName) {
throw new Error('Invalid sender format, missing server name');
}
const separatorIndex = data.sender.indexOf(':');
if (separatorIndex === -1 || separatorIndex === data.sender.length - 1) {
throw new Error('Invalid sender format, missing server name');
}
const serverName = data.sender.slice(separatorIndex + 1);

Comment on lines +90 to 96
if (data.content.membership === 'leave') {
return membershipLeaveAction(data);
}

const room = await Rooms.findOne({ 'federation.mrid': data.room_id }, { projection: { _id: 1 } });
if (!room) {
logger.warn(`No bridged room found for Matrix room_id: ${data.room_id}`);
return;
if (data.content.membership === 'join') {
return membershipJoinAction(data);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Await membership handlers so errors surface

Returning the promise without awaiting bypasses this try/catch, so any rejection from the join/leave handlers bubbles out as an unhandled rejection with no log. Await the calls and return afterward so failures are captured and logged.

-			if (data.content.membership === 'leave') {
-				return membershipLeaveAction(data);
-			}
+			if (data.content.membership === 'leave') {
+				await membershipLeaveAction(data);
+				return;
+			}-			if (data.content.membership === 'join') {
-				return membershipJoinAction(data);
-			}
+			if (data.content.membership === 'join') {
+				await membershipJoinAction(data);
+				return;
+			}
📝 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 (data.content.membership === 'leave') {
return membershipLeaveAction(data);
}
const room = await Rooms.findOne({ 'federation.mrid': data.room_id }, { projection: { _id: 1 } });
if (!room) {
logger.warn(`No bridged room found for Matrix room_id: ${data.room_id}`);
return;
if (data.content.membership === 'join') {
return membershipJoinAction(data);
}
if (data.content.membership === 'leave') {
await membershipLeaveAction(data);
return;
}
if (data.content.membership === 'join') {
await membershipJoinAction(data);
return;
}
🤖 Prompt for AI Agents
In ee/packages/federation-matrix/src/events/member.ts around lines 90 to 96, the
code returns the promise from membershipLeaveAction/membershipJoinAction without
awaiting, which bypasses the surrounding try/catch; change those lines to await
the call (await membershipLeaveAction(data); return; and await
membershipJoinAction(data); return;) so any thrown errors are caught and logged
by the existing try/catch (ensure the enclosing function is async if not
already).

@sampaiodiego sampaiodiego merged commit 41768c5 into chore/federation-backup Sep 25, 2025
4 of 6 checks passed
@sampaiodiego sampaiodiego deleted the chore/federation-backup-3rd-party-invite branch September 25, 2025 21:58
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