Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
59fa789
question
ggazzo Sep 9, 2025
38279bf
refactor: restructure StateService to improve room state retrieval an…
ggazzo Sep 9, 2025
966dfeb
feat: add latest state mapping retrieval method and include _id in St…
ggazzo Sep 10, 2025
08e2f77
lint
ggazzo Sep 12, 2025
7577964
feat: add state and state IDs DTOs and implement corresponding state …
ggazzo Sep 5, 2025
9af8f40
review
ggazzo Sep 5, 2025
8de5790
feat: add method to retrieve the latest state mapping before a given …
ggazzo Sep 8, 2025
fdb9576
refactor: update MakeJoinResponseDto to use MembershipDto for members…
ggazzo Sep 9, 2025
6228c1a
refactor: enforce eventId requirement in getStateIds method and impro…
ggazzo Sep 9, 2025
28f1a66
feat: add _id field to StateStore and update state retrieval logic to…
ggazzo Sep 10, 2025
0340943
review - 1
ggazzo Sep 11, 2025
331e4f5
feat: add logging for eventId in ProfilesService and implement findPr…
ggazzo Sep 12, 2025
0905035
refactor: rename findStateAtEvent to findStateBeforeEvent in Profiles…
ggazzo Sep 12, 2025
9597aeb
wip
ggazzo Sep 12, 2025
ff484ae
fix second event
sampaiodiego Sep 12, 2025
c82c9d1
refactor: improve code readability by adjusting comment indentation i…
ggazzo Sep 13, 2025
ab8bd49
refactor: simplify argument passing in getAuthChain function for impr…
ggazzo Sep 13, 2025
3c27c43
refactor: remove unnecessary console.log statements in ProfilesServic…
ggazzo Sep 13, 2025
21fcfbb
chore: move to rigth scope
ggazzo Sep 13, 2025
5b6f94b
refactor: streamline state ID retrieval in StateService and enhance a…
ggazzo Sep 13, 2025
3c084f4
refactor: remove redundant console.log statements in EventService and…
ggazzo Sep 13, 2025
ce3bb5a
review 1
ggazzo Sep 13, 2025
517bd5e
refactor: enhance state retrieval logic in StateService by consolidat…
ggazzo Sep 13, 2025
234ea6d
refactor: remove unused getLatestStateMappingBeforeEvent method from …
ggazzo Sep 13, 2025
7eaeb18
📝 Add docstrings to `chore/state-ids-for-all-events`
coderabbitai[bot] Sep 15, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type { StateMapKey } from '@hs/room';
import type { PersistentEventBase } from '@hs/room';

export type StateStore = {
_id: ObjectId;
delta: {
identifier: StateMapKey;
eventId: string;
Expand Down
126 changes: 126 additions & 0 deletions packages/federation-sdk/src/services/event.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
type PduForType,
type PduType,
PersistentEventFactory,
getAuthChain,
} from '@hs/room';
import { singleton } from 'tsyringe';
import type { z } from 'zod';
Expand Down Expand Up @@ -679,4 +680,129 @@ export class EventService {
await new Promise((resolve) => setTimeout(resolve, 5000));
}
}

async getStateIds(
roomId: string,
eventId: string,
): Promise<{ pdu_ids: string[]; auth_chain_ids: string[] }> {
try {
// Ensure the event exists and belongs to the requested room
const persisted = await this.eventRepository.findById(eventId);
if (!persisted || persisted.event.room_id !== roomId) {
throw new Error('M_NOT_FOUND');
}

const state = await this.stateService.findStateBeforeEvent(eventId);

const pduIds: string[] = [];
const authChainIds = new Set<string>();

// Get room version for the store
const roomVersion = await this.stateService.getRoomVersion(roomId);
if (!roomVersion) {
throw new Error('Room version not found');
}

// Get the event store
const store = this.stateService._getStore(roomVersion);

// Extract state event IDs and collect auth chain IDs
for (const [, event] of state.entries()) {
// PersistentEventBase has an eventId getter
pduIds.push(event.eventId);
// Get the complete auth chain for this event
try {
const authChain = await getAuthChain(event, store);
for (const authEventId of authChain) {
authChainIds.add(authEventId);
}
} catch (error) {
this.logger.warn(
`Failed to get auth chain for event ${event.eventId}:`,
error,
);
}
}

return {
pdu_ids: pduIds,
auth_chain_ids: Array.from(authChainIds),
};
} catch (error) {
this.logger.error(`Failed to get state IDs for room ${roomId}:`, error);
throw error;
}
}

async getState(
roomId: string,
eventId: string,
): Promise<{
pdus: Record<string, unknown>[];
auth_chain: Record<string, unknown>[];
}> {
try {
// Ensure the event exists and belongs to the requested room
const persisted = await this.eventRepository.findById(eventId);
if (!persisted || persisted.event.room_id !== roomId) {
throw new Error('M_NOT_FOUND');
}

let state: Map<string, any>;

// Get state at a specific event
state = await this.stateService.findStateBeforeEvent(eventId);

const pdus: Record<string, unknown>[] = [];
const authChainIds = new Set<string>();

// Get room version for the store
const roomVersion = await this.stateService.getRoomVersion(roomId);
if (!roomVersion) {
throw new Error('Room version not found');
}

// Get the event store
const store = this.stateService._getStore(roomVersion);
// Extract state event objects and collect auth chain IDs
for (const [, event] of state.entries()) {
// PersistentEventBase has an event getter that contains the actual event data
pdus.push(event.event);

// Get the complete auth chain for this event
try {
const authChain = await getAuthChain(event, store);
for (const authEventId of authChain) {
authChainIds.add(authEventId);
}
} catch (error) {
this.logger.warn(
`Failed to get auth chain for event ${event.eventId}:`,
error,
);
}
}

// Fetch the actual auth event objects
const authChain: Record<string, unknown>[] = [];
if (authChainIds.size > 0) {
try {
const authEvents = await store.getEvents(Array.from(authChainIds));
for (const authEvent of authEvents) {
authChain.push(authEvent.event);
}
} catch (error) {
this.logger.warn('Failed to fetch auth event objects:', error);
}
}

return {
pdus: pdus,
auth_chain: authChain,
};
} catch (error) {
this.logger.error(`Failed to get state for room ${roomId}:`, error);
throw error;
}
}
}
Loading