From a9b824d02692f0af529ee70c14568b0443bc123d Mon Sep 17 00:00:00 2001 From: J Francisco Rader Date: Fri, 26 Mar 2021 19:18:00 -0300 Subject: [PATCH] WIP :D --- resources/server/functions.ts | 21 +++++++++++++++++++++ resources/server/sv_email.ts | 14 +++++++++++++- typings/email.ts | 24 +++++++++++++++++++++--- 3 files changed, 55 insertions(+), 4 deletions(-) diff --git a/resources/server/functions.ts b/resources/server/functions.ts index 3ee4270..effd8bf 100644 --- a/resources/server/functions.ts +++ b/resources/server/functions.ts @@ -43,6 +43,27 @@ export async function getIdentifierByPhoneNumber( return null; } +/** + * Return the attached email for a given identifier + * @param identifier The identifier to return email for + * @param fetch Whether or not to query the database if a given player is offline + **/ +export async function getEmailByIdentifier( + identifier: string, + fetch?: boolean, +): Promise { + const online = PlayersByIdentifier.get(identifier); + if (online) return online.getEmail(); + // Whether we fetch from database if not found in online players + if (fetch) { + const query = `SELECT phone_email FROM users WHERE identifier = ?`; + const [results] = await pool.query(query, [identifier]); + // Get identifier from results + return (results as { phone_email: string }[])[0].phone_email; + } + return null; +} + /** * Return the attached identifier for a given email * @param email The email to return identifier for diff --git a/resources/server/sv_email.ts b/resources/server/sv_email.ts index 676b293..b6deea5 100644 --- a/resources/server/sv_email.ts +++ b/resources/server/sv_email.ts @@ -1,4 +1,11 @@ -import { EmailEvents, EmailMessageInput, IEmail, IEmailMessage } from '../../typings/email'; +import { + EmailEvents, + EmailMessageInput, + GlobalEmailEvents, + IEmail, + IEmailMessage, + SendEmailToIdentifierInput, +} from '../../typings/email'; import { mainLogger } from './sv_logger'; import { getIdentifierByEmail, getPlayer, getSource } from './functions'; import { pool } from './db'; @@ -169,6 +176,11 @@ onNet(EmailEvents.FETCH_INBOX, async () => { } }); +onNet( + GlobalEmailEvents.SEND_EMAIL_TO_IDENTIFIER, + async ({ identifier, provider, externalActions, phoneActions }: SendEmailToIdentifierInput) => {}, +); + onNet( EmailEvents.SEND_EMAIL, async ({ email_id, subject, parent_id, body, receivers }: EmailMessageInput) => { diff --git a/typings/email.ts b/typings/email.ts index 0b8fd75..c733b23 100644 --- a/typings/email.ts +++ b/typings/email.ts @@ -1,10 +1,15 @@ export interface IEmailPhoneAction { - path: string; + href: string; + label: string; + deleteEmail?: boolean; + closePhone?: boolean; } export interface IEmailExternalAction { - eventName: string; - args: Array; + callbackEvent: string; + callbackArg: string | number | boolean | void; + label: string; + deleteEmail?: boolean; closePhone?: boolean; } @@ -36,6 +41,19 @@ export interface EmailMessageInput { receivers: string; } +export interface SendEmailToIdentifierInput { + identifier: string; + provider: string; + subject: string; + body: string; + phoneActions: IEmailPhoneAction[]; + externalActions: IEmailExternalAction[]; +} + +export enum GlobalEmailEvents { + SEND_EMAIL_TO_IDENTIFIER = 'npwd:sendEmailToIdentifier', +} + export enum EmailEvents { FETCH_INBOX = 'npwd:fetchEmailInbox', FETCH_INBOX_SUCCESS = 'npwd:fetchEmailInboxSuccess',