channel -> group

This commit is contained in:
erik-sn committed 2020-11-19 14:05:29 -05:00
1 parent 956070951a
commit 79857fd52a
5 files changed
+21 -22

No files matched your search

@@ -12,8 +12,8 @@ const MessagesList = (): any => {
<List className={classes.root}>
{messages.map((messageGroup) => (
<ListItem
key={messageGroup.channelId}
onClick={() => setActiveMessageGroupId(messageGroup.channelId)}
key={messageGroup.groupId}
onClick={() => setActiveMessageGroupId(messageGroup.groupId)}
divider
button
>
@@ -21,7 +21,7 @@ const MessagesList = (): any => {
<MuiAvatar />
</ListItemAvatar>
<ListItemText>
{messageGroup.channelDisplay}
{messageGroup.groupDisplay}
</ListItemText>
</ListItem>
))}
@@ -25,7 +25,7 @@ export const MessageModal = () => {
const closeModal = () => setActiveMessageGroupId(null);
const isOpen = activeMessageGroupId !== null;
const activeMessageGroup = messages.find(messageGroup => messageGroup.channelId === activeMessageGroupId);
const activeMessageGroup = messages.find(messageGroup => messageGroup.groupId === activeMessageGroupId);
const activeMessages = activeMessageGroup ? activeMessageGroup.messages : [];
return (
Whitespace-only changes.
+3 -3
View File
@@ -2,7 +2,7 @@ export interface Message {
id?: number;
content: string;
user_identifier: string;
channel_id: number;
group_id: number;
isRead: boolean;
isMine: boolean;
avatar?: string;
@@ -13,9 +13,9 @@ export interface Message {
}
export interface MessageGroup {
channelId: string;
groupId: string;
messages: Message[];
channelDisplay: string;
groupDisplay: string;
createdAt?: string;
updatedAt?: string;
}
+14 -15
View File
@@ -26,11 +26,11 @@ async function getMessages(userIdentifier: string): Promise<Message[]> {
npwd_phone_contacts.display,
npwd_phone_contacts.avatar
FROM (
SELECT channel_id
SELECT group_id
FROM npwd_messages_groups
WHERE user_identifier = ?
) as t
LEFT OUTER JOIN npwd_messages on t.channel_id = npwd_messages.channel_id
LEFT OUTER JOIN npwd_messages on t.group_id = npwd_messages.group_id
LEFT OUTER JOIN users on users.identifier = npwd_messages.user_identifier
LEFT OUTER JOIN npwd_phone_contacts on npwd_phone_contacts.number = users.phone_number
WHERE (users.phone_number IS NOT NULL OR npwd_phone_contacts.display IS NOT NULL)
@@ -46,17 +46,17 @@ async function getMessages(userIdentifier: string): Promise<Message[]> {
}));
}
async function groupMessagesByChannel(useIdentifier: string): Promise<GroupMapping> {
async function groupMessagesByGroup(useIdentifier: string): Promise<GroupMapping> {
const messages = await getMessages(useIdentifier);
return messages.reduce((mapping: GroupMapping, message: Message) => {
const channelId = message.channel_id;
if (channelId in mapping) {
const groupId = message.group_id;
if (groupId in mapping) {
// we sort by updatedAt ASCENDING in the SQL query so we can always assume the
// first participant added has the lastest updatedAt. Therefore we don't
// need to updatedAt here
mapping[channelId].messages = mapping[channelId].messages.concat(message);
mapping[groupId].messages = mapping[groupId].messages.concat(message);
} else {
mapping[channelId] = {
mapping[groupId] = {
messages: [message],
updatedAt: message.updatedAt,
createdAt: message.createdAt,
@@ -66,7 +66,7 @@ async function groupMessagesByChannel(useIdentifier: string): Promise<GroupMappi
}, {});
}
function getChannelDisplayName(userIdentifier: string, messages: Message[]): string {
function getGroupDisplayName(userIdentifier: string, messages: Message[]): string {
return messages.reduce((displayTerms, message) => {
if (message.user_identifier === userIdentifier) return displayTerms; // don't display our name
@@ -76,14 +76,13 @@ function getChannelDisplayName(userIdentifier: string, messages: Message[]): str
}
async function getFormattedMessages(userIdentifier: string): Promise<MessageGroup[]> {
const channelMapping = await groupMessagesByChannel(userIdentifier);
const channels = Object.keys(channelMapping);
return channels.map(channel => {
console.log(getChannelDisplayName(userIdentifier, channelMapping[channel].messages))
const groupMapping = await groupMessagesByGroup(userIdentifier);
const groups = Object.keys(groupMapping);
return groups.map(group => {
return {
...channelMapping[channel],
channelId: channel,
channelDisplay: getChannelDisplayName(userIdentifier, channelMapping[channel].messages)
...groupMapping[group],
groupId: group,
groupDisplay: getGroupDisplayName(userIdentifier, groupMapping[group].messages)
}
});
}