v.1.7.9/webhook room cleanup update

This commit is contained in:
2021-02-26 15:46:33 -05:00
parent d3c1895abb
commit 470e335d91
6 changed files with 3801 additions and 307 deletions
+2 -1
View File
@@ -45,7 +45,7 @@ module.exports = async (req, res) => {
createdAt: new Date(),
adminUid: bodydata.uid,
adminOnly: bodydata.adminOnly,
publicParty: !!bodydata.publicParty,
publicParty: Boolean(bodydata.publicParty),
})
.catch((err) => {
res.status(500).send({ message: "Unable to start party." });
@@ -89,4 +89,5 @@ module.exports = async (req, res) => {
.send({ roomId: bodydata.partyName, message: "Started a party!" });
}
}
return;
};
+36 -24
View File
@@ -1,19 +1,24 @@
const admin = require('firebase-admin');
const admin = require("firebase-admin");
module.exports = async (req, res) => {
const bodydata = JSON.parse(req.body);
console.log('Requested Join - UID - ', bodydata.uid, '\n\tRoom - ', bodydata.partyName);
console.log(
"Requested Join - UID - ",
bodydata.uid,
"\n\tRoom - ",
bodydata.partyName
);
// GET ROOM EXISTS, ROOM SOURCE, & ROOM PIN
const [exists, roomSource, roomPin, adminUid] = await admin
.firestore()
.collection('rooms')
.collection("rooms")
.doc(bodydata.partyName)
.get()
.then((roomSnap) => [
roomSnap.exists,
roomSnap.exists ? roomSnap.data().player.source : '',
roomSnap.exists ? roomSnap.data().pin : '',
roomSnap.exists ? roomSnap.data().adminUid : '',
roomSnap.exists ? roomSnap.data().player.source : "",
roomSnap.exists ? roomSnap.data().pin : "",
roomSnap.exists ? roomSnap.data().adminUid : "",
])
.catch((err) => {
console.error(
@@ -22,19 +27,19 @@ module.exports = async (req, res) => {
);
});
if (exists && roomPin !== bodydata.partyPin)
return res.status(401).send({ message: 'Incorrect party pin' });
return res.status(401).send({ message: "Incorrect party pin" });
// GET IF USER EXISTS AND IS ADMIN
const [userExists, userAdmin, userName] = await admin
.firestore()
.collection('rooms')
.collection("rooms")
.doc(bodydata.partyName)
.collection('users')
.collection("users")
.doc(bodydata.uid)
.get()
.then((userSnap) => [
userSnap.exists,
userSnap.exists ? userSnap.data().admin : false,
userSnap.exists ? userSnap.data().name : '',
userSnap.exists ? userSnap.data().name : "",
])
.catch((err) => {
console.error(
@@ -48,13 +53,15 @@ module.exports = async (req, res) => {
if (userName !== bodydata.userName) {
await admin
.firestore()
.collection('rooms')
.collection("rooms")
.doc(bodydata.partyName)
.collection('users')
.collection("users")
.doc(bodydata.uid)
.update({ name: bodydata.userName });
}
res.status(200).send({ partyName: bodydata.partyName, message: 'Joined a party' });
res
.status(200)
.send({ partyName: bodydata.partyName, message: "Joined a party" });
}
// USER DOES NOT PRE-EXIST BUT SAME SOURCE AND RIGHT PIN
else if (
@@ -67,12 +74,12 @@ module.exports = async (req, res) => {
) {
await admin
.firestore()
.collection('rooms')
.collection("rooms")
.doc(bodydata.partyName)
.collection('messages')
.collection("messages")
.add({
uid: '<system>',
name: '',
uid: "<system>",
name: "",
text: `${bodydata.userName} joined the party.`,
time: new Date(),
})
@@ -84,9 +91,9 @@ module.exports = async (req, res) => {
});
await admin
.firestore()
.collection('rooms')
.collection("rooms")
.doc(bodydata.partyName)
.collection('users')
.collection("users")
.doc(bodydata.uid)
.set({
admin: adminUid === bodydata.uid,
@@ -98,7 +105,9 @@ module.exports = async (req, res) => {
err
);
});
res.status(200).send({ partyName: bodydata.partyName, message: 'Joined a party' });
res
.status(200)
.send({ partyName: bodydata.partyName, message: "Joined a party" });
}
// ONLY WRONG SOURCE
else if (exists && roomSource !== bodydata.source) {
@@ -106,13 +115,13 @@ module.exports = async (req, res) => {
if (userAdmin === true) {
await admin
.firestore()
.collection('rooms')
.collection("rooms")
.doc(bodydata.partyName)
.update({
player: {
showName: null,
source: bodydata.source,
status: 'NOT_PLAYING',
status: "NOT_PLAYING",
time: 0,
updatedBy: bodydata.uid,
},
@@ -121,9 +130,12 @@ module.exports = async (req, res) => {
console.error(
`Failed to update player source when admin joining party \n\t Room: ${partyName} \n\t User: ${bodydata.uid}`
);
res.status(500).send({ message: 'Unable to move party, please try again' });
res
.status(500)
.send({ message: "Unable to move party, please try again" });
});
res.status(200).send({ message: `Moved party to ${roomSource}` });
} else res.status(417).send({ message: `This party is on ${roomSource}` });
} else res.status(404).send({ message: 'Party Not Found' });
} else res.status(404).send({ message: "Party Not Found" });
return;
};
+3708 -211
View File
File diff suppressed because it is too large Load Diff
+1
View File
@@ -18,6 +18,7 @@
"express": "^4.17.1",
"firebase-admin": "^9.2.0",
"firebase-functions": "^3.3.0",
"firebase-tools": "^9.5.0",
"node-fetch": "^2.6.1",
"nodemailer": "^6.4.11",
"stripe": "^8.92.0"
-46
View File
@@ -1,46 +0,0 @@
const admin = require("firebase-admin");
module.exports = async (partyName) => {
// DELETE MESSAGES
const partyMessagesRef = admin
.firestore()
.collection("rooms")
.doc(partyName)
.collection("messages");
const partyMessagesQuery = partyMessagesRef.orderBy("time").limit(200);
await deleteCollection(partyMessagesQuery);
// DELETE USERS
const partyUsersRef = admin
.firestore()
.collection("rooms")
.doc(partyName)
.collection("users");
const partyUsersQuery = partyUsersRef.limit(200);
await deleteCollection(partyUsersQuery);
// DELETE PARTY DOC
await admin.firestore().collection("rooms").doc(partyName).delete();
};
async function deleteCollection(query) {
const db = admin.firestore();
return new Promise((resolve, reject) => {
deleteQueryBatch(db, query, resolve).catch(reject);
});
}
async function deleteQueryBatch(db, query, resolve) {
const snapshot = await query.get();
const batchSize = snapshot.size;
if (batchSize === 0) {
resolve();
return;
}
const batch = admin.firestore().batch();
snapshot.docs.forEach((doc) => {
batch.delete(doc.ref);
});
await batch.commit();
process.nextTick(() => {
deleteQueryBatch(db, query, resolve);
});
}
+54 -25
View File
@@ -1,5 +1,5 @@
const admin = require("firebase-admin");
const cleanUpRoom = require("./cleanUpRoom");
const firebase_tools = require("firebase-tools");
const timeDiff = (ds1, ds2) => {
const d1 = new Date(ds1);
@@ -9,9 +9,57 @@ const timeDiff = (ds1, ds2) => {
return Math.abs(Math.round(diff));
};
module.exports = async (req, res) => {
const roomIdList = async (refs) => {
const startedTime = new Date();
return await admin
.firestore()
.getAll(...refs)
.then((roomDataList) =>
roomDataList.map((roomData) => {
if (
!roomData.exists ||
(timeDiff(roomData.createTime.toDate(), startedTime) >= 24 &&
timeDiff(roomData.updateTime.toDate(), startedTime) >= 4) ||
timeDiff(roomData.updateTime.toDate(), startedTime) >= 12
) {
return roomData.id;
}
})
)
.then((ids) => ids.filter((v) => v !== undefined));
};
const batchDeleteRooms = async (roomList, totalNumber) => {
setTimeout(async () => {
if (roomList.length > 0) {
await firebase_tools.firestore
.delete(`rooms/${roomList.splice(0, 1)[0]}`, {
project: process.env.GCLOUD_PROJECT,
recursive: true,
yes: true,
})
.catch((err) =>
console.error(
`[ERROR]\tWEBHOOKS/Room Clean Up\t# OF ROOMS ${roomList.length} REMAINING,\tERROR:${err}`
)
);
console.info(
`[DONE]\tWEBHOOKS/Room Clean Up\t# OF ROOMS ${
totalNumber - roomList.length
}/${totalNumber}`
);
batchDeleteRooms(roomList, totalNumber);
} else {
console.info(
`[DONE]\tWEBHOOKS/Room Clean Up\tDeleted all ${totalNumber} rooms`
);
}
}, 1000);
};
module.exports = async (req, res) => {
res.status(200).send({ message: "Started processing" });
const roomList = await admin
.firestore()
.collection("rooms")
@@ -23,28 +71,9 @@ module.exports = async (req, res) => {
err
);
});
roomList.forEach((roomRef) => {
roomRef
.get()
.then(async (roomSnap) => {
if (
// MORE THAN 24 HOURS AND NOT IN USE
(timeDiff(roomSnap.createTime.toDate(), startedTime) >= 24 &&
timeDiff(roomSnap.updateTime.toDate(), startedTime) >= 4) ||
// LESS THAN 24 HOURS BUT NOT IN USE
timeDiff(roomSnap.updateTime.toDate(), startedTime) >= 12
) {
cleanUpRoom(roomSnap.id);
console.info(`Deleted room ${roomSnap.id} from auto cleanup`);
}
return;
})
.catch((err) => {
console.error(
`Failed to run auto clean up (get room snap) @ ${new Date().toLocaleTimeString()}`,
err
);
});
});
var ids = await roomIdList(roomList);
batchDeleteRooms(ids, ids.length);
return;
};