replies working, need to add root post ID
This commit is contained in:
58
static/db.js
58
static/db.js
@@ -356,6 +356,64 @@ export async function getAllIds(userID) {
|
||||
};
|
||||
});
|
||||
}
|
||||
export async function buildReplyCountMap() {
|
||||
const children = new Map();
|
||||
const knownUsers = [...(await indexedDB.databases())]
|
||||
.map(db => db.name?.replace('user_', '')).filter(Boolean);
|
||||
for (const userID of knownUsers) {
|
||||
try {
|
||||
const { store } = await getDBTransactionStore(userID);
|
||||
const index = store.index("postReplyIndex");
|
||||
const replies = await new Promise((resolve, reject) => {
|
||||
const req = index.getAll();
|
||||
req.onsuccess = () => resolve(req.result);
|
||||
req.onerror = () => reject(req.error);
|
||||
});
|
||||
for (const r of replies) {
|
||||
const parentID = r.data.reply_to_id;
|
||||
const replyID = r.data.post_id;
|
||||
if (parentID && replyID) {
|
||||
if (!children.has(parentID))
|
||||
children.set(parentID, []);
|
||||
children.get(parentID).push(replyID);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (_) { }
|
||||
}
|
||||
const direct = new Map();
|
||||
for (const [parentID, kids] of children) {
|
||||
direct.set(parentID, kids.length);
|
||||
}
|
||||
const countDescendants = (postID) => {
|
||||
const kids = children.get(postID) ?? [];
|
||||
return kids.reduce((sum, kid) => sum + 1 + countDescendants(kid), 0);
|
||||
};
|
||||
const recursive = new Map();
|
||||
for (const postID of children.keys()) {
|
||||
recursive.set(postID, countDescendants(postID));
|
||||
}
|
||||
return { direct, recursive };
|
||||
}
|
||||
export async function getRepliesForPost(postID) {
|
||||
const knownUsers = [...(await indexedDB.databases())]
|
||||
.map(db => db.name?.replace('user_', '')).filter(Boolean);
|
||||
let replies = [];
|
||||
for (const userID of knownUsers) {
|
||||
try {
|
||||
const { store } = await getDBTransactionStore(userID);
|
||||
const index = store.index("postReplyIndex");
|
||||
const results = await new Promise((resolve, reject) => {
|
||||
const req = index.getAll(postID);
|
||||
req.onsuccess = () => resolve(req.result);
|
||||
req.onerror = () => reject(req.error);
|
||||
});
|
||||
replies = replies.concat(results);
|
||||
}
|
||||
catch (_) { }
|
||||
}
|
||||
return replies;
|
||||
}
|
||||
export async function getPostsByIds(userID, postIDs) {
|
||||
const { store } = await getDBTransactionStore(userID);
|
||||
const index = store.index("postIDIndex");
|
||||
|
||||
Reference in New Issue
Block a user