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:
J Francisco Rader authored and GitHub committed 2021-03-18 23:01:54 -03:00
1 parent a2c0f94bd8
commit 0ec2b682e7
12 files changed
+132 -69

No files matched your search

+49 -20
View File
@@ -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,