From 0432c9d1cc160343eb191b8f8239dfc928a1785e Mon Sep 17 00:00:00 2001 From: rainbownapkin Date: Sun, 1 Dec 2024 20:09:45 -0500 Subject: [PATCH] kick/logout on channel/site rank change --- src/schemas/channel/channelSchema.js | 55 ++++++++++++++++++++++++++++ src/schemas/userSchema.js | 4 ++ www/css/theme/movie-night.css | 2 +- 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/schemas/channel/channelSchema.js b/src/schemas/channel/channelSchema.js index 1f13db9..4b21de3 100644 --- a/src/schemas/channel/channelSchema.js +++ b/src/schemas/channel/channelSchema.js @@ -82,6 +82,61 @@ channelSchema.pre('save', async function (next){ } } + //Getting the affected user would be a million times easier elsewhere + //But this ensures it happens every time channel rank gets changed no matter what + if(this.isModified('rankList')){ + //Get the rank list before it was modified (gross but works, find a better way if you dont like it :P) + var chanDB = await module.exports.findOne({_id: this._id}); + //Create empty variable for the found rank object + var foundRank = null; + + //If we're removing one + if(chanDB.rankList.length > this.rankList.length){ + //Child/Parent is *WAY* to atomic family for my tastes :P + var top = chanDB; + var bottom = this; + }else{ + //otherwise reverse the loops + var top = this; + var bottom = chanDB; + } + + //Populate the top doc + await top.populate('rankList.user'); + + + top.rankList.forEach((topObj) => { + //Create empty variable for the matched rank + var matchedRank = null; + //For each rank in the old copy of the rank list + bottom.rankList.forEach((bottomObj) => { + if(topObj.user._id.toString() == bottomObj.user._id.toString()){ + matchedRank = bottomObj; + } + }); + + + if(matchedRank == null || matchedRank.rank != topObj.rank){ + foundRank = topObj; + } + + }); + + //get relevant active channel + const activeChan = server.channelManager.activeChannels.get(this.name); + + //if the channel is online + if(activeChan != null){ + //Get the relevant user connection + const userConn = activeChan.userList.get(foundRank.user.user); + //if the user is online + if(userConn != null){ + //kick the user + userConn.disconnect("Your channel rank has changed!"); + } + } + } + next(); }); diff --git a/src/schemas/userSchema.js b/src/schemas/userSchema.js index 1ab231e..e599256 100644 --- a/src/schemas/userSchema.js +++ b/src/schemas/userSchema.js @@ -112,6 +112,10 @@ userSchema.pre('save', async function (next){ } } + if(this.isModified("rank")){ + await this.killAllSessions(); + } + //All is good, continue on saving. next(); }); diff --git a/www/css/theme/movie-night.css b/www/css/theme/movie-night.css index c8e1dc2..80ab9cb 100644 --- a/www/css/theme/movie-night.css +++ b/www/css/theme/movie-night.css @@ -15,7 +15,7 @@ You should have received a copy of the GNU Affero General Public License along with this program. If not, see .*/ :root{ - --main-font: "open-sans", sans-serif; + --main-font: "open-sans", system-ui; --bg0: rgb(158, 158, 158); --bg0-alpha0: rgba(158, 158, 158, 0.4); --bg0-alpha1: rgba(158, 158, 158, 0.75);