diff --git a/phone/src/apps/messages/components/form/NewMessageGroupForm.tsx b/phone/src/apps/messages/components/form/NewMessageGroupForm.tsx index 04a2fdc..3a38987 100644 --- a/phone/src/apps/messages/components/form/NewMessageGroupForm.tsx +++ b/phone/src/apps/messages/components/form/NewMessageGroupForm.tsx @@ -1,17 +1,19 @@ import React, { useCallback, useEffect, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { Box, Button, TextField } from '@material-ui/core'; - import Nui from '../../../../os/nui-events/utils/Nui'; import { useHistory } from 'react-router-dom'; import { Autocomplete } from '@material-ui/lab'; import { useContacts } from '../../../contacts/hooks/useContacts'; import { Contact } from '../../../../../../typings/contact'; import { MessageEvents } from '../../../../../../typings/messages'; +import { PHONE_NUMBER_REGEX } from '../../../../../../typings/phone'; +import { useSnackbar } from '../../../../ui/hooks/useSnackbar'; const NewMessageGroupForm = ({ phoneNumber }: { phoneNumber?: string }) => { const history = useHistory(); const { t } = useTranslation(); + const { addAlert } = useSnackbar(); const [participants, setParticipants] = useState([]); const [label, setLabel] = useState(''); const { getContactByNumber, contacts } = useContacts(); @@ -45,6 +47,10 @@ const NewMessageGroupForm = ({ phoneNumber }: { phoneNumber?: string }) => { const onAutocompleteChange = (_e, value: any) => { const lastIdx = value.length - 1; if (typeof value[lastIdx] === 'string') { + const isValid = PHONE_NUMBER_REGEX.test(value[lastIdx]); + if (!isValid) { + return addAlert({ message: t('APPS_MESSAGES_INVALID_PHONE_NUMBER'), type: 'error' }); + } value.splice(lastIdx, 1, { number: value[lastIdx] }); setParticipants(value); return; diff --git a/phone/src/apps/messages/hooks/useMessageService.ts b/phone/src/apps/messages/hooks/useMessageService.ts index 7647f7d..d25ff72 100644 --- a/phone/src/apps/messages/hooks/useMessageService.ts +++ b/phone/src/apps/messages/hooks/useMessageService.ts @@ -8,6 +8,7 @@ import { useCallback } from 'react'; import Nui from '../../../os/nui-events/utils/Nui'; import { useLocation } from 'react-router'; import { MessageEvents } from '../../../../../typings/messages'; +import { TOptionsBase } from 'i18next'; export const useMessagesService = () => { const { pathname } = useLocation(); @@ -22,9 +23,9 @@ export const useMessagesService = () => { const { t } = useTranslation(); const handleAddAlert = useCallback( - ({ message, type }: IAlert) => { + ({ message, type, options = {} }: IAlert & { options: TOptionsBase }) => { addAlert({ - message: t(`APPS_${message}`), + message: t(`${message}`, options), type, }); }, diff --git a/phone/src/locale/en.json b/phone/src/locale/en.json index c6157e0..36d1d0d 100644 --- a/phone/src/locale/en.json +++ b/phone/src/locale/en.json @@ -145,6 +145,9 @@ "APPS_MESSAGES_MESSAGE_GROUP_DUPLICATE": "This message group already exists", "APPS_MESSAGES_MESSAGE_GROUP_CREATE_FAILED": "Failed to create message group", "APPS_MESSAGES_MESSAGE_GROUP_CREATE_MINE": "You cannot add yourself!", + "APPS_MESSAGES_MESSAGE_GROUP_CREATE_ALL_NUMBERS_FAILED": "None of the phone numbers could be found!", + "APPS_MESSAGES_MESSAGE_GROUP_CREATE_ONE_NUMBER_FAILED": "{{ number }} is not a registered number", + "APPS_MESSAGES_MESSAGE_GROUP_CREATE_SOME_NUMBERS_FAILED": "({{ numbers }}) are not registered numbers.", "APPS_MESSAGES_INPUT_NAME_OR_NUMBER": "Search contacts or type number and press enter", "APPS_MESSAGES_UNREAD_MESSAGES": "You have {{ count }} unread messages", "APPS_MESSAGES_MEMBERS_TITLE": "Members", diff --git a/phone/src/ui/hooks/useSnackbar.ts b/phone/src/ui/hooks/useSnackbar.ts index b693a5a..de087d6 100644 --- a/phone/src/ui/hooks/useSnackbar.ts +++ b/phone/src/ui/hooks/useSnackbar.ts @@ -6,7 +6,7 @@ export interface IAlert { type: AlertType; } -export type AlertType = 'success' | 'error' | 'info'; +export type AlertType = 'success' | 'error' | 'info' | 'warning'; interface ISnackBar { addAlert: ({ message, type }: IAlert) => void; diff --git a/resources/server/functions.ts b/resources/server/functions.ts index be06295..ab91bc8 100644 --- a/resources/server/functions.ts +++ b/resources/server/functions.ts @@ -47,9 +47,9 @@ export async function getIdentifierByPhoneNumber( * Returns the player phoneNumber for a passed identifier * @param identifier The players phone number */ -export function getPlayerFromIdentifier(identifier: string) { +export function getPlayerFromIdentifier(identifier: string): Player | null { const player = PlayersByIdentifier.get(identifier); - if (!player) mainLogger.error(`Could not find player from identifier: ${identifier}`); + if (!player) return null; return player; } diff --git a/resources/server/sv_messages.ts b/resources/server/sv_messages.ts index ad47407..bb411ef 100644 --- a/resources/server/sv_messages.ts +++ b/resources/server/sv_messages.ts @@ -356,19 +356,29 @@ async function createMessageGroupsFromPhoneNumbers( ): Promise { // we check that each phoneNumber exists before we create the group const identifiers: string[] = []; + const failedNumbers: string[] = []; + for (const phoneNumber of phoneNumbers) { - const identifier = await getIdentifierFromPhoneNumber(phoneNumber); - identifiers.push(identifier); + try { + const identifier = await getIdentifierFromPhoneNumber(phoneNumber); + identifiers.push(identifier); + } catch (e) { + failedNumbers.push(phoneNumber); + } + } + + if (phoneNumbers.length === failedNumbers.length) { + return { error: true, allNumbersFailed: true, failedNumbers }; } // don't allow explicitly adding yourself if (identifiers.some((identifier) => identifier === userIdentifier)) { - return { error: true, mine: true }; + return { error: true, mine: true, failedNumbers }; } const groupId = createGroupHashID([userIdentifier, ...identifiers]); if (await checkIfMessageGroupExists(groupId)) { - return { error: false, duplicate: true, groupId, identifiers }; + return { error: false, duplicate: true, groupId, identifiers, failedNumbers }; } const queryPromises = [ @@ -388,7 +398,7 @@ async function createMessageGroupsFromPhoneNumbers( // group exist while other are left off. await withTransaction(queryPromises); - return { error: false, groupId, identifiers }; + return { error: false, groupId, identifiers, failedNumbers }; } // getting the participants from groupId. @@ -426,9 +436,10 @@ onNet(MessageEvents.CREATE_MESSAGE_GROUP, async (phoneNumbers: string[], label: const _identifier = getIdentifier(_source); const result = await createMessageGroupsFromPhoneNumbers(_identifier, phoneNumbers, label); - if (result.error && result.duplicate) { + if (result.error && result.allNumbersFailed) { + emitNet(MessageEvents.CREATE_MESSAGE_GROUP_FAILED, _source, result); return emitNet(MessageEvents.ACTION_RESULT, _source, { - message: 'MESSAGES_MESSAGE_GROUP_DUPLICATE', + message: `APPS_MESSAGES_MESSAGE_GROUP_CREATE_ALL_NUMBERS_FAILED`, type: 'error', }); } @@ -436,11 +447,27 @@ onNet(MessageEvents.CREATE_MESSAGE_GROUP, async (phoneNumbers: string[], label: if (result.error) { emitNet(MessageEvents.CREATE_MESSAGE_GROUP_FAILED, _source, result); return emitNet(MessageEvents.ACTION_RESULT, _source, { - message: `MESSAGES_MESSAGE_GROUP_CREATE_FAILED:`, + message: `APPS_MESSAGES_MESSAGE_GROUP_CREATE_FAILED`, type: 'error', }); } + if (result.failedNumbers.length === 1) { + emitNet(MessageEvents.ACTION_RESULT, _source, { + message: `APPS_MESSAGES_MESSAGE_GROUP_CREATE_ONE_NUMBER_FAILED`, + type: 'warning', + options: { number: result.failedNumbers[0] }, + }); + } + + if (result.failedNumbers.length > 1) { + emitNet(MessageEvents.ACTION_RESULT, _source, { + message: `APPS_MESSAGES_MESSAGE_GROUP_CREATE_SOME_NUMBERS_FAILED`, + type: 'warning', + options: { numbers: result.failedNumbers.join(', ') }, + }); + } + emitNet(MessageEvents.CREATE_MESSAGE_GROUP_SUCCESS, _source, result); if (result.duplicate) { return; @@ -461,7 +488,7 @@ onNet(MessageEvents.CREATE_MESSAGE_GROUP, async (phoneNumbers: string[], label: } catch (e) { emitNet(MessageEvents.CREATE_MESSAGE_GROUP_FAILED, _source); emitNet(MessageEvents.ACTION_RESULT, _source, { - message: `MESSAGES_MESSAGE_GROUP_CREATE_FAILED:`, + message: `APPS_MESSAGES_MESSAGE_GROUP_CREATE_FAILED`, type: 'error', }); messageLogger.error(`Failed to create message group, ${e.message}`, { @@ -513,14 +540,8 @@ onNet(MessageEvents.SEND_MESSAGE, async (groupId: string, message: string, group } } } catch (e) { - // Not really sure what this does? As I cant find any reference - // of this in the ui part. - emitNet(MessageEvents.SEND_MESSAGE_FAILED, _source); - // sending a new alert - // The message property is always using the locale strings without - // the APPS_ prefix. emitNet(MessageEvents.ACTION_RESULT, _source, { - message: 'MESSAGES_NEW_MESSAGE_FAILED', + message: 'APPS_MESSAGES_NEW_MESSAGE_FAILED', type: 'error', }); messageLogger.error(`Failed to send message, ${e.message}`, { diff --git a/typings/alerts.ts b/typings/alerts.ts index d34e127..c992070 100644 --- a/typings/alerts.ts +++ b/typings/alerts.ts @@ -1,4 +1,4 @@ export interface IAlertProps { message: string; - type: 'success' | 'error'; + type: 'success' | 'error' | 'info' | 'warning'; } diff --git a/typings/messages.ts b/typings/messages.ts index a88cf9c..ae90b45 100644 --- a/typings/messages.ts +++ b/typings/messages.ts @@ -30,6 +30,8 @@ export interface CreateMessageGroupResult { groupId?: string; mine?: boolean; identifiers?: string[]; + allNumbersFailed?: boolean; + failedNumbers: string[]; } export interface CreateMessageBroadcast { diff --git a/typings/phone.ts b/typings/phone.ts index 2b818ef..a547dfa 100644 --- a/typings/phone.ts +++ b/typings/phone.ts @@ -1,3 +1,5 @@ +export const PHONE_NUMBER_REGEX = /^([0-9]{3}-[0-9]{4})|([0-9]{7})$/; + export enum PhoneEvents { PLAYER_IS_READY = 'npwd:playerIsReady', OPEN_APP_CONTACTS = 'npwd:app:CONTACTS',