in src/views/inbox-threads/inbox-threads-helper.ts [97:141]
function mergeThreads(threads: InboxThread[]): InboxThread[] {
const subscriptionThreads: InboxThread[] = threads.filter((it: InboxThread) => getThreadTypeData(it).isSubscription);
const directThreads: InboxThread[] = threads.filter((it: InboxThread) => !getThreadTypeData(it).isSubscription);
let mergedSubscriptionsThreads: InboxThread[] = subscriptionThreads;
const sMap: Record<string, InboxThread> = {};
const dMap: Record<string, InboxThread> = directThreads.reduce(
(akk: Record<string, InboxThread>, it: InboxThread) => {
const iTarget: InboxThreadTarget = it.subject.target;
const iEntity = iTarget?.issue || iTarget?.article || iTarget;
const curr = akk[iEntity.id];
if (curr) {
curr.messages = curr.messages.concat(it.messages);
} else {
akk[iEntity.id] = it;
}
return akk;
}, {});
if (directThreads.length > 0) {
mergedSubscriptionsThreads = subscriptionThreads.reduce((akk: InboxThread[], it: InboxThread) => {
const target: InboxThreadTarget = it.subject.target;
const entity = target?.issue || target?.article || target;
sMap[entity.id] = it;
const direct: InboxThread | undefined = dMap[entity?.id];
const inboxThreadMessages = it.messages.slice(0);
const messages = direct ? [...inboxThreadMessages, ...direct.messages].sort(sortByTimestamp).reverse() : inboxThreadMessages;
return [...akk, {
...it,
messages,
lastTimestamp: it.messages.slice(0, 1)[0].timestamp,
}];
}, [] as InboxThread[]);
}
const orphans = Object.keys(dMap).reduce((akk: (InboxThread & {lastTimestamp: number})[], id: string) => {
if (!sMap[id]) {
akk.push({...dMap[id], lastTimestamp: dMap[id].notified});
}
return akk;
}, []);
return mergedSubscriptionsThreads.concat(orphans).sort((a, b) => {
return doSortBy(a, b, 'lastTimestamp', true);
});
}