The end is near

This commit is contained in:
asyncnomi 2022-12-15 16:23:33 +01:00
parent a4a58c2c55
commit 2c90067310

259
index.js
View file

@ -13,8 +13,20 @@ if (!fs.existsSync(activityPath)) {
fs.writeFileSync(activityPath, "{}");
}
var treasurePath = "treasuredata.txt";
if (!fs.existsSync(treasurePath)) {
fs.writeFileSync(treasurePath, "{}");
}
var goldenUsersPath = "goldenusers.txt";
if (!fs.existsSync(goldenUsersPath)) {
fs.writeFileSync(goldenUsersPath, "{}");
}
let PrankData = JSON.parse(fs.readFileSync(prankPath));
let ActivityData = JSON.parse(fs.readFileSync(activityPath));
let TreasureData = JSON.parse(fs.readFileSync(treasurePath));
let GoldenUsers = JSON.parse(fs.readFileSync(goldenUsersPath));
let AdminUsersUid = ["asyncnomi", "johan", "enthalpine", "fas", "arina", "billy", "remi", "pierre", "matmaz", "", "", ""];
let UsersToken = {};
let TokenDurationSecond = 3600;
@ -96,7 +108,7 @@ fastify.post('/addPrank', async (request, reply) => {
if ("type" in content) {
let prankUid = makeid(16);
if ("prankUid" in content) {
let prankExists = checkPrank(content)
let prankExists = check(content, "prankUid", PrankData)
if (prankExists.success) {
if (PrankData[prankUid].state != "Pending") {
return {
@ -209,17 +221,19 @@ fastify.post('/delPrank', async (request, reply) => {
let content = request.body;
let auth = checkAuthetification(content);
if (auth.success) {
let prankExists = checkPrank(content)
let prankExists = check(content, "prankUid", PrankData)
if (prankExists.success) {
if (PrankData[content.prankUid].creator === content.uid) {
if (PrankData[content.prankUid].creator === content.uid
&& PrankData[content.prankUid].state === "Pending") {
delete PrankData[content.prankUid];
saveData(prankPath, PrankData);
return {
success: true,
}
} else {
return {
success: false,
why: "Not allowed"
why: "You can't delete prank that aren't yours or those already Accepted or Refused"
}
}
} else {
@ -232,10 +246,11 @@ fastify.post('/delPrank', async (request, reply) => {
fastify.post('/acceptPrank', async (request, reply) => {
let content = request.body;
let prankExists = checkManagePrank(content)
let prankExists = checkManage(content, "prankUid", PrankData)
if (prankExists.success) {
PrankData[content.prankUid].state = "Accepted";
PrankData[content.prankUid].manageBy = content.uid;
saveData(prankPath, PrankData);
return {
success: true,
}
@ -246,10 +261,11 @@ fastify.post('/acceptPrank', async (request, reply) => {
fastify.post('/donePrank', async (request, reply) => {
let content = request.body;
let prankExists = checkManagePrank(content)
let prankExists = checkManage(content, "prankUid", PrankData)
if (prankExists.success) {
if (PrankData[content.prankUid].manageBy == content.uid) {
PrankData[content.prankUid].state = "Done";
saveData(prankPath, PrankData);
return {
success: true,
}
@ -266,10 +282,11 @@ fastify.post('/donePrank', async (request, reply) => {
fastify.post('/refusePrank', async (request, reply) => {
let content = request.body;
let prankExists = checkManagePrank(content)
let prankExists = checkManage(content, "prankUid", PrankData)
if (prankExists.success) {
PrankData[content.prankUid].state = "Refused";
PrankData[content.prankUid].manageBy = content.uid;
saveData(prankPath, PrankData);
return {
success: true,
}
@ -291,9 +308,15 @@ fastify.post('/get', async (request, reply) => {
prankData: PrankData
}
} else {
let prankData = {};
for (prank in PrankData) {
if (PrankData[prank].creator == content.uid) {
prankData[prank] = PrankData[prank];
}
}
return {
success: false,
why: "Not Allowed"
success: true,
prankData: prankData
}
}
break;
@ -303,6 +326,25 @@ fastify.post('/get', async (request, reply) => {
activityData: ActivityData
}
break;
case "treasure":
if (AdminUsersUid.includes(content.uid)) {
return {
sucess: true,
prankData: TreasureData
}
} else {
let treasureData = {};
for (treasure in TreasureData) {
if (TreasureData[treasure].creator == content.uid) {
treasureData[treasure] = TreasureData[treasure];
}
}
return {
success: true,
treasureData: treasureData
}
}
break;
default:
return {
success: false,
@ -331,10 +373,11 @@ fastify.post('/addActivity', async (request, reply) => {
&& "start" in content
&& "end" in content
&& "where" in content) {
let activityUid = makeid(16);
if (["event", "treasure"].contains(content.type)) {
let activityUid = makeid(16);
if ("activityUid" in content) {
let activityExists = checkActivity(content)
let activityExists = check(content, "activityUid", ActivityData)
if (activityExists.success) {
activityUid = content.activityUid;
} else {
@ -378,23 +421,15 @@ fastify.post('/addActivity', async (request, reply) => {
fastify.post('/delActivity', async (request, reply) => {
let content = request.body;
let auth = checkAuthetification(content);
if (auth.success) {
if (AdminUsersUid.includes(content.uid)) {
let activityExists = checkActivity(content)
if (activityExists.success) {
delete ActivityData[content.activityUid]
} else {
return activityExists
}
} else {
return {
success: false,
why: "Not allowed"
}
let activityExists = checkManage(content, "activityUid", ActivityData)
if (activityExists.success) {
delete ActivityData[content.activityUid]
saveData(activityPath, ActivityData);
return {
success: true,
}
} else {
return auth
return activityExists
}
})
@ -402,9 +437,56 @@ fastify.post('/sendTreasure', async (request, reply) => {
let content = request.body;
let auth = checkAuthetification(content);
if (auth.success) {
let activityExists = checkActivity(content)
let activityExists = check(content, "activityUid", ActivityData)
if (activityExists.success) {
if ("image" in content
&& "desc" in content
&& "activityUid" in content) {
let treasureUid = makeid(16);
if ("treasureUid" in content) {
let treasureExists = check(content, "activityUid", ActivityData)
if (treasureExists.success) {
if (treasureData[treasureUid].state != "Pending"
&& treasureData[treasureUid].creator == content.uid) {
return {
success: false,
why: "You cannot edit already accepted or refused treasure request, or request form other people"
}
} else {
treasureUid = content.prankUid;
}
} else {
return treasureExists;
}
}
let activityExists = check(content, "activityUid", ActivityData)
if (activityExists.success) {
if (ActivityData[content.activityUid].type == "treasure") {
let imageUid = makeid(128);
fs.writeFileSync("static/images/"+imageUid, content.image);
TreasureData[treasureUid] = {
creator: content.uid,
image: imageUid,
desc: content.desc,
activity: content.activityUid,
state: "Pending"
}
saveData(treasurePath, TreasureData);
} else {
return {
success: false,
why: "The given activityUid refers to an event and not a treasure quest"
}
}
} else {
return activityExists
}
} else {
return {
success: false,
why: "Missing image, desc or activityUid"
}
}
} else {
return activityExists
}
@ -413,6 +495,86 @@ fastify.post('/sendTreasure', async (request, reply) => {
}
})
fastify.post('/delTreasure', async (request, reply) => {
let content = request.body;
let auth = checkAuthetification(content);
if (auth.success) {
let treasureExists = check(content, "treasureUid", TreasureData)
if (treasureExists.success) {
if (TreasureData[content.treasureUid].creator === content.uid
&& TreasureData[content.treasureUid].state == "Pending") {
delete TreasureData[content.treasureUid];
saveData(treasurePath, TreasureData);
return {
success: true,
}
} else {
return {
success: false,
why: "You can't delete treasure that aren't yours or those already Accepted or Refused"
}
}
} else {
return treasureExists
}
} else {
return auth
}
})
fastify.post('/acceptTreasure', async (request, reply) => {
let content = request.body;
let treasureExists = checkManage(content, "treasureUid", TreasureData);
if (treasureExists.success) {
TreasureData[content.treasureUid].state = "Accepted";
saveData(treasurePath, TreasureData);
GoldenUsers[TreasureData[content.treasureUid].activityUid] = {
userUid: TreasureData[content.treasureUid].creator,
activityUid: TreasureData[content.treasureUid].activityUid
}
saveData(goldenUsersPath, GoldenUsers);
return {
success: true,
}
} else {
return treasureExists
}
})
fastify.post('/refuseTreasure', async (request, reply) => {
let content = request.body;
let treasureExists = checkManage(content, "treasureUid", TreasureData);
if (treasureExists.success) {
TreasureData[content.treasureUid].state = "Refused";
saveData(treasurePath, TreasureData);
return {
success: true,
}
} else {
return treasureExists
}
})
fastify.post('/isGolden', async (request, reply) => {
let content = request.body;
let auth = checkAuthetification(content);
if (auth.success) {
for (activityUid in GoldenUsers) {
if (GoldenUsers[activityUid].userUid === content.uid) {
return {
success: true,
userUid: content.uid,
activity: ActivityData[GoldenUsers[activityUid].activityUid]
}
}
}
return {
sucess: false
}
} else {
return auth
}
})
function saveData(path, data) {
fs.writeFileSync(path, JSON.stringify(data));
@ -458,60 +620,45 @@ function checkAuthetification(content) {
why: "Not authentificated"
}
}
} else {
return {
success: false,
why: "Missing uid or token"
}
}
}
function checkPrank(content) {
if ("prankUid" in content) {
if (content.prankUid in PrankData) {
function check(content, input, data) {
if (inputin content) {
if (content[input] in data) {
return {
success: true,
}
} else {
return {
success: false,
why: "Unknow prankUid"
why: "Unknow "+input
}
}
} else {
return {
success: false,
why: "Missing prankUid"
why: "Missing "+input
}
}
}
function checkActivity(content) {
if ("activityUid" in content) {
if (content.activityUid in ActivityData) {
return {
success: true,
}
} else {
return {
success: false,
why: "Unknow activityUid"
}
}
} else {
return {
success: false,
why: "Missing activityUid"
}
}
}
function checkManagePrank(content) {
function checkManage(content, input, data) {
let auth = checkAuthetification(content);
if (auth.success) {
if (AdminUsersUid.includes(content.uid)) {
let prankExists = checkPrank(content)
if (prankExists.success) {
let exists = check(content, input, data)
if (exists.success) {
return {
success: true
}
} else {
return prankExists
return exists
}
} else {
return {