aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPinapelz <yukais@pinapelz.com>2025-10-03 13:18:47 -0700
committerPinapelz <yukais@pinapelz.com>2025-10-03 13:18:47 -0700
commit4da231aeb5bf3ee0296fb8626dae3ac99893a158 (patch)
treee952d79fe1c6f2ef6b7f65ac43b409ec4a7474b9
parent78aa1512446072fdec94c86360c9aee3ea932db1 (diff)
implement api call to save topic for given fcm token
-rw-r--r--middleware/src/app/api/notifications/get/route.ts2
-rw-r--r--middleware/src/app/api/notifications/set/route.ts30
-rw-r--r--site/src/components/NotificationButton.tsx11
3 files changed, 36 insertions, 7 deletions
diff --git a/middleware/src/app/api/notifications/get/route.ts b/middleware/src/app/api/notifications/get/route.ts
index 344e41b..6150822 100644
--- a/middleware/src/app/api/notifications/get/route.ts
+++ b/middleware/src/app/api/notifications/get/route.ts
@@ -7,7 +7,7 @@ const redis = new Redis({
token: process.env.KV_REST_API_TOKEN!,
});
-// /api/fcm/list?topic=<topic>
+// /api/notification/get?topic=<topic>
export async function GET(req: Request) {
const { searchParams } = new URL(req.url);
const topic = searchParams.get("topic");
diff --git a/middleware/src/app/api/notifications/set/route.ts b/middleware/src/app/api/notifications/set/route.ts
index 3a584fb..c87159f 100644
--- a/middleware/src/app/api/notifications/set/route.ts
+++ b/middleware/src/app/api/notifications/set/route.ts
@@ -6,7 +6,19 @@ const redis = new Redis({
token: process.env.KV_REST_API_TOKEN!,
});
-// /api/fcm?topic=<topic>&token=<fcm_token>&action=subscribe|unsubscribe
+const corsHeaders = {
+ "Access-Control-Allow-Origin": "*",
+ "Access-Control-Allow-Methods": "GET, POST, OPTIONS",
+ "Access-Control-Allow-Headers": "Content-Type, Authorization",
+};
+export async function OPTIONS() {
+ return new Response(null, {
+ status: 204,
+ headers: corsHeaders,
+ });
+}
+
+// /api/notification/set?topic=<topic>&token=<fcm_token>&action=subscribe|unsubscribe
export async function GET(req: Request) {
const { searchParams } = new URL(req.url);
const topic = searchParams.get("topic");
@@ -14,8 +26,12 @@ export async function GET(req: Request) {
const action = searchParams.get("action"); // "subscribe" | "unsubscribe"
if (!topic || !token || !action) {
- return new Response(JSON.stringify({ error: "Missing params" }), { status: 400 });
+ return new Response(JSON.stringify({ error: "Missing params" }), {
+ status: 400,
+ headers: corsHeaders,
+ });
}
+
const key = `fcm-${topic}`;
if (action === "subscribe") {
@@ -23,8 +39,14 @@ export async function GET(req: Request) {
} else if (action === "unsubscribe") {
await redis.srem(key, token);
} else {
- return new Response(JSON.stringify({ error: "Invalid action" }), { status: 400 });
+ return new Response(JSON.stringify({ error: "Invalid action" }), {
+ status: 400,
+ headers: corsHeaders,
+ });
}
- return new Response(JSON.stringify({ success: true }), { status: 200 });
+ return new Response(JSON.stringify({ success: true }), {
+ status: 200,
+ headers: corsHeaders,
+ });
}
diff --git a/site/src/components/NotificationButton.tsx b/site/src/components/NotificationButton.tsx
index 3e0342b..1e9bc5e 100644
--- a/site/src/components/NotificationButton.tsx
+++ b/site/src/components/NotificationButton.tsx
@@ -75,7 +75,10 @@ export default function NotificationButton({ className = "", isMoe = false, game
// TODO: Subscribe to topic via backend API
console.log(`Subscribing to topic: ${gameId} with token: ${token}`);
- // Stub for now - will make actual API call to backend to subscribe to topic
+ await fetch(import.meta.env.VITE_MIDDLEWARE_BASE_URL+`/api/notifications/set?topic=${gameId}&token=${token}&action=subscribe`, {
+ method: "GET",
+ headers: { "Content-Type": "application/json" },
+ });
// Update local storage to track subscribed topics
const subscribedTopics = JSON.parse(localStorage.getItem('subscribed_topics') || '[]');
@@ -106,7 +109,11 @@ export default function NotificationButton({ className = "", isMoe = false, game
// TODO: Unsubscribe from topic via backend API
console.log(`Unsubscribing from topic: ${gameId} with token: ${token}`);
- // Stub for now - will make actual API call to backend to unsubscribe from topic
+ await fetch(import.meta.env.VITE_MIDDLEWARE_BASE_URL+`/api/notifications/set?topic=${gameId}&token=${token}&action=unsubscribe`, {
+ method: "GET",
+ headers: { "Content-Type": "application/json" },
+ });
+
// Update local storage to remove topic
const subscribedTopics = JSON.parse(localStorage.getItem('subscribed_topics') || '[]');
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage