2016-12-29 07:49:51 +09:00
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
import Message from '../../models/messaging-message';
|
2017-12-22 14:35:47 +09:00
|
|
|
import Mute from '../../models/mute';
|
2016-12-29 07:49:51 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get count of unread messages
|
|
|
|
*
|
2017-03-01 17:37:01 +09:00
|
|
|
* @param {any} params
|
|
|
|
* @param {any} user
|
|
|
|
* @return {Promise<any>}
|
2016-12-29 07:49:51 +09:00
|
|
|
*/
|
2017-03-04 04:28:38 +09:00
|
|
|
module.exports = (params, user) => new Promise(async (res, rej) => {
|
2017-12-22 14:35:47 +09:00
|
|
|
const mute = await Mute.find({
|
2018-03-29 14:48:47 +09:00
|
|
|
muterId: user._id,
|
|
|
|
deletedAt: { $exists: false }
|
2017-12-22 14:35:47 +09:00
|
|
|
});
|
2018-03-29 14:48:47 +09:00
|
|
|
const mutedUserIds = mute.map(m => m.muteeId);
|
2017-12-22 14:35:47 +09:00
|
|
|
|
2016-12-29 07:49:51 +09:00
|
|
|
const count = await Message
|
|
|
|
.count({
|
2018-03-29 14:48:47 +09:00
|
|
|
userId: {
|
2017-12-22 14:35:47 +09:00
|
|
|
$nin: mutedUserIds
|
|
|
|
},
|
2018-03-29 14:48:47 +09:00
|
|
|
recipientId: user._id,
|
|
|
|
isRead: false
|
2016-12-29 07:49:51 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
res({
|
|
|
|
count: count
|
|
|
|
});
|
|
|
|
});
|