Improvements and fixes for push notifications (#11507)

* Strip markdown from push notifications

* Revert "Strip markdown from push notifications"

This reverts commit 4741e584c63031d0d4609e18a4e4f082d4c4e72e.

* correctly set type for mention push notifications

* Add unformattedText field to chat messages

* fiix lint errors

* Add check that markdown formatting is stripped from messages

* Add check for markdown formatting in messages.
This commit is contained in:
Phillip Thelen 2019-11-29 18:46:26 +01:00 committed by Matteo Pagliazzi
parent 3c394e7448
commit b108b047cd
8 changed files with 28 additions and 9 deletions

5
package-lock.json generated
View file

@ -10881,6 +10881,11 @@
}
}
},
"remove-markdown": {
"version": "0.3.0",
"resolved": "https://registry.npmjs.org/remove-markdown/-/remove-markdown-0.3.0.tgz",
"integrity": "sha1-XktmdJOpNXlyjz1S7MHbnKUF3Jg="
},
"remove-trailing-separator": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/remove-trailing-separator/-/remove-trailing-separator-1.1.0.tgz",

View file

@ -70,7 +70,8 @@
"vinyl-buffer": "^1.0.1",
"winston": "^2.4.3",
"winston-loggly-bulk": "^2.0.2",
"xml2js": "^0.4.4"
"xml2js": "^0.4.4",
"remove-markdown": "^0.3.0"
},
"private": true,
"engines": {

View file

@ -1317,7 +1317,7 @@ describe('Group Model', () => {
it('formats message', () => {
const chatMessage = party.sendChat({
message: 'a new message',
message: 'a _new_ message with *markdown*',
user: {
_id: 'user-id',
profile: { name: 'user name' },
@ -1336,7 +1336,8 @@ describe('Group Model', () => {
const chat = chatMessage;
expect(chat.text).to.eql('a new message');
expect(chat.text).to.eql('a _new_ message with *markdown*');
expect(chat.unformattedText).to.eql('a new message with markdown');
expect(validator.isUUID(chat.id)).to.eql(true);
expect(chat.timestamp).to.be.a('date');
expect(chat.likes).to.eql({});

View file

@ -6,7 +6,8 @@ import {
describe('POST /members/send-private-message', () => {
let userToSendMessage;
const messageToSend = 'Test Private Message';
const messageToSend = 'Test *Private* Message';
const unformattedMessage = 'Test Private Message';
beforeEach(async () => {
userToSendMessage = await generateUser();
@ -110,7 +111,9 @@ describe('POST /members/send-private-message', () => {
const sendersMessageInReceiversInbox = _.find(
updatedReceiver.inbox.messages,
message => message.uuid === userToSendMessage._id && message.text === messageToSend,
message => message.uuid === userToSendMessage._id
&& message.text === messageToSend
&& message.unformattedText === unformattedMessage,
);
const sendersMessageInSendersInbox = _.find(

View file

@ -34,7 +34,7 @@ export async function sendChatPushNotifications (user, group, message, mentions,
member,
{
title: translate('groupActivityNotificationTitle', { user: message.user, group: group.name }, member.preferences.language),
message: message.text,
message: message.unformattedText,
identifier: 'groupActivity',
category: 'groupActivity',
payload: {

View file

@ -23,7 +23,7 @@ export async function sentMessage (sender, receiver, message, translate) {
{ name: getUserInfo(sender, ['name']).name },
receiver.preferences.language,
),
message,
message: messageSent.unformattedText,
identifier: 'newPM',
category: 'newPM',
payload: { replyTo: sender._id, senderName, message },

View file

@ -634,7 +634,12 @@ schema.methods.sendChat = function sendChat (options = {}) {
return;
}
}
sendPushNotification(member, { identifier: 'chatMention', title: `${user.profile.name} mentioned you in ${this.name}`, message });
sendPushNotification(member, {
identifier: 'chatMention',
title: `${user.profile.name} mentioned you in ${this.name}`,
message: newChatMessage.unformattedText,
payload: { type: this.type },
});
});
}
return newChatMessage;

View file

@ -1,12 +1,14 @@
import mongoose from 'mongoose';
import { v4 as uuid } from 'uuid';
import { defaults } from 'lodash';
import removeMd from 'remove-markdown';
import baseModel from '../libs/baseModel';
const defaultSchema = () => ({
id: String,
timestamp: Date,
text: String,
unformattedText: String,
info: { $type: mongoose.Schema.Types.Mixed },
// sender properties
@ -110,10 +112,12 @@ export function setUserStyles (newMessage, user) {
export function messageDefaults (msg, user, client, flagCount = 0, info = {}) {
const id = uuid();
const trimmedMessage = msg.substring(0, 3000);
const message = {
id,
_id: id,
text: msg.substring(0, 3000),
text: trimmedMessage,
unformattedText: removeMd(trimmedMessage),
info,
timestamp: Number(new Date()),
likes: {},