v1.6.0/cleanup fix
This commit is contained in:
@@ -1,32 +1,46 @@
|
|||||||
const admin = require('firebase-admin');
|
const admin = require("firebase-admin");
|
||||||
|
|
||||||
module.exports = async (roomId) => {
|
module.exports = async (partyName) => {
|
||||||
admin
|
// DELETE MESSAGES
|
||||||
|
const partyMessagesRef = admin
|
||||||
.firestore()
|
.firestore()
|
||||||
.collection('rooms')
|
.collection("rooms")
|
||||||
.doc(roomId)
|
.doc(partyName)
|
||||||
.collection('messages')
|
.collection("messages");
|
||||||
.listDocuments()
|
const partyMessagesQuery = partyMessagesRef.orderBy("time").limit(200);
|
||||||
.then((messages) => messages.forEach((message) => message.delete()))
|
deleteCollection(partyMessagesQuery);
|
||||||
.catch((err) => {
|
// DELETE USERS
|
||||||
console.error(`Failed to run auto clean up (delete room messages) @ ${roomId}`, err);
|
const partyUsersRef = admin
|
||||||
});
|
|
||||||
admin
|
|
||||||
.firestore()
|
.firestore()
|
||||||
.collection('rooms')
|
.collection("rooms")
|
||||||
.doc(roomId)
|
.doc(partyName)
|
||||||
.collection('users')
|
.collection("messages");
|
||||||
.listDocuments()
|
const partyUsersQuery = partyUsersRef.orderBy("time").limit(200);
|
||||||
.then((users) => users.forEach((user) => user.delete()))
|
deleteCollection(partyUsersQuery);
|
||||||
.catch((err) => {
|
// DELETE PARTY DOC
|
||||||
console.error(`Failed to run auto clean up (delete room users) @ ${roomId}`, err);
|
admin.firestore().collection("rooms").doc(partyName).delete();
|
||||||
});
|
|
||||||
admin
|
|
||||||
.firestore()
|
|
||||||
.collection('rooms')
|
|
||||||
.doc(roomId)
|
|
||||||
.delete()
|
|
||||||
.catch((err) => {
|
|
||||||
console.error(`Failed to run auto clean up (delete room) @ ${roomId}`, err);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
const admin = require('firebase-admin');
|
const admin = require("firebase-admin");
|
||||||
const cleanUpRoom = require('./cleanUpRoom');
|
const cleanUpRoom = require("./cleanUpRoom");
|
||||||
|
|
||||||
const timeDiff = (ds1, ds2) => {
|
const timeDiff = (ds1, ds2) => {
|
||||||
const d1 = new Date(ds1);
|
const d1 = new Date(ds1);
|
||||||
@@ -11,10 +11,10 @@ const timeDiff = (ds1, ds2) => {
|
|||||||
|
|
||||||
module.exports = async (req, res) => {
|
module.exports = async (req, res) => {
|
||||||
const startedTime = new Date();
|
const startedTime = new Date();
|
||||||
res.status(200).send({message:'Started processing'});
|
res.status(200).send({ message: "Started processing" });
|
||||||
const roomList = await admin
|
const roomList = await admin
|
||||||
.firestore()
|
.firestore()
|
||||||
.collection('rooms')
|
.collection("rooms")
|
||||||
.listDocuments()
|
.listDocuments()
|
||||||
.then((rL) => rL)
|
.then((rL) => rL)
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
@@ -34,7 +34,7 @@ module.exports = async (req, res) => {
|
|||||||
// LESS THAN 24 HOURS BUT NOT IN USE
|
// LESS THAN 24 HOURS BUT NOT IN USE
|
||||||
timeDiff(roomSnap.updateTime.toDate(), startedTime) >= 12
|
timeDiff(roomSnap.updateTime.toDate(), startedTime) >= 12
|
||||||
) {
|
) {
|
||||||
await cleanUpRoom(roomSnap.id);
|
cleanUpRoom(roomSnap.id);
|
||||||
console.info(`Deleted room ${roomSnap.id} from auto cleanup`);
|
console.info(`Deleted room ${roomSnap.id} from auto cleanup`);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user