Skip to content

Commit

Permalink
refactor: cleanup custom logic
Browse files Browse the repository at this point in the history
Signed-off-by: Sai Ranjit Tummalapalli <[email protected]>
  • Loading branch information
sairanjit committed Dec 20, 2024
1 parent 592881a commit bd1966a
Show file tree
Hide file tree
Showing 10 changed files with 340 additions and 728 deletions.
5 changes: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ POSTGRES_ADMIN_USER=
POSTGRES_ADMIN_PASSWORD=

USE_PUSH_NOTIFICATIONS='true'
NOTIFICATION_WEBHOOK_URL=
NOTIFICATION_WEBHOOK_URL=

# We need NGROK Auth token in development and you can get one from here https://dashboard.ngrok.com/get-started/your-authtoken
NGROK_AUTH_TOKEN=
1 change: 1 addition & 0 deletions dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const port = 3000
// TODO: have to add auth token now to use ngrok check this later
void connect({
port,
authtoken: '2TkNMfvaGeeU7o4xNy8aBGPWQOg_kFxWtNs8DsZowhFYfETY',
}).then((app) => {
// eslint-disable-next-line no-console
console.log('Got ngrok url:', app.url())
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
"postinstall": "npx patch-package"
},
"dependencies": {
"@credo-ts/askar": "0.5.6",
"@credo-ts/core": "0.5.6",
"@credo-ts/node": "0.5.6",
"@hyperledger/aries-askar-nodejs": "0.2.1",
"@credo-ts/askar": "^0.5.0",
"@credo-ts/core": "^0.5.0",
"@credo-ts/node": "^0.5.0",
"@hyperledger/aries-askar-nodejs": "^0.2.0",
"dotenv": "^16.0.1",
"express": "^4.18.1",
"prettier": "^2.8.4",
Expand Down
2 changes: 0 additions & 2 deletions src/agent.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { AskarModule, AskarMultiWalletDatabaseScheme } from '@credo-ts/askar'
import {
Agent,
CacheModule,
ConnectionsModule,
DidCommMimeType,
HttpOutboundTransport,
InMemoryLruCache,
MediatorModule,
OutOfBandRole,
OutOfBandState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export class PushNotificationsFcmSetDeviceInfoMessage extends AgentMessage {
this.id = options.id ?? this.generateId()
this.deviceToken = options.deviceToken
this.devicePlatform = options.devicePlatform
this.clientCode = options.clientCode
}
}

Expand All @@ -35,11 +34,6 @@ export class PushNotificationsFcmSetDeviceInfoMessage extends AgentMessage {
@ValidateIf((object, value) => value !== null)
public devicePlatform!: string | null

@Expose({ name: 'client_code' })
@IsString()
@ValidateIf((object, value) => value !== null)
public clientCode!: string | null

@IsValidMessageType(PushNotificationsFcmSetDeviceInfoMessage.type)
public readonly type = PushNotificationsFcmSetDeviceInfoMessage.type.messageTypeUri
public static readonly type = parseMessageType('https://didcomm.org/push-notifications-fcm/1.0/set-device-info')
Expand Down
1 change: 0 additions & 1 deletion src/push-notifications/fcm/models/FcmDeviceInfo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export type FcmDeviceInfo = {
deviceToken: string | null
devicePlatform: string | null
clientCode: string | null
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export interface PushNotificationsFcmStorageProps {
id?: string
deviceToken: string | null
devicePlatform: string | null
clientCode: string | null
connectionId: string
tags?: CustomPushNotificationsFcmTags
}
Expand All @@ -24,7 +23,6 @@ export class PushNotificationsFcmRecord extends BaseRecord<
public deviceToken!: string | null
public devicePlatform!: string | null
public connectionId!: string
public clientCode!: string | null

public static readonly type = 'PushNotificationsFcmRecord'
public readonly type = PushNotificationsFcmRecord.type
Expand All @@ -38,7 +36,6 @@ export class PushNotificationsFcmRecord extends BaseRecord<
this.deviceToken = props.deviceToken
this.connectionId = props.connectionId
this._tags = props.tags ?? {}
this.clientCode = props.clientCode
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export class PushNotificationsFcmService {
threadId,
deviceToken: deviceInfo.deviceToken,
devicePlatform: deviceInfo.devicePlatform,
clientCode: deviceInfo.clientCode,
})
}

Expand Down Expand Up @@ -74,7 +73,6 @@ export class PushNotificationsFcmService {
connectionId: connection.id,
deviceToken: message.deviceToken,
devicePlatform: message.devicePlatform,
clientCode: message.clientCode,
})

await this.pushNotificationsFcmRepository.save(agentContext, pushNotificationsFcmRecord)
Expand Down
19 changes: 6 additions & 13 deletions src/storage/StorageMessageQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,16 @@ import fetch from 'node-fetch'
export interface NotificationMessage {
messageType: string
token: string
clientCode: string
}

@injectable()
export class StorageServiceMessageQueue implements MessagePickupRepository {
private messageRepository: MessageRepository
private agentContext: AgentContext
private pushNotificationsFcmRepository: PushNotificationsFcmRepository

public constructor(
messageRepository: MessageRepository,
agentContext: AgentContext,
pushNotificationsFcmRepository: PushNotificationsFcmRepository
) {
public constructor(messageRepository: MessageRepository, agentContext: AgentContext) {
this.messageRepository = messageRepository
this.agentContext = agentContext
this.pushNotificationsFcmRepository = pushNotificationsFcmRepository
}

public async getAvailableMessageCount(options: GetAvailableMessageCountOptions) {
Expand Down Expand Up @@ -114,8 +107,10 @@ export class StorageServiceMessageQueue implements MessagePickupRepository {

private async sendNotification(agentContext: AgentContext, connectionId: string, messageType?: string) {
try {
const pushNotificationsFcmRepository = agentContext.dependencyManager.resolve(PushNotificationsFcmRepository)

// Get the device token for the connection
const pushNotificationFcmRecord = await this.pushNotificationsFcmRepository.findSingleByQuery(agentContext, {
const pushNotificationFcmRecord = await pushNotificationsFcmRepository.findSingleByQuery(agentContext, {
connectionId,
})

Expand All @@ -127,8 +122,7 @@ export class StorageServiceMessageQueue implements MessagePickupRepository {
// Prepare a message to be sent to the device
const message: NotificationMessage = {
messageType: messageType || 'default',
token: pushNotificationFcmRecord?.deviceToken || '',
clientCode: pushNotificationFcmRecord?.clientCode || '',
token: pushNotificationFcmRecord?.deviceToken,
}

this.agentContext.config.logger.info(`Sending notification to ${pushNotificationFcmRecord?.connectionId}`)
Expand All @@ -144,9 +138,8 @@ export class StorageServiceMessageQueue implements MessagePickupRepository {
private async processNotification(message: NotificationMessage) {
try {
const body = {
fcmToken: message.token || 'abc',
fcmToken: message.token,
messageType: message.messageType,
clientCode: message.clientCode || '5b4d6bc6-362e-4f53-bdad-ee2742bc0de3',
}
const requestOptions = {
method: 'POST',
Expand Down
Loading

0 comments on commit bd1966a

Please sign in to comment.