@@ -44,12 +44,12 @@ function register (socket) {
4444function eventLoop ( ) {
4545updateUsers ( )
4646updateOnlineBubbles ( )
47- updateConversationsNotifications ( )
4847}
4948
5049events . onUpdateUsers = function ( socket ) {
5150socket . on ( 'updateUsers' , updateUsers )
5251}
52+
5353events . onSetUserOnlineStatus = function ( socket ) {
5454socket . on ( socketEventConst . UI_ONLINE_STATUS_SET , data => {
5555const state = data . state
@@ -186,45 +186,34 @@ events.updateOnlineBubbles = function (socket) {
186186} )
187187}
188188
189- async function updateConversationsNotifications ( ) {
190- return // TODO: Disable until we can fix the performance hit on this eventloop.
191- const sockets = await io . fetchSockets ( )
192- sockets . forEach ( function ( socket ) {
193- if ( ! socket . request && ! socket . request . user ) {
194- return
195- }
189+ async function updateConversationsNotifications ( socket ) {
190+ if ( socket && socket . request && socket . request . user ) {
191+ const user = socket . request . user
192+ const Message = require ( '../models/chat/message' )
193+ const Conversation = require ( '../models/chat/conversation' )
196194
197- const userId = socket . request . user . _id
198- const messageSchema = require ( '../models/chat/message' )
199- const conversationSchema = require ( '../models/chat/conversation' )
200- conversationSchema . getConversationsWithLimit ( userId , 10 , function ( err , conversations ) {
195+ Conversation . getConversationsWithLimit ( user . _id , 10 , ( err , conversation ) => {
201196if ( err ) {
202197winston . warn ( err . message )
203198return false
204199}
205200
206201const convos = [ ]
207-
208202async . eachSeries (
209- conversations ,
210- function ( convo , done ) {
203+ conversation ,
204+ ( convo , done ) => {
211205const c = convo . toObject ( )
212206
213- const userMeta =
214- convo . userMeta [
215- _ . findIndex ( convo . userMeta , function ( item ) {
216- return item . userId . toString ( ) === userId . toString ( )
217- } )
218- ]
207+ const userMeta = convo . userMeta [ _ . findIndex ( convo . userMeta , i => i . userId . toString ( ) === user . _id . toString ( ) ) ]
219208if ( ! _ . isUndefined ( userMeta ) && ! _ . isUndefined ( userMeta . deletedAt ) && userMeta . deletedAt > convo . updatedAt ) {
220209return done ( )
221210}
222211
223- messageSchema . getMostRecentMessage ( c . _id , function ( err , rm ) {
212+ Message . getMostRecentMessage ( c . _id , ( err , rm ) => {
224213if ( err ) return done ( err )
225214
226- _ . each ( c . participants , function ( p ) {
227- if ( p . _id . toString ( ) !== userId . toString ( ) ) {
215+ _ . each ( c . participants , p => {
216+ if ( p . _id . toString ( ) !== user . _id . toString ( ) ) {
228217c . partner = p
229218}
230219} )
@@ -248,15 +237,15 @@ async function updateConversationsNotifications () {
248237return done ( )
249238} )
250239} ,
251- function ( err ) {
240+ err => {
252241if ( err ) return false
253242return utils . sendToSelf ( socket , socketEventConst . MESSAGES_UPDATE_UI_CONVERSATION_NOTIFICATIONS , {
254243conversations :convos
255244} )
256245}
257246)
258247} )
259- } )
248+ }
260249}
261250
262251events . updateConversationsNotifications = function ( socket ) {