import { Response } from 'express' ; import { Server } from 'socket.io' ; import { DiscussionDocument } from './models/Discussion' ; import { PostDocument } from './models/Post' ; let io = null ; function setupSockets ( { httpServer , origin , sessionMiddleware } ) { if ( io === null ) { io = new Server ( httpServer , { cors : { origin , methods : 'GET,HEAD,PUT,PATCH,POST,DELETE' , credentials : true } , cookie : { httpOnly : true , maxAge : 14 * 24 * 60 * 60 * 1000 , domain : 'localhost' , secure : false } , serveClient : false , transports : [ 'polling' , 'websocket' ] } ) ; const wrap = ( middleware ) => ( socket , next ) => middleware ( socket . request , { } as Response , next ) ; io . use ( wrap ( sessionMiddleware ) ) ; io . on ( 'connection' , ( socket ) => { if ( ! socket . request . session || ( ( ! socket . request . session . passport || ! socket . request . session . passport . user ) && ! socket . request . session . passwordless ) ) { socket . disconnect ( true ) ; return ; } socket . on ( 'joinTeamRoom' , ( teamId ) => { console . log ( `    joinTeamRoom ${ teamId }` ) ; socket . join ( `teamRoom-${ teamId }` ) ; } ) ; socket . on ( 'leaveTeamRoom' , ( teamId ) => { console . log ( `** leaveTeamRoom ${ teamId }` ) ; socket . leave ( `teamRoom-${ teamId }` ) ; } ) ; socket . on ( 'joinDiscussionRoom' , ( discussionId ) => { console . log ( `    joinDiscussionRoom ${ discussionId }` ) ; socket . join ( `discussionRoom-${ discussionId }` ) ; } ) ; socket . on ( 'leaveDiscussionRoom' , ( discussionId ) => { console . log ( `** leaveDiscussionRoom ${ discussionId }` ) ; socket . leave ( `discussionRoom-${ discussionId }` ) ; } ) ; socket . on ( 'disconnect' , ( reason ) => { console . log ( `disconnected` , `reason: ` + reason ) ; } ) ; } ) ; } } function getSocket ( socketId ? ) { if ( ! io ) { return null ; } if ( socketId && io . sockets . sockets . get ( socketId ) ) { return io . sockets . sockets . get ( socketId ) . broadcast ; } else { return io ; } } function discussionAdded ( { socketId , discussion , } ) { const roomName = `teamRoom-${ discussion . teamId }` ; const socket = getSocket ( socketId ) ; if ( socket ) { socket . to ( roomName ) . emit ( 'discussionEvent' , { actionType : 'added' , discussion } ) ; } } function discussionEdited ( { socketId , discussion , } ) { const roomName = `teamRoom-${ discussion . teamId }` ; const socket = getSocket ( socketId ) ; if ( socket ) { socket . to ( roomName ) . emit ( 'discussionEvent' , { actionType : 'edited' , discussion } ) ; } } function discussionDeleted ( { socketId , teamId , id , } ) { const roomName = `teamRoom-${ teamId }` ; const socket = getSocket ( socketId ) ; if ( socket ) { socket . to ( roomName ) . emit ( 'discussionEvent' , { actionType : 'deleted' , id } ) ; } } function postAdded ( { socketId , post } ) { const roomName = `discussionRoom-${ post . discussionId }` ; const socket = getSocket ( socketId ) ; if ( socket ) { socket . to ( roomName ) . emit ( 'postEvent' , { actionType : 'added' , post } ) ; } } function postEdited ( { socketId , post } ) { const roomName = `discussionRoom-${ post . discussionId }` ; const socket = getSocket ( socketId ) ; if ( socket ) { socket . to ( roomName ) . emit ( 'postEvent' , { actionType : 'edited' , post } ) ; } } function postDeleted ( { socketId , id , discussionId , } ) { const roomName = `discussionRoom-${ discussionId }` ; const socket = getSocket ( socketId ) ; if ( socket ) { socket . to ( roomName ) . emit ( 'postEvent' , { actionType : 'deleted' , id } ) ; } } export { setupSockets , postAdded , postEdited , postDeleted , discussionAdded , discussionEdited , discussionDeleted , } ;