Split pmHandler to create auxServer class for easy creation of server classes auxiliary to the channel.
This commit is contained in:
parent
d874f5e2da
commit
597a984e46
3 changed files with 90 additions and 41 deletions
|
|
@ -14,44 +14,25 @@ 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 auxServer = require('../auxServer');
|
||||
const chatPreprocessor = require('../chatPreprocessor');
|
||||
const loggerUtils = require("../../utils/loggerUtils");
|
||||
const socketUtils = require("../../utils/socketUtils");
|
||||
const message = require("./message");
|
||||
|
||||
/**
|
||||
* Class containg global server-side private message relay logic
|
||||
*/
|
||||
class pmHandler{
|
||||
class pmHandler extends auxServer{
|
||||
/**
|
||||
* Instantiates object containing global server-side private message relay logic
|
||||
* @param {Socket.io} io - Socket.io server instanced passed down from server.js
|
||||
* @param {channelManager} chanServer - Sister channel management server object
|
||||
*/
|
||||
constructor(io, chanServer){
|
||||
/**
|
||||
* Socket.io server instance passed down from server.js
|
||||
*/
|
||||
this.io = io;
|
||||
|
||||
/**
|
||||
* Sister channel management server object
|
||||
*/
|
||||
this.chanServer = chanServer;
|
||||
|
||||
/**
|
||||
* Socket.io server namespace for handling messaging
|
||||
*/
|
||||
this.namespace = io.of('/pm');
|
||||
super(io, chanServer, "/pm");
|
||||
|
||||
this.chatPreprocessor = new chatPreprocessor(null, null);
|
||||
|
||||
//Handle connections from private messaging namespace
|
||||
this.namespace.on("connection", this.handleConnection.bind(this) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -59,31 +40,21 @@ class pmHandler{
|
|||
* @param {Socket} socket - Requesting Socket
|
||||
*/
|
||||
async handleConnection(socket){
|
||||
try{
|
||||
//ensure unbanned ip and valid CSRF token
|
||||
if(!(await socketUtils.validateSocket(socket))){
|
||||
socket.disconnect();
|
||||
return;
|
||||
}
|
||||
//Check if we're properly authorized
|
||||
const authorized = await super.handleConnection(socket, "${user}");
|
||||
|
||||
//If the socket wasn't authorized
|
||||
if(await socketUtils.authSocketLite(socket) == null){
|
||||
socket.disconnect();
|
||||
return;
|
||||
}
|
||||
|
||||
//Throw socket into room named after it's user
|
||||
//If we're authorized
|
||||
if(authorized){
|
||||
//Throw the user into their own unique channel
|
||||
socket.join(socket.user.user);
|
||||
|
||||
//Define network related event listeners against socket
|
||||
//Define listeners
|
||||
this.defineListeners(socket);
|
||||
}catch(err){
|
||||
//Flip a table if something fucks up
|
||||
return loggerUtils.socketCriticalExceptionHandler(socket, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
defineListeners(socket){
|
||||
super.defineListeners(socket);
|
||||
socket.on("pm", (data)=>{this.handlePM(data, socket)});
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue