Skip to content

Commit

Permalink
Merge pull request #1 from RustCONxyz/develop
Browse files Browse the repository at this point in the history
v1.1.0
  • Loading branch information
kop7er committed Oct 24, 2023
2 parents ec85f89 + 2867ada commit a74103c
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 5 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "rustcon-api",
"version": "1.0.0",
"description": "RustCON API - Notifications, Current Versions, ...",
"version": "1.1.0",
"description": "RustCON API - Notifications, Current Versions...",
"type": "module",
"main": "dist/index.js",
"scripts": {
Expand Down
137 changes: 136 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
import "dotenv/config";
import express from "express";
import { Expo } from "expo-server-sdk";
import { Expo, type ExpoPushMessage, type ExpoPushTicket } from "expo-server-sdk";

import connectToDatabase from "./database/index.js";

import PushToken from "./database/models/PushToken.js";
import Version from "./database/models/Version.js";

import type RustToolsWebhook from "./interfaces/RustToolsWebhook.js";

const app = express();

const expo = new Expo({ accessToken: process.env.EXPO_ACCESS_TOKEN });

const notificationMessages = new Map([
["clientUpdates", { title: "Client Update", body: "A new Rust client update is available!"}],
["serverUpdates", { title: "Server Update", body: "A new Rust server update is available!"}],
["oxideUpdates", { title: "Oxide Update", body: "An update for Oxide has been released!"}],
["carbonUpdates", { title: "Carbon Update", body: "An update for Carbon has been released!"}],
["protocolUpdates", { title: "Protocol Update", body: "The protocol has been updated, which means the lastest update is mandatory!"}]
]);

app.use(express.json());

app.get("/", (req, res) => {
Expand All @@ -33,6 +45,8 @@ app.get("/versions", async (req, res) => {

versions.forEach((version) => {

version = version.toObject();

currentVersions[version.type] = {

version: version.version,
Expand Down Expand Up @@ -97,6 +111,127 @@ app.post("/rusttools-webhook", isAuthed, async (req, res) => {

res.sendStatus(200);

const webhook = req.body as RustToolsWebhook;

if (webhook.event !== "update") {

return;

}

const webhookData = webhook.data;

const pushTokens = await PushToken.find({ [`settings.${webhookData.type}Updates`]: true });

const messages: ExpoPushMessage[] = [];

const notificationMessage = notificationMessages.get(`${webhookData.type}Updates`);

if (!notificationMessage) {

return;

}

pushTokens.forEach(pushToken => {

messages.push({

to: pushToken.token,

sound: "default",

title: notificationMessage.title,

body: notificationMessage.body,

});

});

const chunks = expo.chunkPushNotifications(messages);

const tickets: ExpoPushTicket[] = [];

(async () => {

for (const chunk of chunks) {

try {

const ticketChunk = await expo.sendPushNotificationsAsync(chunk);

tickets.push(...ticketChunk);

} catch (error) {

console.error(error);

}

}

})();

const receiptIds = [];

for (let ticket of tickets) {

if (ticket.status === "ok") {

receiptIds.push(ticket.id);

}

}

const receiptIdChunks = expo.chunkPushNotificationReceiptIds(receiptIds);

(async () => {

for (let chunk of receiptIdChunks) {

try {

let receipts = await expo.getPushNotificationReceiptsAsync(chunk);

for (let receiptId in receipts) {

const receipt = receipts[receiptId];

if (receipt.status === "ok") {

continue;

} else if (receipt.status === "error") {

console.error(`There was an error sending a notification: ${receipt.message}`);

if (receipt.details && receipt.details.error) {

if (receipt.details.error === "DeviceNotRegistered") {

// @ts-ignore
await PushToken.deleteOne({ expoPushToken: tickets.find(ticket => ticket.id === receiptId)?.to });

}

}

}

}

} catch (error) {

console.error(error);

}

}

})();

});

app.listen(3000, () => {
Expand Down

0 comments on commit a74103c

Please sign in to comment.