Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions ee/packages/federation-matrix/src/FederationMatrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -729,18 +729,8 @@ export class FederationMatrix extends ServiceClass implements IFederationMatrixS
}
}

async getEventById(eventId: EventID): Promise<any | null> {
if (!this.homeserverServices) {
this.logger.warn('Homeserver services not available');
return null;
}

try {
return await this.homeserverServices.event.getEventById(eventId);
} catch (error) {
this.logger.error('Failed to get event by ID:', error);
throw error;
}
async getEventById(eventId: EventID) {
return this.homeserverServices.event.getEventById(eventId);
}
Comment on lines +732 to 734
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

Add null check for homeserverServices and explicit return type annotation.

The method now directly delegates to this.homeserverServices.event.getEventById(eventId) without checking if homeserverServices is initialized. Other methods in this class (e.g., createRoom at lines 111-114, sendMessage at lines 444-446) guard against missing homeserverServices before use. Additionally, the return type is no longer explicitly annotated, reducing type safety.

If homeserverServices is unavailable (e.g., when the homeserver module fails to initialize at line 106), calling this method will result in a null-pointer exception instead of returning null gracefully.

Apply this diff to add a null check and explicit return type:

-async getEventById(eventId: EventID) {
-  return this.homeserverServices.event.getEventById(eventId);
+async getEventById(eventId: EventID): Promise<EventStore<PduForType<PduType>> | null> {
+  if (!this.homeserverServices) {
+    this.logger.warn('Homeserver services not available, cannot retrieve event');
+    return null;
+  }
+  return this.homeserverServices.event.getEventById(eventId);
}

Additionally, verify that all callers of getEventById handle potential exceptions if you choose not to add the null check:

#!/bin/bash
# Find all calls to getEventById to verify error handling
rg -nP --type=ts -C5 '\.getEventById\s*\('
🤖 Prompt for AI Agents
In ee/packages/federation-matrix/src/FederationMatrix.ts around lines 649 to
651, the async getEventById method lacks an explicit return type and does not
check that this.homeserverServices is initialized; add an explicit return type
(e.g., Promise<Event|null> or the appropriate Event type union) and guard the
call with a null-check that returns null if homeserverServices is absent,
otherwise delegate to this.homeserverServices.event.getEventById(eventId);
ensure the method remains async and update any callers only if you decide not to
return null so they still handle the potential null result.


async leaveRoom(roomId: string, user: IUser): Promise<void> {
Expand Down
2 changes: 1 addition & 1 deletion ee/packages/federation-matrix/src/events/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ export function message(emitter: Emitter<HomeserverEventSignatures>, serverName:
}

const messageEvent = await FederationMatrix.getEventById(redactedEventId);
if (!messageEvent || messageEvent.type !== 'm.room.message') {
if (!messageEvent || messageEvent.event.type !== 'm.room.message') {
logger.debug(`Event ${redactedEventId} is not a message event`);
return;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core-services/src/types/IFederationMatrixService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { IMessage, IRoomFederated, IRoomNativeFederated, IUser } from '@rocket.chat/core-typings';
import type { EventID, PduForType } from '@rocket.chat/federation-sdk';
import type { EventID, PduForType, EventStore } from '@rocket.chat/federation-sdk';

export interface IFederationMatrixService {
createRoom(room: IRoomFederated, owner: IUser, members: string[]): Promise<{ room_id: string; event_id: string }>;
Expand All @@ -9,7 +9,7 @@ export interface IFederationMatrixService {
deleteMessage(matrixRoomId: string, message: IMessage): Promise<void>;
sendReaction(messageId: string, reaction: string, user: IUser): Promise<void>;
removeReaction(messageId: string, reaction: string, user: IUser, oldMessage: IMessage): Promise<void>;
getEventById(eventId: string): Promise<any | null>;
getEventById(eventId: string): Promise<EventStore | null>;
leaveRoom(rid: IRoomFederated['_id'], user: IUser): Promise<void>;
kickUser(room: IRoomNativeFederated, removedUser: IUser, userWhoRemoved: IUser): Promise<void>;
updateMessage(room: IRoomNativeFederated, message: IMessage): Promise<void>;
Expand Down
Loading