Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Create send notification endpoint
- Loading branch information
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { jsonError } from "../../common.js"; | ||
import { sendPushNotification } from "../../gcloud.js"; | ||
|
||
export async function onRequestPost(context: RequestContext) { | ||
const { current_user: currentUser } = context.data; | ||
|
||
if (!currentUser) return jsonError("Unauthorized", 401); | ||
|
||
if (!(currentUser.permissions & (1 << 0))) return jsonError("Forbidden", 403); | ||
|
||
const { body, title } = context.data.body; | ||
|
||
if (typeof body !== "string" || typeof title !== "string") | ||
return jsonError("Body and title must be strings", 400); | ||
|
||
if ( | ||
new Blob([ | ||
JSON.stringify({ | ||
message: { | ||
notification: { | ||
body, | ||
title, | ||
}, | ||
}, | ||
}), | ||
]).size > 4000 | ||
) | ||
return jsonError("Payload too large", 400); | ||
|
||
await sendPushNotification(context.env, title, body); | ||
|
||
return new Response(null, { | ||
status: 204, | ||
}); | ||
} |