From 730d816c459eab74976b129cd155a028aece55c7 Mon Sep 17 00:00:00 2001 From: rainbow napkin Date: Sun, 27 Apr 2025 20:39:47 -0400 Subject: [PATCH] Fixed expiration handlers --- src/schemas/channel/channelSchema.js | 19 +++++++++++++------ src/schemas/user/emailChangeSchema.js | 9 +++++++-- src/schemas/user/passwordResetSchema.js | 9 +++++++-- src/schemas/user/userBanSchema.js | 8 ++++++-- src/schemas/user/userSchema.js | 18 +++++++++++------- 5 files changed, 44 insertions(+), 19 deletions(-) diff --git a/src/schemas/channel/channelSchema.js b/src/schemas/channel/channelSchema.js index 4dbbe2f..b56cdd3 100644 --- a/src/schemas/channel/channelSchema.js +++ b/src/schemas/channel/channelSchema.js @@ -318,16 +318,23 @@ channelSchema.statics.reqPermCheck = function(perm, chanField = "chanName"){ channelSchema.statics.processExpiredBans = async function(){ const chanDB = await this.find({}); - chanDB.forEach((channel) => { - channel.banList.forEach(async (ban, i) => { + for(let chanIndex in chanDB){ + //Pull channel from channels by index + const channel = chanDB[chanIndex]; + + //channel.banList.forEach(async (ban, banIndex) => { + for(let banIndex in channel.banList){ + //Pull ban from channel ban list + const ban = channel.banList[banIndex]; + //ignore permanent and non-expired bans if(ban.expirationDays >= 0 && ban.getDaysUntilExpiration() <= 0){ //Get the index of the ban - channel.banList.splice(i,1); - return await channel.save(); + channel.banList.splice(banIndex,1); + await channel.save(); } - }) - }); + } + } } //methods diff --git a/src/schemas/user/emailChangeSchema.js b/src/schemas/user/emailChangeSchema.js index ca20900..8ad104d 100644 --- a/src/schemas/user/emailChangeSchema.js +++ b/src/schemas/user/emailChangeSchema.js @@ -79,16 +79,21 @@ emailChangeSchema.pre('save', async function (next){ //statics emailChangeSchema.statics.processExpiredRequests = async function(){ //Pull all requests from the DB + //Tested finding request by date, but mongoose kept throwing casting errors. + //This seems to be an intermittent issue online. Maybe it will work in a future version? const requestDB = await this.find({}); //Fire em all off at once without waiting for the last one to complete since we don't fuckin' need to - requestDB.forEach(async (request) => { + for(let requestIndex in requestDB){ + //Pull request from requestDB by index + const request = requestDB[requestIndex]; + //If the request hasn't been processed and it's been expired if(request.getDaysUntilExpiration() <= 0){ //Delete the request await this.deleteOne({_id: request._id}); } - }); + } } //methods diff --git a/src/schemas/user/passwordResetSchema.js b/src/schemas/user/passwordResetSchema.js index 41ee05b..6fad8fd 100644 --- a/src/schemas/user/passwordResetSchema.js +++ b/src/schemas/user/passwordResetSchema.js @@ -70,16 +70,21 @@ passwordResetSchema.pre('save', async function (next){ //statics passwordResetSchema.statics.processExpiredRequests = async function(){ //Pull all requests from the DB + //Tested finding request by date, but mongoose kept throwing casting errors. + //This seems to be an intermittent issue online. Maybe it will work in a future version? const requestDB = await this.find({}); //Fire em all off at once without waiting for the last one to complete since we don't fuckin' need to - requestDB.forEach(async (request) => { + for(let requestIndex in requestDB){ + //pull request from requestDB by index + const request = requestDB[requestIndex]; + //If the request hasn't been processed and it's been expired if(request.getDaysUntilExpiration() <= 0){ //Delete the request await this.deleteOne({_id: request._id}); } - }); + } } //methods diff --git a/src/schemas/user/userBanSchema.js b/src/schemas/user/userBanSchema.js index 0d49cb8..88da80c 100644 --- a/src/schemas/user/userBanSchema.js +++ b/src/schemas/user/userBanSchema.js @@ -353,10 +353,14 @@ userBanSchema.statics.getBans = async function(){ } userBanSchema.statics.processExpiredBans = async function(){ + //Channel ban expirations may vary so there's no way to search for expired bans const banDB = await this.find({}); //Firem all off all at once seperately without waiting for one another - banDB.forEach(async (ban) => { + for(let banIndex in banDB){ + //Pull ban from banlist by index + const ban = banDB[banIndex]; + //This ban was already processed, and it's user has been deleted. There is no more to be done... if(ban.user == null){ return; @@ -394,7 +398,7 @@ userBanSchema.statics.processExpiredBans = async function(){ await this.deleteOne({_id: ban._id}); } } - }); + } } //methods diff --git a/src/schemas/user/userSchema.js b/src/schemas/user/userSchema.js index 639ae15..4c1a983 100644 --- a/src/schemas/user/userSchema.js +++ b/src/schemas/user/userSchema.js @@ -381,22 +381,26 @@ userSchema.statics.processAgedIPRecords = async function(){ //Pull full userlist const users = await this.find({}); - //for every user - users.forEach((userDB) => { + //Not a fan of iterating through the DB but there doesn't seem to be a way to search for a doc based on the properties of it's subdoc + for(let userIndex in users){ + //Pull user record from users by index + const userDB = users[userIndex]; //For every recent ip within the user - userDB.recentIPs.forEach((record, recordI) => { + for(let recordIndex in userDB.recentIPs){ + //Pull record from recent IPs by index + const record = userDB.recentIPs[recordIndex]; //Check how long it's been since we've last seen the IP const daysSinceLastUse = ((new Date() - record.lastLog) / (1000 * 60 * 60 * 24)).toFixed(1); //If it's been more than a week if(daysSinceLastUse >= 7){ //Splice out the IP record - userDB.recentIPs.splice(recordI, 1); + userDB.recentIPs.splice(recordIndex, 1); //No reason to wait on this since we're done with this user - userDB.save(); + await userDB.save(); } - }); - }); + } + } }