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

chore: refactor providers #90

Merged
merged 2 commits into from
Aug 17, 2023
Merged

chore: refactor providers #90

merged 2 commits into from
Aug 17, 2023

Conversation

bonustrack
Copy link
Member

@bonustrack bonustrack commented Aug 16, 2023

  • Move each notification service providers and their function to a separated file
  • Load proposal and subscribed addresses before sending notifications

@ChaituVR ChaituVR requested a review from wa0x6e August 16, 2023 13:29
Comment on lines +18 to +27
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}`
}
}
});
Copy link
Contributor

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

Suggested change
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);

Copy link
Contributor

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.

Copy link
Contributor

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');
Copy link
Contributor

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

Comment on lines +11 to +13
event.token = sha256(`${to}${serviceEventsSalt}`);
event.secret = sha256(`${to}${serviceEventsSalt}`);
const headerSecret = sha256(`${to}${process.env.SERVICE_EVENTS_SALT}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be changed to

Suggested change
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

Copy link
Member Author

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.

src/helpers/utils.ts Outdated Show resolved Hide resolved
Comment on lines +81 to +83
sendBeams(event, proposal, subscribers);
sendDiscord(event, proposal, subscribers);
sendWebhook(event, proposal, subscribers);
Copy link
Contributor

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]>
src/events.ts Show resolved Hide resolved
@bonustrack bonustrack merged commit 844955b into master Aug 17, 2023
1 check passed
@bonustrack bonustrack deleted the fabien/refactor branch August 17, 2023 17:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants