This commit is contained in:
Christopher committed 2021-03-30 16:06:04 +02:00
commit 31241c7e8f
9 files changed
+58 -23

No files matched your search

@@ -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;
@@ -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,
});
},
+3
View File
@@ -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",
+1 -1
View File
@@ -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;
+2 -2
View File
@@ -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;
}
+37 -16
View File
@@ -356,19 +356,29 @@ async function createMessageGroupsFromPhoneNumbers(
): Promise<CreateMessageGroupResult> {
// 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}`, {
+1 -1
View File
@@ -1,4 +1,4 @@
export interface IAlertProps {
message: string;
type: 'success' | 'error';
type: 'success' | 'error' | 'info' | 'warning';
}
+2
View File
@@ -30,6 +30,8 @@ export interface CreateMessageGroupResult {
groupId?: string;
mine?: boolean;
identifiers?: string[];
allNumbersFailed?: boolean;
failedNumbers: string[];
}
export interface CreateMessageBroadcast {
+2
View File
@@ -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',