From e908cea93c626ab7556655fba2378fe9e0496522 Mon Sep 17 00:00:00 2001 From: Saahil Date: Thu, 27 Mar 2025 11:18:10 -0400 Subject: [PATCH] fix: new stuff --- src/index.ts | 2 +- src/modules/flightly.ts | 45 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 8f3274f..b1588dd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -30,7 +30,7 @@ app.dbs = {}; app.dbs.ddm = new JSONdb("data/discord-datamining.json"); app.dbs.memdebug = new JSONdb("data/memdb.json"); app.dbs.seven39 = new JSONdb("data/739.json"); - +app.dbs.flightly = new JSONdb("data/flightly.json"); app.dbs.anondm = new EncryptedJsonDb("data/anondm.json", { password: process.env.ANONDM_PASSWORD, }); diff --git a/src/modules/flightly.ts b/src/modules/flightly.ts index 8bedf33..264c608 100644 --- a/src/modules/flightly.ts +++ b/src/modules/flightly.ts @@ -1,5 +1,6 @@ import dayjs from "dayjs"; import ms from "ms"; +import { ModifiedApp } from "./slackapp"; export interface FlightData { flight: Flight; flightPosition: FlightPosition[]; @@ -159,7 +160,51 @@ export function getFlightData(flightId: string): Promise { } }); } +function detectChanges(newData, previousData) { + if (!previousData) { + console.log("Initial flight data loaded."); + previousData = newData; + return; + } + let changes = []; + function compareObjects(oldObj, newObj, path = "") { + for (let key in newObj) { + let newPath = path ? `${path}.${key}` : key; + if (typeof newObj[key] === "object" && newObj[key] !== null) { + compareObjects(oldObj[key] || {}, newObj[key], newPath); + } else if (oldObj[key] !== newObj[key]) { + changes.push(`${newPath}: ${oldObj[key]} → ${newObj[key]}`); + } + } + } + + compareObjects(previousData, newData); + + if (changes.length > 0) { + return changes; + console.log("Flight data changed:"); + console.log(changes.join("\n")); + } else { + // console.log("No changes detected."); + } + + // previousData = newData; +} + +export async function cronForTrackingData(app: ModifiedApp) { + const IdsToTrack = app.dbs.flightly.get("flightly-ids") + for(const {flightId, userId} of IdsToTrack){ + const flightD = await getFlightData(flightId); + const changes = await detectChanges(flightD, app.dbs.flightly.get(userId)); + await app.client.chat.postMessage({ + channel: userId, + text: changes.join("\n") + }) + app.dbs.flightly.set(userId, flightD); + await new Promise(r => setTimeout(r, 1000)); + } +} export async function getTextVersionOfData(flightId: string) { const flightD = await getFlightData(flightId); return `