aboutsummaryrefslogtreecommitdiffstats
path: root/middleware/src
diff options
context:
space:
mode:
authorPinapelz <yukais@pinapelz.com>2025-10-03 13:07:33 -0700
committerPinapelz <yukais@pinapelz.com>2025-10-03 13:07:33 -0700
commit78aa1512446072fdec94c86360c9aee3ea932db1 (patch)
treeb6d281839c76fca86a060e96d3c0b3a757784e78 /middleware/src
parentab8d1306504bfefdea293172d7e645f83114d50f (diff)
add redis fcm notif topic saving
Diffstat (limited to 'middleware/src')
-rw-r--r--middleware/src/app/api/notifications/get/route.ts26
-rw-r--r--middleware/src/app/api/notifications/set/route.ts30
2 files changed, 56 insertions, 0 deletions
diff --git a/middleware/src/app/api/notifications/get/route.ts b/middleware/src/app/api/notifications/get/route.ts
new file mode 100644
index 0000000..344e41b
--- /dev/null
+++ b/middleware/src/app/api/notifications/get/route.ts
@@ -0,0 +1,26 @@
+import { Redis } from "@upstash/redis";
+
+export const runtime = "edge";
+
+const redis = new Redis({
+ url: process.env.KV_REST_API_URL!,
+ token: process.env.KV_REST_API_TOKEN!,
+});
+
+// /api/fcm/list?topic=<topic>
+export async function GET(req: Request) {
+ const { searchParams } = new URL(req.url);
+ const topic = searchParams.get("topic");
+
+ if (!topic) {
+ return new Response(JSON.stringify({ error: "Missing topic" }), { status: 400 });
+ }
+
+ const key = `fcm-${topic}`;
+ const tokens = await redis.smembers<string[]>(key);
+
+ return new Response(JSON.stringify({ topic, tokens }), {
+ status: 200,
+ headers: { "Content-Type": "application/json" },
+ });
+}
diff --git a/middleware/src/app/api/notifications/set/route.ts b/middleware/src/app/api/notifications/set/route.ts
new file mode 100644
index 0000000..3a584fb
--- /dev/null
+++ b/middleware/src/app/api/notifications/set/route.ts
@@ -0,0 +1,30 @@
+import { Redis } from "@upstash/redis";
+
+export const runtime = "edge";
+const redis = new Redis({
+ url: process.env.KV_REST_API_URL!,
+ token: process.env.KV_REST_API_TOKEN!,
+});
+
+// /api/fcm?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");
+ const token = searchParams.get("token");
+ const action = searchParams.get("action"); // "subscribe" | "unsubscribe"
+
+ if (!topic || !token || !action) {
+ return new Response(JSON.stringify({ error: "Missing params" }), { status: 400 });
+ }
+ const key = `fcm-${topic}`;
+
+ if (action === "subscribe") {
+ await redis.sadd(key, token);
+ } 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({ success: true }), { status: 200 });
+}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage