mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-05 06:28:51 +00:00
Merge branch 'master' of https://github.com/project-error/new-phone-who-dis
This commit is contained in:
commit
31241c7e8f
9 files changed
+56
-21
No files matched your search
@@ -1,17 +1,19 @@
|
|||||||
import React, { useCallback, useEffect, useState } from 'react';
|
import React, { useCallback, useEffect, useState } from 'react';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import { Box, Button, TextField } from '@material-ui/core';
|
import { Box, Button, TextField } from '@material-ui/core';
|
||||||
|
|
||||||
import Nui from '../../../../os/nui-events/utils/Nui';
|
import Nui from '../../../../os/nui-events/utils/Nui';
|
||||||
import { useHistory } from 'react-router-dom';
|
import { useHistory } from 'react-router-dom';
|
||||||
import { Autocomplete } from '@material-ui/lab';
|
import { Autocomplete } from '@material-ui/lab';
|
||||||
import { useContacts } from '../../../contacts/hooks/useContacts';
|
import { useContacts } from '../../../contacts/hooks/useContacts';
|
||||||
import { Contact } from '../../../../../../typings/contact';
|
import { Contact } from '../../../../../../typings/contact';
|
||||||
import { MessageEvents } from '../../../../../../typings/messages';
|
import { MessageEvents } from '../../../../../../typings/messages';
|
||||||
|
import { PHONE_NUMBER_REGEX } from '../../../../../../typings/phone';
|
||||||
|
import { useSnackbar } from '../../../../ui/hooks/useSnackbar';
|
||||||
|
|
||||||
const NewMessageGroupForm = ({ phoneNumber }: { phoneNumber?: string }) => {
|
const NewMessageGroupForm = ({ phoneNumber }: { phoneNumber?: string }) => {
|
||||||
const history = useHistory();
|
const history = useHistory();
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
const { addAlert } = useSnackbar();
|
||||||
const [participants, setParticipants] = useState([]);
|
const [participants, setParticipants] = useState([]);
|
||||||
const [label, setLabel] = useState('');
|
const [label, setLabel] = useState('');
|
||||||
const { getContactByNumber, contacts } = useContacts();
|
const { getContactByNumber, contacts } = useContacts();
|
||||||
@@ -45,6 +47,10 @@ const NewMessageGroupForm = ({ phoneNumber }: { phoneNumber?: string }) => {
|
|||||||
const onAutocompleteChange = (_e, value: any) => {
|
const onAutocompleteChange = (_e, value: any) => {
|
||||||
const lastIdx = value.length - 1;
|
const lastIdx = value.length - 1;
|
||||||
if (typeof value[lastIdx] === 'string') {
|
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] });
|
value.splice(lastIdx, 1, { number: value[lastIdx] });
|
||||||
setParticipants(value);
|
setParticipants(value);
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import { useCallback } from 'react';
|
|||||||
import Nui from '../../../os/nui-events/utils/Nui';
|
import Nui from '../../../os/nui-events/utils/Nui';
|
||||||
import { useLocation } from 'react-router';
|
import { useLocation } from 'react-router';
|
||||||
import { MessageEvents } from '../../../../../typings/messages';
|
import { MessageEvents } from '../../../../../typings/messages';
|
||||||
|
import { TOptionsBase } from 'i18next';
|
||||||
|
|
||||||
export const useMessagesService = () => {
|
export const useMessagesService = () => {
|
||||||
const { pathname } = useLocation();
|
const { pathname } = useLocation();
|
||||||
@@ -22,9 +23,9 @@ export const useMessagesService = () => {
|
|||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
|
||||||
const handleAddAlert = useCallback(
|
const handleAddAlert = useCallback(
|
||||||
({ message, type }: IAlert) => {
|
({ message, type, options = {} }: IAlert & { options: TOptionsBase }) => {
|
||||||
addAlert({
|
addAlert({
|
||||||
message: t(`APPS_${message}`),
|
message: t(`${message}`, options),
|
||||||
type,
|
type,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -145,6 +145,9 @@
|
|||||||
"APPS_MESSAGES_MESSAGE_GROUP_DUPLICATE": "This message group already exists",
|
"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_FAILED": "Failed to create message group",
|
||||||
"APPS_MESSAGES_MESSAGE_GROUP_CREATE_MINE": "You cannot add yourself!",
|
"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_INPUT_NAME_OR_NUMBER": "Search contacts or type number and press enter",
|
||||||
"APPS_MESSAGES_UNREAD_MESSAGES": "You have {{ count }} unread messages",
|
"APPS_MESSAGES_UNREAD_MESSAGES": "You have {{ count }} unread messages",
|
||||||
"APPS_MESSAGES_MEMBERS_TITLE": "Members",
|
"APPS_MESSAGES_MEMBERS_TITLE": "Members",
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ export interface IAlert {
|
|||||||
type: AlertType;
|
type: AlertType;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type AlertType = 'success' | 'error' | 'info';
|
export type AlertType = 'success' | 'error' | 'info' | 'warning';
|
||||||
|
|
||||||
interface ISnackBar {
|
interface ISnackBar {
|
||||||
addAlert: ({ message, type }: IAlert) => void;
|
addAlert: ({ message, type }: IAlert) => void;
|
||||||
|
|||||||
@@ -47,9 +47,9 @@ export async function getIdentifierByPhoneNumber(
|
|||||||
* Returns the player phoneNumber for a passed identifier
|
* Returns the player phoneNumber for a passed identifier
|
||||||
* @param identifier The players phone number
|
* @param identifier The players phone number
|
||||||
*/
|
*/
|
||||||
export function getPlayerFromIdentifier(identifier: string) {
|
export function getPlayerFromIdentifier(identifier: string): Player | null {
|
||||||
const player = PlayersByIdentifier.get(identifier);
|
const player = PlayersByIdentifier.get(identifier);
|
||||||
if (!player) mainLogger.error(`Could not find player from identifier: ${identifier}`);
|
if (!player) return null;
|
||||||
return player;
|
return player;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -356,19 +356,29 @@ async function createMessageGroupsFromPhoneNumbers(
|
|||||||
): Promise<CreateMessageGroupResult> {
|
): Promise<CreateMessageGroupResult> {
|
||||||
// we check that each phoneNumber exists before we create the group
|
// we check that each phoneNumber exists before we create the group
|
||||||
const identifiers: string[] = [];
|
const identifiers: string[] = [];
|
||||||
|
const failedNumbers: string[] = [];
|
||||||
|
|
||||||
for (const phoneNumber of phoneNumbers) {
|
for (const phoneNumber of phoneNumbers) {
|
||||||
|
try {
|
||||||
const identifier = await getIdentifierFromPhoneNumber(phoneNumber);
|
const identifier = await getIdentifierFromPhoneNumber(phoneNumber);
|
||||||
identifiers.push(identifier);
|
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
|
// don't allow explicitly adding yourself
|
||||||
if (identifiers.some((identifier) => identifier === userIdentifier)) {
|
if (identifiers.some((identifier) => identifier === userIdentifier)) {
|
||||||
return { error: true, mine: true };
|
return { error: true, mine: true, failedNumbers };
|
||||||
}
|
}
|
||||||
|
|
||||||
const groupId = createGroupHashID([userIdentifier, ...identifiers]);
|
const groupId = createGroupHashID([userIdentifier, ...identifiers]);
|
||||||
if (await checkIfMessageGroupExists(groupId)) {
|
if (await checkIfMessageGroupExists(groupId)) {
|
||||||
return { error: false, duplicate: true, groupId, identifiers };
|
return { error: false, duplicate: true, groupId, identifiers, failedNumbers };
|
||||||
}
|
}
|
||||||
|
|
||||||
const queryPromises = [
|
const queryPromises = [
|
||||||
@@ -388,7 +398,7 @@ async function createMessageGroupsFromPhoneNumbers(
|
|||||||
// group exist while other are left off.
|
// group exist while other are left off.
|
||||||
await withTransaction(queryPromises);
|
await withTransaction(queryPromises);
|
||||||
|
|
||||||
return { error: false, groupId, identifiers };
|
return { error: false, groupId, identifiers, failedNumbers };
|
||||||
}
|
}
|
||||||
|
|
||||||
// getting the participants from groupId.
|
// getting the participants from groupId.
|
||||||
@@ -426,9 +436,10 @@ onNet(MessageEvents.CREATE_MESSAGE_GROUP, async (phoneNumbers: string[], label:
|
|||||||
const _identifier = getIdentifier(_source);
|
const _identifier = getIdentifier(_source);
|
||||||
const result = await createMessageGroupsFromPhoneNumbers(_identifier, phoneNumbers, label);
|
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, {
|
return emitNet(MessageEvents.ACTION_RESULT, _source, {
|
||||||
message: 'MESSAGES_MESSAGE_GROUP_DUPLICATE',
|
message: `APPS_MESSAGES_MESSAGE_GROUP_CREATE_ALL_NUMBERS_FAILED`,
|
||||||
type: 'error',
|
type: 'error',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -436,11 +447,27 @@ onNet(MessageEvents.CREATE_MESSAGE_GROUP, async (phoneNumbers: string[], label:
|
|||||||
if (result.error) {
|
if (result.error) {
|
||||||
emitNet(MessageEvents.CREATE_MESSAGE_GROUP_FAILED, _source, result);
|
emitNet(MessageEvents.CREATE_MESSAGE_GROUP_FAILED, _source, result);
|
||||||
return emitNet(MessageEvents.ACTION_RESULT, _source, {
|
return emitNet(MessageEvents.ACTION_RESULT, _source, {
|
||||||
message: `MESSAGES_MESSAGE_GROUP_CREATE_FAILED:`,
|
message: `APPS_MESSAGES_MESSAGE_GROUP_CREATE_FAILED`,
|
||||||
type: 'error',
|
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);
|
emitNet(MessageEvents.CREATE_MESSAGE_GROUP_SUCCESS, _source, result);
|
||||||
if (result.duplicate) {
|
if (result.duplicate) {
|
||||||
return;
|
return;
|
||||||
@@ -461,7 +488,7 @@ onNet(MessageEvents.CREATE_MESSAGE_GROUP, async (phoneNumbers: string[], label:
|
|||||||
} catch (e) {
|
} catch (e) {
|
||||||
emitNet(MessageEvents.CREATE_MESSAGE_GROUP_FAILED, _source);
|
emitNet(MessageEvents.CREATE_MESSAGE_GROUP_FAILED, _source);
|
||||||
emitNet(MessageEvents.ACTION_RESULT, _source, {
|
emitNet(MessageEvents.ACTION_RESULT, _source, {
|
||||||
message: `MESSAGES_MESSAGE_GROUP_CREATE_FAILED:`,
|
message: `APPS_MESSAGES_MESSAGE_GROUP_CREATE_FAILED`,
|
||||||
type: 'error',
|
type: 'error',
|
||||||
});
|
});
|
||||||
messageLogger.error(`Failed to create message group, ${e.message}`, {
|
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) {
|
} 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, {
|
emitNet(MessageEvents.ACTION_RESULT, _source, {
|
||||||
message: 'MESSAGES_NEW_MESSAGE_FAILED',
|
message: 'APPS_MESSAGES_NEW_MESSAGE_FAILED',
|
||||||
type: 'error',
|
type: 'error',
|
||||||
});
|
});
|
||||||
messageLogger.error(`Failed to send message, ${e.message}`, {
|
messageLogger.error(`Failed to send message, ${e.message}`, {
|
||||||
|
|||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
export interface IAlertProps {
|
export interface IAlertProps {
|
||||||
message: string;
|
message: string;
|
||||||
type: 'success' | 'error';
|
type: 'success' | 'error' | 'info' | 'warning';
|
||||||
}
|
}
|
||||||
@@ -30,6 +30,8 @@ export interface CreateMessageGroupResult {
|
|||||||
groupId?: string;
|
groupId?: string;
|
||||||
mine?: boolean;
|
mine?: boolean;
|
||||||
identifiers?: string[];
|
identifiers?: string[];
|
||||||
|
allNumbersFailed?: boolean;
|
||||||
|
failedNumbers: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface CreateMessageBroadcast {
|
export interface CreateMessageBroadcast {
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
export const PHONE_NUMBER_REGEX = /^([0-9]{3}-[0-9]{4})|([0-9]{7})$/;
|
||||||
|
|
||||||
export enum PhoneEvents {
|
export enum PhoneEvents {
|
||||||
PLAYER_IS_READY = 'npwd:playerIsReady',
|
PLAYER_IS_READY = 'npwd:playerIsReady',
|
||||||
OPEN_APP_CONTACTS = 'npwd:app:CONTACTS',
|
OPEN_APP_CONTACTS = 'npwd:app:CONTACTS',
|
||||||
|
|||||||
Reference in new issue
Block a user