habitica/website/server/libs/chat.js

44 lines
1.4 KiB
JavaScript
Raw Normal View History

2018-02-11 15:35:24 +00:00
import { model as User } from '../models/user';
import { getUserInfo } from './email';
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, 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')
.exec();
members.forEach(member => {
if (member.preferences.pushNotifications.partyActivity !== false) {
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
}