Skip to content

Commit

Permalink
Michiel/add link push (#602)
Browse files Browse the repository at this point in the history
* Add webpush link for push notifications

* Linting fixes

* Link formatting for push notifications

* Linting

* changelog

* Remove console.log

* Add networkSlug

* Linting
  • Loading branch information
michielmulders authored Apr 18, 2020
1 parent ef8a367 commit 42582fc
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 9 deletions.
1 change: 1 addition & 0 deletions changes/michiel_add-link-push
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[Changed] Add link paramter and formatting for push notifications @michielmulders
2 changes: 2 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ module.exports = {
testnetAPI: process.env.TESTNET_API_URL || 'http://localhost:9071',
SENTRY_DSN: process.env.SENTRY_DSN || '',
firebaseDatabaseUrl: process.env.PUSH_NOTIFICATIONS_DATABASE_URL || '',
firebasePushNotificationsFrontendUrl:
process.env.PUSH_NOTIFICATIONS_FRONTEND_URL || 'https://app.lunie.io',
firebaseAdminKeySet: !!process.env.GOOGLE_APPLICATION_CREDENTIALS
}
10 changes: 7 additions & 3 deletions lib/block-listeners/cosmos-node-subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ class CosmosNodeSubscription {
validators: validatorMap
})
publishBlockAdded(this.network.id, block)
// TODO remove, only for demo purposes
// publishEvent(this.network.id, 'block', '', block)

// For each transaction listed in a block we extract the relevant addresses. This is published to the network.
// A GraphQL resolver is listening for these messages and sends the
Expand All @@ -104,7 +102,13 @@ class CosmosNodeSubscription {
block.transactions.forEach(tx => {
tx.involvedAddresses.forEach(address => {
publishUserTransactionAddedV2(this.network.id, address, tx)
publishEvent(this.network.id, 'transaction', address, tx)
publishEvent(
this.network.id,
this.network.slug,
'transaction',
address,
tx
)
})
})
} catch (error) {
Expand Down
11 changes: 7 additions & 4 deletions lib/block-listeners/polkadot-node-subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,16 +153,19 @@ class PolkadotNodeSubscription {
}
})

// TODO remove, only for demo purposes
// publishEvent(this.network.id, 'block', '', block)

// For each transaction listed in a block we extract the relevant addresses. This is published to the network.
// A GraphQL resolver is listening for these messages and sends the
// transaction to each subscribed user.
block.transactions.forEach(tx => {
tx.involvedAddresses.forEach(address => {
publishUserTransactionAddedV2(this.network.id, address, tx)
publishEvent(this.network.id, 'transaction', address, tx)
publishEvent(
this.network.id,
this.network.slug,
'transaction',
address,
tx
)
})
})
} catch (error) {
Expand Down
16 changes: 15 additions & 1 deletion lib/pushNotifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ async function pushToTopic(topic, event) {
data: {
event: JSON.stringify(event)
},
topic
topic,
webpush: {
fcm_options: {
link: getPushLink(event.eventType, event.networkSlug)
}
}
}

try {
Expand All @@ -49,6 +54,15 @@ function getTopic({ networkId, eventType, resourceId }) {
return [resourceId, eventType, networkId].filter(x => !!x).join('_')
}

function getPushLink(eventType, networkSlug) {
switch (eventType) {
case 'transaction':
return `${config.firebasePushNotificationsFrontendUrl}/${networkSlug}/transactions`
default:
return config.firebasePushNotificationsFrontendUrl
}
}

// users need to be registered individually per topic
function subscribeUser(registrationToken, topics) {
return Promise.all(
Expand Down
3 changes: 2 additions & 1 deletion lib/subscriptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ module.exports = {
})
),

publishEvent: (networkId, eventType, resourceId, properties) =>
publishEvent: (networkId, networkSlug, eventType, resourceId, properties) =>
pubsub.publish(NEW_EVENT, {
event: {
networkId,
networkSlug,
eventType,
resourceId,
properties: JSON.stringify(properties)
Expand Down

0 comments on commit 42582fc

Please sign in to comment.