habitica/website/server/libs/chat.js

53 lines
1.7 KiB
JavaScript
Raw Normal View History

import { model as User } from '../models/user'; // eslint-disable-line import/no-cycle
import { getUserInfo } from './email'; // eslint-disable-line import/no-cycle
2019-10-08 14:57:10 +00:00
import { sendNotification as sendPushNotification } from './pushNotifications';
2018-02-11 15:35:24 +00:00
export async function getAuthorEmailFromMessage (message) {
2019-10-08 14:57:10 +00:00
const authorId = message.uuid;
2018-02-11 15:35:24 +00:00
if (authorId === 'system') {
return 'system';
}
2019-10-08 14:57:10 +00:00
const author = await User.findOne({ _id: authorId }, { auth: 1 }).exec();
2018-02-11 15:35:24 +00:00
if (author) {
return getUserInfo(author, ['email']).email;
}
2019-10-08 14:57:10 +00:00
return 'Author Account Deleted';
2018-02-11 15:35:24 +00:00
}
export async function sendChatPushNotifications (user, group, message, mentions, translate) {
2019-10-08 14:57:10 +00:00
const members = await User.find({
'party._id': group._id,
2019-10-08 14:57:10 +00:00
_id: { $ne: user._id },
})
.select('preferences.pushNotifications preferences.language profile.name pushDevices auth.local.username')
.exec();
members.forEach(member => {
if (member.preferences.pushNotifications.partyActivity !== false) {
if (mentions && mentions.includes(`@${member.auth.local.username}`) && member.preferences.pushNotifications.mentionParty !== false) {
return;
}
sendPushNotification(
member,
{
2019-10-08 14:57:10 +00:00
title: translate('groupActivityNotificationTitle', { user: message.user, group: group.name }, member.preferences.language),
message: message.text,
identifier: 'groupActivity',
category: 'groupActivity',
2019-10-08 14:57:10 +00:00
payload: {
groupID: group._id,
type: group.type,
groupName: group.name,
message: message.text,
timestamp: message.timestamp,
senderName: message.user,
2019-10-08 14:57:10 +00:00
},
},
);
}
});
2019-10-08 14:57:10 +00:00
}