Skip to content

Commit

Permalink
Merge pull request RocketChat#544 from WideChat/push_message_post_pro…
Browse files Browse the repository at this point in the history
…cess

[NEW] Add feature to post process push message and display as a rich message
  • Loading branch information
ear-dev authored Mar 25, 2021
2 parents 6f4ffbe + b4ce43a commit c3d93b6
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 9 deletions.
5 changes: 5 additions & 0 deletions app/lib/server/functions/notifications/mobile.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export function getNotificationPayload({
t: message.t,
},
},
isPushMessage: message.pushm,
userId,
};
}
Expand All @@ -110,6 +111,10 @@ export function sendWebPush(notification, platform) {
return;
}

if (notification.isPushMessage) {
return;
}

const gcmKey = settings.get('Push_gcm_api_key');
const vapidPublic = settings.get('Vapid_public_key');
const vapidPrivate = settings.get('Vapid_private_key');
Expand Down
4 changes: 3 additions & 1 deletion app/lib/server/functions/processWebhookMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ export const processWebhookMessage = function(messageObj, user, defaultValues =

if (messageObj.pushm && messageObj.pushm === 'true') {
message.pushm = true;
message.pushm_encrypted = true;
message.pushm_post_processed = false;
message.pushm_scope = messageObj.pushm_scope;
message.pushm_origin = messageObj.pushm_origin;
}

const messageReturn = sendMessage(user, message, room);
Expand Down
1 change: 1 addition & 0 deletions app/lib/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import './methods/removeOAuthService';
import './methods/removeUserFromPushSubscription';
import './methods/restartServer';
import './methods/robotMethods';
import './methods/savePostProcessedMessage';
import './methods/savePushNotificationSubscription';
import './methods/saveSetting';
import './methods/saveSettings';
Expand Down
15 changes: 15 additions & 0 deletions app/lib/server/methods/savePostProcessedMessage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Meteor } from 'meteor/meteor';

import { Messages } from '../../../models';

Meteor.methods({
savePostProcessedMessage(_id, message) {
const originalMessage = Messages.findOneById(_id);

if (!originalMessage || !originalMessage._id) {
return;
}

return Messages.updatePostProcessedPushMessageById(_id, message);
},
});
14 changes: 14 additions & 0 deletions app/models/server/models/Messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,20 @@ export class Messages extends Base {
return this.update(query, update, { multi: true });
}

updatePostProcessedPushMessageById(_id, post_processed_message) {
const query = { _id };

const update = {
$set: {
post_processed_message,
pushm_post_processed: true,
msg: '',
},
};

return this.update(query, update);
}

// INSERT
createWithTypeRoomIdMessageAndUser(type, roomId, message, user, extraData) {
const record = {
Expand Down
2 changes: 1 addition & 1 deletion app/ui-message/client/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ Template.message.helpers({
},
isPushMessage() {
const { msg } = this;
return msg.isPushMessage;
return msg.pushm;
},
showStar() {
const { msg } = this;
Expand Down
28 changes: 22 additions & 6 deletions app/ui-message/client/pushMessage.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
import { Meteor } from 'meteor/meteor';
import { Template } from 'meteor/templating';

import { timeAgo } from '../../lib/client/lib/formatDate';
import './pushMessage.html';

Template.pushMessage.helpers({
data() {
const { title, options } = this.msg.pushMessage;
const data = {
title,
...options,
};
return data;
const { _id, pushm_post_processed, pushm_scope, pushm_origin, msg, post_processed_message } = this.msg;

if (!pushm_post_processed) {
navigator.serviceWorker.ready.then((serviceWorkerRegistration) => {
console.log('Pushing message to service worker for post processing');
const promise = serviceWorkerRegistration.monitorNotification(pushm_origin);
serviceWorkerRegistration.pushManager.dispatchMessage(pushm_scope, msg);
promise.then((post_processed_message) => {
const newMsg = {};
console.log(post_processed_message);
newMsg.title = post_processed_message.title;
newMsg.body = post_processed_message.body;
newMsg.icon = post_processed_message.icon;
newMsg.actions = post_processed_message.actions;
newMsg.timestamp = post_processed_message.timestamp;
Meteor.call('savePostProcessedMessage', _id, newMsg);
});
});
} else {
return post_processed_message;
}
},
timeAgo(date) {
return timeAgo(date);
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c3d93b6

Please sign in to comment.