-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from sendbird/feat/AC-438/group-short-term-mes…
…sage feat: Merge sender profile when message is sent in short span of time
- Loading branch information
Showing
7 changed files
with
130 additions
and
31 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// eslint-disable-next-line import/no-unresolved | ||
import { EveryMessage } from 'SendbirdUIKitGlobal'; | ||
|
||
const TIME_SPAN = 3 * 60 * 1000; | ||
/** | ||
* Function to group messages based on their creation time | ||
* | ||
* @param {EveryMessage[]} messages - Array of messages to group | ||
* @returns {EveryMessage[]} - Array of messages grouped by creation time | ||
*/ | ||
export function groupMessagesByShortSpanTime( | ||
messages: EveryMessage[] | ||
): EveryMessage[] { | ||
// Create an object to group messages based on their creation time | ||
const groupedMessagesByCreatedAt = messages.reduce((groups, message) => { | ||
const { createdAt } = message; | ||
// Get the key of the previous group | ||
const prevKey = Object.keys(groups)[Object.keys(groups).length - 1]; | ||
|
||
// Check if the time difference between the current message and the previous one is within 3 minutes | ||
if (prevKey && message.createdAt - Number(prevKey) <= TIME_SPAN) { | ||
// Add the message to the existing group | ||
return { | ||
...groups, | ||
[prevKey]: [...(groups[prevKey] ?? []), message], | ||
}; | ||
} else { | ||
// Create a new group for the current message | ||
return { | ||
...groups, | ||
[createdAt]: [message], | ||
}; | ||
} | ||
}, {}); | ||
|
||
// Flatten the grouped messages and add chain indicators | ||
return Object.values(groupedMessagesByCreatedAt).flatMap( | ||
(messages: EveryMessage[]) => { | ||
if (messages.length > 1) { | ||
// Add chain indicators to the first and last messages in the group | ||
return messages.map((message, index) => ({ | ||
...message, | ||
chaintop: index === 0, | ||
chainBottom: index === messages.length - 1, | ||
})); | ||
} | ||
return messages; | ||
} | ||
); | ||
} |