From b93d5faee16fdb8f5f30ef22cee8ef2575a4e1d5 Mon Sep 17 00:00:00 2001 From: J Francisco Rader Date: Sun, 14 Feb 2021 19:14:56 -0500 Subject: [PATCH 1/6] Redirect when new msg group is created, and use contacts to push a new message from there --- .../contacts/components/List/ContactList.tsx | 8 +- .../src/apps/messages/components/AlertBar.tsx | 20 +++-- .../apps/messages/components/MessagesApp.tsx | 80 +++++++++++-------- .../components/form/NewMessageGroupForm.tsx | 38 ++++++--- .../messages/components/list/MessagesList.tsx | 30 ++++++- .../components/modal/MessageGroupModal.tsx | 6 +- phone/src/apps/messages/hooks/useAlerts.ts | 11 +-- phone/src/apps/messages/hooks/useMessages.ts | 13 ++- phone/src/os/debug/InjectDebugData.ts | 4 +- resources/server/sv_messages.ts | 73 ++++++++++++++--- 10 files changed, 195 insertions(+), 88 deletions(-) diff --git a/phone/src/apps/contacts/components/List/ContactList.tsx b/phone/src/apps/contacts/components/List/ContactList.tsx index b120a45..6d03a33 100644 --- a/phone/src/apps/contacts/components/List/ContactList.tsx +++ b/phone/src/apps/contacts/components/List/ContactList.tsx @@ -43,13 +43,13 @@ export const ContactList = () => { }); }; - const handleMessage = () => { + const handleMessage = (phoneNumber: string) => { LogDebugEvent({ action: 'Routing to Message', level: 1, - data: true, + data: { phoneNumber }, }); - // TODO: Integrate to send to message event when route becomes available + history.push(`/messages/new/${phoneNumber}`); }; const filteredRegEx = new RegExp(filteredContacts, 'gi'); @@ -82,7 +82,7 @@ export const ContactList = () => { - - + ); diff --git a/phone/src/apps/messages/hooks/useAlerts.ts b/phone/src/apps/messages/hooks/useAlerts.ts index efa0672..9e802f0 100644 --- a/phone/src/apps/messages/hooks/useAlerts.ts +++ b/phone/src/apps/messages/hooks/useAlerts.ts @@ -1,21 +1,16 @@ -import { useRecoilState } from 'recoil'; +import { useRecoilValue } from 'recoil'; import { CreateMessageGroupResult } from '../../../common/typings/messages'; import { messageState } from './state'; interface IUseAlerts { createMessageGroupResult: CreateMessageGroupResult | null; - clearCreateMessageGroupResult: () => void; } export default (): IUseAlerts => { - const [ - createMessageGroupResult, - setCreateMessageGroupResult, - ] = useRecoilState( + const createMessageGroupResult = useRecoilValue( messageState.createMessageGroupResult ); - const clearCreateMessageGroupResult = () => setCreateMessageGroupResult(null); - return { createMessageGroupResult, clearCreateMessageGroupResult }; + return { createMessageGroupResult }; }; diff --git a/phone/src/apps/messages/hooks/useMessages.ts b/phone/src/apps/messages/hooks/useMessages.ts index f95a291..c093d4b 100644 --- a/phone/src/apps/messages/hooks/useMessages.ts +++ b/phone/src/apps/messages/hooks/useMessages.ts @@ -1,11 +1,13 @@ import { useRecoilState, useRecoilValue } from 'recoil'; -import { Message, MessageGroup } from '../../../common/typings/messages'; +import { CreateMessageGroupResult, Message, MessageGroup } from '../../../common/typings/messages'; import { messageState } from './state'; interface IUseMessages { messages?: Message[] | null; setMessages: (messages: Message[] | null) => void; messageGroups?: MessageGroup[] | null; + createMessageGroupResult?: CreateMessageGroupResult | null; + clearMessageGroupResult(): void; } export default (): IUseMessages => { @@ -15,5 +17,12 @@ export default (): IUseMessages => { const messageGroups = useRecoilValue( messageState.messageGroups ); - return { messages, setMessages, messageGroups }; + + const [createMessageGroupResult, setCreateMessageGroupResult] = useRecoilState( + messageState.createMessageGroupResult + ); + + const clearMessageGroupResult = () => setCreateMessageGroupResult(null); + + return { messages, setMessages, messageGroups, createMessageGroupResult, clearMessageGroupResult }; }; diff --git a/phone/src/os/debug/InjectDebugData.ts b/phone/src/os/debug/InjectDebugData.ts index d2a342f..87ac94f 100644 --- a/phone/src/os/debug/InjectDebugData.ts +++ b/phone/src/os/debug/InjectDebugData.ts @@ -27,7 +27,7 @@ interface DebugEvent { * } * ]) **/ -const InjectDebugData = (events: DebugEvent[]) => { +const InjectDebugData = (events: DebugEvent[], timer = 1000) => { if (process.env.NODE_ENV === 'development') { for (const event of events) { setTimeout(() => { @@ -40,7 +40,7 @@ const InjectDebugData = (events: DebugEvent[]) => { }, }) ); - }, 1000); + }, timer); } } }; diff --git a/resources/server/sv_messages.ts b/resources/server/sv_messages.ts index 2aa079b..01167c5 100644 --- a/resources/server/sv_messages.ts +++ b/resources/server/sv_messages.ts @@ -97,6 +97,48 @@ async function getMessageGroups( return results; } +/** + * Retrieve message group by id (hashed of phoneNumbers) and userIdentifier + * @param groupId - Message Group Hashed ID + * @param userIdentifier - identifier of the user to get message groups for + */ +async function getMessageGroup( + groupId: string + userIdentifier: string +): Promise { + 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.group_id = ? + AND ( + npwd_messages_groups.user_identifier = ? + OR 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, [ + groupId, + userIdentifier, + userIdentifier, + ]); + return results; +} + /** * Retrieve all messages associated with a group and add a field * "isMine" which determines if the message belongs to the user @@ -298,6 +340,24 @@ async function getFormattedMessageGroups( }); } +/** + * Create the same unique ID from an identifiers array. + * They will be always be sorted to ensure always the same ID. + * @param identifiers array of player identifiers + */ +function createGroupHashID(identifiers: string[]) { + // 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! + return md5(mergedIdentifiers); +} + /** * Main method to handle creation of new message groups. First * we retrieve identifiers for each submitted phone number and @@ -335,18 +395,9 @@ async function createMessageGroupsFromPhoneNumbers( 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); + const groupId = createGroupHashID(identifiers); if (await checkIfMessageGroupExists(groupId)) { - return { error: true, duplicate: true }; + return { error: false, duplicate: true, groupId }; } const queryPromises = [ From de952dc555bd36432d0299fb5bf1cd50f4d1ff54 Mon Sep 17 00:00:00 2001 From: J Francisco Rader Date: Sun, 14 Feb 2021 19:15:29 -0500 Subject: [PATCH 2/6] Remove shit --- resources/server/sv_messages.ts | 42 --------------------------------- 1 file changed, 42 deletions(-) diff --git a/resources/server/sv_messages.ts b/resources/server/sv_messages.ts index 01167c5..cfdaac1 100644 --- a/resources/server/sv_messages.ts +++ b/resources/server/sv_messages.ts @@ -97,48 +97,6 @@ async function getMessageGroups( return results; } -/** - * Retrieve message group by id (hashed of phoneNumbers) and userIdentifier - * @param groupId - Message Group Hashed ID - * @param userIdentifier - identifier of the user to get message groups for - */ -async function getMessageGroup( - groupId: string - userIdentifier: string -): Promise { - 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.group_id = ? - AND ( - npwd_messages_groups.user_identifier = ? - OR 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, [ - groupId, - userIdentifier, - userIdentifier, - ]); - return results; -} - /** * Retrieve all messages associated with a group and add a field * "isMine" which determines if the message belongs to the user From c1f72c77c5b7a15df9723d430cfd08db09b0c1c3 Mon Sep 17 00:00:00 2001 From: J Francisco Rader Date: Sun, 14 Feb 2021 19:18:06 -0500 Subject: [PATCH 3/6] fix new msg alone --- .../apps/messages/components/form/NewMessageGroupForm.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/phone/src/apps/messages/components/form/NewMessageGroupForm.tsx b/phone/src/apps/messages/components/form/NewMessageGroupForm.tsx index ca924e4..8e38c16 100644 --- a/phone/src/apps/messages/components/form/NewMessageGroupForm.tsx +++ b/phone/src/apps/messages/components/form/NewMessageGroupForm.tsx @@ -17,10 +17,10 @@ const NewMessageGroupForm = ({ phoneNumber }: { phoneNumber?: string }) => { const classes = useStyles(); const history = useHistory(); const { t } = useTranslation(); - const [participants, setParticipants] = useState(phoneNumber || ''); + const [participants, setParticipants] = useState(''); const [label, setLabel] = useState(''); - useEffect(() => setParticipants(phoneNumber), [phoneNumber]); + useEffect(() => phoneNumber && setParticipants(phoneNumber), [phoneNumber]); // handles phone numbers in a csv format and strips all spaces and // external characters out of them: @@ -53,7 +53,7 @@ const NewMessageGroupForm = ({ phoneNumber }: { phoneNumber?: string }) => {
setParticipants(e.target.value)} + onChange={(e) => setParticipants(e.target.value || '')} placeholder={t('APPS_MESSAGES_NEW_MESSAGE_GROUP')} className={classes.newGroupinput} autoFocus From be479d75cc7d03ff77c013cb23cc16291a67bac7 Mon Sep 17 00:00:00 2001 From: J Francisco Rader Date: Tue, 16 Feb 2021 15:52:56 -0500 Subject: [PATCH 4/6] Fix possibly\ --- .../components/form/NewMessageGroupForm.tsx | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/phone/src/apps/messages/components/form/NewMessageGroupForm.tsx b/phone/src/apps/messages/components/form/NewMessageGroupForm.tsx index 8e38c16..f41bd2f 100644 --- a/phone/src/apps/messages/components/form/NewMessageGroupForm.tsx +++ b/phone/src/apps/messages/components/form/NewMessageGroupForm.tsx @@ -1,4 +1,4 @@ -import React, { FormEvent, useEffect, useMemo, useState } from 'react'; +import React, { FormEvent, useCallback, useEffect, useMemo, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { TextField, Button } from '@material-ui/core'; @@ -8,7 +8,6 @@ import { useHistory } from 'react-router-dom'; const sendNuiCreateMessageGroup = ({ parts, isGroupChat, labelValue }) => Nui.send('phone:createMessageGroup', { - asd: true, phoneNumbers: parts, label: isGroupChat && labelValue ? labelValue : null, }); @@ -25,20 +24,23 @@ const NewMessageGroupForm = ({ phoneNumber }: { phoneNumber?: string }) => { // handles phone numbers in a csv format and strips all spaces and // external characters out of them: // 123-4567, 987-6543, 333-4444 - const parts = useMemo( - () => participants.split(',').map((part) => part.replace(/[^0-9]/g, '')), - [participants] + const getGroupParts = useCallback( + (numbers) => numbers.split(',').map((part) => part.replace(/[^0-9]/g, '')), + [] ); + + const parts = useMemo(() => getGroupParts(participants), [participants, getGroupParts]); + const isGroupChat = parts.length > 1; const labelValue = label.trim(); useEffect(() => { - if (phoneNumber && parts) { - sendNuiCreateMessageGroup({ parts, isGroupChat, labelValue }); + if (phoneNumber) { + sendNuiCreateMessageGroup({ parts: getGroupParts(phoneNumber), isGroupChat: false, labelValue }); history.push('/messages'); } - }, [isGroupChat, parts, phoneNumber, labelValue, history]); + }, [phoneNumber, labelValue, history, getGroupParts]); const handleSubmit = (event: FormEvent) => { event.preventDefault(); From 4f69ae2266e9315fb6f3da2187a3de127b8e6b24 Mon Sep 17 00:00:00 2001 From: Christopher Date: Tue, 16 Feb 2021 23:32:27 +0100 Subject: [PATCH 5/6] fix(phone/messages): change how we get source | remove useEffect for parts --- .../src/apps/messages/components/form/NewMessageGroupForm.tsx | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/phone/src/apps/messages/components/form/NewMessageGroupForm.tsx b/phone/src/apps/messages/components/form/NewMessageGroupForm.tsx index f41bd2f..0fb6fd3 100644 --- a/phone/src/apps/messages/components/form/NewMessageGroupForm.tsx +++ b/phone/src/apps/messages/components/form/NewMessageGroupForm.tsx @@ -19,8 +19,6 @@ const NewMessageGroupForm = ({ phoneNumber }: { phoneNumber?: string }) => { const [participants, setParticipants] = useState(''); const [label, setLabel] = useState(''); - useEffect(() => phoneNumber && setParticipants(phoneNumber), [phoneNumber]); - // handles phone numbers in a csv format and strips all spaces and // external characters out of them: // 123-4567, 987-6543, 333-4444 @@ -37,7 +35,7 @@ const NewMessageGroupForm = ({ phoneNumber }: { phoneNumber?: string }) => { useEffect(() => { if (phoneNumber) { - sendNuiCreateMessageGroup({ parts: getGroupParts(phoneNumber), isGroupChat: false, labelValue }); + sendNuiCreateMessageGroup({ parts: getGroupParts(phoneNumber), isGroupChat: false, labelValue: null }); history.push('/messages'); } }, [phoneNumber, labelValue, history, getGroupParts]); From 15d7d3dceaed60cbb29d9dd29266608931243cec Mon Sep 17 00:00:00 2001 From: Christopher Date: Tue, 16 Feb 2021 23:32:27 +0100 Subject: [PATCH 6/6] fix(phone/messages): change how we get source | remove useEffect for parts --- .../apps/messages/components/form/NewMessageGroupForm.tsx | 4 +--- resources/server/sv_messages.ts | 7 ++++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/phone/src/apps/messages/components/form/NewMessageGroupForm.tsx b/phone/src/apps/messages/components/form/NewMessageGroupForm.tsx index f41bd2f..0fb6fd3 100644 --- a/phone/src/apps/messages/components/form/NewMessageGroupForm.tsx +++ b/phone/src/apps/messages/components/form/NewMessageGroupForm.tsx @@ -19,8 +19,6 @@ const NewMessageGroupForm = ({ phoneNumber }: { phoneNumber?: string }) => { const [participants, setParticipants] = useState(''); const [label, setLabel] = useState(''); - useEffect(() => phoneNumber && setParticipants(phoneNumber), [phoneNumber]); - // handles phone numbers in a csv format and strips all spaces and // external characters out of them: // 123-4567, 987-6543, 333-4444 @@ -37,7 +35,7 @@ const NewMessageGroupForm = ({ phoneNumber }: { phoneNumber?: string }) => { useEffect(() => { if (phoneNumber) { - sendNuiCreateMessageGroup({ parts: getGroupParts(phoneNumber), isGroupChat: false, labelValue }); + sendNuiCreateMessageGroup({ parts: getGroupParts(phoneNumber), isGroupChat: false, labelValue: null }); history.push('/messages'); } }, [phoneNumber, labelValue, history, getGroupParts]); diff --git a/resources/server/sv_messages.ts b/resources/server/sv_messages.ts index cfdaac1..3eaf77d 100644 --- a/resources/server/sv_messages.ts +++ b/resources/server/sv_messages.ts @@ -402,6 +402,7 @@ onNet(events.MESSAGES_FETCH_MESSAGE_GROUPS, async () => { onNet( events.MESSAGES_CREATE_MESSAGE_GROUP, async (phoneNumbers: string[], label: string = null) => { + const pSource = (global as any).source; try { const _identifier = await useIdentifier(); const result = await createMessageGroupsFromPhoneNumbers( @@ -413,18 +414,18 @@ onNet( if (result.error) { emitNet( events.MESSAGES_CREATE_MESSAGE_GROUP_FAILED, - getSource(), + pSource, result ); } else { emitNet( events.MESSAGES_CREATE_MESSAGE_GROUP_SUCCESS, - getSource(), + pSource, result ); } } catch (e) { - emitNet(events.MESSAGES_CREATE_MESSAGE_GROUP_FAILED, getSource()); + emitNet(events.MESSAGES_CREATE_MESSAGE_GROUP_FAILED, pSource); messageLogger.error(`Failed to create message group, ${e.message}`); } }