-
Notifications
You must be signed in to change notification settings - Fork 15
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
chore: refactor providers #90
Conversation
bonustrack
commented
Aug 16, 2023
•
edited
Loading
edited
- Move each notification service providers and their function to a separated file
- Load proposal and subscribed addresses before sending notifications
for await (const walletsChunk of walletsChunks) { | ||
await beams.publishToInterests(walletsChunk, { | ||
web: { | ||
notification: { | ||
title: event.space, | ||
body: proposal.title, | ||
deep_link: `${process.env.SNAPSHOT_URI}/#/${event.space}/${event.id}` | ||
} | ||
} | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it could be improved by making requests execute in parallel
for await (const walletsChunk of walletsChunks) { | |
await beams.publishToInterests(walletsChunk, { | |
web: { | |
notification: { | |
title: event.space, | |
body: proposal.title, | |
deep_link: `${process.env.SNAPSHOT_URI}/#/${event.space}/${event.id}` | |
} | |
} | |
}); | |
const allPromises = walletsChunks.map(walletsChunk => | |
beams.publishToInterests(walletsChunk, { | |
web: { | |
notification: { | |
title: event.space, | |
body: proposal.title, | |
deep_link: `${process.env.SNAPSHOT_URI}/#/${event.space}/${event.id}` | |
} | |
} | |
}) | |
); | |
await Promise.all(allPromises); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could even go one step further, by extracting the beams.publishToInterests
to a sendEvent
function, to be consistent with webhook.ts : send
for building the batch, and sendEvent
to sent each individual request.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same for the discord side, we already have a sendMessage
function, should be renamed to sendEvent
, and all providers will have the same API
import { timeOutgoingRequest } from '../helpers/metrics'; | ||
|
||
const HTTP_WEBHOOK_TIMEOUT = 15000; | ||
const serviceEventsSalt = parseInt(process.env.SERVICE_EVENTS_SALT || '12345'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: no need to use parseInt
event.token = sha256(`${to}${serviceEventsSalt}`); | ||
event.secret = sha256(`${to}${serviceEventsSalt}`); | ||
const headerSecret = sha256(`${to}${process.env.SERVICE_EVENTS_SALT}`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could be changed to
event.token = sha256(`${to}${serviceEventsSalt}`); | |
event.secret = sha256(`${to}${serviceEventsSalt}`); | |
const headerSecret = sha256(`${to}${process.env.SERVICE_EVENTS_SALT}`); | |
const headerSecret = sha256(`${to}${serviceEventsSalt}`); | |
event.token = sha256(headerSecret); | |
event.secret = sha256(headerSecret); |
And probably better to use serviceEventsSalt
instead of process.env.SERVICE_EVENTS_SALT
since the last one could be empty
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hum here it would do a sha256 twice.
sendBeams(event, proposal, subscribers); | ||
sendDiscord(event, proposal, subscribers); | ||
sendWebhook(event, proposal, subscribers); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could create a new file providers/index.ts, with content :
import { send as sendDiscord } from './discord';
import { send as sendBeams } from './beams';
import { send as sendWebhook } from './webhook';
export default function send(event, proposal, subscribers) {
sendBeams(event, proposal, subscribers);
sendDiscord(event, proposal, subscribers);
sendWebhook(event, proposal, subscribers);
}
So that events.ts could just import
import send from './providers';
and use a single send call
send(event, proposal, subscribers);
Co-authored-by: Dmytro Tolok <[email protected]>