From e27ca6fcd6737164d2cc3104fb5102c3297d0bde Mon Sep 17 00:00:00 2001 From: Jack Date: Sun, 29 Aug 2021 14:30:45 +0100 Subject: [PATCH] added a psa system --- backend/api/controllers/psa.js | 14 +++++++++++++ backend/api/routes/psa.js | 11 ++++++++++ backend/dao/psa.js | 9 ++++++++ backend/server.js | 2 ++ gulpfile.js | 1 + src/js/account-controller.js | 2 ++ src/js/elements/notifications.js | 29 ++++++++++++++++++++++--- src/js/elements/psa.js | 36 ++++++++++++++++++++++++++++++++ 8 files changed, 101 insertions(+), 3 deletions(-) create mode 100644 backend/api/controllers/psa.js create mode 100644 backend/api/routes/psa.js create mode 100644 backend/dao/psa.js create mode 100644 src/js/elements/psa.js diff --git a/backend/api/controllers/psa.js b/backend/api/controllers/psa.js new file mode 100644 index 000000000..59da1ca1b --- /dev/null +++ b/backend/api/controllers/psa.js @@ -0,0 +1,14 @@ +const PsaDAO = require("../../dao/psa"); + +class PsaController { + static async get(req, res, next) { + try { + let data = await PsaDAO.get(); + return res.status(200).json(data); + } catch (e) { + return next(e); + } + } +} + +module.exports = PsaController; diff --git a/backend/api/routes/psa.js b/backend/api/routes/psa.js new file mode 100644 index 000000000..175029fa0 --- /dev/null +++ b/backend/api/routes/psa.js @@ -0,0 +1,11 @@ +const { authenticateRequest } = require("../../middlewares/auth"); +const PsaController = require("../controllers/psa"); +const RateLimit = require("../../middlewares/rate-limit"); + +const { Router } = require("express"); + +const router = Router(); + +router.get("/", RateLimit.limit1persec, PsaController.get); + +module.exports = router; diff --git a/backend/dao/psa.js b/backend/dao/psa.js new file mode 100644 index 000000000..156b4668f --- /dev/null +++ b/backend/dao/psa.js @@ -0,0 +1,9 @@ +const { mongoDB } = require("../init/mongodb"); + +class PsaDAO { + static async get(uid, config) { + return await mongoDB().collection("psa").find().toArray(); + } +} + +module.exports = PsaDAO; diff --git a/backend/server.js b/backend/server.js index c38a18165..02074d351 100644 --- a/backend/server.js +++ b/backend/server.js @@ -30,6 +30,8 @@ const presetRouter = require("./api/routes/preset"); app.use("/presets", presetRouter); const quoteRatings = require("./api/routes/quote-ratings"); app.use("/quote-ratings", quoteRatings); +const psaRouter = require("./api/routes/psa"); +app.use("/psa", psaRouter); app.use(function (e, req, res, next) { let uid = undefined; diff --git a/gulpfile.js b/gulpfile.js index 72aaaf8dd..2945a79f9 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -126,6 +126,7 @@ const refactoredSrc = [ "./src/js/elements/loader.js", "./src/js/elements/sign-out-button.js", "./src/js/elements/about-page.js", + "./src/js/elements/psa.js", "./src/js/popups/custom-text-popup.js", "./src/js/popups/quote-search-popup.js", diff --git a/src/js/account-controller.js b/src/js/account-controller.js index a220114ef..85aae7265 100644 --- a/src/js/account-controller.js +++ b/src/js/account-controller.js @@ -13,6 +13,7 @@ import * as DB from "./db"; import * as TestLogic from "./test-logic"; import * as UI from "./ui"; import axiosInstance from "./axios-instance"; +import * as PSA from "./psa"; export const gmailProvider = new firebase.auth.GoogleAuthProvider(); const githubProvider = new firebase.auth.GithubAuthProvider(); @@ -47,6 +48,7 @@ const authListener = firebase.auth().onAuthStateChanged(async function (user) { ChallengeController.setup(challengeName); }, 1000); } + PSA.show(); }); export function signIn() { diff --git a/src/js/elements/notifications.js b/src/js/elements/notifications.js index eb655cc6b..6b49efeb7 100644 --- a/src/js/elements/notifications.js +++ b/src/js/elements/notifications.js @@ -1,7 +1,14 @@ const notificationHistory = []; let id = 0; class Notification { - constructor(message, level, duration, customTitle, customIcon) { + constructor( + message, + level, + duration, + customTitle, + customIcon, + closeCallback = () => {} + ) { this.message = message; this.level = level; if (duration == undefined) { @@ -16,6 +23,7 @@ class Notification { this.customTitle = customTitle; this.customIcon = customIcon; this.id = id++; + this.closeCallback = closeCallback; } //level //0 - notice @@ -117,6 +125,7 @@ class Notification { 125, () => { $(`#notificationCenter .notif[id='${this.id}']`).remove(); + this.closeCallback(); } ); } @@ -124,8 +133,22 @@ class Notification { } } -export function add(message, level, duration, customTitle, customIcon) { +export function add( + message, + level, + duration, + customTitle, + customIcon, + closeCallback +) { notificationHistory.push( - new Notification(message, level, duration, customTitle, customIcon).show() + new Notification( + message, + level, + duration, + customTitle, + customIcon, + closeCallback + ).show() ); } diff --git a/src/js/elements/psa.js b/src/js/elements/psa.js new file mode 100644 index 000000000..81f4a27b5 --- /dev/null +++ b/src/js/elements/psa.js @@ -0,0 +1,36 @@ +import axiosInstance from "./axios-instance"; +import * as Notifications from "./notifications"; + +async function getLatest() { + let psa = await axiosInstance.get("/psa"); + return psa.data; +} + +export async function show() { + const latest = await getLatest(); + if (latest == null || latest.length == 0) { + clearMemory(); + return; + } + let localmemory = getMemory(); + latest.forEach((psa) => { + if (localmemory.includes(psa._id)) return; + Notifications.add(psa.message, -1, 0, "Announcement", "bullhorn", () => { + setMemory(psa._id); + }); + }); +} + +function setMemory(id) { + let list = getMemory(); + list.push(id); + window.localStorage.setItem("confirmedPSAs", JSON.stringify(list)); +} + +function clearMemory() { + window.localStorage.setItem("confirmedPSAs", JSON.stringify([])); +} + +function getMemory() { + return JSON.parse(window.localStorage.getItem("confirmedPSAs")) ?? []; +}