Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: chat notifs compatibility #44

Merged
merged 2 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 66 additions & 11 deletions src/helpers/utilsHelper.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ethers } from 'ethers'
import * as apn from 'apn'
import config from "../config"
import crypto from "crypto"
import config from '../config'
import crypto from 'crypto'
module.exports = {
// To Generate Random Password
generateRandomWord: (length, includeSpecial) => {
/* GENERATE RANDOM WORD */
generateRandomWord: (length: number, includeSpecial: boolean) => {
var result = ''
var characters =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
Expand All @@ -21,11 +21,15 @@ module.exports = {
return result
},

isValidAddress: (address) => {
/* VALIDATES EVM ADDRESS */
isValidAddress: (address: string) => {
return ethers.utils.isAddress(address)
},

// nft:eip155:nftChainId:nftContractAddress:nftTokenId:RandomHash
/**
* VALIDATES NFT ADDRESS
* nft:eip155:nftChainId:nftContractAddress:nftTokenId:RandomHash
*/
isValidNFTAddress: (address: string): boolean => {
const addressComponents = address.split(':')
const epochRegex = /^[0-9]{10}$/
Expand Down Expand Up @@ -74,6 +78,7 @@ module.exports = {
return false
}
},

generateAndrioidVideoCallPayloadFromFeed: (feedPayload) => {
let payload = {
data: feedPayload.notification,
Expand Down Expand Up @@ -108,8 +113,13 @@ module.exports = {
return payload
},

generateIOSVideoCallPayloadFromFeed: (feedPayload, apnConfig = config.apnConfig) => {
const sender = JSON.parse(feedPayload.data.additionalMeta.data).senderAddress
generateIOSVideoCallPayloadFromFeed: (
feedPayload,
apnConfig = config.apnConfig
) => {
const sender = JSON.parse(
feedPayload.data.additionalMeta.data
).senderAddress
const shorthandSenderAdress =
sender.substring(0, 4) + '....' + sender.substring(38)
const note = new apn.Notification()
Expand All @@ -129,18 +139,23 @@ module.exports = {
handle: shorthandSenderAdress,
details,
status: 1, // VideoCallStatus.INITIALIZED,
uuid: crypto.randomUUID()
uuid: crypto.randomUUID(),
}
note.topic =
config.deliveryNodesNet == 'STAGING' || config.deliveryNodesNet == 'DEV'
config.deliveryNodesNet == 'STAGING' ||
config.deliveryNodesNet == 'DEV'
? 'io.epns.epnsstaging.voip'
: config.deliveryNodesNet == 'PROD'
? 'io.epns.epnsproject.voip'
: ''
return note
},

generateMessagingPayloadFromFeed: (feedPayload) => {
/**
* GENERATE FCM MESSAGING PAYLOAD FROM FEED
* Used only for `web` platform tokens
*/
generateWebMessagingPayloadFromFeed: (feedPayload) => {
let payload = {
notification: feedPayload.notification,
apns: {
Expand Down Expand Up @@ -180,6 +195,46 @@ module.exports = {
return payload
},

/**
* GENERATE FCM MESSAGING PAYLOAD FROM FEED
* Used only for `android` | `ios` platform tokens
*/
generateMobileMessagingPayloadFromFeed: (feedPayload) => {
return {
type:
feedPayload.data.app === 'Push Chat'
? 'PUSH_NOTIFICATION_CHANNEL'
: 'PUSH_NOTIFICATION_CHAT',
data: {
notification: {
...feedPayload.notification,
image: feedPayload.data.icon,
},
},
apns: {
payload: {
aps: {
'content-available': 1,
'mutable-content': 1,
category: 'withappicon',
},
},
headers: {
// In future additionalMeta will be used for multiple useCases which will be high priority
'apns-priority':
feedPayload.data.additionalMeta !== null ? '10' : '5',
},
},
android: {
priority:
// In future additionalMeta will be used for multiple useCases which will be high priority
feedPayload.data.additionalMeta !== null
? 'high'
: 'normal',
},
}
},

/**
* @param addressinCAIP This address can be in the CAIP10 format (example: eip155:1:0xabc) or in the changed format eip155:0xabc (without the chainId). When this happens, the chainId will be null
* @returns
Expand Down
14 changes: 8 additions & 6 deletions src/services/feedProcessorService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export default class FeedsService {
voip: boolean
) {
if (!voip) {
return utils.generateMessagingPayloadFromFeed(feed)
return platform === config.platformEnum.web
? utils.generateWebMessagingPayloadFromFeed(feed)
: utils.generateMobileMessagingPayloadFromFeed(feed)
} else {
if (voip && platform == config.platformEnum.android)
return utils.generateAndrioidVideoCallPayloadFromFeed(feed)
Expand Down Expand Up @@ -48,11 +50,11 @@ export default class FeedsService {
)
return
}
if( feed.payload.data.additionalMeta && JSON.parse(feed.payload.data.additionalMeta.data).status == 4){
logger.info(
'Cancel video call feed sid:: %o ',
feed.sid
)
if (
feed.payload.data.additionalMeta &&
JSON.parse(feed.payload.data.additionalMeta.data).status == 4
) {
logger.info('Cancel video call feed sid:: %o ', feed.sid)
return
}
const pushTokens = Container.get(PushTokensService)
Expand Down