nodebb-plugin-undelete-users
Advanced tools
Comparing version 1.0.3 to 1.0.4
@@ -22,3 +22,3 @@ 'use strict'; | ||
async function getEvents(query, eventData = []) { | ||
async function getEvents(query) { | ||
const EVENT_TYPE = 'user-deleteAccount'; | ||
@@ -42,22 +42,31 @@ | ||
const [eventCount, newEventData] = await Promise.all([ | ||
db.sortedSetCount(`events:time:${EVENT_TYPE}`, from || '-inf', to), | ||
events.getEvents(EVENT_TYPE, start, stop, from || '-inf', to), | ||
]); | ||
eventData = eventData.concat(newEventData); | ||
eventData.forEach((event) => { | ||
event.data = JSON.parse(event.jsonString); | ||
event.hide = filterEvent(event, query); | ||
}); | ||
hideDuplicateUids(eventData); | ||
await hideExistingUids(eventData); | ||
await addContentCounts(eventData); | ||
let eventData = []; | ||
let eventCount = 0; | ||
const seenUids = Object.create(null); | ||
const itemsLeft = eventCount - (ITEMS_PER_PAGE * page); | ||
const visibleEventCount = eventData.filter(ev => !ev.hide).length; | ||
if (visibleEventCount < ITEMS_PER_PAGE && itemsLeft > 0) { | ||
query.page = page + 1; | ||
return getEvents(query, eventData); | ||
if (hasSearchFilter(query)) { | ||
let pageStart = 0; | ||
let done = false; | ||
do { | ||
const pageStop = pageStart + 499; | ||
let pageEvents = await events.getEvents(EVENT_TYPE, pageStart, pageStop, from || '-inf', to); | ||
let filteredEvents = pageEvents.filter(e => !filterEvent(e, query)); | ||
filteredEvents = hideDuplicateUids(filteredEvents, seenUids); | ||
filteredEvents = await hideExistingUids(filteredEvents); | ||
eventData.push(...filteredEvents); | ||
const eventsOnPage = eventData.slice(start, stop + 1); | ||
done = (eventsOnPage.length >= ITEMS_PER_PAGE || !pageEvents.length); | ||
pageStart += ITEMS_PER_PAGE; | ||
} while (!done); | ||
eventCount = eventData.length; | ||
eventData = eventData.slice(start, stop + 1); | ||
} else { | ||
eventData = await events.getEvents(EVENT_TYPE, start, stop, from || '-inf', to); | ||
eventCount = await db.sortedSetCount(`events:time:${EVENT_TYPE}`, from || '-inf', to || '+inf'); | ||
eventData = hideDuplicateUids(eventData, seenUids); | ||
eventData = await hideExistingUids(eventData); | ||
} | ||
await addContentCounts(eventData); | ||
return { | ||
@@ -68,19 +77,22 @@ events: eventData, | ||
query, | ||
visibleEventCount, | ||
}; | ||
} | ||
function hasSearchFilter(query) { | ||
return query && (query.username || query.email || query.uid); | ||
} | ||
function filterEvent(event, { username, uid, email }) { | ||
return (username && event.data.username && !event.data.username.toLowerCase().includes(username)) || | ||
(uid && parseInt(event.data.targetUid, 10) !== parseInt(uid, 10)) || | ||
(email && event.data.email && !event.data.email.toLowerCase().includes(email)); | ||
return (username && event.username && !event.username.toLowerCase().includes(username)) || | ||
(uid && parseInt(event.targetUid, 10) !== parseInt(uid, 10)) || | ||
(email && event.email && !event.email.toLowerCase().includes(email)); | ||
} | ||
function hideDuplicateUids(events) { | ||
const uids = events.map(ev => ev.data.targetUid); | ||
events.forEach((ev, idx) => { | ||
const duplicate = uids.indexOf(ev.data.targetUid) !== idx; | ||
if (duplicate && !ev.hide && !events[idx].hide) { | ||
ev.hide = true; | ||
function hideDuplicateUids(events, seenUids) { | ||
return events.filter((e) => { | ||
if (e && e.targetUid && !seenUids[e.targetUid]) { | ||
seenUids[e.targetUid] = true; | ||
return true; | ||
} | ||
return false; | ||
}); | ||
@@ -90,8 +102,5 @@ } | ||
async function hideExistingUids(events) { | ||
const uids = events.filter(ev => !ev.hide).map(ev => ev.data.targetUid); | ||
const isMembers = await db.isSortedSetMembers('users:joindate', uids); | ||
const existingUids = uids.filter((uid, idx) => isMembers[idx]); | ||
events.forEach((ev) => { | ||
ev.hide = ev.hide || existingUids.includes(ev.data.targetUid); | ||
}); | ||
const targetUids = events.map(e => e.targetUid); | ||
const isMembers = await db.isSortedSetMembers('users:joindate', targetUids); | ||
return events.filter((e, idx) => !isMembers[idx]); | ||
} | ||
@@ -102,4 +111,3 @@ | ||
for (const ev of events) { | ||
/* eslint-disable no-await-in-loop */ | ||
await Promise.all(events.map(async (ev) => { | ||
const [postCount, reputation, topicCount] = await getContentCountsFromCids(cids, ev.targetUid); | ||
@@ -109,3 +117,3 @@ ev.postCount = postCount; | ||
ev.topicCount = topicCount; | ||
} | ||
})); | ||
} | ||
@@ -112,0 +120,0 @@ |
{ | ||
"name": "nodebb-plugin-undelete-users", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "A NodeBB plugin to re-associate deleted users with their content that are not removed.", | ||
@@ -5,0 +5,0 @@ "main": "library.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19822
395