working groups & messages

This commit is contained in:
erik-sn committed 2020-11-20 18:14:07 -05:00
1 parent ccbc2d3b87
commit 681ce16912
25 files changed
+476 -162

No files matched your search

+51 -15
View File
@@ -1,27 +1,64 @@
import events from '../utils/events';
import { Message } from '../../phone/src/common/interfaces/messages';
import { Message, MessageGroup } from '../../phone/src/common/interfaces/messages';
import { sendMessageEvent } from '../utils/messages';
/**
* Twitter fetch messages
* Messages get message groups
*/
RegisterNuiCallbackType(events.MESSAGES_FETCH_MESSAGES);
on(`__cfx_nui:${events.MESSAGES_FETCH_MESSAGES}`, () => {
emitNet(events.MESSAGES_FETCH_MESSAGES);
RegisterNuiCallbackType(events.MESSAGES_GET_MESSAGE_GROUPS);
on(`__cfx_nui:${events.MESSAGES_GET_MESSAGE_GROUPS}`, (): void => {
emitNet(events.MESSAGES_GET_MESSAGE_GROUPS);
});
onNet(events.MESSAGES_FETCH_MESSAGES_SUCCESS, (messages: Message[]) => {
onNet(events.MESSAGES_GET_MESSAGE_GROUPS_SUCCESS, (messageGroups: MessageGroup[]): void => {
sendMessageEvent(events.MESSAGES_GET_MESSAGE_GROUPS_SUCCESS, messageGroups);
emitNet(events.MESSAGES_GET_MESSAGE_GROUPS_SUCCESS);
});
onNet(events.MESSAGES_CREATE_MESSAGE_GROUP_FAILED, () => {
sendMessageEvent(events.MESSAGES_CREATE_MESSAGE_GROUP_FAILED);
});
// fetch message groups on client launch
emitNet(events.MESSAGES_GET_MESSAGE_GROUPS);
/**
* Messages create message group
*/
RegisterNuiCallbackType(events.MESSAGES_CREATE_MESSAGE_GROUP);
on(`__cfx_nui:${events.MESSAGES_CREATE_MESSAGE_GROUP}`, ({ phoneNumbers }: any): void => {
emitNet(events.MESSAGES_CREATE_MESSAGE_GROUP, phoneNumbers);
});
onNet(events.MESSAGES_CREATE_MESSAGE_GROUP_SUCCESS, (result: any): void => {
console.log(result);
sendMessageEvent(events.MESSAGES_CREATE_MESSAGE_GROUP_SUCCESS, result);
emitNet(events.MESSAGES_CREATE_MESSAGE_GROUP_SUCCESS);
});
onNet(events.MESSAGES_CREATE_MESSAGE_GROUP_FAILED, (): void => {
sendMessageEvent(events.MESSAGES_CREATE_MESSAGE_GROUP_FAILED);
});
/**
* 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, () => {
onNet(events.MESSAGES_FETCH_MESSAGES_FAILED, (): void => {
sendMessageEvent(events.MESSAGES_FETCH_MESSAGES_FAILED);
});
// fetch messages on client launch
emitNet(events.MESSAGES_FETCH_MESSAGES);
/**
* Messages send message
@@ -31,12 +68,11 @@ on(`__cfx_nui:${events.MESSAGES_SEND_MESSAGE}`, (data: any) => {
emitNet(events.MESSAGES_SEND_MESSAGE, data.groupId, data.message);
});
onNet(events.MESSAGES_SEND_MESSAGE_SUCCESS, (result: any) => {
console.log(result);
sendMessageEvent(events.MESSAGES_SEND_MESSAGE_SUCCESS, result);
emitNet(events.MESSAGES_FETCH_MESSAGES);
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);
});
});
+3 -1
View File
@@ -21,12 +21,14 @@
"connection-string-parser": "^1.0.3",
"fivem-esx-js": "^0.1.12",
"fivem-js": "^1.3.1",
"mysql2": "^2.2.5"
"mysql2": "^2.2.5",
"uuid": "^8.3.1"
},
"devDependencies": {
"@babel/preset-env": "^7.12.1",
"@babel/preset-typescript": "^7.12.1",
"@types/jest": "^26.0.14",
"@types/uuid": "^8.3.0",
"@typescript-eslint/eslint-plugin": "^3.8.0",
"@typescript-eslint/parser": "^4.4.0",
"eslint": "^7.6.0",
+18
View File
@@ -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)
}
}
+140 -53
View File
@@ -1,21 +1,23 @@
import { v4 as uuidv4 } from 'uuid';
import events from '../utils/events';
import { pool } from "./db";
import { Message, MessageGroup } from '../../phone/src/common/interfaces/messages';
import { pool, withTransaction } from "./db";
import { getSource, useIdentifier } from './functions';
interface GroupMapping {
[channelId: string]: {
messages: Message[];
updatedAt: string;
createdAt: string;
}
interface UnformattedMessageGroup {
group_id: string;
participant_identifier: string;
phone_number: string;
display: string;
updatedAt: string;
}
interface GroupMessages {
channelId: string;
messages: Message[];
updatedAt: string;
createdAt: string;
interface MessageGroupMapping {
[groupId: string]: {
participants: string[];
updatedAt: string;
}
}
async function createMessage(userIdentifier: string, groupId: string, message: string): Promise<any> {
@@ -32,79 +34,153 @@ async function createMessage(userIdentifier: string, groupId: string, message: s
return results;
}
async function getMessages(userIdentifier: string): Promise<Message[]> {
async function getMessageGroups(userIdentifier: string): Promise<UnformattedMessageGroup[]> {
const query = `
SELECT
npwd_messages.*,
npwd_messages_groups.group_id,
npwd_messages_groups.participant_identifier,
users.phone_number,
MAX(npwd_messages.updatedAt) AS updatedAt,
npwd_phone_contacts.display
FROM npwd_messages_groups
LEFT OUTER JOIN users on users.identifier = npwd_messages_groups.participant_identifier
LEFT OUTER JOIN npwd_phone_contacts on npwd_phone_contacts.number = users.phone_number
LEFT OUTER JOIN npwd_messages on npwd_messages.group_id = npwd_messages_groups.group_id
WHERE npwd_messages_groups.user_identifier = ? AND npwd_messages_groups.participant_identifier != ?
ORDER BY updatedAt DESC
`
const [results] = await pool.query(query, [ userIdentifier, userIdentifier]);
return <UnformattedMessageGroup[]>results;
}
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 (
SELECT group_id
FROM npwd_messages_groups
WHERE user_identifier = ?
) as t
LEFT OUTER JOIN npwd_messages on t.group_id = npwd_messages.group_id
FROM npwd_messages
LEFT OUTER JOIN users on users.identifier = npwd_messages.user_identifier
LEFT OUTER JOIN npwd_phone_contacts on npwd_phone_contacts.number = users.phone_number
WHERE (users.phone_number IS NOT NULL OR npwd_phone_contacts.display IS NOT NULL)
WHERE npwd_messages.group_id = ?
ORDER BY updatedAt ASC;
`;
const [results] = await pool.query(query, [ userIdentifier]);
const [results] = await pool.query(query, [ groupId]);
const messages = <Message[]>results;
return messages.map(message => ({
...message,
isMine: message.user_identifier === userIdentifier,
createdAt: message.createdAt.toString(),
updatedAt: message.updatedAt.toString(),
}));
}
async function groupMessagesByGroup(useIdentifier: string): Promise<GroupMapping> {
const messages = await getMessages(useIdentifier);
return messages.reduce((mapping: GroupMapping, message: Message) => {
const groupId = message.group_id;
async function getConsolidatedMessageGroups(useIdentifier: string): Promise<MessageGroupMapping> {
const messageGroups = await getMessageGroups(useIdentifier);
return messageGroups.reduce((mapping: MessageGroupMapping, messageGroup: UnformattedMessageGroup) => {
const groupId = messageGroup.group_id;
const displayTerm = messageGroup.display || messageGroup.phone_number || '???';
if (groupId in mapping) {
// we sort by updatedAt ASCENDING in the SQL query so we can always assume the
// first participant added has the lastest updatedAt. Therefore we don't
// need to updatedAt here
mapping[groupId].messages = mapping[groupId].messages.concat(message);
mapping[groupId].participants = mapping[groupId].participants.concat(displayTerm)
} else {
mapping[groupId] = {
messages: [message],
updatedAt: message.updatedAt,
createdAt: message.createdAt,
participants: [displayTerm],
updatedAt: messageGroup.updatedAt ? messageGroup.updatedAt.toString() : null
}
}
return mapping;
}, {});
}
function getGroupDisplayName(userIdentifier: string, messages: Message[]): string {
return messages.reduce((displayTerms, message) => {
if (message.user_identifier === userIdentifier) return displayTerms; // don't display our name
const display = message.display || message.phone_number;
return displayTerms.includes(display) ? [] : displayTerms.concat(display)
}, []).join(', ');
}
async function getFormattedMessages(userIdentifier: string): Promise<MessageGroup[]> {
const groupMapping = await groupMessagesByGroup(userIdentifier);
const groups = Object.keys(groupMapping);
return groups.map(group => {
async function getFormattedMessageGroups(userIdentifier: string): Promise<MessageGroup[]> {
const groupMapping = await getConsolidatedMessageGroups(userIdentifier);
const groupIds = Object.keys(groupMapping);
return groupIds.map(groupId => {
const group = groupMapping[groupId];
return {
...groupMapping[group],
groupId: group,
groupDisplay: getGroupDisplayName(userIdentifier, groupMapping[group].messages)
groupId,
groupDisplay: group.participants.join(','),
updatedAt: group.updatedAt,
}
});
}
onNet(events.MESSAGES_FETCH_MESSAGES, async () => {
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
]);
}
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']
}
async function createMessageGroupsFromPhoneNumbers(userIdentifier: string, phoneNumbers: string[]): Promise<string> {
const groupId = uuidv4();
// we check that each phoneNumber exists before we create the group
const identifiers: string[] = [];
for (const phoneNumber of phoneNumbers) {
const identifier = await getIdentifierFromPhoneNumber(phoneNumber)
identifiers.push(identifier);
}
const connection = await pool.getConnection()
connection.beginTransaction();
const queryPromises = [
// create a row that contains ourselves so others can reference it
createMessageGroup(userIdentifier, groupId, userIdentifier),
...identifiers.map(identifier => createMessageGroup(userIdentifier, groupId, identifier))
]
// 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.
const results = withTransaction(queryPromises);
console.log(results);
return groupId;
}
onNet(events.MESSAGES_GET_MESSAGE_GROUPS, async () => {
try {
const _identifier = await useIdentifier();
const messages = await getFormattedMessages(_identifier);
const messageGroups = await getFormattedMessageGroups(_identifier);
emitNet(events.MESSAGES_GET_MESSAGE_GROUPS_SUCCESS, getSource(), messageGroups);
} catch (e) {
emitNet(events.MESSAGES_GET_MESSAGE_GROUPS_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());
@@ -114,9 +190,20 @@ onNet(events.MESSAGES_FETCH_MESSAGES, async () => {
onNet(events.MESSAGES_SEND_MESSAGE, async (groupId: string, message: string) => {
try {
const _identifier = await useIdentifier();
const result = await createMessage(_identifier, groupId, message)
emitNet(events.MESSAGES_SEND_MESSAGE_SUCCESS, getSource(), result);
await createMessage(_identifier, groupId, message)
emitNet(events.MESSAGES_SEND_MESSAGE_SUCCESS, getSource(), groupId);
} catch (e) {
emitNet(events.MESSAGES_SEND_MESSAGE_FAILED, getSource());
}
});
onNet(events.MESSAGES_CREATE_MESSAGE_GROUP, async (phoneNumbers: string[]) => {
try {
console.log(phoneNumbers);
const _identifier = await useIdentifier();
const groupId = await createMessageGroupsFromPhoneNumbers(_identifier, phoneNumbers)
emitNet(events.MESSAGES_CREATE_MESSAGE_GROUP_SUCCESS, getSource(), groupId);
} catch (e) {
emitNet(events.MESSAGES_CREATE_MESSAGE_GROUP_FAILED, getSource());
}
});
+6
View File
@@ -54,6 +54,12 @@ export default {
BANK_GET_CREDENTIALS: 'phone:getBankCredentials',
//messages
MESSAGES_GET_MESSAGE_GROUPS: 'phone:getMessageGroups',
MESSAGES_GET_MESSAGE_GROUPS_SUCCESS: 'phone:getMessageGroupsSuccess',
MESSAGES_GET_MESSAGE_GROUPS_FAILED: 'phone:getMessageGroupsFailed',
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',
+6 -1
View File
@@ -1292,6 +1292,11 @@
dependencies:
source-map "^0.6.1"
"@types/uuid@^8.3.0":
version "8.3.0"
resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-8.3.0.tgz#215c231dff736d5ba92410e6d602050cce7e273f"
integrity sha512-eQ9qFW/fhfGJF8WKHGEHZEyVWfZxrT+6CLIJGBcZPfxUh/+BnEj+UCGYMlr9qZuX/2AltsvwrGqp0LhEW8D0zQ==
"@types/webpack-sources@*":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@types/webpack-sources/-/webpack-sources-2.0.0.tgz#08216ab9be2be2e1499beaebc4d469cec81e82a7"
@@ -6872,7 +6877,7 @@ uuid@^3.3.2:
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.4.0.tgz#b23e4358afa8a202fe7a100af1f5f883f02007ee"
integrity sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==
uuid@^8.3.0:
uuid@^8.3.0, uuid@^8.3.1:
version "8.3.1"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.1.tgz#2ba2e6ca000da60fce5a196954ab241131e05a31"
integrity sha512-FOmRr+FmWEIG8uhZv6C2bTgEVXsHk08kE7mPlrBbEe+c3r9pjceVPgupIfNIhc4yx55H69OXANrUaSuu9eInKg==