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 = [