Skip to content

Commit 908cf3c

Browse files
author
Erika Perugachi
authored
Merge pull request #1144 from JulianAdams4/opened
Push notifications on opening emails
2 parents b54302c + b3ffa73 commit 908cf3c

File tree

6 files changed

+66
-20
lines changed

6 files changed

+66
-20
lines changed

email_mailbox/src/lang/en.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,6 @@
299299
"atText": "at"
300300
},
301301
"notification": {
302-
"newEmailGroup": {
303-
"prefix": "You have ",
304-
"sufix": " new emails"
305-
},
306302
"export_backup": {
307303
"success": {
308304
"title": "Backup exported successfully!",
@@ -312,6 +308,14 @@
312308
"title": "Criptext",
313309
"message": "Oops! Something went wrong to export backup"
314310
}
311+
},
312+
"newEmailGroup": {
313+
"prefix": "You have ",
314+
"sufix": " new emails"
315+
},
316+
"openEmail": {
317+
"title": "Criptext",
318+
"message": "📩 Email <subject> was opened"
315319
}
316320
},
317321
"popups": {

email_mailbox/src/lang/es.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,6 @@
299299
"atText": "a las"
300300
},
301301
"notification": {
302-
"newEmailGroup": {
303-
"prefix": "Tienes ",
304-
"sufix": " correos nuevos"
305-
},
306302
"export_backup": {
307303
"success": {
308304
"title": "Respaldo exportado exitosamente!",
@@ -312,6 +308,14 @@
312308
"title": "Criptext",
313309
"message": "Ups! Algo salió mal al crear tu archivo de respaldo"
314310
}
311+
},
312+
"newEmailGroup": {
313+
"prefix": "Tienes ",
314+
"sufix": " correos nuevos"
315+
},
316+
"openEmail": {
317+
"title": "Criptext",
318+
"message": "📩 El correo <subject> fue abierto"
315319
}
316320
},
317321
"popups": {

email_mailbox/src/utils/FetchUtils.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ export const fetchGetSingleEvent = async ({ rowId, optionalToken }) => {
143143
};
144144
return eventResponse;
145145
}
146+
if (res.status === 404) {
147+
return {};
148+
}
146149
const expiredResponse = await checkExpiredSession({
147150
response: { status: res.status },
148151
initialRequest: fetchGetSingleEvent,

email_mailbox/src/utils/StringUtils.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,11 @@ export const splitSignalIdentifier = identifier => {
8585
const recipientId = parts.join('.');
8686
return { recipientId, deviceId };
8787
};
88+
89+
export const getSubjectFromPushMessage = (message, emptySubject) => {
90+
const matched = message.match(/"(.*)"/);
91+
if (!matched) return null;
92+
const substr = matched[0];
93+
if (substr === `""`) return `(${emptySubject})`;
94+
return substr;
95+
};

email_mailbox/src/utils/const.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ export const SocketCommand = {
122122
REACTIVATED_ACCOUNT_EVENT: 601
123123
};
124124

125+
export const NOTIFICATION_ACTIONS = {
126+
ANTI_PUSH: 'anti_push',
127+
NEW_EMAIL: 'open_thread',
128+
OPEN_EMAIL: 'open_activity'
129+
};
130+
125131
export const deviceTypes = {
126132
PC: 1,
127133
IOS: 2,

email_mailbox/src/utils/electronEventInterface.js

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,14 @@ import {
6161
appDomain,
6262
EmailStatus,
6363
composerEvents,
64-
EXTERNAL_RECIPIENT_ID_SERVER
64+
EXTERNAL_RECIPIENT_ID_SERVER,
65+
NOTIFICATION_ACTIONS
6566
} from './const';
6667
import Messages from './../data/message';
6768
import { MessageType } from './../components/Message';
6869
import { AttachItemStatus } from '../components/AttachItem';
6970
import { getShowEmailPreviewStatus, getUserGuideStepStatus } from './storage';
71+
import { getSubjectFromPushMessage } from './StringUtils';
7072
import {
7173
fetchAcknowledgeEvents,
7274
fetchEvents,
@@ -84,7 +86,6 @@ const {
8486
NOTIFICATION_SERVICE_STARTED,
8587
TOKEN_UPDATED
8688
} = remote.require('@criptext/electron-push-receiver/src/constants');
87-
const pushActions = ['anti_push', 'open_thread', 'open_activity'];
8889
const senderNotificationId = '73243261136';
8990
const emitter = new EventEmitter();
9091
let totalEmailsPending = null;
@@ -1004,6 +1005,7 @@ ipcRenderer.on('socket-message', async (ev, message) => {
10041005
if (eventId === 400) {
10051006
sendLoadEventsEvent({ showNotification: true });
10061007
} else {
1008+
if (isGettingEvents) return;
10071009
await parseAndDispatchEvent(message);
10081010
}
10091011
});
@@ -1430,21 +1432,40 @@ ipcRenderer.on(TOKEN_UPDATED, async (_, token) => {
14301432
});
14311433

14321434
ipcRenderer.on(NOTIFICATION_RECEIVED, async (_, { data }) => {
1433-
if (pushActions.includes(data.action) || !data.rowId) return;
1434-
isGettingEvents = true;
14351435
try {
1436-
const eventData = await fetchGetSingleEvent({ rowId: data.rowId });
1437-
await parseAndStoreEventsBatch({
1438-
events: [eventData],
1439-
hasMoreEvents: false
1440-
});
1441-
sendNewEmailNotification();
1442-
sendLoadEventsEvent({ showNotification: true });
1436+
switch (data.action) {
1437+
case NOTIFICATION_ACTIONS.NEW_EMAIL: {
1438+
if (isGettingEvents) return;
1439+
isGettingEvents = true;
1440+
const eventData = await fetchGetSingleEvent({ rowId: data.rowId });
1441+
await parseAndDispatchEvent(eventData);
1442+
sendNewEmailNotification();
1443+
isGettingEvents = false;
1444+
sendLoadEventsEvent({ showNotification: true });
1445+
break;
1446+
}
1447+
case NOTIFICATION_ACTIONS.OPEN_EMAIL: {
1448+
const subject = getSubjectFromPushMessage(
1449+
data.body,
1450+
string.mailbox.empty_subject
1451+
);
1452+
if (subject) {
1453+
const title = string.notification.openEmail.title;
1454+
const message = string.notification.openEmail.message.replace(
1455+
'<subject>',
1456+
subject
1457+
);
1458+
showNotificationApp({ title, message });
1459+
}
1460+
break;
1461+
}
1462+
default:
1463+
break;
1464+
}
14431465
} catch (firebaseNotifErr) {
14441466
// eslint-disable-next-line no-console
14451467
console.error(`[Firebase Error]: `, firebaseNotifErr);
14461468
}
1447-
isGettingEvents = false;
14481469
});
14491470

14501471
ipcRenderer.send(START_NOTIFICATION_SERVICE, senderNotificationId);

0 commit comments

Comments
 (0)