refactor(events): Events are now tied to a common enum typings (#167)

* chore(scripts/twitter): Refactored twitter to use event enums

* chore(scripts/marketplace): Refactored marketplace to use event enums

* chore(scripts/messages): Refactored messages to use event enums

* chore(scripts/contacts): Refactored contacts to use event enums

* chore(scripts/calls): Refactored calls to use event enums

* chore(scripts/notes): Refactored notes to use event enums

* chore(scripts/photos): Refactored photos to use event enums

* chore(scripts/bank): Refactored bank to use event enums

* chore(scripts/phone): Refactored phone to use event enums

* chore(events): Removed old events object

* refactor(phone/nui-events): Attached NUI events to respective enum

* tweak(event-enums): Added npwd prefix to all events

* tweak(phone/serve): Added craco to override CRA without eject

Also bumped react-scripts to 4.0.1, with seemingly no issues

* fix(typings/twitter): Fixed method enums for twitter
This commit is contained in:
Taso authored and GitHub committed 2021-03-22 13:37:07 -04:00
1 parent 53d5bb459d
commit 0f61088999
62 files changed
+4734 -3470

No files matched your search

+2 -2
View File
@@ -1,7 +1,7 @@
import { mainLogger } from '../sv_logger';
import { pool } from '../db';
import { generatePhoneNumber } from '../functions';
import events from '../../utils/events';
import { PhoneEvents } from '../../../typings/phone';
export const playerLogger = mainLogger.child({
module: 'player',
@@ -56,7 +56,7 @@ export async function handlePlayerAdd(pSource: number) {
playerLogger.debug(newPlayer);
// Emit to client that player is ready
emitNet(events.PLAYER_IS_READY, pSource);
emitNet(PhoneEvents.PLAYER_IS_READY, pSource);
} catch (e) {
playerLogger.error(`Failed to create a new player, ${e.message}`, {
source: pSource,
+11 -12
View File
@@ -1,8 +1,7 @@
import events from '../utils/events';
import { ESX } from './server';
import { getSource } from './functions';
import { pool } from './db';
import { Transfer, IBankCredentials } from '../../typings/bank';
import { Transfer, IBankCredentials, BankEvents } from '../../typings/bank';
import { getIdentifier } from './functions';
import { mainLogger } from './sv_logger';
@@ -40,9 +39,9 @@ async function addTransfer(identifier: string, transfer: Transfer): Promise<any>
const xTarget = ESX.GetPlayerFromId(transfer.targetID);
if (transfer.transferAmount > bankBalance) {
emitNet(events.BANK_TRANSACTION_ALERT, getSource(), false);
emitNet(BankEvents.TRANSACTION_ALERT, getSource(), false);
} else if (transfer.transferAmount < bankBalance) {
emitNet(events.BANK_TRANSACTION_ALERT, getSource(), true);
emitNet(BankEvents.TRANSACTION_ALERT, getSource(), true);
xTarget.addAccountMoney('bank', transfer.transferAmount);
xPlayer.removeAccountMoney('bank', transfer.transferAmount);
@@ -75,13 +74,13 @@ async function getTransfer(transferId: number): Promise<Transfer> {
return { ...transfer };
}
onNet(events.BANK_FETCH_TRANSACTIONS, async () => {
onNet(BankEvents.FETCH_TRANSACTIONS, async () => {
const _source = getSource();
try {
const _identifier = getIdentifier(_source);
const transfer = await fetchAllTransactions(_identifier);
emitNet(events.BANK_SEND_TRANSFERS, _source, transfer);
emitNet(BankEvents.SEND_TRANSFERS, _source, transfer);
} catch (e) {
bankLogger.error(`Failed to fetch transactions, ${e.message}`, {
source: _source,
@@ -89,7 +88,7 @@ onNet(events.BANK_FETCH_TRANSACTIONS, async () => {
}
});
onNet(events.BANK_ADD_TRANSFER, async (transfer: Transfer) => {
onNet(BankEvents.ADD_TRANSFER, async (transfer: Transfer) => {
const _source = getSource();
const xTarget = ESX.GetPlayerFromId(transfer.targetID);
try {
@@ -101,8 +100,8 @@ onNet(events.BANK_ADD_TRANSFER, async (transfer: Transfer) => {
//emitNet(events.BANK_TRANSACTION_NOTIFICATION, xTarget, transferNotify);
emitNet(events.BANK_ADD_TRANSFER_SUCCESS, _source);
emitNet(events.BANK_TRANSACTION_ALERT, _source, {
emitNet(BankEvents.ADD_TRANSFER_SUCCESS, _source);
emitNet(BankEvents.TRANSACTION_ALERT, _source, {
message: 'BANK_ALERT_TRANSFER_SUCCESS',
type: 'success',
});
@@ -110,18 +109,18 @@ onNet(events.BANK_ADD_TRANSFER, async (transfer: Transfer) => {
bankLogger.error(`Failed to add transaction, ${e.message}`, {
source: _source,
});
emitNet(events.BANK_TRANSACTION_ALERT, _source, {
emitNet(BankEvents.TRANSACTION_ALERT, _source, {
message: 'APPS_BANK_ALERT_TRANSFER_FAILURE',
type: 'error',
});
}
});
onNet(events.BANK_GET_CREDENTIALS, () => {
onNet(BankEvents.GET_CREDENTIALS, () => {
const _source = getSource();
try {
const credentials = fetchCredentials();
emitNet(events.BANK_SEND_CREDENTIALS, _source, credentials);
emitNet(BankEvents.SEND_CREDENTIALS, _source, credentials);
} catch (e) {
bankLogger.error(`Failed to fetch credentials, ${e.message}`, {
source: _source,
+17 -23
View File
@@ -1,4 +1,4 @@
import events from '../utils/events';
import { CallEvents } from '../../typings/call';
import { getIdentifierByPhoneNumber, getPlayer, getSource } from './functions';
import { getPlayerFromIdentifier } from './functions';
import { ICall } from '../../typings/call';
@@ -29,7 +29,7 @@ async function fetchCalls(phoneNumber: string): Promise<ICall[]> {
let calls: Map<string, ICall> = new Map();
onNet(events.PHONE_INITIALIZE_CALL, async (phoneNumber: string) => {
onNet(CallEvents.INITIALIZE_CALL, async (phoneNumber: string) => {
const _source = getSource();
const startCallTimeUnix = new Date().getTime() / 1000;
@@ -72,13 +72,13 @@ onNet(events.PHONE_INITIALIZE_CALL, async (phoneNumber: string) => {
// events
// client that is calling
emitNet(events.PHONE_START_CALL, _source, transmitterNumber, receiverNumber, true);
emitNet(CallEvents.START_CALL, _source, transmitterNumber, receiverNumber, true);
// client that is being called
emitNet(events.PHONE_START_CALL, xReceiver.source, transmitterNumber, receiverNumber, false);
emitNet(CallEvents.START_CALL, xReceiver.source, transmitterNumber, receiverNumber, false);
});
onNet(events.PHONE_ACCEPT_CALL, async (transmitterNumber: string) => {
onNet(CallEvents.ACCEPT_CALL, async (transmitterNumber: string) => {
const pSource = getSource();
try {
const currentCall = calls.get(transmitterNumber);
@@ -91,16 +91,10 @@ onNet(events.PHONE_ACCEPT_CALL, async (transmitterNumber: string) => {
await updateCall(currentCall, true, null);
// player who is being called
emitNet(events.PHONE_CALL_WAS_ACCEPTED, pSource, channelId, currentCall, false);
emitNet(CallEvents.WAS_ACCEPTED, pSource, channelId, currentCall, false);
// player who is calling
emitNet(
events.PHONE_CALL_WAS_ACCEPTED,
currentCall.transmitterSource,
channelId,
currentCall,
true,
);
emitNet(CallEvents.WAS_ACCEPTED, currentCall.transmitterSource, channelId, currentCall, true);
currentCall.accepted = true;
} catch (e) {
@@ -110,7 +104,7 @@ onNet(events.PHONE_ACCEPT_CALL, async (transmitterNumber: string) => {
}
});
onNet(events.PHONE_CALL_REJECTED, async (transmitterNumber: string, timestamp: number) => {
onNet(CallEvents.REJECTED, async (transmitterNumber: string, timestamp: number) => {
const pSource = getSource();
try {
const currentCall = calls.get(transmitterNumber);
@@ -120,10 +114,10 @@ onNet(events.PHONE_CALL_REJECTED, async (transmitterNumber: string, timestamp: n
await updateCall(currentCall, false, timestamp);
// player who is called and initiasted the rejection.
emitNet(events.PHONE_CALL_WAS_REJECTED, currentCall.receiverSource);
emitNet(CallEvents.WAS_REJECTED, currentCall.receiverSource);
// player who is calling and recieved the rejection.
emitNet(events.PHONE_CALL_WAS_REJECTED, currentCall.transmitterSource);
emitNet(CallEvents.WAS_REJECTED, currentCall.transmitterSource);
calls.delete(transmitterNumber);
} catch (e) {
callLogger.error(`Phone Call Rejected Event Error ${e.message}`, {
@@ -132,7 +126,7 @@ onNet(events.PHONE_CALL_REJECTED, async (transmitterNumber: string, timestamp: n
}
});
onNet(events.PHONE_END_CALL, async (transmitterNumber: string) => {
onNet(CallEvents.END_CALL, async (transmitterNumber: string) => {
const pSource = getSource();
try {
const currentTimeUnix = new Date().getTime() / 1000;
@@ -145,13 +139,13 @@ onNet(events.PHONE_END_CALL, async (transmitterNumber: string) => {
await updateCall(currentCall, false, currentTimeUnix);
// player who is being called
emitNet(events.PHONE_CALL_WAS_ENDED, currentCall.receiverSource);
emitNet(CallEvents.WAS_ENDED, currentCall.receiverSource);
// player who is calling
emitNet(events.PHONE_CALL_WAS_ENDED, currentCall.transmitterSource);
emitNet(CallEvents.WAS_ENDED, currentCall.transmitterSource);
// ends animations if call is active
if (currentCall.accepted) {
emitNet(events.PHONE_CALL_SEND_HANGUP_ANIM, currentCall.receiverSource);
emitNet(events.PHONE_CALL_SEND_HANGUP_ANIM, currentCall.transmitterSource);
emitNet(CallEvents.SEND_HANGUP_ANIM, currentCall.receiverSource);
emitNet(CallEvents.SEND_HANGUP_ANIM, currentCall.transmitterSource);
}
calls.delete(transmitterNumber);
} catch (e) {
@@ -161,12 +155,12 @@ onNet(events.PHONE_END_CALL, async (transmitterNumber: string) => {
}
});
onNet(events.PHONE_CALL_FETCH_CALLS, async () => {
onNet(CallEvents.FETCH_CALLS, async () => {
const _source = getSource();
try {
const phoneNumber = getPlayer(_source).getPhoneNumber();
const calls = await fetchCalls(phoneNumber);
emitNet(events.PHONE_CALL_SEND_HISTORY, _source, calls);
emitNet(CallEvents.SEND_HISTORY, _source, calls);
} catch (e) {
callLogger.error(`Failed to fetch calls, ${e.message}`, {
source: _source,
+15 -15
View File
@@ -1,7 +1,7 @@
import { pool } from './db';
import events from '../utils/events';
import { getIdentifier, getSource } from './functions';
import { mainLogger } from './sv_logger';
import { ContactEvents } from '../../typings/contact';
const contactsLogger = mainLogger.child({ module: 'contact' });
@@ -52,30 +52,30 @@ async function deleteContact(contact: ContactId, identifier: string): Promise<an
await pool.query(query, [contact.id, identifier]);
}
onNet(events.CONTACTS_GET_CONTACTS, async () => {
onNet(ContactEvents.GET_CONTACTS, async () => {
const _source = getSource();
try {
const _identifier = getIdentifier(_source);
const contacts = await fetchAllContacts(_identifier);
emitNet(events.CONTACTS_SEND_CONTACTS, _source, contacts);
emitNet(ContactEvents.SEND_CONTACTS, _source, contacts);
} catch (e) {
contactsLogger.error(`Failed to fetch contacts, ${e.message}`);
}
});
onNet(events.CONTACTS_ADD_CONTACT, async (number: string, display: string, avatar: string) => {
onNet(ContactEvents.ADD_CONTACT, async (number: string, display: string, avatar: string) => {
const _source = getSource();
try {
const _identifier = getIdentifier(_source);
await addContact(_identifier, number, display, avatar);
emitNet(events.CONTACTS_ADD_CONTACT_SUCCESS, _source);
emitNet(events.CONTACTS_ACTION_RESULT, _source, {
emitNet(ContactEvents.ADD_CONTACT_SUCCESS, _source);
emitNet(ContactEvents.ACTION_RESULT, _source, {
message: 'CONTACT_ADD_SUCCESS',
type: 'success',
});
} catch (e) {
emitNet(events.CONTACTS_ACTION_RESULT, _source, {
emitNet(ContactEvents.ACTION_RESULT, _source, {
message: 'CONTACT_ADD_FAILED',
type: 'error',
});
@@ -85,21 +85,21 @@ onNet(events.CONTACTS_ADD_CONTACT, async (number: string, display: string, avata
}
});
onNet(events.CONTACTS_UPDATE_CONTACT, async (contact: Contacts) => {
onNet(ContactEvents.UPDATE_CONTACT, async (contact: Contacts) => {
const _source = getSource();
try {
const _identifier = getIdentifier(_source);
await updateContact(contact, _identifier);
emitNet(events.CONTACTS_UPDATE_CONTACT_SUCCESS, _source);
emitNet(ContactEvents.UPDATE_CONTACT_SUCCESS, _source);
emitNet(events.CONTACTS_ACTION_RESULT, _source, {
emitNet(ContactEvents.ACTION_RESULT, _source, {
message: 'CONTACT_UPDATE_SUCCESS',
type: 'success',
});
} catch (e) {
const _source = (global as any).source;
emitNet(events.CONTACTS_ACTION_RESULT, _source, {
emitNet(ContactEvents.ACTION_RESULT, _source, {
message: 'CONTACT_UPDATE_FAILED',
type: 'error',
});
@@ -109,13 +109,13 @@ onNet(events.CONTACTS_UPDATE_CONTACT, async (contact: Contacts) => {
}
});
onNet(events.CONTACTS_DELETE_CONTACT, async (contact: ContactId) => {
onNet(ContactEvents.DELETE_CONTACT, async (contact: ContactId) => {
const _source = getSource();
try {
const _identifier = getIdentifier(_source);
await deleteContact(contact, _identifier);
emitNet(events.CONTACTS_DELETE_CONTACT_SUCCESS, _source);
emitNet(events.CONTACTS_ACTION_RESULT, _source, {
emitNet(ContactEvents.DELETE_CONTACT_SUCCESS, _source);
emitNet(ContactEvents.ACTION_RESULT, _source, {
message: 'CONTACT_DELETE_SUCCESS',
type: 'success',
});
@@ -123,7 +123,7 @@ onNet(events.CONTACTS_DELETE_CONTACT, async (contact: ContactId) => {
contactsLogger.error(`Failed to delete contact, ${e.message}`, {
source: _source,
});
emitNet(events.CONTACTS_ACTION_RESULT, _source, {
emitNet(ContactEvents.ACTION_RESULT, _source, {
message: 'CONTACT_DELETE_FAILED',
type: 'error',
});
+64 -80
View File
@@ -1,5 +1,9 @@
import events from '../utils/events';
import { CreateMessageGroupResult, Message, MessageGroup } from '../../typings/messages';
import {
CreateMessageGroupResult,
Message,
MessageGroup,
MessageEvents,
} from '../../typings/messages';
import { pool, withTransaction } from './db';
import { getIdentifier, getSource, getPlayerFromIdentifier } from './functions';
import { mainLogger } from './sv_logger';
@@ -404,104 +408,84 @@ async function setMessageRead(groupId: string, identifier: string) {
await pool.query(query, [groupId, identifier]);
}
onNet(events.MESSAGES_FETCH_MESSAGE_GROUPS, async () => {
onNet(MessageEvents.FETCH_MESSAGE_GROUPS, async () => {
const _source = getSource();
try {
const identifier = getIdentifier(_source);
const messageGroups = await getFormattedMessageGroups(identifier);
emitNet(events.MESSAGES_FETCH_MESSAGE_GROUPS_SUCCESS, _source, messageGroups);
emitNet(MessageEvents.FETCH_MESSAGE_GROUPS_SUCCESS, _source, messageGroups);
} catch (e) {
emitNet(events.MESSAGES_FETCH_MESSAGE_GROUPS_FAILED, _source);
emitNet(MessageEvents.FETCH_MESSAGE_GROUPS_FAILED, _source);
messageLogger.error(`Failed to fetch messages groups, ${e.message}`);
}
});
onNet(
events.MESSAGES_CREATE_MESSAGE_GROUP,
async (phoneNumbers: string[], label: string = null) => {
const _source = getSource();
try {
const _identifier = getIdentifier(_source);
const result = await createMessageGroupsFromPhoneNumbers(_identifier, phoneNumbers, label);
onNet(MessageEvents.CREATE_MESSAGE_GROUP, async (phoneNumbers: string[], label: string = null) => {
const _source = getSource();
try {
const _identifier = getIdentifier(_source);
const result = await createMessageGroupsFromPhoneNumbers(_identifier, phoneNumbers, label);
if (result.error && result.duplicate) {
return emitNet(events.MESSAGES_ACTION_RESULT, _source, {
message: 'MESSAGES_MESSAGE_GROUP_DUPLICATE',
type: 'error',
});
}
if (result.error && result.duplicate) {
return emitNet(MessageEvents.ACTION_RESULT, _source, {
message: 'MESSAGES_MESSAGE_GROUP_DUPLICATE',
type: 'error',
});
}
// if the phoneNumber is invalid
if (result.error && result.phoneNumber) {
return emitNet(events.MESSAGES_ACTION_RESULT, _source, {
message: 'MESSAGES_INVALID_PHONE_NUMBER',
type: 'error',
});
}
// if you are try to add yourself
if (result.error && result.mine) {
return emitNet(events.MESSAGES_ACTION_RESULT, _source, {
message: 'MESSAGES_MESSAGE_GROUP_CREATE_MINE',
type: 'error',
});
}
// if it just fails, welp
if (result.error) {
emitNet(events.MESSAGES_CREATE_MESSAGE_GROUP_FAILED, _source, result);
return emitNet(events.MESSAGES_ACTION_RESULT, _source, {
message: `MESSAGES_MESSAGE_GROUP_CREATE_FAILED:`,
type: 'error',
});
} else {
emitNet(events.MESSAGES_CREATE_MESSAGE_GROUP_SUCCESS, _source, result);
if (result.duplicate) {
return;
}
if (result.identifiers) {
for (const participantId of result.identifiers) {
// we don't broadcast to the source of the event.
if (participantId !== _identifier) {
const participantPlayer = getPlayerFromIdentifier(participantId);
if (!participantPlayer) {
return;
}
emitNet(events.MESSAGES_FETCH_MESSAGE_GROUPS, participantPlayer.source);
}
}
}
}
} catch (e) {
emitNet(events.MESSAGES_CREATE_MESSAGE_GROUP_FAILED, _source);
emitNet(events.MESSAGES_ACTION_RESULT, _source, {
if (result.error) {
emitNet(MessageEvents.CREATE_MESSAGE_GROUP_FAILED, _source, result);
return emitNet(MessageEvents.ACTION_RESULT, _source, {
message: `MESSAGES_MESSAGE_GROUP_CREATE_FAILED:`,
type: 'error',
});
messageLogger.error(`Failed to create message group, ${e.message}`, {
source: _source,
e,
});
}
},
);
onNet(events.MESSAGES_FETCH_MESSAGES, async (groupId: string) => {
emitNet(MessageEvents.CREATE_MESSAGE_GROUP_SUCCESS, _source, result);
if (result.duplicate) {
return;
}
if (result.identifiers) {
for (const participantId of result.identifiers) {
// we don't broadcast to the source of the event.
if (participantId !== _identifier) {
const participantPlayer = getPlayerFromIdentifier(participantId);
if (!participantPlayer) {
return;
}
emitNet(MessageEvents.FETCH_MESSAGE_GROUPS, participantPlayer.source);
}
}
}
} catch (e) {
emitNet(MessageEvents.CREATE_MESSAGE_GROUP_FAILED, _source);
emitNet(MessageEvents.ACTION_RESULT, _source, {
message: `MESSAGES_MESSAGE_GROUP_CREATE_FAILED:`,
type: 'error',
});
messageLogger.error(`Failed to create message group, ${e.message}`, {
source: _source,
e,
});
}
});
onNet(MessageEvents.FETCH_MESSAGES, async (groupId: string) => {
const _source = getSource();
try {
const _identifier = getIdentifier(_source);
const messages = await getMessages(_identifier, groupId);
emitNet(events.MESSAGES_FETCH_MESSAGES_SUCCESS, _source, messages);
emitNet(MessageEvents.FETCH_MESSAGES_SUCCESS, _source, messages);
} catch (e) {
emitNet(events.MESSAGES_FETCH_MESSAGES_FAILED, _source);
emitNet(MessageEvents.FETCH_MESSAGES_FAILED, _source);
messageLogger.error(`Failed to fetch messages, ${e.message}`, {
source: _source,
});
}
});
onNet(events.MESSAGES_SEND_MESSAGE, async (groupId: string, message: string, groupName: string) => {
onNet(MessageEvents.SEND_MESSAGE, async (groupId: string, message: string, groupName: string) => {
const _source = getSource();
try {
const _identifier = getIdentifier(_source);
@@ -509,7 +493,7 @@ onNet(events.MESSAGES_SEND_MESSAGE, async (groupId: string, message: string, gro
await createMessage(_identifier, groupId, message, userParticipants);
emitNet(events.MESSAGES_SEND_MESSAGE_SUCCESS, _source, groupId);
emitNet(MessageEvents.SEND_MESSAGE_SUCCESS, _source, groupId);
// gets the identifiers foe the participants for current groupId.
@@ -520,8 +504,8 @@ onNet(events.MESSAGES_SEND_MESSAGE, async (groupId: string, message: string, gro
if (!participantPlayer) {
return;
}
emitNet(events.MESSAGES_FETCH_MESSAGE_GROUPS, participantPlayer.source);
emitNet(events.MESSAGES_CREATE_MESSAGE_BROADCAST, participantPlayer.source, {
emitNet(MessageEvents.FETCH_MESSAGE_GROUPS, participantPlayer.source);
emitNet(MessageEvents.CREATE_MESSAGE_BROADCAST, participantPlayer.source, {
groupName,
groupId,
message,
@@ -531,11 +515,11 @@ onNet(events.MESSAGES_SEND_MESSAGE, async (groupId: string, message: string, gro
} catch (e) {
// Not really sure what this does? As I cant find any reference
// of this in the ui part.
emitNet(events.MESSAGES_SEND_MESSAGE_FAILED, _source);
emitNet(MessageEvents.SEND_MESSAGE_FAILED, _source);
// sending a new alert
// The message property is always using the locale strings without
// the APPS_ prefix.
emitNet(events.MESSAGES_ACTION_RESULT, _source, {
emitNet(MessageEvents.ACTION_RESULT, _source, {
message: 'MESSAGES_NEW_MESSAGE_FAILED',
type: 'error',
});
@@ -545,12 +529,12 @@ onNet(events.MESSAGES_SEND_MESSAGE, async (groupId: string, message: string, gro
}
});
onNet(events.MESSAGES_SET_MESSAGE_READ, async (groupId: string) => {
onNet(MessageEvents.SET_MESSAGE_READ, async (groupId: string) => {
const pSource = getSource();
try {
const identifier = getIdentifier(pSource);
await setMessageRead(groupId, identifier);
emitNet(events.MESSAGES_FETCH_MESSAGE_GROUPS, pSource);
emitNet(MessageEvents.FETCH_MESSAGE_GROUPS, pSource);
} catch (e) {
messageLogger.error(`Failed to set message as read, ${e.message}`, {
source: pSource,
+16 -17
View File
@@ -1,7 +1,6 @@
import events from '../utils/events';
import { pool } from './db';
import { getIdentifier, getSource } from './functions';
import { Note, NoteId } from '../../typings/notes';
import { Note, NoteId, NotesEvents } from '../../typings/notes';
import { mainLogger } from './sv_logger';
const notesLogger = mainLogger.child({ module: 'notes' });
@@ -27,13 +26,13 @@ async function updateNote(note: Note, identifier: string): Promise<any> {
await pool.query(query, [note.title, note.content, note.id, identifier]);
}
onNet(events.NOTE_ADD_NOTE, async (note: Note) => {
onNet(NotesEvents.ADD_NOTE, async (note: Note) => {
const pSource = getSource();
try {
const _identifier = getIdentifier(pSource);
await addNote(_identifier, note);
emitNet(events.NOTE_SEND_NOTE_SUCCESS, pSource);
emitNet(events.NOTE_ACTION_RESULT, pSource, {
emitNet(NotesEvents.SEND_NOTE_SUCCESS, pSource);
emitNet(NotesEvents.ACTION_RESULT, pSource, {
message: 'NOTES_ADD_SUCCESS',
type: 'success',
});
@@ -41,19 +40,19 @@ onNet(events.NOTE_ADD_NOTE, async (note: Note) => {
notesLogger.error(`Failed to add note ${e.message}`, {
source: pSource,
});
emitNet(events.NOTE_ACTION_RESULT, pSource, {
emitNet(NotesEvents.ACTION_RESULT, pSource, {
message: 'NOTES_ADD_FAILED',
type: 'error',
});
}
});
onNet(events.NOTE_FETCH_ALL_NOTES, async () => {
onNet(NotesEvents.FETCH_ALL_NOTES, async () => {
const pSource = getSource();
try {
const _identifier = getIdentifier(pSource);
const notes = await fetchAllNotes(_identifier);
emitNet(events.NOTE_SEND_NOTE, pSource, notes);
emitNet(NotesEvents.SEND_NOTE, pSource, notes);
} catch (e) {
notesLogger.error(`Failed to fetch notes ${e.message}`, {
source: pSource,
@@ -61,45 +60,45 @@ onNet(events.NOTE_FETCH_ALL_NOTES, async () => {
}
});
onNet(events.NOTE_DELETE_NOTE, async (noteId: NoteId) => {
onNet(NotesEvents.DELETE_NOTE, async (noteId: NoteId) => {
const pSource = getSource();
try {
const _identifier = getIdentifier(pSource);
await deleteNote(noteId.id, _identifier);
emitNet(events.NOTE_DELETE_NOTE_SUCCESS, pSource);
emitNet(NotesEvents.DELETE_NOTE_SUCCESS, pSource);
emitNet(events.NOTE_ACTION_RESULT, pSource, {
emitNet(NotesEvents.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, {
emitNet(NotesEvents.ACTION_RESULT, pSource, {
message: 'NOTES_DELETE_FAILED',
type: 'error',
});
}
});
onNet(events.NOTE_UPDATE_NOTE, async (note: Note) => {
onNet(NotesEvents.UPDATE_NOTE, async (note: Note) => {
const pSource = getSource();
try {
const _identifier = getIdentifier(pSource);
await updateNote(note, _identifier);
emitNet(events.NOTE_UPDATE_NOTE_SUCCESS, pSource);
emitNet(NotesEvents.UPDATE_NOTE_SUCCESS, pSource);
emitNet(events.NOTE_ACTION_RESULT, pSource, {
emitNet(NotesEvents.ACTION_RESULT, pSource, {
message: 'NOTES_UPDATE_SUCCESS',
type: 'success',
});
} catch (e) {
emitNet(events.NOTE_UPDATE_NOTE_FAILURE, pSource);
emitNet(NotesEvents.UPDATE_NOTE_FAILURE, pSource);
notesLogger.error(`Failed to update note, ${e.message}`, {
source: pSource,
});
emitNet(events.NOTE_ACTION_RESULT, pSource, {
emitNet(NotesEvents.ACTION_RESULT, pSource, {
message: 'NOTES_UPDATE_FAILED',
type: 'error',
});
+7 -8
View File
@@ -1,7 +1,6 @@
import events from '../utils/events';
import { pool } from './db';
import { getIdentifier, getSource } from './functions';
import { IPhoto } from '../../typings/photo';
import { IPhoto, PhotoEvents } from '../../typings/photo';
import { mainLogger } from './sv_logger';
const photoLogger = mainLogger.child({ module: 'photo' });
@@ -22,12 +21,12 @@ async function deletePhoto(photo: IPhoto, identifier: string) {
await pool.query(query, [photo.image, identifier]);
}
onNet(events.CAMERA_UPLOAD_PHOTO, async (image: string) => {
onNet(PhotoEvents.UPLOAD_PHOTO, async (image: string) => {
const _source = getSource();
try {
const identifier = getIdentifier(_source);
await uploadPhoto(identifier, image);
emitNet(events.CAMERA_UPLOAD_PHOTO_SUCCESS, _source);
emitNet(PhotoEvents.UPLOAD_PHOTO_SUCCESS, _source);
} catch (e) {
photoLogger.error(`Failed to upload photo, ${e.message}`, {
source: _source,
@@ -35,12 +34,12 @@ onNet(events.CAMERA_UPLOAD_PHOTO, async (image: string) => {
}
});
onNet(events.CAMERA_FETCH_PHOTOS, async () => {
onNet(PhotoEvents.FETCH_PHOTOS, async () => {
const _source = getSource();
try {
const identifier = getIdentifier(_source);
const photos = await getPhotosByIdentifier(identifier);
emitNet(events.CAMERA_SEND_PHOTOS, _source, photos);
emitNet(PhotoEvents.SEND_PHOTOS, _source, photos);
} catch (e) {
photoLogger.error(`Failed to fetch photos, ${e.message}`, {
source: _source,
@@ -48,12 +47,12 @@ onNet(events.CAMERA_FETCH_PHOTOS, async () => {
}
});
onNet(events.CAMERA_DELETE_PHOTO, async (photo: IPhoto) => {
onNet(PhotoEvents.DELETE_PHOTO, async (photo: IPhoto) => {
const _source = getSource();
try {
const identifier = getIdentifier(_source);
await deletePhoto(photo, identifier);
emitNet(events.CAMERA_DELETE_PHOTO_SUCCESS, _source);
emitNet(PhotoEvents.DELETE_PHOTO_SUCCESS, _source);
} catch (e) {
photoLogger.error(`Failed to fetch photos, ${e.message}`, {
source: _source,
+14 -15
View File
@@ -1,10 +1,9 @@
import events from '../utils/events';
import { getIdentifier, getPlayer, getSource } from './functions';
import { ESX } from './server';
import { pool } from './db';
import { mainLogger } from './sv_logger';
import { MarketplaceListing } from '../../typings/marketplace';
import { reportListingToDiscord } from './discord';
import { MarketplaceEvents } from '../../typings/marketplace';
const selloutLogger = mainLogger.child({ module: 'sellout' });
@@ -62,11 +61,11 @@ async function doesReportExist(listingId: number, profile: string): Promise<bool
return result.length > 0;
}
onNet(events.SELLOUT_FETCH_LISTING, async () => {
onNet(MarketplaceEvents.FETCH_LISTING, async () => {
const _source = getSource();
try {
const listings = await fetchAllListings();
emitNet(events.SELLOUT_SEND_LISTING, _source, listings);
emitNet(MarketplaceEvents.SEND_LISTING, _source, listings);
} catch (e) {
selloutLogger.error(`Failed to fetch listings, ${e.message}`, {
source: _source,
@@ -74,7 +73,7 @@ onNet(events.SELLOUT_FETCH_LISTING, async () => {
}
});
onNet(events.SELLOUT_ADD_LISTING, async (listing: MarketplaceListing) => {
onNet(MarketplaceEvents.ADD_LISTING, async (listing: MarketplaceListing) => {
const _source = getSource();
try {
const player = getPlayer(_source);
@@ -88,8 +87,8 @@ onNet(events.SELLOUT_ADD_LISTING, async (listing: MarketplaceListing) => {
listing,
);
emitNet(events.SELLOUT_ADD_LISTING_SUCCESS, _source);
emitNet(events.SELLOUT_ACTION_RESULT, _source, {
emitNet(MarketplaceEvents.ADD_LISTING_SUCCESS, _source);
emitNet(MarketplaceEvents.ACTION_RESULT, _source, {
message: 'MARKETPLACE_CREATE_LISTING_SUCCESS',
type: 'success',
});
@@ -98,14 +97,14 @@ onNet(events.SELLOUT_ADD_LISTING, async (listing: MarketplaceListing) => {
source: _source,
});
emitNet(events.SELLOUT_ACTION_RESULT, _source, {
emitNet(MarketplaceEvents.ACTION_RESULT, _source, {
message: 'MARKETPLACE_CREATE_LISTING_FAILED',
type: 'error',
});
}
});
onNet(events.SELLOUT_DELETE_LISTING, async (listingId: number) => {
onNet(MarketplaceEvents.DELETE_LISTING, async (listingId: number) => {
const pSource = getSource();
try {
const identifier = getIdentifier(pSource);
@@ -113,9 +112,9 @@ onNet(events.SELLOUT_DELETE_LISTING, async (listingId: number) => {
await deleteListing(listingId, identifier);
// fetches the listings again
emitNet(events.SELLOUT_DELETE_LISTING_SUCCESS, pSource);
emitNet(MarketplaceEvents.DELETE_LISTING_SUCCESS, pSource);
emitNet(events.SELLOUT_ACTION_RESULT, pSource, {
emitNet(MarketplaceEvents.ACTION_RESULT, pSource, {
message: 'MARKETPLACE_DELETE_LISTING_SUCCESS',
type: 'success',
});
@@ -123,14 +122,14 @@ onNet(events.SELLOUT_DELETE_LISTING, async (listingId: number) => {
selloutLogger.error(`Failed to delete listing ${e.message}`, {
source: pSource,
});
emitNet(events.SELLOUT_ACTION_RESULT, pSource, {
emitNet(MarketplaceEvents.ACTION_RESULT, pSource, {
message: 'MARKETPLACE_DELETE_LISTING_FAILED',
type: 'error',
});
}
});
onNet(events.SELLOUT_REPORT_LISTING, async (listing: MarketplaceListing) => {
onNet(MarketplaceEvents.REPORT_LISTING, async (listing: MarketplaceListing) => {
const pSource = getSource();
try {
@@ -142,7 +141,7 @@ onNet(events.SELLOUT_REPORT_LISTING, async (listing: MarketplaceListing) => {
if (reportExists) {
// send an info alert
selloutLogger.error(`This listing has already been reported`);
return emitNet(events.SELLOUT_ACTION_RESULT, pSource, {
return emitNet(MarketplaceEvents.ACTION_RESULT, pSource, {
message: 'MARKETPLACE_REPORT_LISTING_FAILED',
type: 'info',
});
@@ -151,7 +150,7 @@ onNet(events.SELLOUT_REPORT_LISTING, async (listing: MarketplaceListing) => {
await reportListing(rListing.id, rListing.name);
await reportListingToDiscord(rListing, reportingPlayer);
emitNet(events.SELLOUT_ACTION_RESULT, pSource, {
emitNet(MarketplaceEvents.ACTION_RESULT, pSource, {
message: 'MARKETPLACE_REPORT_LISTING_SUCCESS',
type: 'success',
});
+33 -35
View File
@@ -1,13 +1,12 @@
import { ResultSetHeader } from 'mysql2';
import { pool } from './db';
import { getIdentifier, getSource } from './functions';
import { NewTweet, Tweet, Profile } from '../../typings/twitter';
import events from '../utils/events';
import config from '../utils/config';
import { reportTweetToDiscord } from './discord';
import { mainLogger } from './sv_logger';
import { generateProfileName, getDefaultProfileNames } from './players/sv_players';
import { TwitterEvents } from '../../typings/twitter';
const twitterLogger = mainLogger.child({ module: 'twitter' });
@@ -310,36 +309,35 @@ async function doesRetweetExist(tweetId: number, identifier: string): Promise<bo
return counts[0].count > 0;
}
onNet(events.TWITTER_GET_OR_CREATE_PROFILE, async () => {
onNet(TwitterEvents.GET_OR_CREATE_PROFILE, async () => {
const _source = getSource();
const identifier = getIdentifier(_source);
try {
const profile = await getOrCreateProfile(identifier);
// if we got null from getOrCreateProfile it means it doesn't exist and
// we failed to create it. In this case we pass the UI some default
// profile names it can choose from
if (!profile) {
const defaultProfileNames = await getDefaultProfileNames(identifier);
emitNet(events.TWITTER_GET_OR_CREATE_PROFILE_NULL, _source, defaultProfileNames);
emitNet(TwitterEvents.GET_OR_CREATE_PROFILE_NULL, _source, defaultProfileNames);
} else {
emitNet(events.TWITTER_GET_OR_CREATE_PROFILE_SUCCESS, _source, profile);
emitNet(TwitterEvents.GET_OR_CREATE_PROFILE_SUCCESS, _source, profile);
}
} catch (e) {
twitterLogger.error(`Failed to get or create profile, ${e.message}`, {
source: _source,
});
emitNet(events.TWITTER_GET_OR_CREATE_PROFILE_FAILURE, _source);
emitNet(TwitterEvents.GET_OR_CREATE_PROFILE_FAILURE, _source);
}
});
onNet(events.TWITTER_CREATE_PROFILE, async (profile: Profile) => {
onNet(TwitterEvents.CREATE_PROFILE, async (profile: Profile) => {
const _source = getSource();
try {
const identifier = getIdentifier(_source);
await createProfile(identifier, profile.profile_name);
emitNet(events.TWITTER_CREATE_PROFILE_RESULT, _source, {
emitNet(TwitterEvents.CREATE_PROFILE_RESULT, _source, {
message: 'TWITTER_CREATE_PROFILE_SUCCESS',
type: 'success',
});
@@ -347,19 +345,19 @@ onNet(events.TWITTER_CREATE_PROFILE, async (profile: Profile) => {
twitterLogger.error(`Failed to create twitter profile: ${e.message}`, {
source: _source,
});
emitNet(events.TWITTER_CREATE_PROFILE_RESULT, _source, {
emitNet(TwitterEvents.CREATE_PROFILE_RESULT, _source, {
message: 'TWITTER_CREATE_PROFILE_FAILURE',
type: 'error',
});
}
});
onNet(events.TWITTER_UPDATE_PROFILE, async (profile: Profile) => {
onNet(TwitterEvents.UPDATE_PROFILE, async (profile: Profile) => {
const _source = getSource();
try {
const identifier = getIdentifier(_source);
await updateProfile(identifier, profile);
emitNet(events.TWITTER_UPDATE_PROFILE_RESULT, _source, {
emitNet(TwitterEvents.UPDATE_PROFILE_RESULT, _source, {
message: 'TWITTER_EDIT_PROFILE_SUCCESS',
type: 'success',
});
@@ -367,14 +365,14 @@ onNet(events.TWITTER_UPDATE_PROFILE, async (profile: Profile) => {
twitterLogger.error(`Failed to update twitter profile: ${e.message}`, {
source: _source,
});
emitNet(events.TWITTER_UPDATE_PROFILE_RESULT, _source, {
emitNet(TwitterEvents.UPDATE_PROFILE_RESULT, _source, {
message: 'TWITTER_EDIT_PROFILE_FAILURE',
type: 'error',
});
}
});
onNet(events.TWITTER_FETCH_TWEETS, async () => {
onNet(TwitterEvents.FETCH_TWEETS, async () => {
const _source = getSource();
try {
const identifier = getIdentifier(_source);
@@ -387,63 +385,63 @@ onNet(events.TWITTER_FETCH_TWEETS, async () => {
}
const tweets = await fetchAllTweets(profile.id);
emitNet(events.TWITTER_FETCH_TWEETS_SUCCESS, _source, tweets);
emitNet(TwitterEvents.FETCH_TWEETS_SUCCESS, _source, tweets);
} catch (e) {
twitterLogger.error(`Fetching tweets failed, ${e.message}`, {
source: _source,
});
emitNet(events.TWITTER_FETCH_TWEETS_FAILURE, _source);
emitNet(TwitterEvents.FETCH_TWEETS_FAILURE, _source);
}
});
onNet(events.TWITTER_FETCH_TWEETS_FILTERED, async (searchValue: string) => {
onNet(TwitterEvents.FETCH_TWEETS_FILTERED, async (searchValue: string) => {
const _source = getSource();
try {
const identifier = getIdentifier(_source);
const profile = await getProfile(identifier);
const tweets = await fetchTweetsFiltered(profile.id, searchValue);
emitNet(events.TWITTER_FETCH_TWEETS_FILTERED_SUCCESS, _source, tweets);
emitNet(TwitterEvents.FETCH_TWEETS_FILTERED_SUCCESS, _source, tweets);
} catch (e) {
twitterLogger.error(`Fetch filtered tweets failed, ${e.message}`, {
source: _source,
});
emitNet(events.TWITTER_FETCH_TWEETS_FILTERED_FAILURE, _source);
emitNet(TwitterEvents.FETCH_TWEETS_FILTERED_FAILURE, _source);
}
});
onNet(events.TWITTER_CREATE_TWEET, async (tweet: Tweet) => {
onNet(TwitterEvents.CREATE_TWEET, async (tweet: Tweet) => {
const _source = getSource();
try {
const identifier = getIdentifier(_source);
const createdTweet = await createTweet(identifier, tweet);
emitNet(events.TWITTER_CREATE_TWEET_BROADCAST, -1, createdTweet);
emitNet(TwitterEvents.CREATE_TWEET_BROADCAST, -1, createdTweet);
} catch (e) {
twitterLogger.error(`Create tweet failed, ${e.message}`, {
source: _source,
});
emitNet(events.TWITTER_CREATE_TWEET_RESULT, _source, {
emitNet(TwitterEvents.CREATE_TWEET_RESULT, _source, {
message: 'TWITTER_CREATE_FAILED',
type: 'error',
});
}
});
onNet(events.TWITTER_DELETE_TWEET, async (tweetId: number) => {
onNet(TwitterEvents.DELETE_TWEET, async (tweetId: number) => {
const _source = getSource();
try {
const identifier = getIdentifier(_source);
await deleteTweet(identifier, tweetId);
emitNet(events.TWITTER_DELETE_TWEET_SUCCESS, _source);
emitNet(TwitterEvents.DELETE_TWEET_SUCCESS, _source);
} catch (e) {
twitterLogger.error(`Delete tweet failed, ${e.message}`, {
source: _source,
});
emitNet(events.TWITTER_DELETE_TWEET_FAILURE, _source);
emitNet(TwitterEvents.DELETE_TWEET_FAILURE, _source);
}
});
onNet(events.TWITTER_TOGGLE_LIKE, async (tweetId: number) => {
onNet(TwitterEvents.TOGGLE_LIKE, async (tweetId: number) => {
const _source = getSource();
try {
const identifier = getIdentifier(_source);
@@ -454,16 +452,16 @@ onNet(events.TWITTER_TOGGLE_LIKE, async (tweetId: number) => {
} else {
await createLike(profile.id, tweetId);
}
emitNet(events.TWITTER_TOGGLE_LIKE_SUCCESS, _source);
emitNet(TwitterEvents.TOGGLE_LIKE_SUCCESS, _source);
} catch (e) {
twitterLogger.error(`Like failed, ${e.message}`, {
source: _source,
});
emitNet(events.TWITTER_TOGGLE_LIKE_FAILURE, _source);
emitNet(TwitterEvents.TOGGLE_LIKE_FAILURE, _source);
}
});
onNet(events.TWITTER_RETWEET, async (tweetId: number) => {
onNet(TwitterEvents.RETWEET, async (tweetId: number) => {
const _source = getSource();
try {
const identifier = getIdentifier(_source);
@@ -471,7 +469,7 @@ onNet(events.TWITTER_RETWEET, async (tweetId: number) => {
// alert the player that they have already retweeted
// this post (or that they are the original poster)
if (await doesRetweetExist(tweetId, identifier)) {
return emitNet(events.TWITTER_RETWEET_EXISTS, _source, {
return emitNet(TwitterEvents.RETWEET_EXISTS, _source, {
message: 'TWITTER_RETWEET_EXISTS',
type: 'error',
});
@@ -485,16 +483,16 @@ onNet(events.TWITTER_RETWEET, async (tweetId: number) => {
const profile = await getProfile(identifier);
const tweet = await getTweet(profile.id, createdTweet.id);
emitNet(events.TWITTER_CREATE_TWEET_BROADCAST, -1, tweet);
emitNet(TwitterEvents.CREATE_TWEET_BROADCAST, -1, tweet);
} catch (e) {
twitterLogger.error(`Retweet failed, ${e.message}`, {
source: _source,
});
emitNet(events.TWITTER_RETWEET_FAILURE, _source);
emitNet(TwitterEvents.RETWEET_FAILURE, _source);
}
});
onNet(events.TWITTER_REPORT, async (tweetId: number) => {
onNet(TwitterEvents.REPORT, async (tweetId: number) => {
const _source = getSource();
try {
const identifier = getIdentifier(_source);
@@ -510,9 +508,9 @@ onNet(events.TWITTER_REPORT, async (tweetId: number) => {
await createTweetReport(tweet.id, profile.id);
await reportTweetToDiscord(tweet, profile);
emitNet(events.TWITTER_REPORT_SUCCESS, _source);
emitNet(TwitterEvents.REPORT_SUCCESS, _source);
} catch (e) {
emitNet(events.TWITTER_REPORT_FAILURE, _source);
emitNet(TwitterEvents.REPORT_FAILURE, _source);
twitterLogger.error(`Twitter report failed, ${e.message}`, {
source: _source,
});