added a psa system

This commit is contained in:
Jack 2021-08-29 14:30:45 +01:00
parent 09fd2ac6d7
commit e27ca6fcd6
8 changed files with 101 additions and 3 deletions

View file

@ -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;

11
backend/api/routes/psa.js Normal file
View file

@ -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;

9
backend/dao/psa.js Normal file
View file

@ -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;

View file

@ -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;

View file

@ -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",

View file

@ -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() {

View file

@ -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()
);
}

36
src/js/elements/psa.js Normal file
View file

@ -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")) ?? [];
}