Skip to content

Commit fc10b0c

Browse files
authored
refactor: use delay to inject repositories (#293)
1 parent 7a2ad55 commit fc10b0c

15 files changed

+41
-70
lines changed

packages/federation-sdk/src/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { Upload } from './repositories/upload.repository';
2323
import { FederationSDK } from './sdk';
2424
import { DatabaseConnectionService } from './services/database-connection.service';
2525
import { EventEmitterService } from './services/event-emitter.service';
26+
import { EventService } from './services/event.service';
2627

2728
export type {
2829
Pdu,
@@ -320,6 +321,12 @@ export async function init({
320321

321322
// this is required to initialize the listener and register the queue handler
322323
container.resolve(StagingAreaListener);
324+
325+
// once the db is initialized we look for old staged events and try to process them
326+
setTimeout(async () => {
327+
const eventService = container.resolve(EventService);
328+
await eventService.processOldStagedEvents();
329+
}, 5000);
323330
}
324331

325332
export const federationSDK = container.resolve(FederationSDK);

packages/federation-sdk/src/sdk.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { EventStore } from '@rocket.chat/federation-core';
22
import type { PduForType, PduType } from '@rocket.chat/federation-room';
3-
import { delay, inject, singleton } from 'tsyringe';
3+
import { singleton } from 'tsyringe';
44

55
import { AppConfig, ConfigService } from './services/config.service';
66
import { EduService } from './services/edu.service';
@@ -22,29 +22,20 @@ import { WellKnownService } from './services/well-known.service';
2222
@singleton()
2323
export class FederationSDK {
2424
constructor(
25-
@inject(delay(() => RoomService)) private readonly roomService: RoomService,
26-
@inject(delay(() => MessageService))
25+
private readonly roomService: RoomService,
2726
private readonly messageService: MessageService,
28-
@inject(delay(() => InviteService))
2927
private readonly inviteService: InviteService,
30-
@inject(delay(() => EventService))
3128
private readonly eventService: EventService,
32-
@inject(delay(() => EduService)) private readonly eduService: EduService,
33-
@inject(delay(() => ServerService))
29+
private readonly eduService: EduService,
3430
private readonly serverService: ServerService,
3531
private readonly configService: ConfigService,
36-
@inject(delay(() => EventAuthorizationService))
3732
private readonly eventAuthorizationService: EventAuthorizationService,
38-
@inject(delay(() => StateService))
3933
private readonly stateService: StateService,
4034
private readonly mediaService: MediaService,
41-
@inject(delay(() => ProfilesService))
4235
private readonly profilesService: ProfilesService,
43-
@inject(delay(() => SendJoinService))
4436
private readonly sendJoinService: SendJoinService,
4537
private readonly wellKnownService: WellKnownService,
4638
private readonly federationRequestService: FederationRequestService,
47-
@inject(delay(() => FederationService))
4839
private readonly federationService: FederationService,
4940
) {}
5041

packages/federation-sdk/src/services/edu.service.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
import { createLogger } from '@rocket.chat/federation-core';
77
import { singleton } from 'tsyringe';
88
import { ConfigService } from './config.service';
9-
import { EventEmitterService } from './event-emitter.service';
109
import { FederationService } from './federation.service';
1110
import { StateService } from './state.service';
1211

@@ -17,7 +16,6 @@ export class EduService {
1716
constructor(
1817
private readonly configService: ConfigService,
1918
private readonly federationService: FederationService,
20-
private readonly eventEmitterService: EventEmitterService,
2119
private readonly stateService: StateService,
2220
) {}
2321

packages/federation-sdk/src/services/event-authorization.service.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type {
99
Pdu,
1010
PersistentEventBase,
1111
} from '@rocket.chat/federation-room';
12-
import { singleton } from 'tsyringe';
12+
import { delay, inject, singleton } from 'tsyringe';
1313
import { UploadRepository } from '../repositories/upload.repository';
1414
import { ConfigService } from './config.service';
1515
import { EventService } from './event.service';
@@ -31,8 +31,9 @@ export class EventAuthorizationService {
3131
private readonly stateService: StateService,
3232
private readonly eventService: EventService,
3333
private readonly configService: ConfigService,
34-
private readonly uploadRepository: UploadRepository,
3534
private readonly serverService: ServerService,
35+
@inject(delay(() => UploadRepository))
36+
private readonly uploadRepository: UploadRepository,
3637
) {}
3738

3839
async authorizeEvent(event: Pdu, authEvents: Pdu[]): Promise<boolean> {

packages/federation-sdk/src/services/event-fetcher.service.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { isFederationEventWithPDUs } from '@rocket.chat/federation-core';
22
import { createLogger } from '@rocket.chat/federation-core';
33
import { generateId } from '@rocket.chat/federation-core';
44
import { EventID, Pdu } from '@rocket.chat/federation-room';
5-
import { singleton } from 'tsyringe';
5+
import { delay, inject, singleton } from 'tsyringe';
66
import { EventRepository } from '../repositories/event.repository';
77
import { ConfigService } from './config.service';
88
import { FederationService } from './federation.service';
@@ -17,10 +17,9 @@ export class EventFetcherService {
1717
private readonly logger = createLogger('EventFetcherService');
1818

1919
constructor(
20+
@inject(delay(() => EventRepository))
2021
private readonly eventRepository: EventRepository,
21-
2222
private readonly federationService: FederationService,
23-
2423
private readonly configService: ConfigService,
2524
) {}
2625

packages/federation-sdk/src/services/event.service.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
RoomVersion,
2424
getAuthChain,
2525
} from '@rocket.chat/federation-room';
26-
import { singleton } from 'tsyringe';
26+
import { delay, inject, singleton } from 'tsyringe';
2727
import type { z } from 'zod';
2828
import { StagingAreaQueue } from '../queues/staging-area.queue';
2929
import { EventStagingRepository } from '../repositories/event-staging.repository';
@@ -47,22 +47,18 @@ export class EventService {
4747
private currentTransactions = new Set<string>();
4848

4949
constructor(
50-
private readonly eventRepository: EventRepository,
51-
private readonly eventStagingRepository: EventStagingRepository,
52-
private readonly lockRepository: LockRepository,
5350
private readonly configService: ConfigService,
54-
5551
private readonly stagingAreaQueue: StagingAreaQueue,
5652
private readonly stateService: StateService,
5753
private readonly serverService: ServerService,
58-
5954
private readonly eventEmitterService: EventEmitterService,
60-
) {
61-
// on startup we look for old staged events and try to process them
62-
setTimeout(() => {
63-
void this.processOldStagedEvents();
64-
}, 5000);
65-
}
55+
@inject(delay(() => EventRepository))
56+
private readonly eventRepository: EventRepository,
57+
@inject(delay(() => EventStagingRepository))
58+
private readonly eventStagingRepository: EventStagingRepository,
59+
@inject(delay(() => LockRepository))
60+
private readonly lockRepository: LockRepository,
61+
) {}
6662

6763
async getEventById<T extends PduType, P extends EventStore<PduForType<T>>>(
6864
eventId: EventID,
@@ -670,7 +666,7 @@ export class EventService {
670666
return userPowerLevel >= requiredPowerLevel;
671667
}
672668

673-
private async processOldStagedEvents() {
669+
async processOldStagedEvents() {
674670
this.logger.info('Processing old staged events on startup');
675671

676672
const rooms = await this.eventStagingRepository.getDistinctStagedRooms();

packages/federation-sdk/src/services/federation.service.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
PersistentEventFactory,
99
extractDomainFromId,
1010
} from '@rocket.chat/federation-room';
11-
import { delay, inject, singleton } from 'tsyringe';
11+
import { singleton } from 'tsyringe';
1212
import {
1313
FederationEndpoints,
1414
type MakeJoinResponse,
@@ -27,10 +27,7 @@ export class FederationService {
2727

2828
constructor(
2929
private readonly configService: ConfigService,
30-
3130
private readonly requestService: FederationRequestService,
32-
33-
@inject(delay(() => StateService))
3431
private readonly stateService: StateService,
3532
) {}
3633

packages/federation-sdk/src/services/invite.service.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@ import {
1111
} from '@rocket.chat/federation-room';
1212
import { singleton } from 'tsyringe';
1313
import { ConfigService } from './config.service';
14-
import {
15-
AclDeniedError,
16-
EventAuthorizationService,
17-
} from './event-authorization.service';
18-
import { EventService } from './event.service';
14+
import { EventAuthorizationService } from './event-authorization.service';
1915
import { FederationService } from './federation.service';
2016
import { StateService, UnknownRoomError } from './state.service';
2117
// TODO: Have better (detailed/specific) event input type
@@ -37,8 +33,6 @@ export class InviteService {
3733
private readonly logger = createLogger('InviteService');
3834

3935
constructor(
40-
private readonly eventService: EventService,
41-
4236
private readonly federationService: FederationService,
4337
private readonly stateService: StateService,
4438
private readonly configService: ConfigService,

packages/federation-sdk/src/services/message.service.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,10 @@ import { createLogger } from '@rocket.chat/federation-core';
33
import {
44
type EventID,
55
type PersistentEventBase,
6-
PersistentEventFactory,
76
RoomID,
8-
type RoomVersion,
97
UserID,
108
} from '@rocket.chat/federation-room';
119
import { singleton } from 'tsyringe';
12-
import { EventRepository } from '../repositories/event.repository';
13-
import { ConfigService } from './config.service';
1410
import { EventService } from './event.service';
1511
import { FederationService } from './federation.service';
1612
import { RoomService } from './room.service';
@@ -58,13 +54,9 @@ export class MessageService {
5854

5955
constructor(
6056
private readonly eventService: EventService,
61-
private readonly configService: ConfigService,
62-
6357
private readonly federationService: FederationService,
6458
private readonly roomService: RoomService,
6559
private readonly stateService: StateService,
66-
67-
private readonly eventRepository: EventRepository,
6860
) {}
6961

7062
private buildReplyContent(reply: Reply) {

packages/federation-sdk/src/services/profiles.service.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ import {
66
EventID,
77
Pdu,
88
PduForType,
9-
PersistentEventFactory,
109
RoomID,
1110
RoomVersion,
1211
UserID,
1312
} from '@rocket.chat/federation-room';
1413
import { singleton } from 'tsyringe';
15-
import { EventRepository } from '../repositories/event.repository';
1614
import { StateService } from './state.service';
1715

1816
@singleton()
@@ -22,9 +20,6 @@ export class ProfilesService {
2220
constructor(
2321
private readonly configService: ConfigService,
2422
private readonly eventService: EventService,
25-
// private readonly roomService: RoomService,
26-
27-
private readonly eventRepository: EventRepository,
2823
private readonly stateService: StateService,
2924
) {}
3025
async queryProfile(userId: string): Promise<{

0 commit comments

Comments
 (0)