Last user activity now marked on humie-friendly page-loads and last-socket disconnects, ensuring accurate 'online' status when disconnected from a channel.
This commit is contained in:
parent
6222535c47
commit
6445950f90
7 changed files with 99 additions and 2 deletions
75
src/utils/presenceUtils.js
Normal file
75
src/utils/presenceUtils.js
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
/*Canopy - The next generation of stoner streaming software
|
||||
Copyright (C) 2024-2025 Rainbownapkin and the TTN Community
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.*/
|
||||
|
||||
//local includes
|
||||
const userSchema = require('../schemas/user/userSchema');
|
||||
|
||||
//User activity map to keep us from constantly reading off of the DB
|
||||
let activityMap = new Map();
|
||||
|
||||
//How much difference between last write and now until we hit the DB again (in millis)
|
||||
//Defaults to two minutes
|
||||
const tolerance = 2 * (60 * 1000);
|
||||
|
||||
module.exports.presenceMiddleware = function(req, res, next){
|
||||
//Pull user from session
|
||||
const user = req.session.user;
|
||||
|
||||
//if we have a user object
|
||||
if(user != null){
|
||||
//Handle Presence
|
||||
module.exports.handlePresence(user.user);
|
||||
}
|
||||
|
||||
//Go on to next part of the middleware chain
|
||||
next();
|
||||
}
|
||||
|
||||
module.exports.handlePresence = async function(user, userDB, noSave = false){
|
||||
//If we don't have a user
|
||||
if(user == null || user == ''){
|
||||
//Drop that shit
|
||||
return;
|
||||
}
|
||||
|
||||
//Get current date as epoch (millis)
|
||||
const now = new Date();
|
||||
const millis = now.getTime();
|
||||
|
||||
//Check last user activity
|
||||
const activity = activityMap.get(user);
|
||||
|
||||
//If we have no recorded activity, or if the the time between now and the last activity is greater than two minutes
|
||||
if(activity == null || millis - activity > tolerance){
|
||||
//Set last user activity
|
||||
activityMap.set(user, millis);
|
||||
|
||||
//If we wheren't handed a free user doc
|
||||
if(userDB == null){
|
||||
//Pull one from the username
|
||||
userDB = await userSchema.userModel.findOne({user: user});
|
||||
}
|
||||
|
||||
//Set last active in user's DB document
|
||||
userDB.lastActive = now;
|
||||
|
||||
//If saving is enabled
|
||||
if(!noSave){
|
||||
//Save document to
|
||||
await userDB.save();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue