forked from credebl/credo-controller
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from Bhutan-NDI/reuse-connection
Added reuse connection event
- Loading branch information
Showing
2 changed files
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import type { ServerConfig } from '../utils/ServerConfig' | ||
import type { Agent, HandshakeReusedEvent } from '@credo-ts/core' | ||
|
||
import { OutOfBandEventTypes } from '@credo-ts/core' | ||
|
||
import { sendWebSocketEvent } from './WebSocketEvents' | ||
import { sendWebhookEvent } from './WebhookEvent' | ||
|
||
export const reuseConnectionEvents = async (agent: Agent, config: ServerConfig) => { | ||
agent.events.on(OutOfBandEventTypes.HandshakeReused, async (event: HandshakeReusedEvent) => { | ||
const body = { | ||
connectionRecord: event.payload.connectionRecord.toJSON(), | ||
outOfBandRecord: event.payload.outOfBandRecord.toJSON(), | ||
reuseThreadId: event.payload.reuseThreadId, | ||
...event.metadata, | ||
} | ||
|
||
// Only send webhook if webhook url is configured | ||
if (config.webhookUrl) { | ||
await sendWebhookEvent(config.webhookUrl + '/connections', body, agent.config.logger) | ||
} | ||
|
||
if (config.socketServer) { | ||
// Always emit websocket event to clients (could be 0) | ||
sendWebSocketEvent(config.socketServer, { | ||
...event, | ||
payload: { | ||
...event.payload, | ||
connectionRecord: body, | ||
}, | ||
}) | ||
} | ||
}) | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters