@@ -1266,6 +1266,109 @@ function generateWebsocketID() {
12661266 return _websocketIDCounter + "_" + Math . round ( Math . random ( ) * 9999999 ) ;
12671267}
12681268
1269+ var notifyWSS = new ws . WebSocketServer ( { noServer : true , ...wssServerOptions } ) ;
1270+ var notificationsForUsers = { } ;
1271+ var notificationUserTimeouts = { } ;
1272+ notifyWSS . on ( "connection" , ( ws , request ) => {
1273+ ( async function ( ) {
1274+ var usercookie = getCookie ( "account" , getCookieFromRequest ( request ) ) ;
1275+ if ( usercookie ) {
1276+ var decryptedUserdata = encryptor . decrypt ( usercookie ) ;
1277+ decryptedUserdata . username = ( decryptedUserdata . username || "" )
1278+ . toLowerCase ( )
1279+ . trim ( ) ;
1280+ var validation = await validateUserCookie ( decryptedUserdata ) ;
1281+ if ( validation . valid ) {
1282+ ws . _rrUsername = decryptedUserdata . username . toLowerCase ( ) . trim ( ) ;
1283+ ws . _rrDisplayName = validation . displayName ;
1284+ } else {
1285+ ws . terminate ( ) ;
1286+ return ;
1287+ }
1288+ } else {
1289+ ws . terminate ( ) ;
1290+ return ;
1291+ }
1292+ terminateGhostSockets ( ws ) ;
1293+ if ( ! notificationsForUsers [ ws . _rrUsername ] ) {
1294+ notificationsForUsers [ ws . _rrUsername ] = [ ] ;
1295+ }
1296+ clearTimeout ( notificationUserTimeouts [ ws . _rrUsername ] ) ;
1297+ notificationUserTimeouts [ ws . _rrUsername ] = undefined ;
1298+ ws . on ( "message" , ( data ) => {
1299+ var json = JSON . parse ( data . toString ( ) ) ;
1300+ if ( json . type == "read" ) {
1301+ if ( typeof json . id == "number" ) {
1302+ notificationsForUsers [ ws . _rrUsername ] = notificationsForUsers [
1303+ ws . _rrUsername
1304+ ] . filter ( ( n ) => json . id !== n . id ) ;
1305+ for ( var client of notifyWSS . clients ) {
1306+ if ( client . _rrUsername == ws . _rrUsername ) {
1307+ client . send (
1308+ JSON . stringify ( {
1309+ type : "current" ,
1310+ notifications : notificationsForUsers [ ws . _rrUsername ] ,
1311+ } )
1312+ ) ;
1313+ }
1314+ }
1315+ }
1316+ }
1317+ if ( json . type == "readAll" ) {
1318+ notificationsForUsers [ ws . _rrUsername ] = [ ] ;
1319+ for ( var client of notifyWSS . clients ) {
1320+ if ( client . _rrUsername == ws . _rrUsername ) {
1321+ client . send (
1322+ JSON . stringify ( {
1323+ type : "current" ,
1324+ notifications : notificationsForUsers [ ws . _rrUsername ] ,
1325+ } )
1326+ ) ;
1327+ }
1328+ }
1329+ }
1330+ } ) ;
1331+ ws . on ( "close" , ( ) => {
1332+ notificationUserTimeouts [ ws . _rrUsername ] = setTimeout (
1333+ ( ) => {
1334+ notificationsForUsers [ ws . _rrUsername ] = undefined ;
1335+ notificationUserTimeouts [ ws . _rrUsername ] = undefined ;
1336+ } ,
1337+ 1000 * 60 * 10
1338+ ) ; //10 Minutes before all notifications are cleared.
1339+ } ) ;
1340+ ws . send (
1341+ JSON . stringify ( {
1342+ type : "current" ,
1343+ notifications : notificationsForUsers [ ws . _rrUsername ] ,
1344+ } )
1345+ ) ;
1346+ } ) ( ) ;
1347+ } ) ;
1348+ function sendNotify ( username , notifyjson = { } ) {
1349+ if ( ! notificationsForUsers [ username ] ) {
1350+ notificationsForUsers [ username ] = [ ] ;
1351+ }
1352+ var notifyContent = {
1353+ id : Date . now ( ) ,
1354+ ...notifyjson ,
1355+ } ;
1356+ notificationsForUsers [ username ] . push ( notifyContent ) ;
1357+ notificationsForUsers [ username ] = notificationsForUsers [ username ] . slice (
1358+ - cons . MAX_NOTIFICATIONS
1359+ ) ;
1360+ for ( var client of notifyWSS . clients ) {
1361+ if ( client . _rrUsername == username . trim ( ) ) {
1362+ client . send (
1363+ JSON . stringify ( {
1364+ type : "new" ,
1365+ notification : notifyContent ,
1366+ } )
1367+ ) ;
1368+ }
1369+ }
1370+ }
1371+
12691372async function startRoomWSS ( roomid ) {
12701373 var wss = new ws . WebSocketServer ( { noServer : true , ...wssServerOptions } ) ;
12711374 roomWebsockets [ roomid . toString ( ) ] = "loading" ;
@@ -1482,6 +1585,9 @@ async function startRoomWSS(roomid) {
14821585 } ;
14831586 if ( usercookie ) {
14841587 var decryptedUserdata = encryptor . decrypt ( usercookie ) ;
1588+ decryptedUserdata . username = ( decryptedUserdata . username || "" )
1589+ . toLowerCase ( )
1590+ . trim ( ) ;
14851591 var validation = await validateUserCookie ( decryptedUserdata ) ;
14861592 for ( var cli of wss . clients ) {
14871593 if ( cli . _rrUsername && cli . _rrLoggedIn ) {
@@ -1891,8 +1997,6 @@ async function startRoomWSS(roomid) {
18911997 ) ;
18921998 return ;
18931999 }
1894- messageChatNumber += 1 ;
1895- wss . _rrRoomMessages = wss . _rrRoomMessages . slice ( - 100 ) ;
18962000 wss . clients . forEach ( ( cli ) => {
18972001 if ( ! cli . _rrIsReady ) {
18982002 return ;
@@ -1914,13 +2018,15 @@ async function startRoomWSS(roomid) {
19142018
19152019 if ( ! json . message . trim ( ) . startsWith ( ";" ) ) {
19162020 //Filter out command messages in history.
2021+ messageChatNumber += 1 ;
19172022 wss . _rrRoomMessages . push ( {
19182023 displayName : displayName ,
19192024 username : ws . _rrUsername ,
19202025 message : json . message ,
19212026 color : ws . _rrUserColor ,
19222027 font : ws . _rrUserFont ,
19232028 } ) ;
2029+ wss . _rrRoomMessages = wss . _rrRoomMessages . slice ( - 50 ) ;
19242030 }
19252031 }
19262032 }
@@ -3954,6 +4060,12 @@ const server = http.createServer(async function (req, res) {
39544060 }
39554061 }
39564062 profilejson . rooms = rooms ;
4063+ sendNotify ( json . username . toLowerCase ( ) . trim ( ) , {
4064+ type : "invite" ,
4065+ from : decryptedUserdata . username . toLowerCase ( ) . trim ( ) ,
4066+ roomName : json . name ,
4067+ roomID : json . id ,
4068+ } ) ;
39574069 await storage . uploadFile (
39584070 profileFile ,
39594071 JSON . stringify ( profilejson ) ,
@@ -4470,6 +4582,12 @@ server.on("upgrade", async function upgrade(request, socket, head) {
44704582 var id = urlsplit [ 1 ] ;
44714583 var wss = null ;
44724584 try {
4585+ if ( id == "notifications" ) {
4586+ notifyWSS . handleUpgrade ( request , socket , head , function done ( ws ) {
4587+ notifyWSS . emit ( "connection" , ws , request ) ;
4588+ } ) ;
4589+ return ;
4590+ }
44734591 if ( id ) {
44744592 id = id . toLowerCase ( ) ;
44754593 var roomWs = roomWebsockets [ id . toString ( ) ] ;
@@ -4514,6 +4632,15 @@ setInterval(() => {
45144632 debugLogOnlineSockets();
45154633}, 2000);*/
45164634
4635+ /*setInterval(() => {
4636+ for (var username of Object.keys(notificationsForUsers)) {
4637+ sendNotify(username, {
4638+ type: "test",
4639+ });
4640+ console.log("Send to: " + username);
4641+ }
4642+ }, 5000);*/
4643+
45174644var serverPort = 3000 ;
45184645if ( process . env . serverPort ) {
45194646 serverPort = Number ( process . env . serverPort ) ;
0 commit comments