From 04670233bc8fce53673340c028acfcb8a67ffaad Mon Sep 17 00:00:00 2001 From: J Francisco Rader Date: Fri, 19 Mar 2021 01:32:30 -0300 Subject: [PATCH] Playing around with stuff --- phone/src/apps/email/hooks/useEmail.ts | 2 +- phone/src/apps/email/hooks/useEmailService.ts | 22 +++---- resources/client/cl_email.ts | 64 ++++++++++++++++++- resources/client/cl_utils.ts | 35 ++++------ resources/client/client.ts | 1 + resources/server/server.ts | 1 + resources/server/sv_email.ts | 5 +- resources/utils/apps.ts | 1 + resources/utils/messages.ts | 4 ++ 9 files changed, 98 insertions(+), 37 deletions(-) diff --git a/phone/src/apps/email/hooks/useEmail.ts b/phone/src/apps/email/hooks/useEmail.ts index 0e861d2..8b122c0 100644 --- a/phone/src/apps/email/hooks/useEmail.ts +++ b/phone/src/apps/email/hooks/useEmail.ts @@ -11,7 +11,7 @@ export const useEmail = () => { const [fetchInbox, { loading: inboxLoading }] = useNuiEventCallback( 'EMAIL', - 'phone:fetchEmails', + 'phone:fetchInbox', setInbox, ); const [fetchSent, { loading: sentLoading }] = useNuiEventCallback( diff --git a/phone/src/apps/email/hooks/useEmailService.ts b/phone/src/apps/email/hooks/useEmailService.ts index e9c1667..d5b668d 100644 --- a/phone/src/apps/email/hooks/useEmailService.ts +++ b/phone/src/apps/email/hooks/useEmailService.ts @@ -26,11 +26,11 @@ InjectDebugData( data: [ { id: 1, + subject: 'Hey!', messages: [ { id: 1, emailId: 1, - subject: 'Hey!', body: 'Hey did you try this email app? Looking good bruh', receivers: ['kidz@projecterror.dev'], sender: 'rocky@projecterror.org', @@ -40,11 +40,11 @@ InjectDebugData( }, { id: 2, + subject: 'What is up?', messages: [ { id: 2, emailId: 2, - subject: 'What is up?', body: 'Hey did you try this email app? Looking good bruh', receivers: ['kidz@projecterror.dev'], sender: 'rocky@projecterror.org', @@ -60,12 +60,12 @@ InjectDebugData( data: [ { id: 3, + subject: 'Try this great app! 1', messages: [ { id: 11, parentId: 12, emailId: 3, - subject: 'Try this great app! 1', body: 'Hey did you try this email app? Looking good bruh', receivers: ['kidz@projecterror.dev'], sender: 'rocky@projecterror.org', @@ -77,7 +77,6 @@ InjectDebugData( id: 12, parentId: 3, emailId: 3, - subject: 'Try this great app! 2', body: 'Hey did you try this email app? Looking good bruh', receivers: ['rocky@projecterror.org'], sender: 'kidz@projecterror.dev', @@ -89,7 +88,6 @@ InjectDebugData( id: 3, parentId: null, emailId: 3, - subject: 'Try this great app! 3', body: 'Hey did you try this email app? Looking good bruh', receivers: ['kidz@projecterror.dev'], sender: 'rocky@projecterror.org', @@ -101,11 +99,11 @@ InjectDebugData( }, { id: 4, + subject: 'Try this great app!', messages: [ { id: 4, emailId: 1, - subject: 'Try this great app!', body: 'Hey did you try this email app? Looking good bruh', receivers: ['rocky@projecterror.org'], sender: 'kidz@projecterror.dev', @@ -116,11 +114,11 @@ InjectDebugData( }, { id: 5, + subject: 'Try this great app!', messages: [ { id: 5, emailId: 5, - subject: 'Try this great app!', body: 'Hey did you try this email app? Looking good bruh', receivers: ['rocky@projecterror.org'], sender: 'kidz@projecterror.dev', @@ -131,11 +129,11 @@ InjectDebugData( }, { id: 6, + subject: 'Try this great app!', messages: [ { id: 6, emailId: 6, - subject: 'Try this great app!', body: 'Hey did you try this email app? Looking good bruh', receivers: ['rocky@projecterror.org'], sender: 'chip@projecterror.dev', @@ -146,11 +144,11 @@ InjectDebugData( }, { id: 7, + subject: 'Try this great app!', messages: [ { id: 7, emailId: 7, - subject: 'Try this great app!', body: 'Hey did you try this email app? Looking good bruh', receivers: ['rocky@projecterror.org'], sender: 'taso@projecterror.dev', @@ -161,11 +159,11 @@ InjectDebugData( }, { id: 8, + subject: 'Try this great app!', messages: [ { id: 8, emailId: 8, - subject: 'Try this great app!', body: 'Hey did you try this email app? Looking good bruh', receivers: ['rocky@projecterror.org'], sender: 'kidz@projecterror.dev', @@ -176,11 +174,11 @@ InjectDebugData( }, { id: 9, + subject: 'Try this great app!', messages: [ { id: 9, emailId: 9, - subject: 'Try this great app!', body: 'Hey did you try this email app? Looking good bruh', receivers: ['rocky@projecterror.org'], sender: 'kidz@projecterror.dev', @@ -191,11 +189,11 @@ InjectDebugData( }, { id: 10, + subject: 'Try this great app!', messages: [ { id: 10, emailId: 10, - subject: 'Try this great app!', body: 'Hey did you try this email app? Looking good bruh', receivers: ['rocky@projecterror.org'], sender: 'kidz@projecterror.dev', diff --git a/resources/client/cl_email.ts b/resources/client/cl_email.ts index fd77dc7..409f45e 100644 --- a/resources/client/cl_email.ts +++ b/resources/client/cl_email.ts @@ -1,4 +1,66 @@ import { ClUtils } from './client'; import events from '../utils/events'; +import { uuidv4 } from 'fivem-js'; +import { sendEmailEvent } from '../utils/messages'; +import apps from '../utils/apps'; -ClUtils.registerNuiServerCallback(events.EMAIL_FETCH_INBOX); +// ClUtils.registerNuiServerCallback(apps.EMAIL, events.EMAIL_FETCH_INBOX); + +/* RegisterNuiCallbackType(events.EMAIL_FETCH_INBOX); +on(`__cfx_nui:${events.EMAIL_FETCH_INBOX}`, (data: any, cb: any) => { + console.log('wtf', data); + cb([]); +}); + */ + +interface ICallback { + cb: (err: any, data?: unknown) => void; + timeout: NodeJS.Timeout; +} + +const event = events.EMAIL_FETCH_INBOX; +const _callbacks = new Map(); + +const uid = uuidv4(); +const callbackId = `${event}:${uid}`; + +RegisterNuiCallbackType(event); +on(`__cfx_nui:${event}`, (data: any, cb: (err: any, data?: any) => void) => { + if (_callbacks.has(callbackId)) { + cb(new Error(`Nui Server Callback with name "${event}" is already waiting for server!`)); + return; + } + emitNet(event, data); + + _callbacks.set(callbackId, { + cb, + timeout: setTimeout(() => { + if (_callbacks.has(callbackId)) { + _callbacks + .get(callbackId) + .cb(new Error(`Nui Server Callback with name "${event}" timed out`)); + _callbacks.delete(callbackId); + } + }, 20000), + }); +}); + +onNet(`${event}Success`, (response: any) => { + if (_callbacks.has(callbackId)) { + const callback = _callbacks.get(callbackId); + clearTimeout(callback.timeout); + callback.cb(null, response); + _callbacks.delete(callbackId); + sendEmailEvent(`${event}Success`, response); + } +}); + +onNet(`${event}Error`, (err: any) => { + if (_callbacks.has(callbackId)) { + const callback = _callbacks.get(callbackId); + clearTimeout(callback.timeout); + callback.cb(err); + _callbacks.delete(callbackId); + sendEmailEvent(`${event}Error`, err); + } +}); diff --git a/resources/client/cl_utils.ts b/resources/client/cl_utils.ts index d3b92b8..45226c6 100644 --- a/resources/client/cl_utils.ts +++ b/resources/client/cl_utils.ts @@ -1,4 +1,5 @@ import { uuidv4 } from '../utils/fivem'; +import { sendMessage } from '../utils/messages'; interface ISettings { promiseTimeout: number; @@ -59,51 +60,43 @@ export default class ClientUtils { }); } - public registerNuiServerCallback(event: string) { - RegisterNuiCallbackType(event); - + public registerNuiServerCallback( + 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); - - this._callbacks.set(callbackId, { - cb, - timeout: setTimeout(() => { - if (this._callbacks.has(callbackId)) { - this._callbacks - .get(callbackId) - .cb( - new Error( - `Nui Server Callback with name "${event}" timed out after ${this._settings.promiseTimeout}ms`, - ), - ); - this._callbacks.delete(callbackId); - } - }, this._settings.promiseTimeout), - }); }); - onNet(`${event}Success`, (response: R) => { + 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); } }); - onNet(`${event}Error`, (err: any) => { + 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); } }); } diff --git a/resources/client/client.ts b/resources/client/client.ts index c7dbed6..88b58c3 100644 --- a/resources/client/client.ts +++ b/resources/client/client.ts @@ -10,6 +10,7 @@ import './cl_photo'; import './cl_messages'; import './cl_call'; import './cl_match'; +import './cl_email'; import './functions'; export const ClUtils = new ClientUtils(); diff --git a/resources/server/server.ts b/resources/server/server.ts index fa984a5..da057e5 100644 --- a/resources/server/server.ts +++ b/resources/server/server.ts @@ -11,6 +11,7 @@ import './sv_photo'; import './sv_messages'; import './sv_match'; import './sv_call'; +import './sv_email'; import './players/sv_player_events'; import { mainLogger } from './sv_logger'; diff --git a/resources/server/sv_email.ts b/resources/server/sv_email.ts index 90546d9..7c34849 100644 --- a/resources/server/sv_email.ts +++ b/resources/server/sv_email.ts @@ -120,12 +120,13 @@ onNet(events.EMAIL_FETCH_INBOX, async () => { try { const email = 'kidz@projecterror.dev'; const messages = await fetchInbox(email); + await new Promise((res) => setTimeout(res, 6000)); const inbox = getEmailsFromMessages(messages, email); - emitNet(events.EMAIL_FETCH_INBOX_SUCCESS, inbox); + emitNet(events.EMAIL_FETCH_INBOX_SUCCESS, _source, inbox); } catch (e) { emailLogger.error(`Failed to fetch email inbox, ${e.message}`, { source: _source, }); - emitNet(events.EMAIL_FETCH_INBOX_ERROR, e); + emitNet(events.EMAIL_FETCH_INBOX_ERROR, _source, e); } }); diff --git a/resources/utils/apps.ts b/resources/utils/apps.ts index dbc326d..380d4e4 100644 --- a/resources/utils/apps.ts +++ b/resources/utils/apps.ts @@ -1,4 +1,5 @@ export default { + EMAIL: 'EMAIL', TWITTER: 'TWITTER', MATCH: 'MATCH', MESSAGES: 'MESSAGES', diff --git a/resources/utils/messages.ts b/resources/utils/messages.ts index ea0b640..f326c40 100644 --- a/resources/utils/messages.ts +++ b/resources/utils/messages.ts @@ -37,3 +37,7 @@ export function sendCameraEvent(method: string, data: any = {}) { export function sendMatchEvent(method: string, data: any = {}) { return sendMessage(apps.MATCH, method, data); } + +export function sendEmailEvent(method: string, data: any = {}) { + sendMessage(apps.EMAIL, method, data); +}