sharkey/src/server/api/endpoints/messaging/unread.ts

34 lines
614 B
TypeScript
Raw Normal View History

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({
muter_id: user._id,
deleted_at: { $exists: false }
});
const mutedUserIds = mute.map(m => m.mutee_id);
2016-12-29 07:49:51 +09:00
const count = await Message
.count({
2017-12-22 14:35:47 +09:00
user_id: {
$nin: mutedUserIds
},
2016-12-29 07:49:51 +09:00
recipient_id: user._id,
is_read: false
});
res({
count: count
});
});