Phone actions can be triggered from outside the phone via email

This commit is contained in:
J Francisco Rader committed 2021-03-30 02:08:53 -03:00
1 parent 901d7e95d3
commit 0a5a49b3d5
8 files changed
+67 -32

No files matched your search

+2 -1
View File
@@ -32,7 +32,8 @@
"allowEdtiableProfileName": true "allowEdtiableProfileName": true
}, },
"email": { "email": {
"provider": "project-error.dev" "provider": "project-error.dev",
"welcomeEmail": "You have got a new phone number registered to you"
}, },
"bank": { "bank": {
"showNotifications": true "showNotifications": true
@@ -3,10 +3,22 @@ import ReplyAllIcon from '@material-ui/icons/ReplyAll';
import ReplyIcon from '@material-ui/icons/Reply'; import ReplyIcon from '@material-ui/icons/Reply';
import { Box, Button, Card, CardActions, CardContent, Typography } from '@material-ui/core'; import { Box, Button, Card, CardActions, CardContent, Typography } from '@material-ui/core';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { IEmailMessage, EmailEvents } from '../../../../../../typings/email'; import { IEmailMessage, EmailEvents, IEmail } from '../../../../../../typings/email';
import { useEmail } from '../../hooks/useEmail'; import { useEmail } from '../../hooks/useEmail';
import { useNuiEventCallback } from '../../../../os/nui-events/hooks/useNuiEventCallback'; import { useNuiEventCallback } from '../../../../os/nui-events/hooks/useNuiEventCallback';
import { useHistory } from 'react-router'; 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 (
<Typography variant="caption" paragraph>
{text}
</Typography>
);
};
export const EmailMessage = ({ message, subject }: { message: IEmailMessage; subject: string }) => { export const EmailMessage = ({ message, subject }: { message: IEmailMessage; subject: string }) => {
const history = useHistory(); const history = useHistory();
@@ -17,17 +29,16 @@ export const EmailMessage = ({ message, subject }: { message: IEmailMessage; sub
Pick<IEmailMessage, 'phoneActions' | 'externalActions'> Pick<IEmailMessage, 'phoneActions' | 'externalActions'>
>({}); >({});
const [fetchEmailActions, { loading }] = useNuiEventCallback( const [fetchEmailActions, { loading }] = useNuiEventCallback<
'EMAIL', { messageId: number },
EmailEvents.FETCH_MESSAGE_ACTIONS, Pick<IEmailMessage, 'phoneActions' | 'externalActions'>
setActions, >('EMAIL', EmailEvents.FETCH_MESSAGE_ACTIONS, setActions);
);
useEffect(() => { useEffect(() => {
if (message.hasActions) { if (message.hasActions) {
fetchEmailActions(); fetchEmailActions({ messageId: message.id });
} }
}, [fetchEmailActions, message.hasActions]); }, [fetchEmailActions, message.hasActions, message.id]);
return ( return (
<Box px={2}> <Box px={2}>
@@ -45,13 +56,7 @@ export const EmailMessage = ({ message, subject }: { message: IEmailMessage; sub
</Card> </Card>
<Box mt={2}> <Box mt={2}>
<Card> <Card>
<CardContent> <CardContent>{message.body.split('\n').map((p) => mapParagraphs(p, t))}</CardContent>
{message.body.split('\n').map((paragraph) => (
<Typography variant="caption" paragraph>
{paragraph}
</Typography>
))}
</CardContent>
</Card> </Card>
{message.hasActions && phoneActions && ( {message.hasActions && phoneActions && (
<CardActions> <CardActions>
+2 -1
View File
@@ -32,7 +32,8 @@
"allowEdtiableProfileName": true "allowEdtiableProfileName": true
}, },
"email": { "email": {
"provider": "project-error.dev" "provider": "project-error.dev",
"welcomeEmail": "You have got a new phone number registered to you"
}, },
"bank": { "bank": {
"showNotifications": true "showNotifications": true
+1
View File
@@ -203,6 +203,7 @@
"APPS_MARKETPLACE_FORM_IMAGE": "Image or URL", "APPS_MARKETPLACE_FORM_IMAGE": "Image or URL",
"APPS_MARKETPLACE_FORM_DESCRIPTION": "Description", "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_INBOX": "Inbox",
"APPS_EMAIL_SENT": "Sent", "APPS_EMAIL_SENT": "Sent",
"APPS_EMAIL_SUBJECT": "Subject", "APPS_EMAIL_SUBJECT": "Subject",
+16 -11
View File
@@ -43,27 +43,32 @@ export async function fetchMessageActions(
}> { }> {
const queryPhoneActions = ` const queryPhoneActions = `
SELECT SELECT
npwd_emails_phone_actions.label as action_phone_label, npwd_emails_phone_actions.label,
npwd_emails_phone_actions.close_phone as action_phone_close_phone, npwd_emails_phone_actions.close_phone,
npwd_emails_phone_actions.delete_email as action_phone_delete_email, npwd_emails_phone_actions.delete_email,
npwd_emails_phone_actions.href as action_phone_href npwd_emails_phone_actions.href
FROM npwd_emails_phone_actions FROM npwd_emails_phone_actions
WHERE message_id = ?; WHERE message_id = ?
`; `;
const queryExternalActions = ` const queryExternalActions = `
SELECT SELECT
npwd_emails_external_actions.label as action_ext_label, npwd_emails_external_actions.label,
npwd_emails_external_actions.close_phone as action_ext_close_phone, npwd_emails_external_actions.close_phone,
npwd_emails_external_actions.delete_email as action_ext_delete_email, npwd_emails_external_actions.delete_email,
npwd_emails_external_actions.event_name as action_ext_event_name, npwd_emails_external_actions.event_name,
npwd_emails_external_actions.event_arg as action_ext_event_arg, npwd_emails_external_actions.event_arg
FROM npwd_emails_external_actions FROM npwd_emails_external_actions
WHERE message_id = ?; WHERE message_id = ?
`; `;
const [phoneActions] = await pool.query(queryPhoneActions, [id]); const [phoneActions] = await pool.query(queryPhoneActions, [id]);
const [externalActions] = await pool.query(queryExternalActions, [id]); const [externalActions] = await pool.query(queryExternalActions, [id]);
console.log({
phoneActions: phoneActions as UnformattedEmailPhoneAction[],
externalActions: externalActions as UnformattedEmailExternalAction[],
});
return { return {
phoneActions: phoneActions as UnformattedEmailPhoneAction[], phoneActions: phoneActions as UnformattedEmailPhoneAction[],
externalActions: externalActions as UnformattedEmailExternalAction[], externalActions: externalActions as UnformattedEmailExternalAction[],
+1 -1
View File
@@ -106,7 +106,7 @@ onNet(EmailEvents.FETCH_MESSAGE_ACTIONS, async ({ messageId }: { messageId: numb
const actions = await fetchMessageActions(messageId); const actions = await fetchMessageActions(messageId);
const phoneActions = mapUnformattedEmailPhoneActions(actions.phoneActions); const phoneActions = mapUnformattedEmailPhoneActions(actions.phoneActions);
const externalActions = mapUnformattedEmailExternalActions(actions.externalActions); const externalActions = mapUnformattedEmailExternalActions(actions.externalActions);
emitNet(EmailEvents.FETCH_MESSAGE_ACTIONS_SUCCESS, { phoneActions, externalActions }); emitNet(EmailEvents.FETCH_MESSAGE_ACTIONS_SUCCESS, _source, { phoneActions, externalActions });
} catch (e) { } catch (e) {
emailLogger.error(`Failed to fetch email message actions, ${e.message}`, { emailLogger.error(`Failed to fetch email message actions, ${e.message}`, {
source: _source, source: _source,
+5 -1
View File
@@ -135,7 +135,10 @@ export async function generatePhoneNumber(identifier: string): Promise<string> {
return phoneNumber; return phoneNumber;
} }
export async function generateEmail(identifier: string): Promise<string> { export async function generateEmail(
identifier: string,
cb?: (email: string) => void,
): Promise<string> {
const getQuery = `SELECT phone_email FROM users WHERE identifier = ?`; const getQuery = `SELECT phone_email FROM users WHERE identifier = ?`;
const [results] = await pool.query(getQuery, [identifier]); const [results] = await pool.query(getQuery, [identifier]);
const result = <any[]>results; const result = <any[]>results;
@@ -153,6 +156,7 @@ export async function generateEmail(identifier: string): Promise<string> {
const newEmail = `${userProfileNames[0]}@${emailProvider}`.trim().toLowerCase(); const newEmail = `${userProfileNames[0]}@${emailProvider}`.trim().toLowerCase();
const query = 'UPDATE users SET phone_email = ? WHERE identifier = ?'; const query = 'UPDATE users SET phone_email = ? WHERE identifier = ?';
await pool.query(query, [newEmail, identifier]); await pool.query(query, [newEmail, identifier]);
cb?.(newEmail);
mainLogger.debug(`Inserting email into Database: ${newEmail}`); mainLogger.debug(`Inserting email into Database: ${newEmail}`);
return newEmail; return newEmail;
} }
+20 -2
View File
@@ -2,6 +2,8 @@ import { mainLogger } from '../sv_logger';
import { pool } from '../db'; import { pool } from '../db';
import { generateEmail, generatePhoneNumber } from '../functions'; import { generateEmail, generatePhoneNumber } from '../functions';
import { PhoneEvents } from '../../../typings/phone'; import { PhoneEvents } from '../../../typings/phone';
import { sendEmail } from './../email/sv_email';
import config from '../../utils/config';
export const playerLogger = mainLogger.child({ export const playerLogger = mainLogger.child({
module: 'player', module: 'player',
@@ -17,7 +19,7 @@ export async function handlePlayerAdd(pSource: number) {
// Parse specifically for license identifier as its // Parse specifically for license identifier as its
// guranteed // guranteed
let playerIdentifer; let playerIdentifer: string = null;
for (const identifier of playerIdentifiers) { for (const identifier of playerIdentifiers) {
if (identifier.includes('license:')) { if (identifier.includes('license:')) {
playerIdentifer = identifier.split(':')[1]; playerIdentifer = identifier.split(':')[1];
@@ -37,7 +39,23 @@ export async function handlePlayerAdd(pSource: number) {
// Get player info to populate class instance // Get player info to populate class instance
const { firstname, lastname, phone_number } = await getPlayerInfo(playerIdentifer); 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({ const newPlayer = new Player({
identifier: playerIdentifer, identifier: playerIdentifer,