From 0a5a49b3d5a1bd4eeb3b444edc9e298a3a5c0b37 Mon Sep 17 00:00:00 2001 From: J Francisco Rader Date: Tue, 30 Mar 2021 02:05:51 -0300 Subject: [PATCH] Phone actions can be triggered from outside the phone via email --- config.json | 3 +- .../email/components/shared/EmailMessage.tsx | 35 +++++++++++-------- phone/src/config.autogen.json | 3 +- phone/src/locale/en.json | 1 + resources/server/email/sv_email.ts | 27 ++++++++------ resources/server/email/sv_email_events.ts | 2 +- resources/server/functions.ts | 6 +++- resources/server/players/sv_players.ts | 22 ++++++++++-- 8 files changed, 67 insertions(+), 32 deletions(-) diff --git a/config.json b/config.json index 61cb6c8..932fc51 100644 --- a/config.json +++ b/config.json @@ -32,7 +32,8 @@ "allowEdtiableProfileName": true }, "email": { - "provider": "project-error.dev" + "provider": "project-error.dev", + "welcomeEmail": "You have got a new phone number registered to you" }, "bank": { "showNotifications": true diff --git a/phone/src/apps/email/components/shared/EmailMessage.tsx b/phone/src/apps/email/components/shared/EmailMessage.tsx index d063d81..c92ff00 100644 --- a/phone/src/apps/email/components/shared/EmailMessage.tsx +++ b/phone/src/apps/email/components/shared/EmailMessage.tsx @@ -3,10 +3,22 @@ import ReplyAllIcon from '@material-ui/icons/ReplyAll'; import ReplyIcon from '@material-ui/icons/Reply'; import { Box, Button, Card, CardActions, CardContent, Typography } from '@material-ui/core'; import { useTranslation } from 'react-i18next'; -import { IEmailMessage, EmailEvents } from '../../../../../../typings/email'; +import { IEmailMessage, EmailEvents, IEmail } from '../../../../../../typings/email'; import { useEmail } from '../../hooks/useEmail'; import { useNuiEventCallback } from '../../../../os/nui-events/hooks/useNuiEventCallback'; import { useHistory } from 'react-router'; +import { TFunction } from 'i18next'; + +const mapParagraphs = (paragraph: string, t: TFunction) => { + const isTranslate = paragraph.includes('--translate-'); + const [, tKey] = paragraph.split('--translate-'); + const text = isTranslate ? t(tKey) : paragraph; + return ( + + {text} + + ); +}; export const EmailMessage = ({ message, subject }: { message: IEmailMessage; subject: string }) => { const history = useHistory(); @@ -17,17 +29,16 @@ export const EmailMessage = ({ message, subject }: { message: IEmailMessage; sub Pick >({}); - const [fetchEmailActions, { loading }] = useNuiEventCallback( - 'EMAIL', - EmailEvents.FETCH_MESSAGE_ACTIONS, - setActions, - ); + const [fetchEmailActions, { loading }] = useNuiEventCallback< + { messageId: number }, + Pick + >('EMAIL', EmailEvents.FETCH_MESSAGE_ACTIONS, setActions); useEffect(() => { if (message.hasActions) { - fetchEmailActions(); + fetchEmailActions({ messageId: message.id }); } - }, [fetchEmailActions, message.hasActions]); + }, [fetchEmailActions, message.hasActions, message.id]); return ( @@ -45,13 +56,7 @@ export const EmailMessage = ({ message, subject }: { message: IEmailMessage; sub - - {message.body.split('\n').map((paragraph) => ( - - {paragraph} - - ))} - + {message.body.split('\n').map((p) => mapParagraphs(p, t))} {message.hasActions && phoneActions && ( diff --git a/phone/src/config.autogen.json b/phone/src/config.autogen.json index 61cb6c8..932fc51 100644 --- a/phone/src/config.autogen.json +++ b/phone/src/config.autogen.json @@ -32,7 +32,8 @@ "allowEdtiableProfileName": true }, "email": { - "provider": "project-error.dev" + "provider": "project-error.dev", + "welcomeEmail": "You have got a new phone number registered to you" }, "bank": { "showNotifications": true diff --git a/phone/src/locale/en.json b/phone/src/locale/en.json index 795628c..230ad56 100644 --- a/phone/src/locale/en.json +++ b/phone/src/locale/en.json @@ -203,6 +203,7 @@ "APPS_MARKETPLACE_FORM_IMAGE": "Image or URL", "APPS_MARKETPLACE_FORM_DESCRIPTION": "Description", + "APPS_EMAIL_WELCOME": "Thanks for using New Phone Who Dis, your email is listed above and your registered phone number is: ", "APPS_EMAIL_INBOX": "Inbox", "APPS_EMAIL_SENT": "Sent", "APPS_EMAIL_SUBJECT": "Subject", diff --git a/resources/server/email/sv_email.ts b/resources/server/email/sv_email.ts index b2b4ad1..ef48891 100644 --- a/resources/server/email/sv_email.ts +++ b/resources/server/email/sv_email.ts @@ -43,27 +43,32 @@ export async function fetchMessageActions( }> { const queryPhoneActions = ` SELECT - npwd_emails_phone_actions.label as action_phone_label, - npwd_emails_phone_actions.close_phone as action_phone_close_phone, - npwd_emails_phone_actions.delete_email as action_phone_delete_email, - npwd_emails_phone_actions.href as action_phone_href + npwd_emails_phone_actions.label, + npwd_emails_phone_actions.close_phone, + npwd_emails_phone_actions.delete_email, + npwd_emails_phone_actions.href FROM npwd_emails_phone_actions - WHERE message_id = ?; + WHERE message_id = ? `; const queryExternalActions = ` SELECT - npwd_emails_external_actions.label as action_ext_label, - npwd_emails_external_actions.close_phone as action_ext_close_phone, - npwd_emails_external_actions.delete_email as action_ext_delete_email, - npwd_emails_external_actions.event_name as action_ext_event_name, - npwd_emails_external_actions.event_arg as action_ext_event_arg, + npwd_emails_external_actions.label, + npwd_emails_external_actions.close_phone, + npwd_emails_external_actions.delete_email, + npwd_emails_external_actions.event_name, + npwd_emails_external_actions.event_arg FROM npwd_emails_external_actions - WHERE message_id = ?; + WHERE message_id = ? `; const [phoneActions] = await pool.query(queryPhoneActions, [id]); const [externalActions] = await pool.query(queryExternalActions, [id]); + console.log({ + phoneActions: phoneActions as UnformattedEmailPhoneAction[], + externalActions: externalActions as UnformattedEmailExternalAction[], + }); + return { phoneActions: phoneActions as UnformattedEmailPhoneAction[], externalActions: externalActions as UnformattedEmailExternalAction[], diff --git a/resources/server/email/sv_email_events.ts b/resources/server/email/sv_email_events.ts index e5485d2..a6f9025 100644 --- a/resources/server/email/sv_email_events.ts +++ b/resources/server/email/sv_email_events.ts @@ -106,7 +106,7 @@ onNet(EmailEvents.FETCH_MESSAGE_ACTIONS, async ({ messageId }: { messageId: numb const actions = await fetchMessageActions(messageId); const phoneActions = mapUnformattedEmailPhoneActions(actions.phoneActions); const externalActions = mapUnformattedEmailExternalActions(actions.externalActions); - emitNet(EmailEvents.FETCH_MESSAGE_ACTIONS_SUCCESS, { phoneActions, externalActions }); + emitNet(EmailEvents.FETCH_MESSAGE_ACTIONS_SUCCESS, _source, { phoneActions, externalActions }); } catch (e) { emailLogger.error(`Failed to fetch email message actions, ${e.message}`, { source: _source, diff --git a/resources/server/functions.ts b/resources/server/functions.ts index effd8bf..f6c43e7 100644 --- a/resources/server/functions.ts +++ b/resources/server/functions.ts @@ -135,7 +135,10 @@ export async function generatePhoneNumber(identifier: string): Promise { return phoneNumber; } -export async function generateEmail(identifier: string): Promise { +export async function generateEmail( + identifier: string, + cb?: (email: string) => void, +): Promise { const getQuery = `SELECT phone_email FROM users WHERE identifier = ?`; const [results] = await pool.query(getQuery, [identifier]); const result = results; @@ -153,6 +156,7 @@ export async function generateEmail(identifier: string): Promise { const newEmail = `${userProfileNames[0]}@${emailProvider}`.trim().toLowerCase(); const query = 'UPDATE users SET phone_email = ? WHERE identifier = ?'; await pool.query(query, [newEmail, identifier]); + cb?.(newEmail); mainLogger.debug(`Inserting email into Database: ${newEmail}`); return newEmail; } diff --git a/resources/server/players/sv_players.ts b/resources/server/players/sv_players.ts index 5f87aa4..9960f6a 100644 --- a/resources/server/players/sv_players.ts +++ b/resources/server/players/sv_players.ts @@ -2,6 +2,8 @@ import { mainLogger } from '../sv_logger'; import { pool } from '../db'; import { generateEmail, generatePhoneNumber } from '../functions'; import { PhoneEvents } from '../../../typings/phone'; +import { sendEmail } from './../email/sv_email'; +import config from '../../utils/config'; export const playerLogger = mainLogger.child({ module: 'player', @@ -17,7 +19,7 @@ export async function handlePlayerAdd(pSource: number) { // Parse specifically for license identifier as its // guranteed - let playerIdentifer; + let playerIdentifer: string = null; for (const identifier of playerIdentifiers) { if (identifier.includes('license:')) { playerIdentifer = identifier.split(':')[1]; @@ -37,7 +39,23 @@ export async function handlePlayerAdd(pSource: number) { // Get player info to populate class instance const { firstname, lastname, phone_number } = await getPlayerInfo(playerIdentifer); - const email = await generateEmail(playerIdentifer); + const email = await generateEmail(playerIdentifer, (email) => { + sendEmail( + 'npwd', + `no-reply@${config.email.provider || 'project-error.dev'}`, + [ + { + email, + identifier: playerIdentifer, + }, + ], + '--translate-APPS_EMAIL_WELCOME', + 'New Phone Who Dis', + null, + [{ href: '/settings', label: 'Go to Settings', deleteEmail: false, closePhone: false }], + [], + ); + }); const newPlayer = new Player({ identifier: playerIdentifer,