mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-03 06:28:51 +00:00
Messages bugfix again :D (#164)
* Handle offline messages | Fetch messages when broadcasted | Fetch message groups * Fetch messages on notification click * Dont fetch groups if duplicate group * Only submit new message on click submit * Fix infinite loading when duplicated group is creatded * Fix infinite loading when group already existed * Make code prettier * Centralize fetch messages on goToConversation * Remove duplicated event * Improve message groups mapping to include yourself for unreadCount + Fix redirection after adding contact + General improvements * Remove conflicts * Fix media paths
This commit is contained in:
1 parent
a2c0f94bd8
commit
0ec2b682e7
12 files changed
+132
-69
No files matched your search
+2
-2
@@ -80,14 +80,14 @@ function Phone() {
|
||||
<div
|
||||
className="PhoneFrame"
|
||||
style={{
|
||||
backgroundImage: `url(./media/frames/${settings.frame.value})`,
|
||||
backgroundImage: `url(media/frames/${settings.frame.value})`,
|
||||
}}
|
||||
/>
|
||||
<div
|
||||
id="phone"
|
||||
className="PhoneScreen"
|
||||
style={{
|
||||
backgroundImage: `url(./media/backgrounds/${settings.wallpaper.value})`,
|
||||
backgroundImage: `url(media/backgrounds/${settings.wallpaper.value})`,
|
||||
}}
|
||||
>
|
||||
{isPhoneReady ? (
|
||||
|
||||
@@ -16,7 +16,7 @@ export const ExampleApp = () => {
|
||||
const exampleString = useExample();
|
||||
const example = useApp('EXAMPLE');
|
||||
const [soundName, setSound] = useState('pixel.ogg');
|
||||
const { play, playing, stop } = useSound('./media/ringtones/' + soundName);
|
||||
const { play, playing, stop } = useSound('media/ringtones/' + soundName);
|
||||
|
||||
const toggleSound = () => {
|
||||
if (soundName === 'pixel.ogg') {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import React, { FormEvent, useCallback, useEffect, useState } from 'react';
|
||||
import React, { useCallback, useEffect, useState } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { Box, Button, TextField } from '@material-ui/core';
|
||||
|
||||
@@ -24,27 +24,22 @@ const NewMessageGroupForm = ({ phoneNumber }: { phoneNumber?: string }) => {
|
||||
|
||||
const isGroupChat = participants.length > 1;
|
||||
|
||||
const handleSubmit = useCallback(
|
||||
(event: FormEvent) => {
|
||||
event.preventDefault();
|
||||
const handleSubmit = useCallback(() => {
|
||||
// handles phone numbers in a csv format and strips all spaces and
|
||||
// external characters out of them:
|
||||
// 123-4567, 987-6543, 333-4444
|
||||
const phoneNumbers = participants.map(({ number }) => number.replace(/[^0-9]/g, ''));
|
||||
const labelValue = isGroupChat ? label.trim() : null;
|
||||
|
||||
// handles phone numbers in a csv format and strips all spaces and
|
||||
// external characters out of them:
|
||||
// 123-4567, 987-6543, 333-4444
|
||||
const phoneNumbers = participants.map(({ number }) => number.replace(/[^0-9]/g, ''));
|
||||
const labelValue = isGroupChat ? label.trim() : null;
|
||||
|
||||
if (phoneNumbers.length) {
|
||||
Nui.send('phone:createMessageGroup', {
|
||||
phoneNumbers,
|
||||
label: labelValue,
|
||||
});
|
||||
setParticipants([]);
|
||||
history.push('/messages');
|
||||
}
|
||||
},
|
||||
[history, label, participants, isGroupChat],
|
||||
);
|
||||
if (phoneNumbers.length) {
|
||||
Nui.send('phone:createMessageGroup', {
|
||||
phoneNumbers,
|
||||
label: labelValue,
|
||||
});
|
||||
setParticipants([]);
|
||||
history.push('/messages');
|
||||
}
|
||||
}, [history, label, participants, isGroupChat]);
|
||||
|
||||
const onAutocompleteChange = (_e, value: any) => {
|
||||
const lastIdx = value.length - 1;
|
||||
@@ -77,7 +72,7 @@ const NewMessageGroupForm = ({ phoneNumber }: { phoneNumber?: string }) => {
|
||||
const submitDisabled = !participants.length || (isGroupChat && !label);
|
||||
|
||||
return (
|
||||
<form onSubmit={handleSubmit}>
|
||||
<Box>
|
||||
<Box px={2} py={3}>
|
||||
<Autocomplete<Contact, boolean, boolean, boolean>
|
||||
value={participants}
|
||||
@@ -102,6 +97,7 @@ const NewMessageGroupForm = ({ phoneNumber }: { phoneNumber?: string }) => {
|
||||
)}
|
||||
<Box px={2} py={3}>
|
||||
<Button
|
||||
onClick={() => handleSubmit()}
|
||||
disabled={submitDisabled}
|
||||
variant="contained"
|
||||
fullWidth
|
||||
@@ -111,7 +107,7 @@ const NewMessageGroupForm = ({ phoneNumber }: { phoneNumber?: string }) => {
|
||||
{t('APPS_MESSAGES_NEW_MESSAGE_GROUP_SUBMIT')}
|
||||
</Button>
|
||||
</Box>
|
||||
</form>
|
||||
</Box>
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ const MessageGroupItem = ({ messageGroup, handleClick }: IProps): any => {
|
||||
<ListItem key={messageGroup.groupId} onClick={handleClick(messageGroup)} divider button>
|
||||
<ListItemAvatar>
|
||||
<Badge color="error" variant="dot" invisible={!hasUnread}>
|
||||
<MuiAvatar src={messageGroup?.avatar} />
|
||||
<MuiAvatar src={messageGroup.avatar} />
|
||||
</Badge>
|
||||
</ListItemAvatar>
|
||||
<ListItemText>{messageGroup.label || messageGroup.groupDisplay}</ListItemText>
|
||||
|
||||
@@ -15,7 +15,13 @@ const MessagesList = (): any => {
|
||||
const history = useHistory();
|
||||
const { t } = useTranslation();
|
||||
|
||||
const { messageGroups, createMessageGroupResult, clearMessageGroupResult } = useMessages();
|
||||
const {
|
||||
messageGroups,
|
||||
createMessageGroupResult,
|
||||
clearMessageGroupResult,
|
||||
getMessageGroupById,
|
||||
setActiveMessageGroup,
|
||||
} = useMessages();
|
||||
|
||||
const [searchValue, setSearchValue] = useState('');
|
||||
|
||||
@@ -32,18 +38,24 @@ const MessagesList = (): any => {
|
||||
|
||||
useEffect(() => {
|
||||
if (createMessageGroupResult?.groupId) {
|
||||
const findGroup = messageGroups.find((g) => g.groupId === createMessageGroupResult.groupId);
|
||||
const findGroup = getMessageGroupById(createMessageGroupResult.groupId);
|
||||
clearMessageGroupResult();
|
||||
if (findGroup) {
|
||||
goToConversation(findGroup, history);
|
||||
}
|
||||
}
|
||||
}, [messageGroups, createMessageGroupResult, clearMessageGroupResult, history]);
|
||||
}, [
|
||||
messageGroups,
|
||||
createMessageGroupResult,
|
||||
clearMessageGroupResult,
|
||||
history,
|
||||
setActiveMessageGroup,
|
||||
getMessageGroupById,
|
||||
]);
|
||||
|
||||
if (!messageGroups) return null;
|
||||
|
||||
const handleClick = (messageGroup: MessageGroup) => () => {
|
||||
Nui.send('phone:fetchMessages', { groupId: messageGroup.groupId });
|
||||
goToConversation(messageGroup, history);
|
||||
};
|
||||
|
||||
|
||||
@@ -77,7 +77,6 @@ export const MessageModal = () => {
|
||||
|
||||
useEffect(() => {
|
||||
if (!groupId) return;
|
||||
|
||||
setActiveMessageGroup(groupId);
|
||||
}, [groupId, setActiveMessageGroup]);
|
||||
|
||||
@@ -109,10 +108,11 @@ export const MessageModal = () => {
|
||||
|
||||
const handleAddContact = (number) => {
|
||||
const exists = getContactByNumber(number);
|
||||
const referal = encodeURIComponent(pathname);
|
||||
if (exists) {
|
||||
return history.push(`/contacts/${exists.id}/?referal=${pathname}`);
|
||||
return history.push(`/contacts/${exists.id}/?referal=${referal}`);
|
||||
}
|
||||
return history.push(`/contacts/-1/?addNumber=${number}&referal=${pathname}`);
|
||||
return history.push(`/contacts/-1/?addNumber=${number}&referal=${referal}`);
|
||||
};
|
||||
|
||||
const phoneNumbers = activeMessageGroup?.phoneNumbers || [];
|
||||
@@ -164,7 +164,7 @@ export const MessageModal = () => {
|
||||
) : null}
|
||||
</Box>
|
||||
</Paper>
|
||||
{isLoaded ? (
|
||||
{isLoaded && activeMessageGroup ? (
|
||||
<Conversation
|
||||
onClickDisplay={handleAddContact}
|
||||
messages={messages}
|
||||
|
||||
@@ -10,11 +10,7 @@ const NOTIFICATION_ID = 'messages:broadcast';
|
||||
export const useMessageNotifications = () => {
|
||||
const { t } = useTranslation();
|
||||
const history = useHistory();
|
||||
const {
|
||||
removeId,
|
||||
addNotification,
|
||||
addNotificationAlert,
|
||||
} = useNotifications();
|
||||
const { removeId, addNotification, addNotificationAlert } = useNotifications();
|
||||
const { icon, notificationIcon } = useApp('MESSAGES');
|
||||
const { getMessageGroupById } = useMessages();
|
||||
|
||||
|
||||
@@ -1,12 +1,16 @@
|
||||
import { useSetRecoilState } from 'recoil';
|
||||
import { useRecoilState, useSetRecoilState } from 'recoil';
|
||||
import { messageState } from './state';
|
||||
import { useNuiEvent } from '../../../os/nui-events/hooks/useNuiEvent';
|
||||
import { IAlert, useSnackbar } from '../../../ui/hooks/useSnackbar';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { useMessageNotifications } from './useMessageNotifications';
|
||||
import { useCallback } from 'react';
|
||||
import Nui from '../../../os/nui-events/utils/Nui';
|
||||
|
||||
export const useMessagesService = () => {
|
||||
const [activeMessageGroup, setActiveMessageGroup] = useRecoilState(
|
||||
messageState.activeMessageGroup,
|
||||
);
|
||||
const setMessageGroups = useSetRecoilState(messageState.messageGroups);
|
||||
const setMessages = useSetRecoilState(messageState.messages);
|
||||
const setCreateMessageGroupResult = useSetRecoilState(messageState.createMessageGroupResult);
|
||||
@@ -24,10 +28,32 @@ export const useMessagesService = () => {
|
||||
[addAlert, t],
|
||||
);
|
||||
|
||||
useNuiEvent('MESSAGES', 'phone:fetchMessageGroupsSuccess', setMessageGroups);
|
||||
const handleMessageBroadcast = useCallback(
|
||||
({ groupId, message }) => {
|
||||
if (groupId === activeMessageGroup?.groupId) {
|
||||
Nui.send('phone:fetchMessages', { groupId: activeMessageGroup.groupId });
|
||||
}
|
||||
setNotification({ groupId, message });
|
||||
},
|
||||
[activeMessageGroup, setNotification],
|
||||
);
|
||||
|
||||
const _setMessageGroups = useCallback(
|
||||
(groups) => {
|
||||
if (activeMessageGroup && activeMessageGroup.groupId) {
|
||||
setActiveMessageGroup(
|
||||
(curr) => groups.find((g) => g.groupId === activeMessageGroup.groupId) || curr,
|
||||
);
|
||||
}
|
||||
setMessageGroups(groups);
|
||||
},
|
||||
[activeMessageGroup, setActiveMessageGroup, setMessageGroups],
|
||||
);
|
||||
|
||||
useNuiEvent('MESSAGES', 'phone:fetchMessageGroupsSuccess', _setMessageGroups);
|
||||
useNuiEvent('MESSAGES', 'phone:fetchMessagesSuccess', setMessages);
|
||||
useNuiEvent('MESSAGES', 'phone:createMessageGroupSuccess', setCreateMessageGroupResult);
|
||||
useNuiEvent('MESSAGES', 'phone:createMessageGroupFailed', setCreateMessageGroupResult);
|
||||
useNuiEvent('MESSAGES', 'createMessagesBroadcast', setNotification);
|
||||
useNuiEvent('MESSAGES', 'createMessagesBroadcast', handleMessageBroadcast);
|
||||
useNuiEvent('MESSAGES', 'phone:setMessagesAlert', handleAddAlert);
|
||||
};
|
||||
@@ -1,2 +1,7 @@
|
||||
export const goToConversation = (messageGroup, history) =>
|
||||
import Nui from '../../../os/nui-events/utils/Nui';
|
||||
|
||||
export const goToConversation = (messageGroup, history) => {
|
||||
if (!messageGroup?.groupId || !history) return;
|
||||
history.push(`/messages/conversations/${messageGroup.groupId}`);
|
||||
Nui.send('phone:fetchMessages', { groupId: messageGroup.groupId });
|
||||
};
|
||||
@@ -1,4 +1,3 @@
|
||||
export const getRingtonePath = (name?: string) => `./media/ringtones/${name || 'pixel'}.ogg`;
|
||||
export const getRingtonePath = (name?: string) => `media/ringtones/${name || 'pixel'}.ogg`;
|
||||
|
||||
export const getNotificationPath = (name?: string) =>
|
||||
`./media/notifications/${name || 'online'}.ogg`;
|
||||
export const getNotificationPath = (name?: string) => `media/notifications/${name || 'online'}.ogg`;
|
||||
@@ -1,5 +1,5 @@
|
||||
export const IMG_DEFAULT_AVATAR = './media/twitter/egg.png';
|
||||
export const IMG_INVALID_AVATAR = './media/twitter/invalid.png';
|
||||
export const IMG_DEFAULT_AVATAR = 'media/twitter/egg.png';
|
||||
export const IMG_INVALID_AVATAR = 'media/twitter/invalid.png';
|
||||
export const APP_TWITTER = 'TWITTER';
|
||||
|
||||
export const SETTING_MENTIONS = 'mentions';
|
||||
|
||||
@@ -28,6 +28,7 @@ interface UnformattedMessageGroup {
|
||||
interface MessageGroupMapping {
|
||||
[groupId: string]: {
|
||||
user_identifier: string;
|
||||
// Participant displays
|
||||
participants: string[];
|
||||
phoneNumbers: string[];
|
||||
label?: string;
|
||||
@@ -97,7 +98,6 @@ async function getMessageGroups(userIdentifier: string): Promise<UnformattedMess
|
||||
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, [userIdentifier, userIdentifier, userIdentifier]);
|
||||
@@ -231,27 +231,45 @@ async function getConsolidatedMessageGroups(userIdentifier: string): Promise<Mes
|
||||
(mapping: MessageGroupMapping, messageGroup: UnformattedMessageGroup) => {
|
||||
const groupId = messageGroup.group_id;
|
||||
const displayTerm = messageGroup.display || messageGroup.phone_number || '???';
|
||||
|
||||
const isUser = messageGroup.participant_identifier === userIdentifier;
|
||||
// Is already mapped?
|
||||
if (groupId in mapping) {
|
||||
mapping[groupId].participants = mapping[groupId].participants.concat(displayTerm);
|
||||
// Add participant phone number
|
||||
mapping[groupId].phoneNumbers = mapping[groupId].phoneNumbers.concat(
|
||||
messageGroup.phone_number,
|
||||
);
|
||||
if (messageGroup.participant_identifier === userIdentifier) {
|
||||
if (isUser) {
|
||||
// Add unread count if its user's message group
|
||||
mapping[groupId].unreadCount = messageGroup.unreadCount;
|
||||
return mapping;
|
||||
}
|
||||
} else {
|
||||
// Add participant if its not user's message group
|
||||
mapping[groupId].participants = mapping[groupId].participants.concat(displayTerm);
|
||||
return mapping;
|
||||
}
|
||||
if (isUser) {
|
||||
// Group not mapped and its current user
|
||||
mapping[groupId] = {
|
||||
user_identifier: messageGroup.user_identifier,
|
||||
unreadCount:
|
||||
messageGroup.participant_identifier === userIdentifier ? messageGroup.unreadCount : 0,
|
||||
avatar: messageGroup.avatar,
|
||||
label: messageGroup.label,
|
||||
participants: [displayTerm],
|
||||
unreadCount: messageGroup.unreadCount,
|
||||
avatar: null,
|
||||
label: null,
|
||||
participants: [],
|
||||
phoneNumbers: [messageGroup.phone_number],
|
||||
updatedAt: messageGroup.updatedAt ? messageGroup.updatedAt.toString() : null,
|
||||
};
|
||||
return mapping;
|
||||
}
|
||||
// Group not mapped and its not current user
|
||||
mapping[groupId] = {
|
||||
user_identifier: messageGroup.user_identifier,
|
||||
unreadCount: 0,
|
||||
avatar: messageGroup.avatar,
|
||||
label: messageGroup.label,
|
||||
participants: [displayTerm],
|
||||
phoneNumbers: [messageGroup.phone_number],
|
||||
updatedAt: messageGroup.updatedAt ? messageGroup.updatedAt.toString() : null,
|
||||
};
|
||||
return mapping;
|
||||
},
|
||||
{},
|
||||
@@ -287,7 +305,7 @@ async function getFormattedMessageGroups(userIdentifier: string): Promise<Messag
|
||||
...group,
|
||||
groupId,
|
||||
groupDisplay: group.participants.join(', '),
|
||||
// note that 1 here references how many participants besides the user
|
||||
// note that the 1 here references how many participants besides the user
|
||||
isGroupChat: group.participants.length > 1,
|
||||
};
|
||||
});
|
||||
@@ -436,15 +454,21 @@ onNet(
|
||||
message: `MESSAGES_MESSAGE_GROUP_CREATE_FAILED:`,
|
||||
type: 'error',
|
||||
});
|
||||
}
|
||||
|
||||
emitNet(events.MESSAGES_CREATE_MESSAGE_GROUP_SUCCESS, _source, result);
|
||||
if (result.identifiers) {
|
||||
for (const participantId of result.identifiers) {
|
||||
// we don't broadcast to the source of the event.
|
||||
if (participantId !== _identifier) {
|
||||
const participantPlayer = getPlayerFromIdentifier(participantId);
|
||||
emitNet(events.MESSAGES_FETCH_MESSAGE_GROUPS, participantPlayer.source);
|
||||
} else {
|
||||
emitNet(events.MESSAGES_CREATE_MESSAGE_GROUP_SUCCESS, _source, result);
|
||||
if (result.duplicate) {
|
||||
return;
|
||||
}
|
||||
if (result.identifiers) {
|
||||
for (const participantId of result.identifiers) {
|
||||
// we don't broadcast to the source of the event.
|
||||
if (participantId !== _identifier) {
|
||||
const participantPlayer = getPlayerFromIdentifier(participantId);
|
||||
if (!participantPlayer) {
|
||||
return;
|
||||
}
|
||||
emitNet(events.MESSAGES_FETCH_MESSAGE_GROUPS, participantPlayer.source);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -493,6 +517,10 @@ onNet(events.MESSAGES_SEND_MESSAGE, async (groupId: string, message: string, gro
|
||||
// we don't broadcast to the source of the event.
|
||||
if (participantId !== _identifier) {
|
||||
const participantPlayer = getPlayerFromIdentifier(participantId);
|
||||
if (!participantPlayer) {
|
||||
return;
|
||||
}
|
||||
emitNet(events.MESSAGES_FETCH_MESSAGE_GROUPS, participantPlayer.source);
|
||||
emitNet(events.MESSAGES_CREATE_MESSAGE_BROADCAST, participantPlayer.source, {
|
||||
groupName,
|
||||
groupId,
|
||||
@@ -522,6 +550,7 @@ onNet(events.MESSAGES_SET_MESSAGE_READ, async (groupId: string) => {
|
||||
try {
|
||||
const identifier = getIdentifier(pSource);
|
||||
await setMessageRead(groupId, identifier);
|
||||
emitNet(events.MESSAGES_FETCH_MESSAGE_GROUPS, pSource);
|
||||
} catch (e) {
|
||||
messageLogger.error(`Failed to set message as read, ${e.message}`, {
|
||||
source: pSource,
|
||||
|
||||
Reference in new issue
Block a user