improve duplicate activities handle

This commit is contained in:
SrGooglo 2025-02-05 02:51:18 +00:00
parent 9d0afcb329
commit 3990ef45c9

View File

@ -29,7 +29,7 @@ export default {
const type = IdToTypes[id] const type = IdToTypes[id]
// get latest 20 activities // get latest 20 activities
const latestActivities = await RecentActivity.find({ let latestActivities = await RecentActivity.find({
user_id: user_id, user_id: user_id,
type: type, type: type,
}) })
@ -37,12 +37,15 @@ export default {
.sort({ created_at: -1 }) .sort({ created_at: -1 })
// check if the activity is already in some position and remove // check if the activity is already in some position and remove
latestActivities.find((activity, index) => { const sameLatestActivityIndex = latestActivities.findIndex((activity) => {
if (activity.payload === payload && activity.type === type) { return activity.payload === payload && activity.type === type
latestActivities.splice(index, 1)
}
}) })
// if the activity is already in some position, remove it from that position
if (sameLatestActivityIndex !== -1) {
latestActivities.splice(sameLatestActivityIndex, 1)
}
// if the list is full, remove the oldest activity and add the new one // if the list is full, remove the oldest activity and add the new one
if (latestActivities.length >= 20) { if (latestActivities.length >= 20) {
await RecentActivity.findByIdAndDelete(latestActivities[latestActivities.length - 1]._id) await RecentActivity.findByIdAndDelete(latestActivities[latestActivities.length - 1]._id)