working multi-chat between groups

This commit is contained in:
erik-sn committed 2020-11-21 14:23:40 -05:00
1 parent 8191a9431c
commit 8009c073db
4 files changed
+52 -22

No files matched your search

@@ -1,25 +1,20 @@
import React from "react"; import React from "react";
import { Paper, Typography } from "@material-ui/core"; import { Paper, Typography } from "@material-ui/core";
import { Message } from "../../../../common/interfaces/messages"; import { Message, MessageGroup } from "../../../../common/interfaces/messages";
import MessageInput from "../form/MessageInput"; import MessageInput from "../form/MessageInput";
import useStyles from "./modal.styles"; import useStyles from "./modal.styles";
interface IProps { interface IProps {
activeMessageGroupId: string | undefined; activeMessageGroup: MessageGroup;
messages: Message[]; messages: Message[];
} }
export const CONVERSATION_ELEMENT_ID = "message-modal-conversation"; export const CONVERSATION_ELEMENT_ID = "message-modal-conversation";
const Conversation = ({ activeMessageGroupId, messages }: IProps) => { const Conversation = ({ activeMessageGroup, messages }: IProps) => {
const classes = useStyles(); const classes = useStyles();
const uniqueAuthors = Array.from(
new Set(messages.map((message) => message.display))
);
const isGroupChat = uniqueAuthors.length > 2;
return ( return (
<> <>
<div id={CONVERSATION_ELEMENT_ID} className={classes.messageList}> <div id={CONVERSATION_ELEMENT_ID} className={classes.messageList}>
@@ -31,7 +26,7 @@ const Conversation = ({ activeMessageGroupId, messages }: IProps) => {
> >
<div>{message.message}</div> <div>{message.message}</div>
<Typography variant="subtitle1" color="secondary"> <Typography variant="subtitle1" color="secondary">
{isGroupChat && !message.isMine {activeMessageGroup.isGroupChat && !message.isMine
? message.display || message.phone_number ? message.display || message.phone_number
: null} : null}
</Typography> </Typography>
@@ -39,7 +34,7 @@ const Conversation = ({ activeMessageGroupId, messages }: IProps) => {
</div> </div>
))} ))}
</div> </div>
<MessageInput messageGroupId={activeMessageGroupId} /> <MessageInput messageGroupId={activeMessageGroup.groupId} />
</> </>
); );
}; };
@@ -90,7 +90,7 @@ export const MessageModal = () => {
{messages && hasLoaded ? ( {messages && hasLoaded ? (
<Conversation <Conversation
messages={messages} messages={messages}
activeMessageGroupId={activeMessageGroup?.groupId} activeMessageGroup={activeMessageGroup}
/> />
) : ( ) : (
<MessageSkeletonList /> <MessageSkeletonList />
+1
View File
@@ -13,6 +13,7 @@ export interface Message {
export interface MessageGroup { export interface MessageGroup {
groupId: string; groupId: string;
groupDisplay: string; groupDisplay: string;
isGroupChat: boolean;
avatar?: string; avatar?: string;
label?: string; label?: string;
updatedAt: string; updatedAt: string;
+45 -11
View File
@@ -10,6 +10,7 @@ import { getSource, useIdentifier } from './functions';
*/ */
interface UnformattedMessageGroup { interface UnformattedMessageGroup {
group_id: string; group_id: string;
user_identifier: string;
participant_identifier: string; participant_identifier: string;
phone_number: string; phone_number: string;
label?: string; label?: string;
@@ -24,6 +25,7 @@ interface UnformattedMessageGroup {
*/ */
interface MessageGroupMapping { interface MessageGroupMapping {
[groupId: string]: { [groupId: string]: {
user_identifier: string;
participants: string[]; participants: string[];
label?: string; label?: string;
avatar?: string; avatar?: string;
@@ -61,18 +63,24 @@ async function getMessageGroups(userIdentifier: string): Promise<UnformattedMess
SELECT SELECT
npwd_messages_groups.group_id, npwd_messages_groups.group_id,
npwd_messages_groups.participant_identifier, npwd_messages_groups.participant_identifier,
npwd_messages_groups.user_identifier,
npwd_messages_labels.label, npwd_messages_labels.label,
users.phone_number, users.phone_number,
npwd_phone_contacts.avatar, npwd_phone_contacts.avatar,
npwd_phone_contacts.display npwd_phone_contacts.display
FROM npwd_messages_groups FROM (
SELECT group_id
FROM npwd_messages_groups
WHERE npwd_messages_groups.participant_identifier = ?
) as t
LEFT OUTER JOIN npwd_messages_groups on npwd_messages_groups.group_id = t.group_id
LEFT OUTER JOIN users on users.identifier = npwd_messages_groups.participant_identifier 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_messages_labels on npwd_messages_labels.group_id = npwd_messages_groups.group_id
LEFT OUTER JOIN npwd_phone_contacts on npwd_phone_contacts.number = users.phone_number 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 = ? WHERE npwd_messages_groups.participant_identifier != ?
ORDER BY npwd_messages_groups.createdAt DESC ORDER BY npwd_messages_groups.createdAt DESC
` `
const [results] = await pool.query(query, [ userIdentifier, userIdentifier]); const [results] = await pool.query(query, [ userIdentifier, userIdentifier, userIdentifier ]);
return <UnformattedMessageGroup[]>results; return <UnformattedMessageGroup[]>results;
} }
@@ -96,11 +104,11 @@ async function getMessages(userIdentifier: string, groupId: string): Promise<Mes
npwd_phone_contacts.avatar npwd_phone_contacts.avatar
FROM npwd_messages FROM npwd_messages
LEFT OUTER JOIN users on users.identifier = npwd_messages.user_identifier LEFT OUTER JOIN users on users.identifier = npwd_messages.user_identifier
LEFT OUTER JOIN npwd_phone_contacts on REGEXP_REPLACE(npwd_phone_contacts.number, '[^0-9]', '') = REGEXP_REPLACE(users.phone_number, '[^0-9]', '') 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.group_id = ? WHERE npwd_messages.group_id = ?
ORDER BY createdAt ASC; ORDER BY createdAt ASC;
`; `;
const [results] = await pool.query(query, [ groupId]); const [results] = await pool.query(query, [ userIdentifier, groupId ]);
const messages = <Message[]>results; const messages = <Message[]>results;
return messages.map(message => ({ return messages.map(message => ({
...message, ...message,
@@ -180,14 +188,25 @@ async function checkIfMessageGroupExists(groupId: string): Promise<boolean> {
return count > 0; return count > 0;
} }
async function getMessageCountByGroup(groupId: string): Promise<number> {
const query = `
SELECT COUNT(*) as count
FROM npwd_messages
WHERE group_id = ?;
`;
const [results] = await pool.query(query, [ groupId]);
const result = <any>results;
return result[0].count;
}
/** /**
* Consolidate raw message groups into a mapping that groups participants * Consolidate raw message groups into a mapping that groups participants
* by the message group. The goal of this is to reduce the rows of message * by the message group. The goal of this is to reduce the rows of message
* groups into a single MessageGroup object. * groups into a single MessageGroup object.
* @param useIdentifier - the user identifier to get message groups for * @param userIdentifier - the user identifier to get message groups for
*/ */
async function getConsolidatedMessageGroups(useIdentifier: string): Promise<MessageGroupMapping> { async function getConsolidatedMessageGroups(userIdentifier: string): Promise<MessageGroupMapping> {
const messageGroups = await getMessageGroups(useIdentifier); const messageGroups = await getMessageGroups(userIdentifier);
return messageGroups.reduce((mapping: MessageGroupMapping, messageGroup: UnformattedMessageGroup) => { return messageGroups.reduce((mapping: MessageGroupMapping, messageGroup: UnformattedMessageGroup) => {
const groupId = messageGroup.group_id; const groupId = messageGroup.group_id;
const displayTerm = messageGroup.display || messageGroup.phone_number || '???'; const displayTerm = messageGroup.display || messageGroup.phone_number || '???';
@@ -196,9 +215,10 @@ async function getConsolidatedMessageGroups(useIdentifier: string): Promise<Mess
mapping[groupId].participants = mapping[groupId].participants.concat(displayTerm) mapping[groupId].participants = mapping[groupId].participants.concat(displayTerm)
} else { } else {
mapping[groupId] = { mapping[groupId] = {
user_identifier: messageGroup.user_identifier,
avatar: messageGroup.avatar,
label: messageGroup.label, label: messageGroup.label,
participants: [displayTerm], participants: [displayTerm],
avatar: messageGroup.avatar,
updatedAt: messageGroup.updatedAt ? messageGroup.updatedAt.toString() : null updatedAt: messageGroup.updatedAt ? messageGroup.updatedAt.toString() : null
} }
} }
@@ -206,6 +226,17 @@ async function getConsolidatedMessageGroups(useIdentifier: string): Promise<Mess
}, {}); }, {});
} }
async function getGroupIds(userIdentifier: string, groupMapping: MessageGroupMapping): Promise<string[]> {
const groupIds: string[] = [];
for (const groupId of Object.keys(groupMapping)) {
const isMine = groupMapping[groupId].user_identifier === userIdentifier
if (isMine || await getMessageCountByGroup(groupId) > 0) {
groupIds.push(groupId)
}
}
return groupIds;
}
/** /**
* Given the mapping of groupId: fields contents reduce it into a list * Given the mapping of groupId: fields contents reduce it into a list
* of MessageGroup objects ready for the UI to consume * of MessageGroup objects ready for the UI to consume
@@ -213,13 +244,16 @@ async function getConsolidatedMessageGroups(useIdentifier: string): Promise<Mess
*/ */
async function getFormattedMessageGroups(userIdentifier: string): Promise<MessageGroup[]> { async function getFormattedMessageGroups(userIdentifier: string): Promise<MessageGroup[]> {
const groupMapping = await getConsolidatedMessageGroups(userIdentifier); const groupMapping = await getConsolidatedMessageGroups(userIdentifier);
const groupIds = Object.keys(groupMapping); const groupIds = await getGroupIds(userIdentifier, groupMapping);
return groupIds.map(groupId => { return groupIds.map(groupId => {
const group = groupMapping[groupId]; const group = groupMapping[groupId];
return { return {
...group, ...group,
groupId, groupId,
groupDisplay: group.participants.join(', '), groupDisplay: group.participants.join(', '),
// note that 1 here references how many participants besides the user
isGroupChat: group.participants.length > 1,
} }
}); });
} }