Skip to content

Commit

Permalink
Merge pull request #56 from ethereum-push-notification-service/pre-re…
Browse files Browse the repository at this point in the history
…lease-1.1.13

Release 1.1.13
  • Loading branch information
0xNilesh authored Feb 22, 2024
2 parents 55a1d7d + 40b171a commit f66a1af
Show file tree
Hide file tree
Showing 34 changed files with 1,020 additions and 321 deletions.
26 changes: 26 additions & 0 deletions push-snap-site/components/buttons/DisableSnoozeButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export default function DisableSnoozeButton() {
const defaultSnapOrigin = `local:http://localhost:8080`;

const disableSnooze = async () => {
console.log(
await window.ethereum?.request({
method: "wallet_invokeSnap",
params: {
snapId: defaultSnapOrigin,
request: {
method: "pushproto_disablesnooze",
},
},
})
);
};

return (
<button
onClick={disableSnooze}
className="flex bg-white text-black font-bold text-sm p-2 rounded-lg border-2 border-text-secondary ring-1 ring-white"
>
Disable Snooze
</button>
);
}
20 changes: 0 additions & 20 deletions push-snap-site/components/buttons/SendMessageButton.tsx

This file was deleted.

46 changes: 46 additions & 0 deletions push-snap-site/components/buttons/SnoozeButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React, { useState } from 'react';

export default function SnoozeButton() {
const defaultSnapOrigin = `local:http://localhost:8080`;
const [snoozeDuration, setSnoozeDuration] = useState('');

const Snooze = async () => {
// Check if snoozeDuration is within the range of 1 to 72
const duration = parseInt(snoozeDuration);
if (duration >= 1 && duration <= 72) {
console.log(await window.ethereum?.request({
method: "wallet_invokeSnap",
params: {
snapId: defaultSnapOrigin,
request: {
method: 'pushproto_setsnoozeduration',
params: { snoozeDuration: snoozeDuration }
},
},
}));
} else {
// Display an error message if the input is invalid
console.error('Invalid input. Please enter a number between 1 and 72.');
}
};

return (
<div className='flex'>
<input
type="number"
min="1"
max="72"
value={snoozeDuration}
onChange={(e) => setSnoozeDuration(e.target.value)}
className="mr-2 p-2 border border-gray-300 rounded-lg grow"
placeholder="Enter duration (1-72)"
/>
<button
onClick={Snooze}
className="flex bg-white text-black font-bold text-sm p-2 rounded-lg border-2 border-text-secondary ring-1 ring-white"
>
Snooze
</button>
</div>
);
}
2 changes: 1 addition & 1 deletion push-snap-site/components/content/Note.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import SendMessageButton from "../buttons/SendMessageButton"
import SendMessageButton from "../buttons/SnoozeButton"
import { NOTE } from "@/utils/constants"

export default function Note() {
Expand Down
12 changes: 8 additions & 4 deletions push-snap-site/components/content/NotificationTests.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import SendMessageButton from "../buttons/SendMessageButton"
import DisableSnoozeButton from "../buttons/DisableSnoozeButton"
import SendMessageButton from "../buttons/SnoozeButton"
import { NOTIFICATION_TESTS } from "@/utils/constants"

export default function NotificationTests() {
return (
<div className="dark:bg-bg-secondary rounded-[20px] border-2 dark:border-white/30 w-[275px] h-[226px] p-6">
<div className="dark:bg-bg-secondary rounded-[20px] border-2 dark:border-white/30 w-[350px] h-auto p-6">
<h2 className="text-xl font-semibold">
{NOTIFICATION_TESTS.HEAD_1}
</h2>
<p className="text-sm pt-5 pb-7">
<p className="text-sm pt-5 pb-2">
{NOTIFICATION_TESTS.PARA_1}
</p>
<SendMessageButton/>
<p className="text-sm pt-5 pb-2">
{NOTIFICATION_TESTS.PARA_2}
</p>
<DisableSnoozeButton />
</div>
)
}

3 changes: 2 additions & 1 deletion push-snap-site/utils/constants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export const INSTALL_SNAP = {

export const NOTIFICATION_TESTS = {
HEAD_1: "Notification Tests",
PARA_1: "Check all the channels opted in and new notification will be delivered directly to your MetaMask Wallet."
PARA_1: "Set Snooze Duration (between 1 to 72 hrs)",
PARA_2: "Disable Snooze"
}

export const NOTE = {
Expand Down
2 changes: 1 addition & 1 deletion snap/dist/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion snap/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pushprotocol/snap",
"version": "1.1.12",
"version": "1.1.13",
"description": "Get Push Notifications directly in your MetaMask wallet!",
"repository": {
"type": "git",
Expand Down
10 changes: 8 additions & 2 deletions snap/snap.manifest.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"version": "1.1.12",
"version": "1.1.13",
"description": "Get Push Notifications directly in your MetaMask wallet!",
"proposedName": "Push",
"repository": {
"type": "git",
"url": "https://github.com/ethereum-push-notification-service/push-protocol-snaps"
},
"source": {
"shasum": "3h8QmRFISxpD5m+g41R5ZvNlMFMSe14ITrjkVnLwI9M=",
"shasum": "8nCT1jxp2n3KFx7rzJGAmJKO5XmSeAUrk97gE44Tde8=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand All @@ -31,6 +31,12 @@
"request": {
"method": "notifCronJob"
}
},
{
"expression": "0 * * * *",
"request": {
"method": "garbageCollectCronJob"
}
}
]
},
Expand Down
17 changes: 14 additions & 3 deletions snap/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,23 @@ export const allowedSnapOrigins = [
"https://app.push.org",
"https://staging.push.org",
"https://dev.push.org",
"http://localhost:3000",
"http://localhost:3000", // Remove localhost port before production deployment
];

export const BASE_URL = 'https://backend.epns.io/apis/v1'; // Modify this as needed

export const CHAIN_ID = 1; // For prod (change this while testing in diff env)

export const defaultLatestSnapState: LatestSnapState = {
version: 1,
addresses: {}
}
addresses: {},
pendingInAppNotifs: [],
popupsTimestamp: [],
snoozeInfo: {
enabledDuration: 0,
disabledDuration: 0
}
};

export const SNOOZE_ALERT_THRESHOLD = 6; // show snooze alert after these many popups appear in an hour
export const SNOOZE_DISABLE_DURATION = 24; // disable snooze alert for these many hrs once user rejects the alert
11 changes: 9 additions & 2 deletions snap/src/handlers/cronJobHandler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { OnCronjobHandler } from "@metamask/snaps-types";
import { notifCronJob } from "../methods";
import { garbageCollectCronJob, notifCronJob } from "../methods";
import { SnapCronJobMethod } from "../types";
import { getModifiedSnapState } from "../utils";

/**
* Handles cronjobs for the Snap, executing the appropriate method based on the request.
Expand All @@ -11,9 +12,15 @@ import { SnapCronJobMethod } from "../types";
*/
export const onCronjob: OnCronjobHandler = async ({ request }) => {
try {
// Just modify the state version
const state = await getModifiedSnapState({ encrypted: false });

switch (request.method as SnapCronJobMethod) {
case SnapCronJobMethod.NotifCronJob:
await notifCronJob();
await notifCronJob(state);
break;
case SnapCronJobMethod.GarbageCollectCronJob:
await garbageCollectCronJob(state);
break;
// case SnapCronJobMethod.CheckActivityCronJob:
// await checkActivityCronJob();
Expand Down
20 changes: 16 additions & 4 deletions snap/src/handlers/rpcRequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { ApiParams, ApiRequestParams, SnapRpcMethod } from "../types";
import {
addAddress,
channelOptin,
disableSnoozeNotifs,
getSnoozeInfo,
removeAddress,
setSnoozeEnabledDuration,
welcomeDialog,
} from "../methods";
import { getEnabledAddresses, getModifiedSnapState } from "../utils";
Expand Down Expand Up @@ -55,10 +58,19 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
// // Handles the togglePopup RPC method
// return togglePopup(apiParams);
// }
// case SnapRpcMethod.SnoozeDuration: {
// await snoozeDuration();
// break;
// }
case SnapRpcMethod.SetSnoozeDuration: {
// Handles the set snooze duration RPC method
await setSnoozeEnabledDuration(apiParams);
break;
}
case SnapRpcMethod.GetSnoozeInfo: {
return getSnoozeInfo(apiParams);
}
case SnapRpcMethod.DisableSnoozeNotifs: {
// Handles the disable snooze duration RPC method
await disableSnoozeNotifs(apiParams);
break;
}
case SnapRpcMethod.OptIn: {
// Handles the optIn RPC method
return channelOptin(apiParams);
Expand Down
43 changes: 43 additions & 0 deletions snap/src/methods/cronJobs/garbageCollectCronJob.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { LatestSnapState } from "../../types";
import { getPopupsCountSinceTimestamp, updateSnapState } from "../../utils";

/**
* Performs garbage collection on the snapshot state by removing popups older than two hours.
* @param state - The latest snapshot state.
* @returns A promise that resolves when the garbage collection is complete.
*/
export const garbageCollectCronJob = async (
state: LatestSnapState
): Promise<void> => {
try {
const updatedState = { ...state };
if (updatedState.popupsTimestamp.length === 0) return;

// need to remove all popupsTimestamp storage that came two hours ago
const lastTwoHourTimestamp = Date.now() - 2 * 60 * 60 * 1000; // Timestamp of two hours ago
const popupsCountSinceLastTwoHours = getPopupsCountSinceTimestamp(
updatedState,
lastTwoHourTimestamp
); // Get the number of popups shown since two hours ago

// Calculate the number of popups to remove
const popupsToRemove = Math.max(
0,
updatedState.popupsTimestamp.length - popupsCountSinceLastTwoHours
);

// Remove the extra popups from the beginning of the array
updatedState.popupsTimestamp.splice(0, popupsToRemove);

// update the state
await updateSnapState({
newState: updatedState,
encrypted: false,
});
} catch (error) {
// Handle or log the error as needed
console.error("Error in garbageCollectCronJob:", error);
// Optionally rethrow the error if you want it to propagate further
throw error;
}
};
1 change: 1 addition & 0 deletions snap/src/methods/cronJobs/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./notifCronJob";
// export * from "./checkActivityCronJob";
// export * from "./removeSnoozeCronJob";
export * from "./garbageCollectCronJob";
Loading

0 comments on commit f66a1af

Please sign in to comment.