fix(post ids): fall back to comment cid for missing ids (#1167)

* fix(post ids): fall back to comment cid for missing ids

* fix(post ids): count cid-resolved account replies
This commit is contained in:
Tommaso Casaburi
2026-06-09 19:08:02 +07:00
committed by GitHub
parent 9de2e6a851
commit c0f1a77958
8 changed files with 208 additions and 68 deletions
@@ -27,13 +27,14 @@ describe('getThreadPostCountsByAuthor', () => {
expect(counts.get('author-b')).toBe(2);
});
it('skips comments missing a cid or short address', () => {
it('uses cid fallback for comments without author metadata', () => {
const counts = getThreadPostCountsByAuthor(
{ cid: 'post-1', author: { shortAddress: 'author-a' } } as any,
[{ cid: 'reply-1' }, { author: { shortAddress: 'author-b' } }] as any[],
);
expect(counts.get('author-a')).toBe(1);
expect(counts.get('reply-1')).toBe(1);
expect(counts.has('author-b')).toBe(false);
});
});
+5 -4
View File
@@ -1,4 +1,5 @@
import { Comment } from '@bitsocial/bitsocial-react-hooks';
import type { Comment } from '@bitsocial/bitsocial-react-hooks';
import { getCommentUserID } from './comment-user-id-utils';
export function getThreadPostCountsByAuthor(post: Comment | undefined, replies: Comment[] = []): Map<string, number> {
const counts = new Map<string, number>();
@@ -6,11 +7,11 @@ export function getThreadPostCountsByAuthor(post: Comment | undefined, replies:
for (const comment of [post, ...replies]) {
const cid = comment?.cid;
const shortAddress = comment?.author?.shortAddress;
if (!cid || !shortAddress || seenCids.has(cid)) continue;
const userID = getCommentUserID(comment);
if (!cid || !userID || seenCids.has(cid)) continue;
seenCids.add(cid);
counts.set(shortAddress, (counts.get(shortAddress) ?? 0) + 1);
counts.set(userID, (counts.get(userID) ?? 0) + 1);
}
return counts;
+7
View File
@@ -0,0 +1,7 @@
import type { Comment } from '@bitsocial/bitsocial-react-hooks';
import getShortAddress from '../get-short-address';
export function getCommentUserID(comment: Comment | undefined): string {
const { address, shortAddress } = comment?.author || {};
return (address ? getShortAddress(address) : '') || shortAddress || comment?.cid || '';
}