mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-03 14:38:45 +00:00
refactor(server/source-handling): Altered how we handle source
These changes are made in case source is not guranteed to remain the same Due to how JS should operate, this shouldn't be an issue but this just ensures that
This commit is contained in:
1 parent
acb8e056c3
commit
1aa6a383ab
12 files changed
+222
-140
No files matched your search
Whitespace-only changes.
@@ -16,10 +16,10 @@ export async function usePhoneNumber(identifier: string): Promise<string> {
|
||||
return phoneNumber[0].phone_number;
|
||||
}
|
||||
|
||||
export const getSource = () => parseInt(source);
|
||||
export const getSource = () => (global as any).source;
|
||||
|
||||
export async function useIdentifier(): Promise<string> {
|
||||
return ESX.GetPlayerFromId(getSource()).getIdentifier();
|
||||
export function getIdentifier(source: number): string {
|
||||
return ESX.GetPlayerFromId(source).getIdentifier();
|
||||
}
|
||||
|
||||
export async function getIdentifierByPhoneNumber(
|
||||
|
||||
@@ -14,8 +14,6 @@ import { mainLogger } from './sv_logger';
|
||||
|
||||
export let ESX: Server = null;
|
||||
|
||||
export const getSource = () => (global as any).source;
|
||||
|
||||
emit('esx:getSharedObject', (obj: Server) => (ESX = obj));
|
||||
|
||||
on('onServerResourceStart', (resource: string) => {
|
||||
|
||||
+18
-10
@@ -6,7 +6,7 @@ import {
|
||||
Transfer,
|
||||
IBankCredentials,
|
||||
} from '../../phone/src/common/typings/bank';
|
||||
import { useIdentifier } from './functions';
|
||||
import { getIdentifier } from './functions';
|
||||
import { mainLogger } from './sv_logger';
|
||||
|
||||
const bankLogger = mainLogger.child({ module: 'bank' });
|
||||
@@ -85,35 +85,43 @@ async function getTransfer(transferId: number): Promise<Transfer> {
|
||||
}
|
||||
|
||||
onNet(events.BANK_FETCH_TRANSACTIONS, async () => {
|
||||
const _source = getSource();
|
||||
try {
|
||||
const _source = (global as any).source;
|
||||
const _identifier = await useIdentifier();
|
||||
const _identifier = await getIdentifier(_source);
|
||||
const transfer = await fetchAllTransactions(_identifier);
|
||||
|
||||
emitNet(events.BANK_SEND_TRANSFERS, _source, transfer);
|
||||
} catch (e) {
|
||||
bankLogger.error(`Failed to fetch transactions, ${e.message}`);
|
||||
bankLogger.error(`Failed to fetch transactions, ${e.message}`, {
|
||||
source: _source,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
onNet(events.BANK_ADD_TRANSFER, async (transfer: Transfer) => {
|
||||
const _source = getSource();
|
||||
const xTarget = ESX.GetPlayerFromId(transfer.targetID);
|
||||
try {
|
||||
const _identifier = await useIdentifier();
|
||||
const _identifier = getIdentifier(_source);
|
||||
const transferNotify = await addTransfer(_identifier, transfer);
|
||||
emitNet(events.BANK_TRANSACTION_NOTIFICATION, xTarget, transferNotify);
|
||||
emitNet(events.BANK_ADD_TRANSFER_SUCCESS, getSource());
|
||||
emitNet(events.BANK_ADD_TRANSFER_SUCCESS, _source);
|
||||
} catch (e) {
|
||||
bankLogger.error(`Failed to add transaction, ${e.message}`);
|
||||
emitNet(events.BANK_TRANSACTION_ALERT, getSource(), false);
|
||||
bankLogger.error(`Failed to add transaction, ${e.message}`, {
|
||||
source: _source,
|
||||
});
|
||||
emitNet(events.BANK_TRANSACTION_ALERT, _source, false);
|
||||
}
|
||||
});
|
||||
|
||||
onNet(events.BANK_GET_CREDENTIALS, () => {
|
||||
const _source = getSource();
|
||||
try {
|
||||
const credentials = fetchCredentials();
|
||||
emitNet(events.BANK_SEND_CREDENTIALS, getSource(), credentials);
|
||||
emitNet(events.BANK_SEND_CREDENTIALS, _source, credentials);
|
||||
} catch (e) {
|
||||
bankLogger.error(`Failed to fetch credentials, ${e.message}`);
|
||||
bankLogger.error(`Failed to fetch credentials, ${e.message}`, {
|
||||
source: _source,
|
||||
});
|
||||
}
|
||||
});
|
||||
+17
-11
@@ -3,7 +3,8 @@ import events from '../utils/events';
|
||||
import {
|
||||
getIdentifierByPhoneNumber,
|
||||
usePhoneNumber,
|
||||
useIdentifier,
|
||||
getIdentifier,
|
||||
getSource,
|
||||
} from './functions';
|
||||
import { XPlayer } from 'esx.js/@types/server';
|
||||
import { ICall } from '../../phone/src/common/typings/call';
|
||||
@@ -66,7 +67,7 @@ let calls: Map<string, ICall> = new Map();
|
||||
onNet(
|
||||
events.PHONE_INITIALIZE_CALL,
|
||||
async (phoneNumber: string, timestamp: number) => {
|
||||
const _source = (global as any).source;
|
||||
const _source = getSource();
|
||||
|
||||
const callIdentifier = uuidv4();
|
||||
|
||||
@@ -115,9 +116,8 @@ onNet(
|
||||
);
|
||||
|
||||
onNet(events.PHONE_ACCEPT_CALL, async (transmitterNumber: string) => {
|
||||
const pSource = getSource();
|
||||
try {
|
||||
const pSource = (global as any).source;
|
||||
|
||||
const currentCall = calls.get(transmitterNumber);
|
||||
const channelId = pSource;
|
||||
|
||||
@@ -141,15 +141,17 @@ onNet(events.PHONE_ACCEPT_CALL, async (transmitterNumber: string) => {
|
||||
true
|
||||
);
|
||||
} catch (e) {
|
||||
callLogger.error('Accepting Call Error', e.message);
|
||||
callLogger.error('Accepting Call Error', e.message, {
|
||||
source: pSource,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
onNet(
|
||||
events.PHONE_CALL_REJECTED,
|
||||
async (transmitterNumber: string, timestamp: number) => {
|
||||
const pSource = getSource();
|
||||
try {
|
||||
const pSource = (global as any).source;
|
||||
const currentCall = calls.get(transmitterNumber);
|
||||
|
||||
await updateCall(currentCall, false, timestamp);
|
||||
@@ -159,7 +161,9 @@ onNet(
|
||||
// player who is calling
|
||||
emitNet(events.PHONE_CALL_WAS_REJECTED, currentCall.transmitterSource);
|
||||
} catch (e) {
|
||||
callLogger.error(`Phone Call Rejected Event Error ${e.message}`);
|
||||
callLogger.error(`Phone Call Rejected Event Error ${e.message}`, {
|
||||
source: pSource,
|
||||
});
|
||||
}
|
||||
}
|
||||
);
|
||||
@@ -167,8 +171,8 @@ onNet(
|
||||
onNet(
|
||||
events.PHONE_END_CALL,
|
||||
async (transmitterNumber: string, timestamp: number) => {
|
||||
const pSource = getSource();
|
||||
try {
|
||||
const pSource = (global as any).source;
|
||||
const currentCall = calls.get(transmitterNumber);
|
||||
|
||||
const endTime = timestamp / 1000;
|
||||
@@ -182,14 +186,16 @@ onNet(
|
||||
|
||||
calls.delete(transmitterNumber);
|
||||
} catch (e) {
|
||||
callLogger.error(`Error ending Phone Call, ${e.message}`);
|
||||
callLogger.error(`Error ending Phone Call, ${e.message}`, {
|
||||
source: pSource,
|
||||
});
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
onNet(events.PHONE_CALL_FETCH_CALLS, async () => {
|
||||
const _source = (global as any).source;
|
||||
const identifier = await useIdentifier();
|
||||
const _source = getSource();
|
||||
const identifier = await getIdentifier(_source);
|
||||
const phoneNumber = await usePhoneNumber(identifier);
|
||||
|
||||
const calls = await fetchCalls(phoneNumber);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { ESX } from './server';
|
||||
import { pool } from './db';
|
||||
import events from '../utils/events';
|
||||
import { useIdentifier, getSource } from './functions';
|
||||
import { getIdentifier, getSource } from './functions';
|
||||
import { mainLogger } from './sv_logger';
|
||||
|
||||
const contactsLogger = mainLogger.child({ module: 'contact' });
|
||||
@@ -66,10 +66,9 @@ async function deleteContact(
|
||||
}
|
||||
|
||||
onNet(events.CONTACTS_GET_CONTACTS, async () => {
|
||||
const _source = getSource();
|
||||
try {
|
||||
const _source = (global as any).source;
|
||||
const xPlayer = ESX.GetPlayerFromId(_source);
|
||||
const _identifier = xPlayer.getIdentifier();
|
||||
const _identifier = getIdentifier(_source);
|
||||
|
||||
const contacts = await fetchAllContacts(_identifier);
|
||||
emitNet(events.CONTACTS_SEND_CONTACTS, _source, contacts);
|
||||
@@ -81,23 +80,24 @@ onNet(events.CONTACTS_GET_CONTACTS, async () => {
|
||||
onNet(
|
||||
events.CONTACTS_ADD_CONTACT,
|
||||
async (number: string, display: string, avatar: string) => {
|
||||
const _source = getSource();
|
||||
try {
|
||||
const _source = (global as any).source;
|
||||
const _identifier = await useIdentifier();
|
||||
addContact(_identifier, number, display, avatar);
|
||||
const _identifier = getIdentifier(_source);
|
||||
await addContact(_identifier, number, display, avatar);
|
||||
emitNet(events.CONTACTS_ADD_CONTACT_SUCCESS, _source);
|
||||
emitNet(events.CONTACTS_ACTION_RESULT, _source, 'CONTACT_ADD_SUCCESS');
|
||||
} catch (e) {
|
||||
const _source = (global as any).source;
|
||||
emitNet(events.CONTACTS_ACTION_RESULT, _source, 'CONTACT_ADD_FAILED');
|
||||
contactsLogger.error(`Failed to add contact, ${e.message}`);
|
||||
contactsLogger.error(`Failed to add contact, ${e.message}`, {
|
||||
source: _source,
|
||||
});
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
onNet(events.CONTACTS_UPDATE_CONTACT, async (contact: Contacts) => {
|
||||
const _source = getSource();
|
||||
try {
|
||||
const _source = (global as any).source;
|
||||
const _identifier = ESX.GetPlayerFromId(_source).getIdentifier();
|
||||
// console.log('nice identifier bro', _identifier);
|
||||
await updateContact(contact, _identifier);
|
||||
@@ -111,26 +111,23 @@ onNet(events.CONTACTS_UPDATE_CONTACT, async (contact: Contacts) => {
|
||||
} catch (e) {
|
||||
const _source = (global as any).source;
|
||||
emitNet(events.CONTACTS_ACTION_RESULT, _source, 'CONTACT_UPDATE_FAILED');
|
||||
contactsLogger.error(`Failed to update contact, ${e.message}`);
|
||||
contactsLogger.error(`Failed to update contact, ${e.message}`, {
|
||||
source: _source,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
onNet(events.CONTACTS_DELETE_CONTACT, async (contact: ContactId) => {
|
||||
const _source = getSource();
|
||||
try {
|
||||
const _identifier = await useIdentifier();
|
||||
deleteContact(contact, _identifier);
|
||||
emitNet(events.CONTACTS_DELETE_CONTACT_SUCCESS, getSource());
|
||||
emitNet(
|
||||
events.CONTACTS_ACTION_RESULT,
|
||||
getSource(),
|
||||
'CONTACT_DELETE_SUCCESS'
|
||||
);
|
||||
const _identifier = await getIdentifier(_source);
|
||||
await deleteContact(contact, _identifier);
|
||||
emitNet(events.CONTACTS_DELETE_CONTACT_SUCCESS, _source);
|
||||
emitNet(events.CONTACTS_ACTION_RESULT, _source, 'CONTACT_DELETE_SUCCESS');
|
||||
} catch (e) {
|
||||
contactsLogger.error(`Failed to delete contact, ${e.message}`);
|
||||
emitNet(
|
||||
events.CONTACTS_ACTION_RESULT,
|
||||
getSource(),
|
||||
'CONTACT_DELETE_FAILED'
|
||||
);
|
||||
contactsLogger.error(`Failed to delete contact, ${e.message}`, {
|
||||
source: _source,
|
||||
});
|
||||
emitNet(events.CONTACTS_ACTION_RESULT, _source, 'CONTACT_DELETE_FAILED');
|
||||
}
|
||||
});
|
||||
@@ -1,7 +1,12 @@
|
||||
import { ESX } from './server';
|
||||
import { pool } from './db';
|
||||
import config from '../utils/config';
|
||||
import { getIdentifierByPhoneNumber, usePhoneNumber } from './functions';
|
||||
import {
|
||||
getIdentifier,
|
||||
getIdentifierByPhoneNumber,
|
||||
getSource,
|
||||
usePhoneNumber,
|
||||
} from './functions';
|
||||
import { mainLogger } from './sv_logger';
|
||||
|
||||
//db = DatabaseConfig //helper variable for use in server function
|
||||
@@ -66,13 +71,14 @@ onNet('esx:playerLoaded', async (playerId: number, xPlayer: any) => {
|
||||
});
|
||||
|
||||
onNet('phone:getCredentials', async () => {
|
||||
const _source = getSource();
|
||||
try {
|
||||
const _source = (global as any).source;
|
||||
const xPlayer = ESX.GetPlayerFromId(_source);
|
||||
const _identifier = xPlayer.getIdentifier();
|
||||
const number = await getCredentials(_identifier);
|
||||
const identifier = getIdentifier(_source);
|
||||
const number = await getCredentials(identifier);
|
||||
emitNet('phone:sendCredentials', _source, number);
|
||||
} catch (e) {
|
||||
mainLogger.error(`Failed to get a number, ${e.message}`);
|
||||
mainLogger.error(`Failed to get a number, ${e.message}`, {
|
||||
source: _source,
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -7,7 +7,7 @@ import {
|
||||
CreateMessageGroupResult,
|
||||
} from '../../phone/src/common/typings/messages';
|
||||
import { pool, withTransaction } from './db';
|
||||
import { getSource, useIdentifier } from './functions';
|
||||
import { getSource, getIdentifier } from './functions';
|
||||
import { mainLogger } from './sv_logger';
|
||||
|
||||
const messageLogger = mainLogger.child({ module: 'messages' });
|
||||
@@ -376,16 +376,17 @@ async function createMessageGroupsFromPhoneNumbers(
|
||||
}
|
||||
|
||||
onNet(events.MESSAGES_FETCH_MESSAGE_GROUPS, async () => {
|
||||
const _source = getSource();
|
||||
try {
|
||||
const _identifier = await useIdentifier();
|
||||
const messageGroups = await getFormattedMessageGroups(_identifier);
|
||||
const identifier = getIdentifier(_source);
|
||||
const messageGroups = await getFormattedMessageGroups(identifier);
|
||||
emitNet(
|
||||
events.MESSAGES_FETCH_MESSAGE_GROUPS_SUCCESS,
|
||||
getSource(),
|
||||
messageGroups
|
||||
);
|
||||
} catch (e) {
|
||||
emitNet(events.MESSAGES_FETCH_MESSAGE_GROUPS_FAILED, getSource());
|
||||
emitNet(events.MESSAGES_FETCH_MESSAGE_GROUPS_FAILED, _source);
|
||||
messageLogger.error(`Failed to fetch messages groups, ${e.message}`);
|
||||
}
|
||||
});
|
||||
@@ -393,8 +394,9 @@ onNet(events.MESSAGES_FETCH_MESSAGE_GROUPS, async () => {
|
||||
onNet(
|
||||
events.MESSAGES_CREATE_MESSAGE_GROUP,
|
||||
async (phoneNumbers: string[], label: string = null) => {
|
||||
const _source = getSource();
|
||||
try {
|
||||
const _identifier = await useIdentifier();
|
||||
const _identifier = await getIdentifier(_source);
|
||||
const result = await createMessageGroupsFromPhoneNumbers(
|
||||
_identifier,
|
||||
phoneNumbers,
|
||||
@@ -416,32 +418,40 @@ onNet(
|
||||
}
|
||||
} catch (e) {
|
||||
emitNet(events.MESSAGES_CREATE_MESSAGE_GROUP_FAILED, getSource());
|
||||
messageLogger.error(`Failed to create message group, ${e.message}`);
|
||||
messageLogger.error(`Failed to create message group, ${e.message}`, {
|
||||
source: _source,
|
||||
});
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
onNet(events.MESSAGES_FETCH_MESSAGES, async (groupId: string) => {
|
||||
const _source = getSource();
|
||||
try {
|
||||
const _identifier = await useIdentifier();
|
||||
const _identifier = await getIdentifier(_source);
|
||||
const messages = await getMessages(_identifier, groupId);
|
||||
emitNet(events.MESSAGES_FETCH_MESSAGES_SUCCESS, getSource(), messages);
|
||||
emitNet(events.MESSAGES_FETCH_MESSAGES_SUCCESS, _source, messages);
|
||||
} catch (e) {
|
||||
emitNet(events.MESSAGES_FETCH_MESSAGES_FAILED, getSource());
|
||||
messageLogger.error(`Failed to fetch messages, ${e.message}`);
|
||||
emitNet(events.MESSAGES_FETCH_MESSAGES_FAILED, _source);
|
||||
messageLogger.error(`Failed to fetch messages, ${e.message}`, {
|
||||
source: _source,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
onNet(
|
||||
events.MESSAGES_SEND_MESSAGE,
|
||||
async (groupId: string, message: string) => {
|
||||
const _source = getSource();
|
||||
try {
|
||||
const _identifier = await useIdentifier();
|
||||
const _identifier = getIdentifier(_source);
|
||||
await createMessage(_identifier, groupId, message);
|
||||
emitNet(events.MESSAGES_SEND_MESSAGE_SUCCESS, getSource(), groupId);
|
||||
emitNet(events.MESSAGES_SEND_MESSAGE_SUCCESS, _source, groupId);
|
||||
} catch (e) {
|
||||
emitNet(events.MESSAGES_SEND_MESSAGE_FAILED, getSource());
|
||||
messageLogger.error(`Failed to send message, ${e.message}`);
|
||||
emitNet(events.MESSAGES_SEND_MESSAGE_FAILED, _source);
|
||||
messageLogger.error(`Failed to send message, ${e.message}`, {
|
||||
source: _source,
|
||||
});
|
||||
}
|
||||
}
|
||||
);
|
||||
@@ -1,6 +1,6 @@
|
||||
import events from '../utils/events';
|
||||
import { pool } from './db';
|
||||
import { useIdentifier } from './functions';
|
||||
import { getIdentifier, getSource } from './functions';
|
||||
import { Note, NoteId } from '../../phone/src/common/typings/notes';
|
||||
import { mainLogger } from './sv_logger';
|
||||
|
||||
@@ -31,56 +31,79 @@ async function updateNote(note: Note, identifier: string): Promise<any> {
|
||||
}
|
||||
|
||||
onNet(events.NOTE_ADD_NOTE, async (note: Note) => {
|
||||
const pSource = getSource();
|
||||
try {
|
||||
const pSource = (global as any).source;
|
||||
const _identifier = await useIdentifier();
|
||||
const _identifier = getIdentifier(pSource);
|
||||
await addNote(_identifier, note);
|
||||
emitNet(events.NOTE_SEND_NOTE_SUCCESS, pSource);
|
||||
emitNet(events.NOTE_ACTION_RESULT, pSource, { message: 'NOTES_ADD_SUCCESS', type: 'success' })
|
||||
emitNet(events.NOTE_ACTION_RESULT, pSource, {
|
||||
message: 'NOTES_ADD_SUCCESS',
|
||||
type: 'success',
|
||||
});
|
||||
} catch (e) {
|
||||
const pSource = (global as any).source;
|
||||
notesLogger.error(`Failed to add note ${e.message}`);
|
||||
emitNet(events.NOTE_ACTION_RESULT, pSource, { message: 'NOTES_ADD_FAILED', type: 'error' })
|
||||
notesLogger.error(`Failed to add note ${e.message}`, {
|
||||
source: pSource,
|
||||
});
|
||||
emitNet(events.NOTE_ACTION_RESULT, pSource, {
|
||||
message: 'NOTES_ADD_FAILED',
|
||||
type: 'error',
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
onNet(events.NOTE_FETCH_ALL_NOTES, async () => {
|
||||
const pSource = getSource();
|
||||
try {
|
||||
const pSource = (global as any).source;
|
||||
const _identifier = await useIdentifier();
|
||||
const _identifier = await getIdentifier(pSource);
|
||||
const notes = await fetchAllNotes(_identifier);
|
||||
emitNet(events.NOTE_SEND_NOTE, pSource, notes);
|
||||
} catch (e) {
|
||||
notesLogger.error(`Failed to fetch notes ${e.message}`);
|
||||
notesLogger.error(`Failed to fetch notes ${e.message}`, {
|
||||
source: pSource,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
onNet(events.NOTE_DELETE_NOTE, async (noteId: NoteId) => {
|
||||
const pSource = getSource();
|
||||
try {
|
||||
const pSource = (global as any).source;
|
||||
const _identifier = await useIdentifier();
|
||||
const _identifier = getIdentifier(pSource);
|
||||
await deleteNote(noteId.id, _identifier);
|
||||
|
||||
emitNet(events.NOTE_ACTION_RESULT, pSource, { message: 'NOTES_DELETE_SUCCESS', type: 'success' })
|
||||
emitNet(events.NOTE_ACTION_RESULT, pSource, {
|
||||
message: 'NOTES_DELETE_SUCCESS',
|
||||
type: 'success',
|
||||
});
|
||||
} catch (e) {
|
||||
const pSource = (global as any).source;
|
||||
notesLogger.error(`Failed to delete note, ${e.message}`);
|
||||
emitNet(events.NOTE_ACTION_RESULT, pSource, { message: 'NOTES_DELETE_FAILED', type: 'error' })
|
||||
emitNet(events.NOTE_ACTION_RESULT, pSource, {
|
||||
message: 'NOTES_DELETE_FAILED',
|
||||
type: 'error',
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
onNet(events.NOTE_UPDATE_NOTE, async (note: Note) => {
|
||||
const pSource = getSource();
|
||||
try {
|
||||
const pSource = (global as any).source;
|
||||
const _identifier = await useIdentifier();
|
||||
const _identifier = await getIdentifier(pSource);
|
||||
await updateNote(note, _identifier);
|
||||
emitNet(events.NOTE_UPDATE_NOTE_SUCCESS, pSource);
|
||||
|
||||
emitNet(events.NOTE_ACTION_RESULT, pSource, { message: 'NOTES_UPDATE_SUCCESS', type: 'success' })
|
||||
|
||||
emitNet(events.NOTE_ACTION_RESULT, pSource, {
|
||||
message: 'NOTES_UPDATE_SUCCESS',
|
||||
type: 'success',
|
||||
});
|
||||
} catch (e) {
|
||||
const pSource = (global as any).source;
|
||||
emitNet(events.NOTE_UPDATE_NOTE_FAILURE, pSource);
|
||||
notesLogger.error(`Failed to update note, ${e.message}`);
|
||||
emitNet(events.NOTE_ACTION_RESULT, pSource, { message: 'NOTES_UPDATE_FAILED', type: 'error' })
|
||||
notesLogger.error(`Failed to update note, ${e.message}`, {
|
||||
source: pSource,
|
||||
});
|
||||
emitNet(events.NOTE_ACTION_RESULT, pSource, {
|
||||
message: 'NOTES_UPDATE_FAILED',
|
||||
type: 'error',
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -1,7 +1,6 @@
|
||||
import events from '../utils/events';
|
||||
import { getSource } from './server';
|
||||
import { pool } from './db';
|
||||
import { useIdentifier } from './functions';
|
||||
import { getIdentifier, getSource } from './functions';
|
||||
import { IPhoto } from '../../phone/src/common/typings/photo';
|
||||
import { mainLogger } from './sv_logger';
|
||||
|
||||
@@ -28,28 +27,34 @@ async function deletePhoto(photo: IPhoto, identifier: string) {
|
||||
}
|
||||
|
||||
onNet(events.CAMERA_UPLOAD_PHOTO, async (image: string) => {
|
||||
const _source = getSource();
|
||||
try {
|
||||
const pSource = (global as any).source;
|
||||
const identifier = await useIdentifier();
|
||||
const identifier = getIdentifier(_source);
|
||||
await uploadPhoto(identifier, image);
|
||||
emitNet(events.CAMERA_UPLOAD_PHOTO_SUCCESS, pSource);
|
||||
emitNet(events.CAMERA_UPLOAD_PHOTO_SUCCESS, _source);
|
||||
} catch (e) {
|
||||
photoLogger.error(`Failed to upload photo, ${e.message}`);
|
||||
photoLogger.error(`Failed to upload photo, ${e.message}`, {
|
||||
source: _source,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
onNet(events.CAMERA_FETCH_PHOTOS, async () => {
|
||||
const identifier = await useIdentifier();
|
||||
const _source = getSource();
|
||||
const identifier = getIdentifier(_source);
|
||||
const photos = await getPhotosByIdentifier(identifier);
|
||||
emitNet(events.CAMERA_SEND_PHOTOS, getSource(), photos);
|
||||
emitNet(events.CAMERA_SEND_PHOTOS, source, photos);
|
||||
});
|
||||
|
||||
onNet(events.CAMERA_DELETE_PHOTO, async (photo: IPhoto) => {
|
||||
const _source = getSource();
|
||||
try {
|
||||
const identifier = await useIdentifier();
|
||||
const identifier = getIdentifier(_source);
|
||||
await deletePhoto(photo, identifier);
|
||||
emitNet(events.CAMERA_DELETE_PHOTO_SUCCESS, getSource());
|
||||
emitNet(events.CAMERA_DELETE_PHOTO_SUCCESS, _source);
|
||||
} catch (e) {
|
||||
photoLogger.error(`Failed to fetch photos, ${e.message}`);
|
||||
photoLogger.error(`Failed to fetch photos, ${e.message}`, {
|
||||
source: _source,
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -42,24 +42,29 @@ async function addListing(
|
||||
}
|
||||
|
||||
onNet(events.SELLOUT_FETCH_LISTING, async () => {
|
||||
const _source = getSource();
|
||||
try {
|
||||
const _source = (global as any).source;
|
||||
const listings = await fetchAllListings();
|
||||
emitNet(events.SELLOUT_SEND_LISTING, _source, listings);
|
||||
} catch (e) {
|
||||
selloutLogger.error(`Failed to fetch listings, ${e.message}`);
|
||||
selloutLogger.error(`Failed to fetch listings, ${e.message}`, {
|
||||
source: _source,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
onNet(events.SELLOUT_ADD_LISTING, async (listing: Listing) => {
|
||||
const _source = getSource();
|
||||
try {
|
||||
const xPlayer = ESX.GetPlayerFromId(getSource());
|
||||
const xPlayer = ESX.GetPlayerFromId(_source);
|
||||
const _identifier = xPlayer.getIdentifier();
|
||||
const name = xPlayer.getName();
|
||||
|
||||
const phoneNumber = await usePhoneNumber(_identifier);
|
||||
addListing(_identifier, name, phoneNumber, listing);
|
||||
await addListing(_identifier, name, phoneNumber, listing);
|
||||
} catch (e) {
|
||||
selloutLogger.error(`Failed to add listing ${e.message}`);
|
||||
selloutLogger.error(`Failed to add listing ${e.message}`, {
|
||||
source: _source,
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -1,6 +1,6 @@
|
||||
import { pool } from './db';
|
||||
import { ESX } from './server';
|
||||
import { getSource } from './functions';
|
||||
import { getIdentifier, getSource } from './functions';
|
||||
import { Tweet, Profile } from '../../phone/src/common/typings/twitter';
|
||||
import events from '../utils/events';
|
||||
import config from '../utils/config';
|
||||
@@ -293,79 +293,98 @@ async function doesReportExist(
|
||||
}
|
||||
|
||||
onNet(events.TWITTER_GET_OR_CREATE_PROFILE, async () => {
|
||||
const _source = getSource();
|
||||
try {
|
||||
const identifier = ESX.GetPlayerFromId(getSource()).getIdentifier();
|
||||
const identifier = getIdentifier(_source);
|
||||
const profile = await getOrCreateProfile(identifier);
|
||||
emitNet(events.TWITTER_GET_OR_CREATE_PROFILE_SUCCESS, getSource(), profile);
|
||||
emitNet(events.TWITTER_GET_OR_CREATE_PROFILE_SUCCESS, _source, profile);
|
||||
} catch (e) {
|
||||
twitterLogger.error(`Failed to get or create profile, ${e.message}`);
|
||||
emitNet(events.TWITTER_GET_OR_CREATE_PROFILE_FAILURE, getSource());
|
||||
twitterLogger.error(`Failed to get or create profile, ${e.message}`, {
|
||||
source: _source,
|
||||
});
|
||||
emitNet(events.TWITTER_GET_OR_CREATE_PROFILE_FAILURE, _source);
|
||||
}
|
||||
});
|
||||
|
||||
onNet(events.TWITTER_UPDATE_PROFILE, async (profile: Profile) => {
|
||||
const _source = getSource();
|
||||
try {
|
||||
const identifier = ESX.GetPlayerFromId(getSource()).getIdentifier();
|
||||
const identifier = getIdentifier(_source);
|
||||
await updateProfile(identifier, profile);
|
||||
emitNet(events.TWITTER_UPDATE_PROFILE_RESULT, getSource(), true);
|
||||
emitNet(events.TWITTER_UPDATE_PROFILE_RESULT, _source, true);
|
||||
} catch (e) {
|
||||
twitterLogger.error(`Failed to update twitter profile: ${e.message}`);
|
||||
emitNet(events.TWITTER_UPDATE_PROFILE_RESULT, getSource(), false);
|
||||
twitterLogger.error(`Failed to update twitter profile: ${e.message}`, {
|
||||
source: _source,
|
||||
});
|
||||
emitNet(events.TWITTER_UPDATE_PROFILE_RESULT, _source, false);
|
||||
}
|
||||
});
|
||||
|
||||
onNet(events.TWITTER_FETCH_TWEETS, async () => {
|
||||
const _source = getSource();
|
||||
try {
|
||||
const identifier = ESX.GetPlayerFromId(getSource()).getIdentifier();
|
||||
const identifier = getIdentifier(_source);
|
||||
const profile = await getProfile(identifier);
|
||||
const tweets = await fetchAllTweets(profile.id);
|
||||
emitNet(events.TWITTER_FETCH_TWEETS_SUCCESS, getSource(), tweets);
|
||||
emitNet(events.TWITTER_FETCH_TWEETS_SUCCESS, _source, tweets);
|
||||
} catch (e) {
|
||||
twitterLogger.error(`Fetching tweets failed, ${e.message}`);
|
||||
emitNet(events.TWITTER_FETCH_TWEETS_FAILURE, getSource());
|
||||
twitterLogger.error(`Fetching tweets failed, ${e.message}`, {
|
||||
source: _source,
|
||||
});
|
||||
emitNet(events.TWITTER_FETCH_TWEETS_FAILURE, _source);
|
||||
}
|
||||
});
|
||||
|
||||
onNet(events.TWITTER_FETCH_TWEETS_FILTERED, async (searchValue: string) => {
|
||||
const _source = getSource();
|
||||
try {
|
||||
const identifier = ESX.GetPlayerFromId(getSource()).getIdentifier();
|
||||
const identifier = getIdentifier(_source);
|
||||
const profile = await getProfile(identifier);
|
||||
const tweets = await fetchTweetsFiltered(profile.id, searchValue);
|
||||
emitNet(events.TWITTER_FETCH_TWEETS_FILTERED_SUCCESS, getSource(), tweets);
|
||||
emitNet(events.TWITTER_FETCH_TWEETS_FILTERED_SUCCESS, _source, tweets);
|
||||
} catch (e) {
|
||||
twitterLogger.error(`Fetch filtered tweets failed, ${e.message}`);
|
||||
emitNet(events.TWITTER_FETCH_TWEETS_FILTERED_FAILURE, getSource());
|
||||
twitterLogger.error(`Fetch filtered tweets failed, ${e.message}`, {
|
||||
source: _source,
|
||||
});
|
||||
emitNet(events.TWITTER_FETCH_TWEETS_FILTERED_FAILURE, _source);
|
||||
}
|
||||
});
|
||||
|
||||
onNet(events.TWITTER_CREATE_TWEET, async (tweet: Tweet) => {
|
||||
const _source = getSource();
|
||||
try {
|
||||
const identifier = ESX.GetPlayerFromId(getSource()).getIdentifier();
|
||||
const identifier = getIdentifier(_source);
|
||||
const createdTweet = await createTweet(identifier, tweet);
|
||||
|
||||
emitNet(events.TWITTER_CREATE_TWEET_RESULT, getSource(), true);
|
||||
emitNet(events.TWITTER_CREATE_TWEET_RESULT, _source, true);
|
||||
emitNet(events.TWITTER_CREATE_TWEET_BROADCAST, -1, createdTweet);
|
||||
} catch (e) {
|
||||
twitterLogger.error(`Create tweet failed, ${e.message}`);
|
||||
emitNet(events.TWITTER_CREATE_TWEET_RESULT, getSource(), false);
|
||||
twitterLogger.error(`Create tweet failed, ${e.message}`, {
|
||||
source: _source,
|
||||
});
|
||||
emitNet(events.TWITTER_CREATE_TWEET_RESULT, _source, false);
|
||||
}
|
||||
});
|
||||
|
||||
onNet(events.TWITTER_DELETE_TWEET, async (tweetId: number) => {
|
||||
const _source = getSource();
|
||||
try {
|
||||
const identifier = ESX.GetPlayerFromId(getSource()).getIdentifier();
|
||||
const identifier = getIdentifier(_source);
|
||||
await deleteTweet(identifier, tweetId);
|
||||
|
||||
emitNet(events.TWITTER_DELETE_TWEET_SUCCESS, getSource());
|
||||
emitNet(events.TWITTER_DELETE_TWEET_SUCCESS, _source);
|
||||
} catch (e) {
|
||||
twitterLogger.error(`Delete tweet failed, ${e.message}`);
|
||||
emitNet(events.TWITTER_DELETE_TWEET_FAILURE, getSource());
|
||||
twitterLogger.error(`Delete tweet failed, ${e.message}`, {
|
||||
source: _source,
|
||||
});
|
||||
emitNet(events.TWITTER_DELETE_TWEET_FAILURE, _source);
|
||||
}
|
||||
});
|
||||
|
||||
onNet(events.TWITTER_TOGGLE_LIKE, async (tweetId: number) => {
|
||||
const _source = getSource();
|
||||
try {
|
||||
const identifier = ESX.GetPlayerFromId(getSource()).getIdentifier();
|
||||
const identifier = getIdentifier(_source);
|
||||
const profile = await getOrCreateProfile(identifier);
|
||||
const likeExists = await doesLikeExist(profile.id, tweetId);
|
||||
if (likeExists) {
|
||||
@@ -373,16 +392,19 @@ onNet(events.TWITTER_TOGGLE_LIKE, async (tweetId: number) => {
|
||||
} else {
|
||||
await createLike(profile.id, tweetId);
|
||||
}
|
||||
emitNet(events.TWITTER_TOGGLE_LIKE_SUCCESS, getSource());
|
||||
emitNet(events.TWITTER_TOGGLE_LIKE_SUCCESS, _source);
|
||||
} catch (e) {
|
||||
twitterLogger.error(`Like failed, ${e.message}`);
|
||||
emitNet(events.TWITTER_TOGGLE_LIKE_FAILURE, getSource());
|
||||
twitterLogger.error(`Like failed, ${e.message}`, {
|
||||
source: _source,
|
||||
});
|
||||
emitNet(events.TWITTER_TOGGLE_LIKE_FAILURE, _source);
|
||||
}
|
||||
});
|
||||
|
||||
onNet(events.TWITTER_REPORT, async (tweetId: number) => {
|
||||
const _source = getSource();
|
||||
try {
|
||||
const identifier = ESX.GetPlayerFromId(getSource()).getIdentifier();
|
||||
const identifier = getIdentifier(_source);
|
||||
const profile = await getProfile(identifier);
|
||||
const tweet = await getTweet(profile.id, tweetId);
|
||||
|
||||
@@ -394,9 +416,11 @@ onNet(events.TWITTER_REPORT, async (tweetId: number) => {
|
||||
await reportTweetToDiscord(tweet, profile);
|
||||
}
|
||||
|
||||
emitNet(events.TWITTER_REPORT_SUCCESS, getSource());
|
||||
emitNet(events.TWITTER_REPORT_SUCCESS, _source);
|
||||
} catch (e) {
|
||||
emitNet(events.TWITTER_REPORT_FAILURE, getSource());
|
||||
twitterLogger.error(`Twitter report failed, ${e.message}`);
|
||||
emitNet(events.TWITTER_REPORT_FAILURE, _source);
|
||||
twitterLogger.error(`Twitter report failed, ${e.message}`, {
|
||||
source: _source,
|
||||
});
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user