mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 22:16:40 +00:00
Merge branch 'master' into feature/camera-gallery
This commit is contained in:
53 files changed
+1540
-46
No files matched your search
@@ -0,0 +1,77 @@
|
||||
import events from '../utils/events';
|
||||
import { Message, MessageGroup } from '../../phone/src/common/interfaces/messages';
|
||||
import { sendMessageEvent } from '../utils/messages';
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Messages get message groups
|
||||
*/
|
||||
RegisterNuiCallbackType(events.MESSAGES_FETCH_MESSAGE_GROUPS);
|
||||
on(`__cfx_nui:${events.MESSAGES_FETCH_MESSAGE_GROUPS}`, (): void => {
|
||||
emitNet(events.MESSAGES_FETCH_MESSAGE_GROUPS);
|
||||
});
|
||||
|
||||
onNet(events.MESSAGES_FETCH_MESSAGE_GROUPS_SUCCESS, (messageGroups: MessageGroup[]): void => {
|
||||
sendMessageEvent(events.MESSAGES_FETCH_MESSAGE_GROUPS_SUCCESS, messageGroups);
|
||||
emitNet(events.MESSAGES_FETCH_MESSAGE_GROUPS_SUCCESS);
|
||||
});
|
||||
|
||||
onNet(events.MESSAGES_FETCH_MESSAGE_GROUPS_FAILED, () => {
|
||||
sendMessageEvent(events.MESSAGES_FETCH_MESSAGE_GROUPS_FAILED);
|
||||
});
|
||||
|
||||
// fetch message groups on client launch
|
||||
emitNet(events.MESSAGES_FETCH_MESSAGE_GROUPS);
|
||||
|
||||
|
||||
/**
|
||||
* Messages create message group
|
||||
*/
|
||||
RegisterNuiCallbackType(events.MESSAGES_CREATE_MESSAGE_GROUP);
|
||||
on(`__cfx_nui:${events.MESSAGES_CREATE_MESSAGE_GROUP}`, ({ phoneNumbers, label }: any): void => {
|
||||
emitNet(events.MESSAGES_CREATE_MESSAGE_GROUP, phoneNumbers, label);
|
||||
});
|
||||
|
||||
onNet(events.MESSAGES_CREATE_MESSAGE_GROUP_SUCCESS, (result: any): void => {
|
||||
sendMessageEvent(events.MESSAGES_CREATE_MESSAGE_GROUP_SUCCESS, result);
|
||||
emitNet(events.MESSAGES_FETCH_MESSAGE_GROUPS);
|
||||
});
|
||||
|
||||
onNet(events.MESSAGES_CREATE_MESSAGE_GROUP_FAILED, (result: any): void => {
|
||||
sendMessageEvent(events.MESSAGES_CREATE_MESSAGE_GROUP_FAILED, result);
|
||||
});
|
||||
|
||||
/**
|
||||
* Messages fetch messages
|
||||
*/
|
||||
RegisterNuiCallbackType(events.MESSAGES_FETCH_MESSAGES);
|
||||
on(`__cfx_nui:${events.MESSAGES_FETCH_MESSAGES}`, ({ groupId }: any): void => {
|
||||
emitNet(events.MESSAGES_FETCH_MESSAGES, groupId);
|
||||
});
|
||||
|
||||
onNet(events.MESSAGES_FETCH_MESSAGES_SUCCESS, (messages: Message[]): void => {
|
||||
sendMessageEvent(events.MESSAGES_FETCH_MESSAGES_SUCCESS, messages);
|
||||
});
|
||||
|
||||
onNet(events.MESSAGES_FETCH_MESSAGES_FAILED, (): void => {
|
||||
sendMessageEvent(events.MESSAGES_FETCH_MESSAGES_FAILED);
|
||||
});
|
||||
|
||||
|
||||
/**
|
||||
* Messages send message
|
||||
*/
|
||||
RegisterNuiCallbackType(events.MESSAGES_SEND_MESSAGE);
|
||||
on(`__cfx_nui:${events.MESSAGES_SEND_MESSAGE}`, (data: any) => {
|
||||
emitNet(events.MESSAGES_SEND_MESSAGE, data.groupId, data.message);
|
||||
});
|
||||
|
||||
onNet(events.MESSAGES_SEND_MESSAGE_SUCCESS, (groupId: string) => {
|
||||
sendMessageEvent(events.MESSAGES_SEND_MESSAGE_SUCCESS);
|
||||
emitNet(events.MESSAGES_FETCH_MESSAGES, groupId);
|
||||
});
|
||||
|
||||
onNet(events.MESSAGES_SEND_MESSAGE_FAILED, () => {
|
||||
sendMessageEvent(events.MESSAGES_SEND_MESSAGE_FAILED);
|
||||
});
|
||||
@@ -6,6 +6,7 @@ import './cl_sellout';
|
||||
import './cl_bank';
|
||||
import './cl_notes';
|
||||
import './cl_photo';
|
||||
import './cl_messages';
|
||||
|
||||
export let ESX: ESXClient = null;
|
||||
|
||||
|
||||
+53
-7
@@ -1,10 +1,13 @@
|
||||
-- comment this out if your server has a different table/location for phone_number
|
||||
-- or if it already exists in the users table
|
||||
ALTER TABLE `users` ADD COLUMN `phone_number` VARCHAR(20) NULL DEFAULT NULL;
|
||||
INSERT INTO `items` (`name`, `label`, `weight`, `rare`, `can_remove`) VALUES ('phone', 'Phone', 1, 0, 1);
|
||||
INSERT INTO `items` (`name`, `label`, `weight`, `rare`, `can_remove`) VALUES ('dphone', 'Destroyed Phone', 1, 0, 1);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS npwd_phone_contacts (
|
||||
id int(11) NOT NULL AUTO_INCREMENT,
|
||||
identifier varchar(60) DEFAULT NULL,
|
||||
avatar varchar(512) DEFAULT NULL,
|
||||
number varchar(10) DEFAULT NULL,
|
||||
display varchar(60) NOT NULL DEFAULT '',
|
||||
PRIMARY KEY (id)
|
||||
@@ -13,10 +16,7 @@ ENGINE = INNODB,
|
||||
AUTO_INCREMENT = 6,
|
||||
AVG_ROW_LENGTH = 4096,
|
||||
CHARACTER SET utf8mb4,
|
||||
COLLATE utf8mb4_general_ci;
|
||||
|
||||
INSERT INTO `items` (`name`, `label`, `weight`, `rare`, `can_remove`) VALUES ('phone', 'Phone', 1, 0, 1);
|
||||
INSERT INTO `items` (`name`, `label`, `weight`, `rare`, `can_remove`) VALUES ('dphone', 'Destroyed Phone', 1, 0, 1);
|
||||
COLLATE utf8mb4_0900_ai_ci;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `npwd_twitter_tweets` (
|
||||
`id` int NOT NULL AUTO_INCREMENT,
|
||||
@@ -28,7 +28,7 @@ CREATE TABLE IF NOT EXISTS `npwd_twitter_tweets` (
|
||||
`visible` tinyint NOT NULL DEFAULT '1',
|
||||
`images` varchar(1000) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT '',
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=190 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=190 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `npwd_twitter_profiles` (
|
||||
@@ -43,7 +43,7 @@ CREATE TABLE IF NOT EXISTS `npwd_twitter_profiles` (
|
||||
`updatedAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `profile_name_UNIQUE` (`profile_name`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `npwd_twitter_likes` (
|
||||
`id` int NOT NULL AUTO_INCREMENT,
|
||||
@@ -71,7 +71,7 @@ AVG_ROW_LENGTH = 4096,
|
||||
CHARACTER SET utf8mb4,
|
||||
COLLATE utf8mb4_general_ci;
|
||||
|
||||
CREATE TABLE npwd_sellout_listings (
|
||||
CREATE TABLE IF NOT EXISTS npwd_sellout_listings (
|
||||
id int(11) NOT NULL AUTO_INCREMENT,
|
||||
identifier varchar(255) DEFAULT NULL,
|
||||
name varchar(50) DEFAULT NULL,
|
||||
@@ -98,3 +98,49 @@ CREATE TABLE IF NOT EXISTS `npwd_twitter_reports` (
|
||||
CONSTRAINT `report_profile` FOREIGN KEY (`profile_id`) REFERENCES `npwd_twitter_profiles` (`id`),
|
||||
CONSTRAINT `report_tweet` FOREIGN KEY (`tweet_id`) REFERENCES `npwd_twitter_tweets` (`id`) ON DELETE CASCADE
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=45 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `npwd_messages_groups` (
|
||||
`id` int NOT NULL AUTO_INCREMENT,
|
||||
`user_identifier` varchar(40) NOT NULL,
|
||||
`createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
`updatedAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
`group_id` varchar(60) NOT NULL,
|
||||
`participant_identifier` varchar(40) NOT NULL,
|
||||
`label` varchar(60) DEFAULT '',
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `npwd_messages_groups_group_id` (`group_id`),
|
||||
KEY `npwd_messages_groups_user_identifier_idx` (`user_identifier`),
|
||||
KEY `npwd_messages_groups_participant_identifier_idx` (`participant_identifier`),
|
||||
CONSTRAINT `npwd_messages_groups_participant_identifier` FOREIGN KEY (`participant_identifier`) REFERENCES `users` (`identifier`),
|
||||
CONSTRAINT `npwd_messages_groups_user_identifier` FOREIGN KEY (`user_identifier`) REFERENCES `users` (`identifier`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=66 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `npwd_messages_labels` (
|
||||
`id` int NOT NULL AUTO_INCREMENT,
|
||||
`user_identifier` varchar(40) NOT NULL,
|
||||
`label` varchar(60) NOT NULL,
|
||||
`createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
`updatedAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
`group_id` varchar(45) NOT NULL,
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `npwd_messages_labels_group_id_idx` (`group_id`),
|
||||
KEY `npwd_messages_labels_group_id_idx1` (`user_identifier`),
|
||||
CONSTRAINT `npwd_messages_labels_group_id` FOREIGN KEY (`group_id`) REFERENCES `npwd_messages_groups` (`group_id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `npwd_messages` (
|
||||
`id` int NOT NULL AUTO_INCREMENT,
|
||||
`message` varchar(512) NOT NULL,
|
||||
`user_identifier` varchar(40) NOT NULL,
|
||||
`group_id` varchar(60) NOT NULL,
|
||||
`isRead` tinyint NOT NULL DEFAULT '0',
|
||||
`createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
`updatedAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
`visible` tinyint NOT NULL DEFAULT '1',
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `messages_identifier_idx` (`user_identifier`),
|
||||
KEY `npwd_messages_group_id_idx` (`group_id`),
|
||||
CONSTRAINT `npwd_messages_group_id` FOREIGN KEY (`group_id`) REFERENCES `npwd_messages_groups` (`group_id`),
|
||||
CONSTRAINT `npwd_messages_user_identifier` FOREIGN KEY (`user_identifier`) REFERENCES `users` (`identifier`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=120 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
||||
@@ -21,12 +21,14 @@
|
||||
"connection-string-parser": "^1.0.3",
|
||||
"fivem-esx-js": "^0.1.12",
|
||||
"fivem-js": "^1.3.1",
|
||||
"md5": "^2.3.0",
|
||||
"mysql2": "^2.2.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/preset-env": "^7.12.1",
|
||||
"@babel/preset-typescript": "^7.12.1",
|
||||
"@types/jest": "^26.0.14",
|
||||
"@types/md5": "^2.2.1",
|
||||
"@typescript-eslint/eslint-plugin": "^3.8.0",
|
||||
"@typescript-eslint/parser": "^4.4.0",
|
||||
"eslint": "^7.6.0",
|
||||
|
||||
@@ -64,3 +64,21 @@ export function generateConnectionPool() {
|
||||
}
|
||||
|
||||
export const pool = generateConnectionPool();
|
||||
|
||||
|
||||
export async function withTransaction(queries: Promise<any>[]): Promise<any[]> {
|
||||
const connection = await pool.getConnection()
|
||||
connection.beginTransaction();
|
||||
|
||||
try {
|
||||
const results = await Promise.all(queries)
|
||||
await connection.commit()
|
||||
await connection.release()
|
||||
return results;
|
||||
} catch (err) {
|
||||
console.warn('Error when submitting queries');
|
||||
await connection.rollback()
|
||||
await connection.release()
|
||||
return Promise.reject(err)
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { pool } from './db';
|
||||
import { ESX, getSource } from './server';
|
||||
import { ESX } from "./server";
|
||||
|
||||
interface IPhoneNumber {
|
||||
phone_number: string;
|
||||
@@ -16,7 +16,10 @@ export async function usePhoneNumber(identifier: string): Promise<string> {
|
||||
return phoneNumber[0].phone_number;
|
||||
}
|
||||
|
||||
export const getSource = () => parseInt(source);
|
||||
|
||||
export async function useIdentifier(): Promise<string> {
|
||||
return ESX.GetPlayerFromId(getSource()).getIdentifier();
|
||||
const identifier = await ESX.GetPlayerFromId(getSource()).getIdentifier()
|
||||
return identifier;
|
||||
}
|
||||
|
||||
@@ -8,9 +8,9 @@ import './sv_sellout';
|
||||
import './sv_bank';
|
||||
import './sv_notes';
|
||||
import './sv_photo';
|
||||
import './sv_messages';
|
||||
|
||||
export let ESX: ESXServer = null;
|
||||
|
||||
emit("esx:getSharedObject", (obj: ESXServer) => (ESX = obj));
|
||||
|
||||
export const getSource = () => parseInt(source);
|
||||
@@ -1,5 +1,6 @@
|
||||
import events from '../utils/events';
|
||||
import { ESX, getSource } from './server';
|
||||
import { ESX } from './server';
|
||||
import { getSource } from "./functions";
|
||||
import { pool } from './db';
|
||||
import { Transfer, Credentials } from '../../phone/src/common/interfaces/bank';
|
||||
import { useIdentifier } from './functions';
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { ESX, getSource } from "./server";
|
||||
import { ESX } from "./server";
|
||||
import { pool } from "./db";
|
||||
import events from "../utils/events";
|
||||
import { useIdentifier } from './functions'
|
||||
import { useIdentifier, getSource } from './functions'
|
||||
|
||||
interface Contacts {
|
||||
id?: number;
|
||||
@@ -31,7 +31,7 @@ async function addContact(
|
||||
const query =
|
||||
"INSERT INTO npwd_phone_contacts (identifier, number, display, avatar) VALUES (?, ?, ?, ?)";
|
||||
|
||||
await pool.query(query, [identifier, number, display, avatar]);
|
||||
const [result] = await pool.query(query, [identifier, number, display, avatar]);
|
||||
}
|
||||
|
||||
async function updateContact(contact: Contacts, identifier: string): Promise<any> {
|
||||
|
||||
@@ -0,0 +1,379 @@
|
||||
import md5 from 'md5';
|
||||
|
||||
import events from '../utils/events';
|
||||
import { Message, MessageGroup, CreateMessageGroupResult } from '../../phone/src/common/interfaces/messages';
|
||||
import { pool, withTransaction } from "./db";
|
||||
import { getSource, useIdentifier } from './functions';
|
||||
|
||||
/**
|
||||
* Used for the raw npwd_messages_groups row responses
|
||||
*/
|
||||
interface UnformattedMessageGroup {
|
||||
group_id: string;
|
||||
user_identifier: string;
|
||||
participant_identifier: string;
|
||||
phone_number: string;
|
||||
label?: string;
|
||||
avatar?: string;
|
||||
display?: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to help consolidate raw npwd_messages_groups rows into
|
||||
* a mapping of a single message group
|
||||
*/
|
||||
interface MessageGroupMapping {
|
||||
[groupId: string]: {
|
||||
user_identifier: string;
|
||||
participants: string[];
|
||||
label?: string;
|
||||
avatar?: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a message in the database
|
||||
* @param userIdentifier - identifier of the user creating this message
|
||||
* @param groupId - the message group ID to attach this message to
|
||||
* @param message - content of the message
|
||||
*/
|
||||
async function createMessage(userIdentifier: string, groupId: string, message: string): Promise<any> {
|
||||
const query = `
|
||||
INSERT INTO npwd_messages
|
||||
(user_identifier, message, group_id)
|
||||
VALUES (?, ?, ?)
|
||||
`;
|
||||
const [results] = await pool.query(query, [
|
||||
userIdentifier,
|
||||
message,
|
||||
groupId,
|
||||
]);
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve all message groups associated with a user. This will
|
||||
* populate the list of message groups on the UI
|
||||
* @param userIdentifier - identifier of the user to get message groups for
|
||||
*/
|
||||
async function getMessageGroups(userIdentifier: string): Promise<UnformattedMessageGroup[]> {
|
||||
const query = `
|
||||
SELECT
|
||||
npwd_messages_groups.group_id,
|
||||
npwd_messages_groups.participant_identifier,
|
||||
npwd_messages_groups.user_identifier,
|
||||
npwd_messages_labels.label,
|
||||
users.phone_number,
|
||||
npwd_phone_contacts.avatar,
|
||||
npwd_phone_contacts.display
|
||||
FROM (
|
||||
SELECT group_id
|
||||
FROM npwd_messages_groups
|
||||
WHERE npwd_messages_groups.participant_identifier = ?
|
||||
) as t
|
||||
LEFT OUTER JOIN npwd_messages_groups on npwd_messages_groups.group_id = t.group_id
|
||||
LEFT OUTER JOIN users on users.identifier = npwd_messages_groups.participant_identifier
|
||||
LEFT OUTER JOIN npwd_messages_labels on npwd_messages_labels.group_id = npwd_messages_groups.group_id
|
||||
LEFT OUTER JOIN npwd_phone_contacts on REGEXP_REPLACE(npwd_phone_contacts.number, '[^0-9]', '') = REGEXP_REPLACE(users.phone_number, '[^0-9]', '') AND npwd_phone_contacts.identifier = ?
|
||||
WHERE npwd_messages_groups.participant_identifier != ?
|
||||
ORDER BY npwd_messages_groups.createdAt DESC
|
||||
`
|
||||
const [results] = await pool.query(query, [ userIdentifier, userIdentifier, userIdentifier ]);
|
||||
return <UnformattedMessageGroup[]>results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve all messages associated with a group and add a field
|
||||
* "isMine" which determines if the message belongs to the user
|
||||
* making the request
|
||||
* @param userIdentifier - user to get messages for
|
||||
* @param groupId - the group to get messages from
|
||||
*/
|
||||
async function getMessages(userIdentifier: string, groupId: string): Promise<Message[]> {
|
||||
const query = `
|
||||
SELECT
|
||||
npwd_messages.id,
|
||||
npwd_messages.message,
|
||||
npwd_messages.user_identifier,
|
||||
npwd_messages.isRead,
|
||||
npwd_messages.updatedAt,
|
||||
users.phone_number,
|
||||
npwd_phone_contacts.display,
|
||||
npwd_phone_contacts.avatar
|
||||
FROM npwd_messages
|
||||
LEFT OUTER JOIN users on users.identifier = npwd_messages.user_identifier
|
||||
LEFT OUTER JOIN npwd_phone_contacts on REGEXP_REPLACE(npwd_phone_contacts.number, '[^0-9]', '') = REGEXP_REPLACE(users.phone_number, '[^0-9]', '') AND npwd_phone_contacts.identifier = ?
|
||||
WHERE npwd_messages.group_id = ?
|
||||
ORDER BY createdAt ASC;
|
||||
`;
|
||||
const [results] = await pool.query(query, [ userIdentifier, groupId ]);
|
||||
const messages = <Message[]>results;
|
||||
return messages.map(message => ({
|
||||
...message,
|
||||
isMine: message.user_identifier === userIdentifier,
|
||||
updatedAt: message.updatedAt.toString(),
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a message group label
|
||||
* @param userIdentifier - user who is creating the message group
|
||||
* @param groupId - groupId this label is attached to
|
||||
* @param label - the label itself
|
||||
*/
|
||||
async function createLabel(userIdentifier: string, groupId: string, label: string): Promise<any> {
|
||||
const query = `
|
||||
INSERT INTO npwd_messages_labels
|
||||
(user_identifier, group_id, label)
|
||||
VALUES (?, ?, ?)
|
||||
`;
|
||||
await pool.query(query, [ userIdentifier, groupId, label ]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a message group
|
||||
* @param userIdentifier - the user creating the message group
|
||||
* @param groupId - the unique group ID this corresponds to
|
||||
* @param participantIdentifier - the participant user identifier. This identifier is what attaches
|
||||
* other players to the message group
|
||||
*/
|
||||
async function createMessageGroup(userIdentifier: string, groupId: string, participantIdentifier: string): Promise<any> {
|
||||
const query = `
|
||||
INSERT INTO npwd_messages_groups
|
||||
(user_identifier, group_id, participant_identifier)
|
||||
VALUES (?, ?, ?)
|
||||
`;
|
||||
const [results] = await pool.query(query, [
|
||||
userIdentifier,
|
||||
groupId,
|
||||
participantIdentifier
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a players identifier from their phone number
|
||||
* @param phoneNumber - the phone number to search for
|
||||
*/
|
||||
async function getIdentifierFromPhoneNumber(phoneNumber: string): Promise<string> {
|
||||
const query = `
|
||||
SELECT identifier
|
||||
FROM users
|
||||
WHERE REGEXP_REPLACE(phone_number, '[^0-9]', '') = ?
|
||||
LIMIT 1
|
||||
`;
|
||||
const [results] = await pool.query(query, [ phoneNumber]);
|
||||
const identifiers = <any>results;
|
||||
return identifiers[0]['identifier']
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This method checks if the input groupId already exists in
|
||||
* the database. As long as the groupId is derived from the input
|
||||
* identifiers this means that the user is trying to create a
|
||||
* duplicate!
|
||||
* @param groupId - group Id to check that it exists
|
||||
*/
|
||||
async function checkIfMessageGroupExists(groupId: string): Promise<boolean> {
|
||||
const query = `
|
||||
SELECT COUNT(*) as count
|
||||
FROM npwd_messages_groups
|
||||
WHERE group_id = ?;
|
||||
`;
|
||||
const [results] = await pool.query(query, [ groupId]);
|
||||
const result = <any>results;
|
||||
const count = result[0].count;
|
||||
return count > 0;
|
||||
}
|
||||
|
||||
async function getMessageCountByGroup(groupId: string): Promise<number> {
|
||||
const query = `
|
||||
SELECT COUNT(*) as count
|
||||
FROM npwd_messages
|
||||
WHERE group_id = ?;
|
||||
`;
|
||||
const [results] = await pool.query(query, [ groupId]);
|
||||
const result = <any>results;
|
||||
return result[0].count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Consolidate raw message groups into a mapping that groups participants
|
||||
* by the message group. The goal of this is to reduce the rows of message
|
||||
* groups into a single MessageGroup object.
|
||||
* @param userIdentifier - the user identifier to get message groups for
|
||||
*/
|
||||
async function getConsolidatedMessageGroups(userIdentifier: string): Promise<MessageGroupMapping> {
|
||||
const messageGroups = await getMessageGroups(userIdentifier);
|
||||
return messageGroups.reduce((mapping: MessageGroupMapping, messageGroup: UnformattedMessageGroup) => {
|
||||
const groupId = messageGroup.group_id;
|
||||
const displayTerm = messageGroup.display || messageGroup.phone_number || '???';
|
||||
|
||||
if (groupId in mapping) {
|
||||
mapping[groupId].participants = mapping[groupId].participants.concat(displayTerm)
|
||||
} else {
|
||||
mapping[groupId] = {
|
||||
user_identifier: messageGroup.user_identifier,
|
||||
avatar: messageGroup.avatar,
|
||||
label: messageGroup.label,
|
||||
participants: [displayTerm],
|
||||
updatedAt: messageGroup.updatedAt ? messageGroup.updatedAt.toString() : null
|
||||
}
|
||||
}
|
||||
return mapping;
|
||||
}, {});
|
||||
}
|
||||
|
||||
async function getGroupIds(userIdentifier: string, groupMapping: MessageGroupMapping): Promise<string[]> {
|
||||
const groupIds: string[] = [];
|
||||
for (const groupId of Object.keys(groupMapping)) {
|
||||
const isMine = groupMapping[groupId].user_identifier === userIdentifier
|
||||
if (isMine || await getMessageCountByGroup(groupId) > 0) {
|
||||
groupIds.push(groupId)
|
||||
}
|
||||
}
|
||||
return groupIds;
|
||||
}
|
||||
|
||||
/**
|
||||
* Given the mapping of groupId: fields contents reduce it into a list
|
||||
* of MessageGroup objects ready for the UI to consume
|
||||
* @param userIdentifier - user to generate the MessageGroups for
|
||||
*/
|
||||
async function getFormattedMessageGroups(userIdentifier: string): Promise<MessageGroup[]> {
|
||||
const groupMapping = await getConsolidatedMessageGroups(userIdentifier);
|
||||
const groupIds = await getGroupIds(userIdentifier, groupMapping);
|
||||
|
||||
return groupIds.map(groupId => {
|
||||
const group = groupMapping[groupId];
|
||||
return {
|
||||
...group,
|
||||
groupId,
|
||||
groupDisplay: group.participants.join(', '),
|
||||
// note that 1 here references how many participants besides the user
|
||||
isGroupChat: group.participants.length > 1,
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Main method to handle creation of new message groups. First
|
||||
* we retrieve identifiers for each submitted phone number and
|
||||
* then rows in npwd_messages_groups are created for each of them
|
||||
* bound to a unique groupId. The groupId is any unique string - we
|
||||
* use hashes here.
|
||||
*
|
||||
* These queries are batched with a transaction so that if any of
|
||||
* them fail the queries are not committed to the database. This helps
|
||||
* avoid situations where some participants get added to the group but
|
||||
* one fails resulting in a partial group which would be very confusing
|
||||
* to the player.
|
||||
* @param userIdentifier - user who is creating the group
|
||||
* @param phoneNumbers - list of phone numbers to add to the grup
|
||||
* @param groupLabel - optional group label to give the group
|
||||
*/
|
||||
async function createMessageGroupsFromPhoneNumbers(
|
||||
userIdentifier: string,
|
||||
phoneNumbers: string[],
|
||||
groupLabel: string): Promise<CreateMessageGroupResult> {
|
||||
|
||||
// we check that each phoneNumber exists before we create the group
|
||||
const identifiers: string[] = [];
|
||||
for (const phoneNumber of phoneNumbers) {
|
||||
try {
|
||||
const identifier = await getIdentifierFromPhoneNumber(phoneNumber)
|
||||
identifiers.push(identifier);
|
||||
} catch (err) {
|
||||
return { error: true, phoneNumber };
|
||||
}
|
||||
}
|
||||
|
||||
// don't allow explicitly adding yourself
|
||||
if (identifiers.some(identifier => identifier === userIdentifier)) {
|
||||
return { error: true, mine: true };
|
||||
}
|
||||
|
||||
// make sure we are always in a consistent order. It is very important
|
||||
// that this not change! Changing this order can result in the ability
|
||||
// of duplicate message groups being created.
|
||||
identifiers.sort();
|
||||
const mergedIdentifiers = identifiers.join('-');
|
||||
// we don't need this to be secure. Its purpose is to create a unique
|
||||
// string derived from the identifiers. In this way we can check
|
||||
// that this groupId isn't used before. If it has then it means
|
||||
// we are trying to create a duplicate message group!
|
||||
const groupId = md5(mergedIdentifiers);
|
||||
if (await checkIfMessageGroupExists(groupId)) {
|
||||
return { error: true, duplicate: true };
|
||||
}
|
||||
|
||||
const queryPromises = [
|
||||
// create a row that contains ourselves so reverse accessing from
|
||||
// other players works as expected
|
||||
createMessageGroup(userIdentifier, groupId, userIdentifier),
|
||||
...identifiers.map(identifier => createMessageGroup(userIdentifier, groupId, identifier))
|
||||
]
|
||||
|
||||
// we allow users to attach labels to name their group chats
|
||||
if (groupLabel) {
|
||||
queryPromises.push(createLabel(userIdentifier, groupId, groupLabel));
|
||||
}
|
||||
|
||||
// wrap this in a transaction to make sure ALL of these INSERTs succeed
|
||||
// so we are not left in a situation where only some of the member of the
|
||||
// group exist while other are left off.
|
||||
try {
|
||||
await withTransaction(queryPromises);
|
||||
} catch (err) {
|
||||
return { error: true };
|
||||
}
|
||||
|
||||
return { error: false, groupId };
|
||||
}
|
||||
|
||||
onNet(events.MESSAGES_FETCH_MESSAGE_GROUPS, async () => {
|
||||
try {
|
||||
const _identifier = await useIdentifier();
|
||||
const messageGroups = await getFormattedMessageGroups(_identifier);
|
||||
emitNet(events.MESSAGES_FETCH_MESSAGE_GROUPS_SUCCESS, getSource(), messageGroups);
|
||||
} catch (e) {
|
||||
emitNet(events.MESSAGES_FETCH_MESSAGE_GROUPS_FAILED, getSource());
|
||||
}
|
||||
});
|
||||
|
||||
onNet(events.MESSAGES_CREATE_MESSAGE_GROUP, async (phoneNumbers: string[], label: string = null) => {
|
||||
try {
|
||||
const _identifier = await useIdentifier();
|
||||
const result = await createMessageGroupsFromPhoneNumbers(_identifier, phoneNumbers, label)
|
||||
|
||||
if (result.error) {
|
||||
emitNet(events.MESSAGES_CREATE_MESSAGE_GROUP_FAILED, getSource(), result);
|
||||
} else {
|
||||
emitNet(events.MESSAGES_CREATE_MESSAGE_GROUP_SUCCESS, getSource(), result);
|
||||
}
|
||||
} catch (e) {
|
||||
emitNet(events.MESSAGES_CREATE_MESSAGE_GROUP_FAILED, getSource());
|
||||
}
|
||||
});
|
||||
|
||||
onNet(events.MESSAGES_FETCH_MESSAGES, async (groupId: string) => {
|
||||
try {
|
||||
const _identifier = await useIdentifier();
|
||||
const messages = await getMessages(_identifier, groupId);
|
||||
emitNet(events.MESSAGES_FETCH_MESSAGES_SUCCESS, getSource(), messages);
|
||||
} catch (e) {
|
||||
emitNet(events.MESSAGES_FETCH_MESSAGES_FAILED, getSource());
|
||||
}
|
||||
});
|
||||
|
||||
onNet(events.MESSAGES_SEND_MESSAGE, async (groupId: string, message: string) => {
|
||||
try {
|
||||
const _identifier = await useIdentifier();
|
||||
await createMessage(_identifier, groupId, message)
|
||||
emitNet(events.MESSAGES_SEND_MESSAGE_SUCCESS, getSource(), groupId);
|
||||
} catch (e) {
|
||||
emitNet(events.MESSAGES_SEND_MESSAGE_FAILED, getSource());
|
||||
}
|
||||
});
|
||||
@@ -1,7 +1,6 @@
|
||||
import events from "../utils/events";
|
||||
import { ESX, getSource } from "./server";
|
||||
import { pool } from "./db";
|
||||
import { useIdentifier } from "./functions";
|
||||
import { useIdentifier, getSource } from "./functions";
|
||||
import { Note, NoteId } from "../../phone/src/common/interfaces/notes";
|
||||
|
||||
async function addNote(identifier: string, note: Note): Promise<any> {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
|
||||
import events from '../utils/events';
|
||||
import { ESX, getSource } from './server';
|
||||
import { ESX } from './server';
|
||||
import { getSource } from "./functions";
|
||||
import { pool } from './db';
|
||||
import { usePhoneNumber } from './functions';
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { pool } from "./db";
|
||||
import { ESX, getSource } from "./server";
|
||||
import { ESX } from "./server";
|
||||
import { getSource } from "./functions";
|
||||
import { Tweet, Profile } from '../../phone/src/common/interfaces/twitter';
|
||||
import events from "../utils/events";
|
||||
import config from "../utils/config";
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
export default {
|
||||
TWITTER: "TWITTER",
|
||||
MESSAGES: "MESSAGES"
|
||||
};
|
||||
@@ -43,7 +43,7 @@ export default {
|
||||
SELLOUT_FETCH_LISTING: 'phone:fetchAllListings',
|
||||
SELLOUT_SEND_LISTING: 'phone:sendAllListings',
|
||||
|
||||
// Bank
|
||||
//
|
||||
BANK_ADD_TRANSFER: 'phone:addTransfer',
|
||||
BANK_ADD_TRANSFER_SUCCESS: 'phone:addTransferSuccess',
|
||||
BANK_FETCH_TRANSACTIONS: 'phone:fetchAllTransactions',
|
||||
@@ -53,6 +53,20 @@ export default {
|
||||
BANK_SEND_CREDENTIALS: 'phone:sendBankCredentials',
|
||||
BANK_GET_CREDENTIALS: 'phone:getBankCredentials',
|
||||
|
||||
//messages
|
||||
MESSAGES_FETCH_MESSAGE_GROUPS: 'phone:fetchMessageGroups',
|
||||
MESSAGES_FETCH_MESSAGE_GROUPS_SUCCESS: 'phone:fetchMessageGroupsSuccess',
|
||||
MESSAGES_FETCH_MESSAGE_GROUPS_FAILED: 'phone:fetchMessageGroupsFailed',
|
||||
MESSAGES_CREATE_MESSAGE_GROUP: 'phone:createMessageGroup',
|
||||
MESSAGES_CREATE_MESSAGE_GROUP_SUCCESS: 'phone:createMessageGroupSuccess',
|
||||
MESSAGES_CREATE_MESSAGE_GROUP_FAILED: 'phone:createMessageGroupFailed',
|
||||
MESSAGES_SEND_MESSAGE: 'phone:sendMessage',
|
||||
MESSAGES_SEND_MESSAGE_SUCCESS: 'phone:sendMessageSuccess',
|
||||
MESSAGES_SEND_MESSAGE_FAILED: 'phone:sendMessageFailed',
|
||||
MESSAGES_FETCH_MESSAGES: 'phone:fetchMessages',
|
||||
MESSAGES_FETCH_MESSAGES_SUCCESS: 'phone:fetchMessagesSuccess',
|
||||
MESSAGES_FETCH_MESSAGES_FAILED: 'phone:fetchMessagesFailed',
|
||||
|
||||
// Notes
|
||||
NOTE_ADD_NOTE: 'phone:addNote',
|
||||
NOTE_FETCH_ALL_NOTES: 'phone:fetchAllNotes',
|
||||
|
||||
@@ -13,3 +13,7 @@ function sendMessage(app: string, method: string, data: any) {
|
||||
export function sendTwitterMessage(method: string, data: any = {}) {
|
||||
return sendMessage(apps.TWITTER, method, data);
|
||||
}
|
||||
|
||||
export function sendMessageEvent(method: string, data: any = {}) {
|
||||
return sendMessage(apps.MESSAGES, method, data);
|
||||
}
|
||||
+27
-1
@@ -1245,6 +1245,13 @@
|
||||
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.6.tgz#f4c7ec43e81b319a9815115031709f26987891f0"
|
||||
integrity sha512-3c+yGKvVP5Y9TYBEibGNR+kLtijnj7mYrXRg+WpFb2X9xm04g/DXYkfg4hmzJQosc9snFNUPkbYIhu+KAm6jJw==
|
||||
|
||||
"@types/md5@^2.2.1":
|
||||
version "2.2.1"
|
||||
resolved "https://registry.yarnpkg.com/@types/md5/-/md5-2.2.1.tgz#0e2d65d8f6cc91aafbc9be5a4c08fee9250406ce"
|
||||
integrity sha512-bZB0jqBL7JETFqvRKyuDETFceFaVcLm2MBPP5LFEEL/SZuqLnyvzF37tXmMERDncC3oeEj/fOUw88ftJeMpZaw==
|
||||
dependencies:
|
||||
"@types/node" "*"
|
||||
|
||||
"@types/node@*":
|
||||
version "14.11.5"
|
||||
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.11.5.tgz#fecad41c041cae7f2404ad4b2d0742fdb628b305"
|
||||
@@ -2200,6 +2207,11 @@ char-regex@^1.0.2:
|
||||
resolved "https://registry.yarnpkg.com/char-regex/-/char-regex-1.0.2.tgz#d744358226217f981ed58f479b1d6bcc29545dcf"
|
||||
integrity sha512-kWWXztvZ5SBQV+eRgKFeh8q5sLuZY2+8WUIzlxWVTg+oGwY14qylx1KbKzHd8P6ZYkAg0xyIDU9JMHhyJMZ1jw==
|
||||
|
||||
[email protected]:
|
||||
version "0.0.2"
|
||||
resolved "https://registry.yarnpkg.com/charenc/-/charenc-0.0.2.tgz#c0a1d2f3a7092e03774bfa83f14c0fc5790a8667"
|
||||
integrity sha1-wKHS86cJLgN3S/qD8UwPxXkKhmc=
|
||||
|
||||
chokidar@^2.1.8:
|
||||
version "2.1.8"
|
||||
resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-2.1.8.tgz#804b3a7b6a99358c3c5c61e71d8728f041cff917"
|
||||
@@ -2511,6 +2523,11 @@ cross-spawn@^7.0.0, cross-spawn@^7.0.2:
|
||||
shebang-command "^2.0.0"
|
||||
which "^2.0.1"
|
||||
|
||||
[email protected]:
|
||||
version "0.0.2"
|
||||
resolved "https://registry.yarnpkg.com/crypt/-/crypt-0.0.2.tgz#88d7ff7ec0dfb86f713dc87bbb42d044d3e6c41b"
|
||||
integrity sha1-iNf/fsDfuG9xPch7u0LQRNPmxBs=
|
||||
|
||||
crypto-browserify@^3.11.0:
|
||||
version "3.12.0"
|
||||
resolved "https://registry.yarnpkg.com/crypto-browserify/-/crypto-browserify-3.12.0.tgz#396cf9f3137f03e4b8e532c58f698254e00f80ec"
|
||||
@@ -3863,7 +3880,7 @@ is-binary-path@~2.1.0:
|
||||
dependencies:
|
||||
binary-extensions "^2.0.0"
|
||||
|
||||
is-buffer@^1.1.5:
|
||||
is-buffer@^1.1.5, is-buffer@~1.1.6:
|
||||
version "1.1.6"
|
||||
resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-1.1.6.tgz#efaa2ea9daa0d7ab2ea13a97b2b8ad51fefbe8be"
|
||||
integrity sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w==
|
||||
@@ -4811,6 +4828,15 @@ md5.js@^1.3.4:
|
||||
inherits "^2.0.1"
|
||||
safe-buffer "^5.1.2"
|
||||
|
||||
md5@^2.3.0:
|
||||
version "2.3.0"
|
||||
resolved "https://registry.yarnpkg.com/md5/-/md5-2.3.0.tgz#c3da9a6aae3a30b46b7b0c349b87b110dc3bda4f"
|
||||
integrity sha512-T1GITYmFaKuO91vxyoQMFETst+O71VUPEU3ze5GNzDm0OWdP8v1ziTaAEPUr/3kLsY3Sftgz242A1SetQiDL7g==
|
||||
dependencies:
|
||||
charenc "0.0.2"
|
||||
crypt "0.0.2"
|
||||
is-buffer "~1.1.6"
|
||||
|
||||
memory-fs@^0.4.1:
|
||||
version "0.4.1"
|
||||
resolved "https://registry.yarnpkg.com/memory-fs/-/memory-fs-0.4.1.tgz#3a9a20b8462523e447cfbc7e8bb80ed667bfc552"
|
||||
|
||||
Reference in new issue
Block a user