1- import type { Membership , MessageType } from '@rocket.chat/federation-core' ;
2- import type { EventID , PduForType } from '@rocket.chat/federation-room' ;
1+ import 'reflect-metadata' ;
2+
3+ import type { Emitter } from '@rocket.chat/emitter' ;
4+ import type {
5+ EventStagingStore ,
6+ Membership ,
7+ MessageType ,
8+ } from '@rocket.chat/federation-core' ;
9+ import type {
10+ EventID ,
11+ EventStore ,
12+ PduForType ,
13+ } from '@rocket.chat/federation-room' ;
14+ import { Collection } from 'mongodb' ;
315import { container } from 'tsyringe' ;
4- import { ConfigService } from './services/config.service' ;
5- import { EduService } from './services/edu.service' ;
6- import { EventAuthorizationService } from './services/event-authorization.service' ;
16+ import { StagingAreaListener } from './listeners/staging-area.listener' ;
17+ import { Key } from './repositories/key.repository' ;
18+ import { Lock } from './repositories/lock.repository' ;
19+ import { Room } from './repositories/room.repository' ;
20+ import { Server } from './repositories/server.repository' ;
21+ import { StateGraphStore } from './repositories/state-graph.repository' ;
22+ import { Upload } from './repositories/upload.repository' ;
23+ import { FederationSDK } from './sdk' ;
24+ import { DatabaseConnectionService } from './services/database-connection.service' ;
725import { EventEmitterService } from './services/event-emitter.service' ;
8- import { EventService } from './services/event.service' ;
9- import { FederationRequestService } from './services/federation-request.service' ;
10- import { InviteService } from './services/invite.service' ;
11- import { MediaService } from './services/media.service' ;
12- import { MessageService } from './services/message.service' ;
13- import { ProfilesService } from './services/profiles.service' ;
14- import { RoomService } from './services/room.service' ;
15- import { SendJoinService } from './services/send-join.service' ;
16- import { ServerService } from './services/server.service' ;
17- import { StateService } from './services/state.service' ;
18- import { WellKnownService } from './services/well-known.service' ;
1926
2027export type {
2128 Pdu ,
@@ -38,7 +45,6 @@ export type {
3845} from '@rocket.chat/federation-core' ;
3946export { generateEd25519RandomSecretKey } from '@rocket.chat/federation-crypto' ;
4047
41- export { FederationEndpoints } from './specs/federation-api' ;
4248export type {
4349 MakeJoinResponse ,
4450 SendJoinResponse ,
@@ -49,38 +55,6 @@ export type {
4955 Version ,
5056} from './specs/federation-api' ;
5157
52- export { FederationModule } from './federation.module' ;
53-
54- export { FederationRequestService } from './services/federation-request.service' ;
55- export { FederationService } from './services/federation.service' ;
56- export { WellKnownService } from './services/well-known.service' ;
57- export { ConfigService } from './services/config.service' ;
58- export type { AppConfig } from './services/config.service' ;
59- export { DatabaseConnectionService } from './services/database-connection.service' ;
60- export { EduService } from './services/edu.service' ;
61-
62- export { ServerService } from './services/server.service' ;
63- export { EventAuthorizationService } from './services/event-authorization.service' ;
64- export { MissingEventService } from './services/missing-event.service' ;
65- export { ProfilesService } from './services/profiles.service' ;
66- export { EventFetcherService } from './services/event-fetcher.service' ;
67- export type { FetchedEvents } from './services/event-fetcher.service' ;
68- export { InviteService } from './services/invite.service' ;
69- export type { ProcessInviteEvent } from './services/invite.service' ;
70- export { MessageService } from './services/message.service' ;
71- export { EventService } from './services/event.service' ;
72- export { RoomService } from './services/room.service' ;
73- export { StateService } from './services/state.service' ;
74- export { StagingAreaService } from './services/staging-area.service' ;
75- export { SendJoinService } from './services/send-join.service' ;
76- export { EventEmitterService } from './services/event-emitter.service' ;
77- export { MediaService } from './services/media.service' ;
78- // Repository interfaces and implementations
79-
80- // Queue implementations
81- export { BaseQueue , type QueueHandler } from './queues/base.queue' ;
82- export { StagingAreaQueue } from './queues/staging-area.queue' ;
83-
8458// Utility exports
8559export { getErrorMessage } from './utils/get-error-message' ;
8660export { USERNAME_REGEX , ROOM_ID_REGEX } from './utils/validation-regex' ;
@@ -92,29 +66,6 @@ export {
9266export { errCodes } from './utils/response-codes' ;
9367export { NotAllowedError } from './services/invite.service' ;
9468
95- export { EventRepository } from './repositories/event.repository' ;
96- export { RoomRepository } from './repositories/room.repository' ;
97- export { ServerRepository } from './repositories/server.repository' ;
98- export { KeyRepository } from './repositories/key.repository' ;
99-
100- export interface HomeserverServices {
101- room : RoomService ;
102- message : MessageService ;
103- event : EventService ;
104- invite : InviteService ;
105- wellKnown : WellKnownService ;
106- profile : ProfilesService ;
107- state : StateService ;
108- sendJoin : SendJoinService ;
109- server : ServerService ;
110- config : ConfigService ;
111- edu : EduService ;
112- media : MediaService ;
113- request : FederationRequestService ;
114- federationAuth : EventAuthorizationService ;
115- emitter : EventEmitterService ;
116- }
117-
11869type RelatesTo =
11970 | {
12071 rel_type : 'm.replace' ;
@@ -304,37 +255,71 @@ export type HomeserverEventSignatures = {
304255 } ;
305256} ;
306257
307- export function getAllServices ( ) : HomeserverServices {
308- return {
309- room : container . resolve ( RoomService ) ,
310- message : container . resolve ( MessageService ) ,
311- event : container . resolve ( EventService ) ,
312- invite : container . resolve ( InviteService ) ,
313- wellKnown : container . resolve ( WellKnownService ) ,
314- profile : container . resolve ( ProfilesService ) ,
315- state : container . resolve ( StateService ) ,
316- sendJoin : container . resolve ( SendJoinService ) ,
317- server : container . resolve ( ServerService ) ,
318- config : container . resolve ( ConfigService ) ,
319- edu : container . resolve ( EduService ) ,
320- media : container . resolve ( MediaService ) ,
321- request : container . resolve ( FederationRequestService ) ,
322- federationAuth : container . resolve ( EventAuthorizationService ) ,
323- emitter : container . resolve ( EventEmitterService ) ,
324- } ;
325- }
326-
327- export { StagingAreaListener } from './listeners/staging-area.listener' ;
328-
329- export {
330- createFederationContainer ,
331- type FederationContainerOptions ,
332- } from './container' ;
333-
334- export { DependencyContainer } from 'tsyringe' ;
335-
336258export {
337259 roomIdSchema ,
338260 userIdSchema ,
339261 eventIdSchema ,
340262} from '@rocket.chat/federation-room' ;
263+
264+ export async function init ( {
265+ emitter,
266+ dbConfig,
267+ } : {
268+ emitter ?: Emitter < HomeserverEventSignatures > ;
269+ dbConfig : {
270+ uri : string ;
271+ name : string ;
272+ poolSize : number ;
273+ } ;
274+ } ) {
275+ const dbConnection = new DatabaseConnectionService ( dbConfig ) ;
276+ const db = await dbConnection . getDb ( ) ;
277+
278+ container . register < Collection < EventStore > > ( 'EventCollection' , {
279+ useValue : db . collection < EventStore > ( 'rocketchat_federation_events' ) ,
280+ } ) ;
281+
282+ container . register < Collection < EventStagingStore > > ( 'EventStagingCollection' , {
283+ useValue : db . collection < EventStagingStore > (
284+ 'rocketchat_federation_events_staging' ,
285+ ) ,
286+ } ) ;
287+
288+ container . register < Collection < Key > > ( 'KeyCollection' , {
289+ useValue : db . collection < Key > ( 'rocketchat_federation_keys' ) ,
290+ } ) ;
291+
292+ container . register < Collection < Lock > > ( 'LockCollection' , {
293+ useValue : db . collection < Lock > ( 'rocketchat_federation_locks' ) ,
294+ } ) ;
295+
296+ container . register < Collection < Room > > ( 'RoomCollection' , {
297+ useValue : db . collection < Room > ( 'rocketchat_federation_rooms' ) ,
298+ } ) ;
299+
300+ container . register < Collection < Server > > ( 'ServerCollection' , {
301+ useValue : db . collection < Server > ( 'rocketchat_federation_servers' ) ,
302+ } ) ;
303+
304+ container . register < Collection < Upload > > ( 'UploadCollection' , {
305+ useValue : db . collection < Upload > ( 'rocketchat_uploads' ) ,
306+ } ) ;
307+
308+ container . register < Collection < StateGraphStore > > ( 'StateGraphCollection' , {
309+ useValue : db . collection < StateGraphStore > (
310+ 'rocketchat_federation_state_graphs' ,
311+ ) ,
312+ } ) ;
313+
314+ const eventEmitterService = container . resolve ( EventEmitterService ) ;
315+ if ( emitter ) {
316+ eventEmitterService . setEmitter ( emitter ) ;
317+ } else {
318+ eventEmitterService . initializeStandalone ( ) ;
319+ }
320+
321+ // this is required to initialize the listener and register the queue handler
322+ container . resolve ( StagingAreaListener ) ;
323+ }
324+
325+ export const federationSDK = container . resolve ( FederationSDK ) ;
0 commit comments