v.1.7.9/webhook room cleanup update
This commit is contained in:
@@ -45,7 +45,7 @@ module.exports = async (req, res) => {
|
|||||||
createdAt: new Date(),
|
createdAt: new Date(),
|
||||||
adminUid: bodydata.uid,
|
adminUid: bodydata.uid,
|
||||||
adminOnly: bodydata.adminOnly,
|
adminOnly: bodydata.adminOnly,
|
||||||
publicParty: !!bodydata.publicParty,
|
publicParty: Boolean(bodydata.publicParty),
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
res.status(500).send({ message: "Unable to start party." });
|
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!" });
|
.send({ roomId: bodydata.partyName, message: "Started a party!" });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,19 +1,24 @@
|
|||||||
const admin = require('firebase-admin');
|
const admin = require("firebase-admin");
|
||||||
|
|
||||||
module.exports = async (req, res) => {
|
module.exports = async (req, res) => {
|
||||||
const bodydata = JSON.parse(req.body);
|
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
|
// GET ROOM EXISTS, ROOM SOURCE, & ROOM PIN
|
||||||
const [exists, roomSource, roomPin, adminUid] = await admin
|
const [exists, roomSource, roomPin, adminUid] = await admin
|
||||||
.firestore()
|
.firestore()
|
||||||
.collection('rooms')
|
.collection("rooms")
|
||||||
.doc(bodydata.partyName)
|
.doc(bodydata.partyName)
|
||||||
.get()
|
.get()
|
||||||
.then((roomSnap) => [
|
.then((roomSnap) => [
|
||||||
roomSnap.exists,
|
roomSnap.exists,
|
||||||
roomSnap.exists ? roomSnap.data().player.source : '',
|
roomSnap.exists ? roomSnap.data().player.source : "",
|
||||||
roomSnap.exists ? roomSnap.data().pin : '',
|
roomSnap.exists ? roomSnap.data().pin : "",
|
||||||
roomSnap.exists ? roomSnap.data().adminUid : '',
|
roomSnap.exists ? roomSnap.data().adminUid : "",
|
||||||
])
|
])
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.error(
|
console.error(
|
||||||
@@ -22,19 +27,19 @@ module.exports = async (req, res) => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
if (exists && roomPin !== bodydata.partyPin)
|
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
|
// GET IF USER EXISTS AND IS ADMIN
|
||||||
const [userExists, userAdmin, userName] = await admin
|
const [userExists, userAdmin, userName] = await admin
|
||||||
.firestore()
|
.firestore()
|
||||||
.collection('rooms')
|
.collection("rooms")
|
||||||
.doc(bodydata.partyName)
|
.doc(bodydata.partyName)
|
||||||
.collection('users')
|
.collection("users")
|
||||||
.doc(bodydata.uid)
|
.doc(bodydata.uid)
|
||||||
.get()
|
.get()
|
||||||
.then((userSnap) => [
|
.then((userSnap) => [
|
||||||
userSnap.exists,
|
userSnap.exists,
|
||||||
userSnap.exists ? userSnap.data().admin : false,
|
userSnap.exists ? userSnap.data().admin : false,
|
||||||
userSnap.exists ? userSnap.data().name : '',
|
userSnap.exists ? userSnap.data().name : "",
|
||||||
])
|
])
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.error(
|
console.error(
|
||||||
@@ -48,13 +53,15 @@ module.exports = async (req, res) => {
|
|||||||
if (userName !== bodydata.userName) {
|
if (userName !== bodydata.userName) {
|
||||||
await admin
|
await admin
|
||||||
.firestore()
|
.firestore()
|
||||||
.collection('rooms')
|
.collection("rooms")
|
||||||
.doc(bodydata.partyName)
|
.doc(bodydata.partyName)
|
||||||
.collection('users')
|
.collection("users")
|
||||||
.doc(bodydata.uid)
|
.doc(bodydata.uid)
|
||||||
.update({ name: bodydata.userName });
|
.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
|
// USER DOES NOT PRE-EXIST BUT SAME SOURCE AND RIGHT PIN
|
||||||
else if (
|
else if (
|
||||||
@@ -67,12 +74,12 @@ module.exports = async (req, res) => {
|
|||||||
) {
|
) {
|
||||||
await admin
|
await admin
|
||||||
.firestore()
|
.firestore()
|
||||||
.collection('rooms')
|
.collection("rooms")
|
||||||
.doc(bodydata.partyName)
|
.doc(bodydata.partyName)
|
||||||
.collection('messages')
|
.collection("messages")
|
||||||
.add({
|
.add({
|
||||||
uid: '<system>',
|
uid: "<system>",
|
||||||
name: '',
|
name: "",
|
||||||
text: `${bodydata.userName} joined the party.`,
|
text: `${bodydata.userName} joined the party.`,
|
||||||
time: new Date(),
|
time: new Date(),
|
||||||
})
|
})
|
||||||
@@ -84,9 +91,9 @@ module.exports = async (req, res) => {
|
|||||||
});
|
});
|
||||||
await admin
|
await admin
|
||||||
.firestore()
|
.firestore()
|
||||||
.collection('rooms')
|
.collection("rooms")
|
||||||
.doc(bodydata.partyName)
|
.doc(bodydata.partyName)
|
||||||
.collection('users')
|
.collection("users")
|
||||||
.doc(bodydata.uid)
|
.doc(bodydata.uid)
|
||||||
.set({
|
.set({
|
||||||
admin: adminUid === bodydata.uid,
|
admin: adminUid === bodydata.uid,
|
||||||
@@ -98,7 +105,9 @@ module.exports = async (req, res) => {
|
|||||||
err
|
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
|
// ONLY WRONG SOURCE
|
||||||
else if (exists && roomSource !== bodydata.source) {
|
else if (exists && roomSource !== bodydata.source) {
|
||||||
@@ -106,13 +115,13 @@ module.exports = async (req, res) => {
|
|||||||
if (userAdmin === true) {
|
if (userAdmin === true) {
|
||||||
await admin
|
await admin
|
||||||
.firestore()
|
.firestore()
|
||||||
.collection('rooms')
|
.collection("rooms")
|
||||||
.doc(bodydata.partyName)
|
.doc(bodydata.partyName)
|
||||||
.update({
|
.update({
|
||||||
player: {
|
player: {
|
||||||
showName: null,
|
showName: null,
|
||||||
source: bodydata.source,
|
source: bodydata.source,
|
||||||
status: 'NOT_PLAYING',
|
status: "NOT_PLAYING",
|
||||||
time: 0,
|
time: 0,
|
||||||
updatedBy: bodydata.uid,
|
updatedBy: bodydata.uid,
|
||||||
},
|
},
|
||||||
@@ -121,9 +130,12 @@ module.exports = async (req, res) => {
|
|||||||
console.error(
|
console.error(
|
||||||
`Failed to update player source when admin joining party \n\t Room: ${partyName} \n\t User: ${bodydata.uid}`
|
`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}` });
|
res.status(200).send({ message: `Moved party to ${roomSource}` });
|
||||||
} else res.status(417).send({ message: `This party is on ${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;
|
||||||
};
|
};
|
||||||
|
|||||||
Generated
+3708
-211
File diff suppressed because it is too large
Load Diff
@@ -18,6 +18,7 @@
|
|||||||
"express": "^4.17.1",
|
"express": "^4.17.1",
|
||||||
"firebase-admin": "^9.2.0",
|
"firebase-admin": "^9.2.0",
|
||||||
"firebase-functions": "^3.3.0",
|
"firebase-functions": "^3.3.0",
|
||||||
|
"firebase-tools": "^9.5.0",
|
||||||
"node-fetch": "^2.6.1",
|
"node-fetch": "^2.6.1",
|
||||||
"nodemailer": "^6.4.11",
|
"nodemailer": "^6.4.11",
|
||||||
"stripe": "^8.92.0"
|
"stripe": "^8.92.0"
|
||||||
|
|||||||
@@ -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);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
const admin = require("firebase-admin");
|
const admin = require("firebase-admin");
|
||||||
const cleanUpRoom = require("./cleanUpRoom");
|
const firebase_tools = require("firebase-tools");
|
||||||
|
|
||||||
const timeDiff = (ds1, ds2) => {
|
const timeDiff = (ds1, ds2) => {
|
||||||
const d1 = new Date(ds1);
|
const d1 = new Date(ds1);
|
||||||
@@ -9,9 +9,57 @@ const timeDiff = (ds1, ds2) => {
|
|||||||
return Math.abs(Math.round(diff));
|
return Math.abs(Math.round(diff));
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = async (req, res) => {
|
const roomIdList = async (refs) => {
|
||||||
const startedTime = new Date();
|
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" });
|
res.status(200).send({ message: "Started processing" });
|
||||||
|
|
||||||
const roomList = await admin
|
const roomList = await admin
|
||||||
.firestore()
|
.firestore()
|
||||||
.collection("rooms")
|
.collection("rooms")
|
||||||
@@ -23,28 +71,9 @@ module.exports = async (req, res) => {
|
|||||||
err
|
err
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
roomList.forEach((roomRef) => {
|
|
||||||
roomRef
|
var ids = await roomIdList(roomList);
|
||||||
.get()
|
batchDeleteRooms(ids, ids.length);
|
||||||
.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
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user