@@ -3,26 +3,19 @@ import { Meteor } from 'meteor/meteor';
33import { Push } from '../../../push/server' ;
44import { settings } from '../../../settings/server' ;
55import { metrics } from '../../../metrics/server' ;
6+ import { Messages , Rooms , Users } from '../../../models/server' ;
67import { 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
812export 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
68123export default new PushNotification ( ) ;
0 commit comments