Skip to content

Commit 79bc9c5

Browse files
Add new endpoint to receive the notification data
1 parent faeffbd commit 79bc9c5

File tree

2 files changed

+84
-13
lines changed

2 files changed

+84
-13
lines changed

app/api/server/v1/push.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { Meteor } from 'meteor/meteor';
22
import { Random } from 'meteor/random';
3+
import { Match, check } from 'meteor/check';
34

45
import { appTokensCollection } from '../../../push/server';
56
import { API } from '../api';
7+
import PushNotification from '../../../push-notifications/server/lib/PushNotification';
8+
69

710
API.v1.addRoute('push.token', { authRequired: true }, {
811
post() {
@@ -63,3 +66,16 @@ API.v1.addRoute('push.token', { authRequired: true }, {
6366
return API.v1.success();
6467
},
6568
});
69+
70+
API.v1.addRoute('push.get', { authRequired: true }, {
71+
get() {
72+
const params = this.requestParams();
73+
check(params, Match.ObjectIncluding({
74+
id: String,
75+
}));
76+
77+
const data = PushNotification.getNotificationForMessageId(params.id, Meteor.userId());
78+
79+
return API.v1.success({ data });
80+
},
81+
});

app/push-notifications/server/lib/PushNotification.js

Lines changed: 68 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,19 @@ import { Meteor } from 'meteor/meteor';
33
import { Push } from '../../../push/server';
44
import { settings } from '../../../settings/server';
55
import { metrics } from '../../../metrics/server';
6+
import { Messages, Rooms, Users } from '../../../models/server';
67
import { RocketChatAssets } from '../../../assets/server';
8+
import { replaceMentionedUsernamesWithFullNames, parseMessageTextPerUser } from '../../../lib/server/functions/notifications';
9+
import { callbacks } from '../../../callbacks/server';
10+
import { getPushData } from '../../../lib/server/functions/notifications/mobile';
711

812
export class PushNotification {
913
getNotificationId(roomId) {
1014
const serverId = settings.get('uniqueID');
1115
return this.hash(`${ serverId }|${ roomId }`); // hash
1216
}
1317

14-
hash(str) {
15-
let hash = 0;
16-
let i = str.length;
17-
18-
while (i) {
19-
hash = ((hash << 5) - hash) + str.charCodeAt(--i);
20-
hash &= hash; // Convert to 32bit integer
21-
}
22-
return hash;
23-
}
24-
25-
send({ rid, uid: userId, mid: messageId, roomName, username, message, payload, badge = 1, category }) {
18+
getNotificationConfig({ rid, uid: userId, mid: messageId, roomName, username, message, payload, badge = 1, category, idOnly = false }) {
2619
let title;
2720
if (roomName && roomName !== '') {
2821
title = `${ roomName }`;
@@ -31,7 +24,6 @@ export class PushNotification {
3124
title = `${ username }`;
3225
}
3326

34-
const idOnly = settings.get('Push_request_content_from_server');
3527
const config = {
3628
from: 'push',
3729
badge,
@@ -60,9 +52,72 @@ export class PushNotification {
6052
};
6153
}
6254

55+
return config;
56+
}
57+
58+
hash(str) {
59+
let hash = 0;
60+
let i = str.length;
61+
62+
while (i) {
63+
hash = ((hash << 5) - hash) + str.charCodeAt(--i);
64+
hash &= hash; // Convert to 32bit integer
65+
}
66+
return hash;
67+
}
68+
69+
send({ rid, uid: userId, mid: messageId, roomName, username, message, payload, badge = 1, category }) {
70+
const idOnly = settings.get('Push_request_content_from_server');
71+
const config = this.getNotificationConfig({ rid, userId, messageId, roomName, username, message, payload, badge, category, idOnly });
72+
6373
metrics.notificationsSent.inc({ notification_type: 'mobile' });
6474
return Push.send(config);
6575
}
76+
77+
getNotificationForMessageId(messageId, receiverUserId) {
78+
const message = Messages.findOneById(messageId);
79+
if (!message) {
80+
throw new Error('Message not found');
81+
}
82+
83+
const receiver = Users.findOne(receiverUserId);
84+
if (!receiver) {
85+
throw new Error('User not found');
86+
}
87+
88+
const sender = Users.findOne(message.u._id, { fields: { username: 1, name: 1 } });
89+
if (!sender) {
90+
throw new Error('Message sender not found');
91+
}
92+
93+
let notificationMessage = callbacks.run('beforeSendMessageNotifications', message.msg);
94+
if (message.mentions?.length > 0 && settings.get('UI_Use_Real_Name')) {
95+
notificationMessage = replaceMentionedUsernamesWithFullNames(message.msg, message.mentions);
96+
}
97+
notificationMessage = parseMessageTextPerUser(notificationMessage, message, receiver);
98+
99+
const room = Rooms.findOneById(message.rid);
100+
const pushData = Promise.await(getPushData({
101+
room,
102+
message,
103+
userId: receiverUserId,
104+
receiverUsername: receiver.username,
105+
senderUsername: sender.username,
106+
senderName: sender.name,
107+
notificationMessage,
108+
}));
109+
110+
return {
111+
message,
112+
notification: this.getNotificationConfig({
113+
...pushData,
114+
rid: message.rid,
115+
uid: message.u._id,
116+
mid: messageId,
117+
idOnly: false,
118+
}),
119+
};
120+
}
66121
}
67122

68123
export default new PushNotification();

0 commit comments

Comments
 (0)