Parcourir la source

Emit socketio event to user when new notification

sbkwgh il y a 8 ans
Parent
commit
e8c194d047
1 fichiers modifiés avec 8 ajouts et 1 suppressions
  1. 8 1
      routes/post.js

+ 8 - 1
routes/post.js

@@ -156,7 +156,14 @@ router.post('/', async (req, res) => {
 
 		if(req.body.mentions) {
 			for(var i = 0; i < req.body.mentions.length; i++) {
-				await Notification.createMention({ mention: req.body.mentions[i], user, post })
+				let mention = req.body.mentions[i]
+				let ioUsers = req.app.get('io-users')
+
+				let notification = await Notification.createMention({ mention, user, post })
+				
+				if(ioUsers[mention]) {
+					req.app.get('io').to(ioUsers[mention]).emit('notification', notification.toJSON())
+				}
 			}
 		}