Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
38e6b89
question
ggazzo Sep 9, 2025
110ed72
refactor: restructure StateService to improve room state retrieval an…
ggazzo Sep 9, 2025
e72a930
feat: add latest state mapping retrieval method and include _id in St…
ggazzo Sep 10, 2025
4f9fe79
lint
ggazzo Sep 12, 2025
94364ab
feat: add state and state IDs DTOs and implement corresponding state …
ggazzo Sep 5, 2025
ed08cf9
review
ggazzo Sep 5, 2025
9a1ec5f
feat: add method to retrieve the latest state mapping before a given …
ggazzo Sep 8, 2025
605a3d3
refactor: enforce eventId requirement in getStateIds method and impro…
ggazzo Sep 9, 2025
e5ed38b
feat: add _id field to StateStore and update state retrieval logic to…
ggazzo Sep 10, 2025
806e088
review - 1
ggazzo Sep 11, 2025
1d98f08
feat: add logging for eventId in ProfilesService and implement findPr…
ggazzo Sep 12, 2025
6d48d3d
refactor: rename findStateAtEvent to findStateBeforeEvent in Profiles…
ggazzo Sep 12, 2025
a91cd5e
wip
ggazzo Sep 12, 2025
7deff9b
fix second event
sampaiodiego Sep 12, 2025
1eaa2f7
refactor: improve code readability by adjusting comment indentation i…
ggazzo Sep 13, 2025
01b8fff
refactor: remove unnecessary console.log statements in ProfilesServic…
ggazzo Sep 13, 2025
bbba720
chore: move to rigth scope
ggazzo Sep 13, 2025
aa9034e
refactor: streamline state ID retrieval in StateService and enhance a…
ggazzo Sep 13, 2025
3bad914
refactor: remove redundant console.log statements in EventService and…
ggazzo Sep 13, 2025
c461b76
review 1
ggazzo Sep 13, 2025
3c44859
refactor: enhance state retrieval logic in StateService by consolidat…
ggazzo Sep 13, 2025
b432145
refactor: remove unused getLatestStateMappingBeforeEvent method from …
ggazzo Sep 13, 2025
3b31b7e
refactor: remove unused findPreviousStateId method from StateService
ggazzo Sep 15, 2025
2675b3a
refactor: update state retrieval methods in StateService and EventSer…
ggazzo Sep 15, 2025
d2be5a1
refactor: replace eventPlugin with statePlugin in homeserver module a…
ggazzo Sep 15, 2025
00a8e6d
refactor: remove outdated state handling logic in StateService
ggazzo Sep 15, 2025
23f2ec1
Merge branch 'main' into chore/state-ids-for-all-events
ggazzo 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
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.findStateAtEvent(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.findStateAtEvent(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