Base implementation of PM back-end started.
This commit is contained in:
parent
7da07c8717
commit
67edef9035
5 changed files with 182 additions and 12 deletions
|
|
@ -20,20 +20,20 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.*/
|
|||
class message{
|
||||
/**
|
||||
* Instantiates a chat message object
|
||||
* @param {connectedUser} sender - User who sent the message
|
||||
* @param {Array} recipients - Array of connected users who are supposed to receive the message
|
||||
* @param {String} sender - Name of user who sent the message
|
||||
* @param {Array} recipients - Array of usernames who are supposed to receive the message
|
||||
* @param {String} msg - Contents of the message, with links replaced with numbered file-seperator markers
|
||||
* @param {Array} links - Array of URLs/Links included in the message.
|
||||
*/
|
||||
constructor(sender, recipients, msg, links){
|
||||
|
||||
/**
|
||||
* User who sent the message
|
||||
* Name of user who sent the message
|
||||
*/
|
||||
this.sender = sender;
|
||||
|
||||
/**
|
||||
* Array of strings containing usernames to send message to
|
||||
* Array of usernames who are supposed to receive the message
|
||||
*/
|
||||
this.recipients = recipients;
|
||||
|
||||
|
|
|
|||
|
|
@ -14,10 +14,13 @@ 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/>.*/
|
||||
|
||||
//NPM Imports
|
||||
const validator = require('validator');//No express here, so regular validator it is!
|
||||
|
||||
//local includes
|
||||
const config = require("../../../config.json");
|
||||
const csrfUtils = require("../../utils/csrfUtils");
|
||||
const userBanModel = require("../../schemas/user/userBanSchema");
|
||||
const loggerUtils = require("../../utils/loggerUtils");
|
||||
const socketUtils = require("../../utils/socketUtils");
|
||||
const message = require("./message");
|
||||
|
||||
/**
|
||||
* Class containg global server-side private message relay logic
|
||||
|
|
@ -43,7 +46,123 @@ class pmHandler{
|
|||
}
|
||||
|
||||
async handleConnection(socket){
|
||||
try{
|
||||
//ensure unbanned ip and valid CSRF token
|
||||
if(!(await socketUtils.validateSocket(socket))){
|
||||
socket.disconnect();
|
||||
return;
|
||||
}
|
||||
|
||||
//If the socket wasn't authorized
|
||||
if(await socketUtils.authSocketLite(socket) == null){
|
||||
socket.disconnect();
|
||||
return;
|
||||
}
|
||||
|
||||
//Throw socket into room named after it's user
|
||||
socket.join(socket.user.user);
|
||||
|
||||
//Define network related event listeners against socket
|
||||
this.defineListeners(socket);
|
||||
}catch(err){
|
||||
//Flip a table if something fucks up
|
||||
return loggerUtils.socketCriticalExceptionHandler(socket, err);
|
||||
}
|
||||
}
|
||||
|
||||
defineListeners(socket){
|
||||
socket.on("pm", (data)=>{this.handlePM(data, socket)});
|
||||
}
|
||||
|
||||
async handlePM(data, socket){
|
||||
try{
|
||||
//Create empty list of recipients
|
||||
let recipients = [];
|
||||
|
||||
//For each requested recipient
|
||||
for(let user of data.recipients){
|
||||
//If the given user is online and didn't send the message
|
||||
if(this.checkPresence(user) && user != socket.user.user){
|
||||
//Add the recipient to the list
|
||||
recipients.push(user);
|
||||
}
|
||||
}
|
||||
|
||||
//If we don't have any valid recipients
|
||||
if(recipients.length <= 0){
|
||||
//Drop that shit
|
||||
return;
|
||||
}
|
||||
|
||||
//Sanatize Message
|
||||
const msg = this.sanatizeMessage(data.msg);
|
||||
|
||||
//If we have an invalid message
|
||||
if(msg == null){
|
||||
//Drop that shit
|
||||
return;
|
||||
}
|
||||
|
||||
//Create message object and relay it off to the recipients
|
||||
this.relayPMObj(new message(
|
||||
socket.user.user,
|
||||
recipients,
|
||||
msg,
|
||||
[]
|
||||
));
|
||||
|
||||
//If something fucked up
|
||||
}catch(err){
|
||||
//Bitch and moan
|
||||
return loggerUtils.socketExceptionHandler(socket, err);
|
||||
}
|
||||
}
|
||||
|
||||
relayPMObj(msg){
|
||||
//For each recipient
|
||||
for(let user of msg.recipients){
|
||||
//Send the message
|
||||
this.namespace.to(user).emit("message", msg);
|
||||
}
|
||||
|
||||
//Acknowledge the sent message
|
||||
this.namespace.to(msg.sender).emit("sent", msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Basic function for checking presence
|
||||
* This could be done using Channel Presence, but running off of bare Socket.io functionality makes this easier to implement outside the channel if need be
|
||||
* @param {String} user - Username to check presence of
|
||||
* @returns {Boolean} Whether or not the user is currently able to accept messages
|
||||
*/
|
||||
checkPresence(user){
|
||||
//Pull room map from the guts of socket.io and run a null check against the given username
|
||||
return this.namespace.adapter.rooms.get(user) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanatizes and Validates a single message, Temporary until we get commandPreprocessor split up.
|
||||
* @param {String} msg - message to validate/sanatize
|
||||
* @returns {String} sanatized/validates message, returns null on validation failure
|
||||
*/
|
||||
sanatizeMessage(msg){
|
||||
//if msg is empty or null
|
||||
if(msg == null || msg == ''){
|
||||
//Pimp slap that shit into fucking oblivion
|
||||
return null;
|
||||
}
|
||||
|
||||
//Trim and Sanatize for XSS
|
||||
msg = validator.trim(validator.escape(msg));
|
||||
|
||||
//Return whether or not the shit was long enough
|
||||
if(validator.isLength(msg, {min: 1, max: 255})){
|
||||
//If it's valid return the message
|
||||
return msg;
|
||||
}
|
||||
|
||||
//if not return nothing
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue