mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 22:16:40 +00:00
Create email and Reply functionality
This commit is contained in:
1 parent
15a8db1293
commit
b83a890f48
18 files changed
+229
-179
No files matched your search
@@ -1,59 +1,26 @@
|
||||
import { uuidv4 } from 'fivem-js';
|
||||
import { sendEmailEvent } from '../utils/messages';
|
||||
import { EmailEvents, EmailMessageInput, IEmailMessage } from '../../typings/email';
|
||||
|
||||
interface ICallback {
|
||||
cb: (err: any, data?: unknown) => void;
|
||||
timeout: NodeJS.Timeout;
|
||||
}
|
||||
|
||||
const _timeout = 10000;
|
||||
const _callbacks = new Map<string, ICallback>();
|
||||
import { ClUtils } from './client';
|
||||
|
||||
/**
|
||||
* Fetch Inbox
|
||||
*/
|
||||
RegisterNuiCallbackType(EmailEvents.FETCH_INBOX);
|
||||
on(`__cfx_nui:${EmailEvents.FETCH_INBOX}`, (data: any, cb: (err: any, data?: any) => void) => {
|
||||
if (_callbacks.has(EmailEvents.FETCH_INBOX)) {
|
||||
cb(
|
||||
new Error(
|
||||
`Nui Server Callback with name "${EmailEvents.FETCH_INBOX}" is already waiting for server!`,
|
||||
),
|
||||
);
|
||||
return;
|
||||
if (ClUtils.registerNuiCallback(EmailEvents.FETCH_INBOX, cb)) {
|
||||
emitNet(EmailEvents.FETCH_INBOX, data);
|
||||
}
|
||||
emitNet(EmailEvents.FETCH_INBOX, data);
|
||||
|
||||
_callbacks.set(EmailEvents.FETCH_INBOX, {
|
||||
cb,
|
||||
timeout: setTimeout(() => {
|
||||
if (_callbacks.has(EmailEvents.FETCH_INBOX)) {
|
||||
_callbacks
|
||||
.get(EmailEvents.FETCH_INBOX)
|
||||
.cb(new Error(`Nui Server Callback with name "${EmailEvents.FETCH_INBOX}" timed out`));
|
||||
_callbacks.delete(EmailEvents.FETCH_INBOX);
|
||||
}
|
||||
}, _timeout),
|
||||
});
|
||||
});
|
||||
|
||||
onNet(EmailEvents.FETCH_INBOX_SUCCESS, (response: any) => {
|
||||
if (_callbacks.has(EmailEvents.FETCH_INBOX)) {
|
||||
const callback = _callbacks.get(EmailEvents.FETCH_INBOX);
|
||||
clearTimeout(callback.timeout);
|
||||
callback.cb(null, response);
|
||||
_callbacks.delete(EmailEvents.FETCH_INBOX);
|
||||
if (ClUtils.registerNuiCallbackSuccess(EmailEvents.FETCH_INBOX, response)) {
|
||||
sendEmailEvent(EmailEvents.FETCH_INBOX_SUCCESS, response);
|
||||
}
|
||||
});
|
||||
|
||||
onNet(EmailEvents.FETCH_INBOX_ERROR, (err: any) => {
|
||||
if (_callbacks.has(EmailEvents.FETCH_INBOX)) {
|
||||
const callback = _callbacks.get(EmailEvents.FETCH_INBOX);
|
||||
clearTimeout(callback.timeout);
|
||||
callback.cb(err);
|
||||
_callbacks.delete(EmailEvents.FETCH_INBOX);
|
||||
if (ClUtils.registerNuiCallbackError(EmailEvents.FETCH_INBOX, err)) {
|
||||
sendEmailEvent(EmailEvents.FETCH_INBOX_ERROR, err);
|
||||
}
|
||||
});
|
||||
@@ -65,46 +32,20 @@ RegisterNuiCallbackType(EmailEvents.SEND_EMAIL);
|
||||
on(
|
||||
`__cfx_nui:${EmailEvents.SEND_EMAIL}`,
|
||||
(data: EmailMessageInput, cb: (err: any, data?: IEmailMessage) => void) => {
|
||||
if (_callbacks.has(EmailEvents.SEND_EMAIL)) {
|
||||
cb(
|
||||
new Error(
|
||||
`Nui Server Callback with name "${EmailEvents.SEND_EMAIL}" is already waiting for server!`,
|
||||
),
|
||||
);
|
||||
return;
|
||||
if (ClUtils.registerNuiCallback(EmailEvents.SEND_EMAIL, cb)) {
|
||||
emitNet(EmailEvents.SEND_EMAIL, data);
|
||||
}
|
||||
emitNet(EmailEvents.SEND_EMAIL, data);
|
||||
|
||||
_callbacks.set(EmailEvents.SEND_EMAIL, {
|
||||
cb,
|
||||
timeout: setTimeout(() => {
|
||||
if (_callbacks.has(EmailEvents.SEND_EMAIL)) {
|
||||
_callbacks
|
||||
.get(EmailEvents.SEND_EMAIL)
|
||||
.cb(new Error(`Nui Server Callback with name "${EmailEvents.SEND_EMAIL}" timed out`));
|
||||
_callbacks.delete(EmailEvents.SEND_EMAIL);
|
||||
}
|
||||
}, _timeout),
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
onNet(EmailEvents.SEND_EMAIL_SUCCESS, (response: any) => {
|
||||
if (_callbacks.has(EmailEvents.SEND_EMAIL)) {
|
||||
const callback = _callbacks.get(EmailEvents.SEND_EMAIL);
|
||||
clearTimeout(callback.timeout);
|
||||
callback.cb(null, response);
|
||||
_callbacks.delete(EmailEvents.SEND_EMAIL);
|
||||
if (ClUtils.registerNuiCallbackSuccess(EmailEvents.SEND_EMAIL, response)) {
|
||||
sendEmailEvent(EmailEvents.SEND_EMAIL_SUCCESS, response);
|
||||
}
|
||||
});
|
||||
|
||||
onNet(EmailEvents.SEND_EMAIL_ERROR, (err: any) => {
|
||||
if (_callbacks.has(EmailEvents.SEND_EMAIL)) {
|
||||
const callback = _callbacks.get(EmailEvents.SEND_EMAIL);
|
||||
clearTimeout(callback.timeout);
|
||||
callback.cb(err);
|
||||
_callbacks.delete(EmailEvents.SEND_EMAIL);
|
||||
if (ClUtils.registerNuiCallbackError(EmailEvents.SEND_EMAIL, err)) {
|
||||
sendEmailEvent(EmailEvents.SEND_EMAIL_ERROR, err);
|
||||
}
|
||||
});
|
||||
@@ -60,45 +60,45 @@ export default class ClientUtils {
|
||||
});
|
||||
}
|
||||
|
||||
public registerNuiServerCallback<I = any, R = any>(
|
||||
app: string,
|
||||
event: string,
|
||||
successEvent?: string,
|
||||
errorEvent?: string,
|
||||
) {
|
||||
const uid = uuidv4();
|
||||
const callbackId = `${event}:${uid}`;
|
||||
|
||||
RegisterNuiCallbackType(event);
|
||||
on(`__cfx_nui:${event}`, (data: I, cb: (err: any, data?: R) => void) => {
|
||||
if (this._callbacks.has(callbackId)) {
|
||||
cb(new Error(`Nui Server Callback with name "${event}" is already waiting for server!`));
|
||||
return;
|
||||
}
|
||||
emitNet(event, data);
|
||||
registerNuiCallback<D = unknown>(event: string, cb: (error: any, result?: D) => void): boolean {
|
||||
if (this._callbacks.has(event)) {
|
||||
cb(new Error(`Nui Server Callback with name "${event}" is already waiting for server!`));
|
||||
return false;
|
||||
}
|
||||
this._callbacks.set(event, {
|
||||
cb,
|
||||
timeout: setTimeout(() => {
|
||||
if (this._callbacks.has(event)) {
|
||||
this._callbacks
|
||||
.get(event)
|
||||
.cb(new Error(`Nui Server Callback with name "${event}" timed out`));
|
||||
this._callbacks.delete(event);
|
||||
}
|
||||
}, this._settings.promiseTimeout),
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
const _success = successEvent || `${event}Success`;
|
||||
onNet(_success, (response: R) => {
|
||||
if (this._callbacks.has(callbackId)) {
|
||||
const callback = this._callbacks.get(callbackId);
|
||||
clearTimeout(callback.timeout);
|
||||
callback.cb(null, response);
|
||||
this._callbacks.delete(callbackId);
|
||||
sendMessage(app, _success, response);
|
||||
}
|
||||
});
|
||||
registerNuiCallbackSuccess<R = unknown>(event: string, response?: R) {
|
||||
if (this._callbacks.has(event)) {
|
||||
const callback = this._callbacks.get(event);
|
||||
clearTimeout(callback.timeout);
|
||||
callback.cb(null, response);
|
||||
this._callbacks.delete(event);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
const _error = errorEvent || `${event}Error`;
|
||||
onNet(_error, (err: any) => {
|
||||
if (this._callbacks.has(callbackId)) {
|
||||
const callback = this._callbacks.get(callbackId);
|
||||
clearTimeout(callback.timeout);
|
||||
callback.cb(err);
|
||||
this._callbacks.delete(callbackId);
|
||||
sendMessage(app, _error, err);
|
||||
}
|
||||
});
|
||||
registerNuiCallbackError(event: string, error?: any) {
|
||||
if (this._callbacks.has(event)) {
|
||||
const callback = this._callbacks.get(event);
|
||||
clearTimeout(callback.timeout);
|
||||
callback.cb(error);
|
||||
this._callbacks.delete(event);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -121,15 +121,15 @@ export async function generateEmail(identifier: string): Promise<string> {
|
||||
const email = result[0]?.phone_email;
|
||||
|
||||
if (!email) {
|
||||
const emailUser = await getDefaultProfileNames(identifier);
|
||||
const userProfileNames = await getDefaultProfileNames(identifier);
|
||||
|
||||
if (!emailUser) {
|
||||
if (!userProfileNames) {
|
||||
throw new Error('Must provide a fullName or phoneNumber option to create an email');
|
||||
}
|
||||
|
||||
const emailProvider = config.email.provider || 'project-error.dev';
|
||||
|
||||
const newEmail = `${emailUser}@${emailProvider}`;
|
||||
const newEmail = `${userProfileNames[0]}@${emailProvider}`.trim().toLowerCase();
|
||||
const query = 'UPDATE users SET phone_email = ? WHERE identifier = ?';
|
||||
await pool.query(query, [newEmail, identifier]);
|
||||
mainLogger.debug(`Inserting email into Database: ${newEmail}`);
|
||||
|
||||
@@ -240,7 +240,7 @@ export class Player {
|
||||
* Set the stored phone email for a user
|
||||
**/
|
||||
public setEmail(email: string): void {
|
||||
this._email = email;
|
||||
this._email = email.trim().toLowerCase();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
import { EmailEvents, EmailMessageInput } from '../../typings/email';
|
||||
import { EmailEvents, EmailMessageInput, IEmail, IEmailMessage } from '../../typings/email';
|
||||
import { mainLogger } from './sv_logger';
|
||||
import { getIdentifierByEmail, getPlayer, getSource } from './functions';
|
||||
import { IEmail, IEmailMessage } from '../../phone/src/common/typings/email';
|
||||
import { pool } from './db';
|
||||
import config from '../utils/config';
|
||||
import { getPlayerInfo } from './players/sv_players';
|
||||
|
||||
const emailLogger = mainLogger.child({ module: 'contact' });
|
||||
const emailLogger = mainLogger.child({ module: 'email' });
|
||||
|
||||
export interface UnformattedEmailMessage {
|
||||
message_id: number;
|
||||
@@ -125,7 +122,7 @@ async function sendEmail(
|
||||
email_id_or_subject: number | string,
|
||||
parent_id?: number,
|
||||
) {
|
||||
let email_id = email_id_or_subject === 'number' ? email_id_or_subject : undefined;
|
||||
let email_id = typeof email_id_or_subject === 'number' ? email_id_or_subject : undefined;
|
||||
if (typeof email_id_or_subject === 'string') {
|
||||
const emailQuery = `INSERT INTO npwd_emails (subject) VALUES (?)`;
|
||||
const [emailResult] = await pool.query(emailQuery, [email_id_or_subject]);
|
||||
@@ -168,7 +165,7 @@ onNet(EmailEvents.FETCH_INBOX, async () => {
|
||||
emailLogger.error(`Failed to fetch email inbox, ${e.message}`, {
|
||||
source: _source,
|
||||
});
|
||||
emitNet(EmailEvents.FETCH_INBOX_ERROR, _source, e);
|
||||
emitNet(EmailEvents.SEND_EMAIL_ERROR, _source, 'APPS_EMAIL_FETCH_INBOX_ERROR');
|
||||
}
|
||||
});
|
||||
|
||||
@@ -176,37 +173,59 @@ onNet(
|
||||
EmailEvents.SEND_EMAIL,
|
||||
async ({ email_id, subject, parent_id, body, receivers }: EmailMessageInput) => {
|
||||
const _source = getSource();
|
||||
|
||||
if (!body || !receivers) {
|
||||
emitNet(EmailEvents.SEND_EMAIL_ERROR, _source, 'APPS_EMAIL_SEND_EMAIL_BAD_INPUT');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const player = getPlayer(_source);
|
||||
if (!player) {
|
||||
throw new Error('Couldnt find player');
|
||||
}
|
||||
|
||||
const parsedReceivers = receivers
|
||||
.split(',')
|
||||
.map((email: string) => email.trim().toLowerCase());
|
||||
|
||||
const mappedReceivers = await Promise.all(
|
||||
receivers
|
||||
.split(',')
|
||||
.map((s: string) => s.trim())
|
||||
.map(async (email: string) => {
|
||||
const identifier = await getIdentifierByEmail(email);
|
||||
return { email, identifier };
|
||||
}),
|
||||
parsedReceivers.map(async (email: string) => {
|
||||
const identifier = await getIdentifierByEmail(email);
|
||||
return { email, identifier };
|
||||
}),
|
||||
);
|
||||
|
||||
const email = await sendEmail(
|
||||
player.identifier,
|
||||
player.getEmail(),
|
||||
mappedReceivers,
|
||||
mappedReceivers.filter((r) => !!r.identifier),
|
||||
body,
|
||||
email_id || subject,
|
||||
parent_id || null,
|
||||
);
|
||||
|
||||
const failedReceivers = mappedReceivers.filter((r) => !r.identifier);
|
||||
|
||||
if (failedReceivers.length) {
|
||||
failedReceivers.map((r) =>
|
||||
emailLogger.debug(`Email receiver not found in DB: "${r.email}"`, {
|
||||
source: _source,
|
||||
}),
|
||||
);
|
||||
|
||||
if (failedReceivers.length === mappedReceivers.length) {
|
||||
emitNet(EmailEvents.SEND_EMAIL_ERROR, _source, 'APPS_EMAIL_SEND_EMAIL_ERROR_NOT_FOUND');
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
emitNet(EmailEvents.SEND_EMAIL_SUCCESS, _source, email);
|
||||
} catch (e) {
|
||||
emailLogger.error(`Failed to send email, ${e.message}`, {
|
||||
source: _source,
|
||||
});
|
||||
emitNet(EmailEvents.SEND_EMAIL_ERROR, _source, e);
|
||||
emitNet(EmailEvents.SEND_EMAIL_ERROR, _source, 'GENERIC_UNEXPECTED_ERROR');
|
||||
}
|
||||
},
|
||||
);
|
||||
Reference in new issue
Block a user