Send push notifications on mentions

This commit is contained in:
Phillip Thelen
2019-09-30 15:10:20 +02:00
parent ed266adfc2
commit 0072a3968d
4 changed files with 33 additions and 6 deletions

View File

@@ -20,6 +20,7 @@ import { getMatchesByWordArray } from '../../libs/stringUtils';
import bannedSlurs from '../../libs/bannedSlurs';
import apiError from '../../libs/apiError';
import {highlightMentions} from '../../libs/highlightMentions';
import {sendNotification} from '../../libs/pushNotifications';
const FLAG_REPORT_EMAILS = nconf.get('FLAG_REPORT_EMAIL').split(',').map((email) => {
return { email, canSend: true };
@@ -183,7 +184,7 @@ api.postChat = {
throw new NotAuthorized(res.t('messageGroupChatSpam'));
}
const [message, mentions] = await highlightMentions(req.body.message);
const [message, mentions, mentionedMembers] = await highlightMentions(req.body.message);
let client = req.headers['x-client'] || '3rd Party';
if (client) {
client = client.replace('habitica-', '');
@@ -192,7 +193,6 @@ api.postChat = {
let flagCount = 0;
if (group.privacy === 'public' && user.flags.chatShadowMuted) {
flagCount = common.constants.CHAT_FLAG_FROM_SHADOW_MUTE;
let message = req.body.message;
// Email the mods
let authorEmail = getUserInfo(user, ['email']).email;
@@ -232,6 +232,29 @@ api.postChat = {
toSave.push(user.save());
}
mentionedMembers.forEach((member) => {
if (member._id === user._id) return;
const pushNotifPrefs = member.preferences.pushNotifications;
if (group.type === 'party') {
if (pushNotifPrefs.mentionParty !== true) {
return;
}
} else if (member.guilds.contains(group._id)) {
if (pushNotifPrefs.mentionJoinedGuild !== true) {
return;
}
} else {
if (group.privacy !== 'public') {
return;
}
if (pushNotifPrefs.mentionUnjoinedGuild !== true) {
return;
}
}
sendNotification(member, {identifier: 'chatMention', title: `${user.profile.name} mentioned you in ${group.name}`, message: req.body.message});
});
await Promise.all(toSave);
let analyticsObject = {

View File

@@ -4,13 +4,14 @@ const mentionRegex = new RegExp('\\B@[-\\w]+', 'g');
export async function highlightMentions (text) {
const mentions = text.match(mentionRegex);
let members = [];
if (mentions !== null && mentions.length <= 5) {
const usernames = mentions.map((mention) => {
return mention.substr(1);
});
let members = await User
members = await User
.find({'auth.local.username': {$in: usernames}, 'flags.verifiedUsername': true})
.select(['auth.local.username', '_id'])
.select(['auth.local.username', '_id', 'preferences.pushNotifications', 'pushDevices'])
.lean()
.exec();
members.forEach((member) => {
@@ -18,5 +19,5 @@ export async function highlightMentions (text) {
text = text.replace(new RegExp(`@${username}(?![\\-\\w])`, 'g'), `[@${username}](/profile/${member._id})`);
});
}
return [text, mentions];
return [text, mentions, members];
}

View File

@@ -56,7 +56,7 @@ function sendNotification (user, details = {}) {
case 'ios':
if (apnProvider) {
const notification = new apn.Notification({
alert: details.message,
alert: {title: details.title, body: details.message},
sound: 'default',
category: details.category,
topic: 'com.habitrpg.ios.Habitica',

View File

@@ -503,6 +503,9 @@ let schema = new Schema({
questStarted: {$type: Boolean, default: true},
invitedQuest: {$type: Boolean, default: true},
majorUpdates: {$type: Boolean, default: true},
mentionParty: {$type: Boolean, default: true},
mentionJoinedGuild: {$type: Boolean, default: true},
mentionUnjoinedGuild: {$type: Boolean, default: true},
},
suppressModals: {
levelUp: {$type: Boolean, default: false},