Skip to content

Commit

Permalink
Merge pull request #104 from dvishal485/main
Browse files Browse the repository at this point in the history
update test notif code
  • Loading branch information
dvishal485 authored Aug 26, 2024
2 parents 0625934 + 2c70a8e commit 39d923b
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 8 deletions.
1 change: 1 addition & 0 deletions .env.local
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ HOST="http://team.dtutimes.com"
ENABLE_EMAIL="false" # set to true if you want to enable email service
TOTP_SECRET=--TOTP 2FA SECRET--
NOTIF_SECRET=--PASSWORD-- # this is for notifications
NOTIF_PRIVATE_KEY=--PASSWORD-- # this is for notifications
112 changes: 111 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"multer": "^1.4.5-lts.1",
"nodemailer": "^6.9.14",
"sharp": "^0.32.6",
"totp-generator": "^1.0.0"
"totp-generator": "^1.0.0",
"web-push": "^3.6.7"
},
"devDependencies": {
"@types/bcrypt": "^5.0.2",
Expand All @@ -38,6 +39,7 @@
"@types/multer": "^1.4.11",
"@types/node": "^20.14.12",
"@types/nodemailer": "^6.4.15",
"@types/web-push": "^3.6.3",
"@typescript-eslint/eslint-plugin": "^7.17.0",
"@typescript-eslint/parser": "^7.17.0",
"colors": "^1.4.0",
Expand Down
33 changes: 33 additions & 0 deletions src/api/controllers/notificationController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,39 @@ import {
Notification,
} from "../models/notificationModel";
import { Subscription } from "../models/subscriptionModel";
import webpush from "web-push";

export const push_notification = async () => {
const PRIVATE_KEY = process.env.NOTIF_PRIVATE_KEY;
if (!PRIVATE_KEY) {
throw new Error("Private key for notification API not configured");
}

const subscriptions = await Subscription.find();

const notificationPayload = JSON.stringify({
title: "New Notification",
body: "This is a new notification",
icon: "https://dtutimes.com/favicon.ico",
});

webpush.setVapidDetails(
`mailto:${process.env.EMAIL_SERVICE_USER}`,
"BCOsRaxpJeR0KyIPIg1rHx3pUtWVsGDGOxH65dDkqyU5ycF-CjPJxuqiXF4M0LpUMG_rk_YxSZX34uHbrV5umJQ",
PRIVATE_KEY,
);

for (const subscription of subscriptions) {
await webpush.sendNotification(subscription, notificationPayload);
}
};

export const test_notif = asyncErrorHandler(async (req, res, _next) => {
await push_notification();
res.json({
done: true,
});
});

export const save_notif = asyncErrorHandler(async (req, res, _next) => {
if (req.body.secret != process.env.NOTIF_SECRET) {
Expand Down
12 changes: 6 additions & 6 deletions src/api/models/subscriptionModel.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import mongoose, { Schema } from "mongoose";
import { type PushSubscription } from "web-push";

const susbcriberSchema = new Schema<
Omit<PushSubscriptionJSON, "expirationTime">
>({
const susbcriberSchema = new Schema<PushSubscription>({
endpoint: {
type: String,
required: true,
Expand All @@ -19,8 +18,9 @@ const susbcriberSchema = new Schema<
},
});

const Subscription = mongoose.model<
Omit<PushSubscriptionJSON, "expirationTime">
>("subscriber", susbcriberSchema);
const Subscription = mongoose.model<PushSubscription>(
"subscriber",
susbcriberSchema,
);

export { Subscription };
2 changes: 2 additions & 0 deletions src/api/routes/notificationRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import {
get_notif,
save_notif,
subscribe,
test_notif,
} from "../controllers/notificationController";

const router = express.Router();

router.route("/").get(get_notif);
router.route("/subscribe").post(subscribe);
router.route("/save").post(save_notif);
router.route("/test_notif").get(test_notif);

export default router;

0 comments on commit 39d923b

Please sign in to comment.