Skip to content

Commit

Permalink
refactor!: module to api and module config
Browse files Browse the repository at this point in the history
Signed-off-by: Timo Glastra <timo@animo.id>
  • Loading branch information
TimoGlastra committed Jul 13, 2022
1 parent c8e6e53 commit 5141660
Show file tree
Hide file tree
Showing 58 changed files with 820 additions and 504 deletions.
16 changes: 9 additions & 7 deletions packages/core/src/agent/Agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import { CacheRepository } from '../cache'
import { InjectionSymbols } from '../constants'
import { JwsService } from '../crypto/JwsService'
import { AriesFrameworkError } from '../error'
import { BasicMessagesModule } from '../modules/basic-messages/BasicMessagesModule'
import { ConnectionsModule } from '../modules/connections/ConnectionsModule'
import { CredentialsModule } from '../modules/credentials/CredentialsModule'
import { DidsModule } from '../modules/dids/DidsModule'
import { BasicMessagesModule } from '../modules/basic-messages'
import { ConnectionsModule } from '../modules/connections'
import { CredentialsModule } from '../modules/credentials'
import { DidsModule } from '../modules/dids'
import { DiscoverFeaturesModule } from '../modules/discover-features'
import { GenericRecordsModule } from '../modules/generic-records/GenericRecordsModule'
import { IndyModule } from '../modules/indy/module'
Expand Down Expand Up @@ -182,19 +182,21 @@ export class Agent extends BaseAgent {

// Register all modules
dependencyManager.registerModules(
ConnectionsModule,
new ConnectionsModule({
autoAcceptConnections: this.agentConfig.autoAcceptConnections,
}),
new CredentialsModule({
autoAcceptCredentials: this.agentConfig.autoAcceptCredentials,
}),
ProofsModule,
MediatorModule,
RecipientModule,
BasicMessagesModule,
new BasicMessagesModule(),
QuestionAnswerModule,
GenericRecordsModule,
LedgerModule,
DiscoverFeaturesModule,
DidsModule,
new DidsModule(),
WalletModule,
OutOfBandModule,
IndyModule,
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/agent/AgentConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ export class AgentConfig {
return this.initConfig.walletConfig
}

/**
* @deprecated use autoAcceptConnections from the `ConnectionsModuleConfig` class
*/
public get autoAcceptConnections() {
return this.initConfig.autoAcceptConnections ?? false
}
Expand All @@ -55,6 +58,9 @@ export class AgentConfig {
return this.initConfig.autoAcceptProofs ?? AutoAcceptProof.Never
}

/**
* @deprecated use autoAcceptCredentials from the `CredentialsModuleConfig` class
*/
public get autoAcceptCredentials() {
return this.initConfig.autoAcceptCredentials ?? AutoAcceptCredential.Never
}
Expand Down
18 changes: 9 additions & 9 deletions packages/core/src/agent/BaseAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import type { AgentConfig } from './AgentConfig'
import type { TransportSession } from './TransportService'

import { AriesFrameworkError } from '../error'
import { BasicMessagesModule } from '../modules/basic-messages/BasicMessagesModule'
import { ConnectionsModule } from '../modules/connections/ConnectionsModule'
import { BasicMessagesApi } from '../modules/basic-messages/BasicMessagesApi'
import { ConnectionsApi } from '../modules/connections/ConnectionsApi'
import { CredentialsApi } from '../modules/credentials/CredentialsApi'
import { DidsModule } from '../modules/dids/DidsModule'
import { DidsApi } from '../modules/dids/DidsApi'
import { DiscoverFeaturesModule } from '../modules/discover-features'
import { GenericRecordsModule } from '../modules/generic-records/GenericRecordsModule'
import { LedgerModule } from '../modules/ledger/LedgerModule'
Expand Down Expand Up @@ -39,17 +39,17 @@ export abstract class BaseAgent {
protected _isInitialized = false
protected agentContext: AgentContext

public readonly connections: ConnectionsModule
public readonly connections: ConnectionsApi
public readonly proofs: ProofsModule
public readonly basicMessages: BasicMessagesModule
public readonly basicMessages: BasicMessagesApi
public readonly genericRecords: GenericRecordsModule
public readonly ledger: LedgerModule
public readonly questionAnswer!: QuestionAnswerModule
public readonly credentials: CredentialsApi
public readonly mediationRecipient: RecipientModule
public readonly mediator: MediatorModule
public readonly discovery: DiscoverFeaturesModule
public readonly dids: DidsModule
public readonly dids: DidsApi
public readonly wallet: WalletModule
public readonly oob: OutOfBandModule

Expand Down Expand Up @@ -81,17 +81,17 @@ export abstract class BaseAgent {
this.agentContext = this.dependencyManager.resolve(AgentContext)

// We set the modules in the constructor because that allows to set them as read-only
this.connections = this.dependencyManager.resolve(ConnectionsModule)
this.connections = this.dependencyManager.resolve(ConnectionsApi)
this.credentials = this.dependencyManager.resolve(CredentialsApi) as CredentialsApi
this.proofs = this.dependencyManager.resolve(ProofsModule)
this.mediator = this.dependencyManager.resolve(MediatorModule)
this.mediationRecipient = this.dependencyManager.resolve(RecipientModule)
this.basicMessages = this.dependencyManager.resolve(BasicMessagesModule)
this.basicMessages = this.dependencyManager.resolve(BasicMessagesApi)
this.questionAnswer = this.dependencyManager.resolve(QuestionAnswerModule)
this.genericRecords = this.dependencyManager.resolve(GenericRecordsModule)
this.ledger = this.dependencyManager.resolve(LedgerModule)
this.discovery = this.dependencyManager.resolve(DiscoverFeaturesModule)
this.dids = this.dependencyManager.resolve(DidsModule)
this.dids = this.dependencyManager.resolve(DidsApi)
this.wallet = this.dependencyManager.resolve(WalletModule)
this.oob = this.dependencyManager.resolve(OutOfBandModule)
}
Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/agent/__tests__/Agent.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { getBaseConfig } from '../../../tests/helpers'
import { InjectionSymbols } from '../../constants'
import { BasicMessageRepository, BasicMessageService } from '../../modules/basic-messages'
import { BasicMessagesModule } from '../../modules/basic-messages/BasicMessagesModule'
import { ConnectionsModule } from '../../modules/connections/ConnectionsModule'
import { BasicMessagesApi } from '../../modules/basic-messages/BasicMessagesApi'
import { ConnectionsApi } from '../../modules/connections/ConnectionsApi'
import { ConnectionRepository } from '../../modules/connections/repository/ConnectionRepository'
import { ConnectionService } from '../../modules/connections/services/ConnectionService'
import { TrustPingService } from '../../modules/connections/services/TrustPingService'
Expand Down Expand Up @@ -110,7 +110,7 @@ describe('Agent', () => {
const container = agent.dependencyManager

// Modules
expect(container.resolve(ConnectionsModule)).toBeInstanceOf(ConnectionsModule)
expect(container.resolve(ConnectionsApi)).toBeInstanceOf(ConnectionsApi)
expect(container.resolve(ConnectionService)).toBeInstanceOf(ConnectionService)
expect(container.resolve(ConnectionRepository)).toBeInstanceOf(ConnectionRepository)
expect(container.resolve(TrustPingService)).toBeInstanceOf(TrustPingService)
Expand All @@ -122,7 +122,7 @@ describe('Agent', () => {
expect(container.resolve(CredentialsApi)).toBeInstanceOf(CredentialsApi)
expect(container.resolve(CredentialRepository)).toBeInstanceOf(CredentialRepository)

expect(container.resolve(BasicMessagesModule)).toBeInstanceOf(BasicMessagesModule)
expect(container.resolve(BasicMessagesApi)).toBeInstanceOf(BasicMessagesApi)
expect(container.resolve(BasicMessageService)).toBeInstanceOf(BasicMessageService)
expect(container.resolve(BasicMessageRepository)).toBeInstanceOf(BasicMessageRepository)

Expand Down Expand Up @@ -152,7 +152,7 @@ describe('Agent', () => {
const container = agent.dependencyManager

// Modules
expect(container.resolve(ConnectionsModule)).toBe(container.resolve(ConnectionsModule))
expect(container.resolve(ConnectionsApi)).toBe(container.resolve(ConnectionsApi))
expect(container.resolve(ConnectionService)).toBe(container.resolve(ConnectionService))
expect(container.resolve(ConnectionRepository)).toBe(container.resolve(ConnectionRepository))
expect(container.resolve(TrustPingService)).toBe(container.resolve(TrustPingService))
Expand All @@ -164,7 +164,7 @@ describe('Agent', () => {
expect(container.resolve(CredentialsApi)).toBe(container.resolve(CredentialsApi))
expect(container.resolve(CredentialRepository)).toBe(container.resolve(CredentialRepository))

expect(container.resolve(BasicMessagesModule)).toBe(container.resolve(BasicMessagesModule))
expect(container.resolve(BasicMessagesApi)).toBe(container.resolve(BasicMessagesApi))
expect(container.resolve(BasicMessageService)).toBe(container.resolve(BasicMessageService))
expect(container.resolve(BasicMessageRepository)).toBe(container.resolve(BasicMessageRepository))

Expand Down
49 changes: 49 additions & 0 deletions packages/core/src/modules/basic-messages/BasicMessagesApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import type { BasicMessageTags } from './repository/BasicMessageRecord'

import { AgentContext } from '../../agent'
import { Dispatcher } from '../../agent/Dispatcher'
import { MessageSender } from '../../agent/MessageSender'
import { createOutboundMessage } from '../../agent/helpers'
import { injectable } from '../../plugins'
import { ConnectionService } from '../connections'

import { BasicMessageHandler } from './handlers'
import { BasicMessageService } from './services'

@injectable()
export class BasicMessagesApi {
private basicMessageService: BasicMessageService
private messageSender: MessageSender
private connectionService: ConnectionService
private agentContext: AgentContext

public constructor(
dispatcher: Dispatcher,
basicMessageService: BasicMessageService,
messageSender: MessageSender,
connectionService: ConnectionService,
agentContext: AgentContext
) {
this.basicMessageService = basicMessageService
this.messageSender = messageSender
this.connectionService = connectionService
this.agentContext = agentContext
this.registerHandlers(dispatcher)
}

public async sendMessage(connectionId: string, message: string) {
const connection = await this.connectionService.getById(this.agentContext, connectionId)

const basicMessage = await this.basicMessageService.createMessage(this.agentContext, message, connection)
const outboundMessage = createOutboundMessage(connection, basicMessage)
await this.messageSender.sendMessage(this.agentContext, outboundMessage)
}

public async findAllByQuery(query: Partial<BasicMessageTags>) {
return this.basicMessageService.findAllByQuery(this.agentContext, query)
}

private registerHandlers(dispatcher: Dispatcher) {
dispatcher.registerHandler(new BasicMessageHandler(this.basicMessageService))
}
}
50 changes: 4 additions & 46 deletions packages/core/src/modules/basic-messages/BasicMessagesModule.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,19 @@
import type { DependencyManager } from '../../plugins'
import type { BasicMessageTags } from './repository/BasicMessageRecord'

import { AgentContext } from '../../agent'
import { Dispatcher } from '../../agent/Dispatcher'
import { MessageSender } from '../../agent/MessageSender'
import { createOutboundMessage } from '../../agent/helpers'
import { injectable, module } from '../../plugins'
import { ConnectionService } from '../connections'
import { module } from '../../plugins'

import { BasicMessageHandler } from './handlers'
import { BasicMessagesApi } from './BasicMessagesApi'
import { BasicMessageRepository } from './repository'
import { BasicMessageService } from './services'

@module()
@injectable()
export class BasicMessagesModule {
private basicMessageService: BasicMessageService
private messageSender: MessageSender
private connectionService: ConnectionService
private agentContext: AgentContext

public constructor(
dispatcher: Dispatcher,
basicMessageService: BasicMessageService,
messageSender: MessageSender,
connectionService: ConnectionService,
agentContext: AgentContext
) {
this.basicMessageService = basicMessageService
this.messageSender = messageSender
this.connectionService = connectionService
this.agentContext = agentContext
this.registerHandlers(dispatcher)
}

public async sendMessage(connectionId: string, message: string) {
const connection = await this.connectionService.getById(this.agentContext, connectionId)

const basicMessage = await this.basicMessageService.createMessage(this.agentContext, message, connection)
const outboundMessage = createOutboundMessage(connection, basicMessage)
await this.messageSender.sendMessage(this.agentContext, outboundMessage)
}

public async findAllByQuery(query: Partial<BasicMessageTags>) {
return this.basicMessageService.findAllByQuery(this.agentContext, query)
}

private registerHandlers(dispatcher: Dispatcher) {
dispatcher.registerHandler(new BasicMessageHandler(this.basicMessageService))
}

/**
* Registers the dependencies of the basic message module on the dependency manager.
*/
public static register(dependencyManager: DependencyManager) {
public register(dependencyManager: DependencyManager) {
// Api
dependencyManager.registerContextScoped(BasicMessagesModule)
dependencyManager.registerContextScoped(BasicMessagesApi)

// Services
dependencyManager.registerSingleton(BasicMessageService)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { DependencyManager } from '../../../plugins/DependencyManager'
import { BasicMessagesApi } from '../BasicMessagesApi'
import { BasicMessagesModule } from '../BasicMessagesModule'
import { BasicMessageRepository } from '../repository'
import { BasicMessageService } from '../services'

jest.mock('../../../plugins/DependencyManager')
const DependencyManagerMock = DependencyManager as jest.Mock<DependencyManager>

const dependencyManager = new DependencyManagerMock()

describe('BasicMessagesModule', () => {
test('registers dependencies on the dependency manager', () => {
new BasicMessagesModule().register(dependencyManager)

expect(dependencyManager.registerContextScoped).toHaveBeenCalledTimes(1)
expect(dependencyManager.registerContextScoped).toHaveBeenCalledWith(BasicMessagesApi)

expect(dependencyManager.registerSingleton).toHaveBeenCalledTimes(2)
expect(dependencyManager.registerSingleton).toHaveBeenCalledWith(BasicMessageService)
expect(dependencyManager.registerSingleton).toHaveBeenCalledWith(BasicMessageRepository)
})
})
3 changes: 2 additions & 1 deletion packages/core/src/modules/basic-messages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ export * from './messages'
export * from './services'
export * from './repository'
export * from './BasicMessageEvents'
export * from './BasicMessagesModule'
export * from './BasicMessagesApi'
export * from './BasicMessageRole'
export * from './BasicMessagesModule'
Loading

0 comments on commit 5141660

Please sign in to comment.