fix(directories): support multisub public keys

This commit is contained in:
Tommaso Casaburi
2026-04-13 14:54:23 +07:00
parent 420c7207e1
commit f52545ec5b
7 changed files with 198 additions and 78 deletions
+43 -22
View File
@@ -2,10 +2,11 @@
"title": "/all/ - All 5chan Directories",
"description": "Each 5chan directory is assigned to a board handpicked by the team, until DAO curation is implemented.\n\nhttps://github.com/bitsocialnet/lists/blob/master/5chan-directories.json",
"createdAt": 1762179811,
"updatedAt": 1771140295,
"updatedAt": 1776065598,
"communities": [
{
"address": "anime-and-manga.bso",
"name": "anime-and-manga.bso",
"publicKey": "12D3KooWMDt3RMVMHrm2pEEyfraBtHP86dmkim7EVAapDj3Mchdy",
"title": "/a/ - Anime & Manga",
"directoryCode": "a",
"features": {
@@ -21,7 +22,8 @@
}
},
{
"address": "food-and-cooking.bso",
"name": "food-and-cooking.bso",
"publicKey": "12D3KooWFfANpSeemwvqqGsXNPHDuyDMCK6wE8cxGTuNw7xoWUp7",
"title": "/ck/ - Food & Cooking",
"directoryCode": "ck",
"features": {
@@ -37,7 +39,8 @@
}
},
{
"address": "politically-incorrect.bso",
"name": "politically-incorrect.bso",
"publicKey": "12D3KooWMVob74DQoTLGZ4B8kWgPwDfbtiWqdJE4DrGtf2rmVw36",
"title": "/pol/ - Politically Incorrect",
"directoryCode": "pol",
"features": {
@@ -53,7 +56,8 @@
}
},
{
"address": "business-and-finance.bso",
"name": "business-and-finance.bso",
"publicKey": "12D3KooWNMybS8JqELi38ZBX897PrjWbCrGoMKfw3bgoqzC2n1Dh",
"title": "/biz/ - Business & Finance",
"directoryCode": "biz",
"features": {
@@ -69,7 +73,8 @@
}
},
{
"address": "science-and-math.bso",
"name": "science-and-math.bso",
"publicKey": "12D3KooWEtHqwtEpgHsXS8VSFcGDt2DhzeqaLm9tTCRFucXaWp62",
"title": "/sci/ - Science & Math",
"directoryCode": "sci",
"features": {
@@ -85,7 +90,8 @@
}
},
{
"address": "technology-posting.bso",
"name": "technology-posting.bso",
"publicKey": "12D3KooWFnLrUYHpvqki7gbL4w9JzdxjpQPKE2JBDEd23Ly6X82X",
"title": "/g/ - Technology",
"directoryCode": "g",
"features": {
@@ -101,7 +107,8 @@
}
},
{
"address": "videogame-generals.bso",
"name": "videogame-generals.bso",
"publicKey": "12D3KooWMvAcJFV45CS3hSZcx7EFhwhQrXfXhVHXQJFPfKeakWfq",
"title": "/vg/ - Video Game Generals",
"directoryCode": "vg",
"features": {
@@ -117,7 +124,8 @@
}
},
{
"address": "fitness-posting.bso",
"name": "fitness-posting.bso",
"publicKey": "12D3KooWHcqcJJmjAiBJA3uyhSW6ep4HFadUfteDrzU1Pn9ALkkw",
"title": "/fit/ - Fitness",
"directoryCode": "fit",
"features": {
@@ -133,7 +141,8 @@
}
},
{
"address": "advice-posting.bso",
"name": "advice-posting.bso",
"publicKey": "12D3KooWH5UFNs9yfwJVsSUhEW5bYrtF8KmzfeJzwg84qWMoFJVY",
"title": "/adv/ - Advice",
"directoryCode": "adv",
"features": {
@@ -149,7 +158,8 @@
}
},
{
"address": "worksafe-gif.bso",
"name": "worksafe-gif.bso",
"publicKey": "12D3KooWAGHV9th2FN48KPvHMJwxen8h5mSM39pkaZWvvzcWMPr9",
"title": "/wsg/ - Worksafe GIF",
"directoryCode": "wsg",
"features": {
@@ -165,7 +175,8 @@
}
},
{
"address": "do-it-yourself.bso",
"name": "do-it-yourself.bso",
"publicKey": "12D3KooWKKbiaFu7x2EwueN6wxw3DJRQsvCv8KVRGbTTYpsGZXVa",
"title": "/diy/ - Do It Yourself",
"directoryCode": "diy",
"features": {
@@ -181,7 +192,8 @@
}
},
{
"address": "outdoors-posting.bso",
"name": "outdoors-posting.bso",
"publicKey": "12D3KooWQ2nPGxQxuSMY92kM3zKu1rBekJsXxXb5uNkHLDfSSxTm",
"title": "/out/ - Outdoors",
"directoryCode": "out",
"features": {
@@ -197,7 +209,8 @@
}
},
{
"address": "artwork-critique.bso",
"name": "artwork-critique.bso",
"publicKey": "12D3KooWQZ5HkAspHUPGEVoSaAZgqachZGtxCXxAXYBDTreR4uWJ",
"title": "/ic/ - Artwork/Critique",
"directoryCode": "ic",
"features": {
@@ -213,7 +226,8 @@
}
},
{
"address": "music-posting.bso",
"name": "music-posting.bso",
"publicKey": "12D3KooWQdQ6TkVA1Xe9zzaFP6vXBgsLeMAewpLpLwbsAYKivnQy",
"title": "/mu/ - Music",
"directoryCode": "mu",
"features": {
@@ -229,7 +243,8 @@
}
},
{
"address": "international-sfw.bso",
"name": "international-sfw.bso",
"publicKey": "12D3KooWMSUYcergS2BwbRzFynR5hQURc962W9iR81eTJKz89Q6n",
"title": "/int/ - International",
"directoryCode": "int",
"features": {
@@ -245,7 +260,8 @@
}
},
{
"address": "literature-posting.bso",
"name": "literature-posting.bso",
"publicKey": "12D3KooWKXimxiZWtgF3LoTdaW3btHhDzJN7P3qrsgHwRkow186x",
"title": "/lit/ - Literature",
"directoryCode": "lit",
"features": {
@@ -261,7 +277,8 @@
}
},
{
"address": "television-and-film.bso",
"name": "television-and-film.bso",
"publicKey": "12D3KooWGSRg4crzV5vXEya1ghXD7UBTqimqXTQvNUqy9NdzZNyd",
"title": "/tv/ - Television & Film",
"directoryCode": "tv",
"features": {
@@ -277,7 +294,8 @@
}
},
{
"address": "very-important-posts.bso",
"name": "very-important-posts.bso",
"publicKey": "12D3KooWNMnBJ5phkBWrcjfdnzdqPwCARmXwGWeZDKNJvCVPR8GT",
"title": "/vip/ - Very Important Posts",
"directoryCode": "vip",
"features": {
@@ -293,7 +311,8 @@
}
},
{
"address": "adult-gif.bso",
"name": "adult-gif.bso",
"publicKey": "12D3KooWJP8v5VUzKUfxr38BbhQPb5yidF3WapGQ6CjLctBV6QC1",
"title": "/gif/ - Adult GIF",
"directoryCode": "gif",
"features": {
@@ -309,7 +328,8 @@
}
},
{
"address": "international-nsfw.bso",
"name": "international-nsfw.bso",
"publicKey": "12D3KooWPuYtTzrq8gFnv6yM1r4egy1rd4cxLr1nsXTjfdYkfwcN",
"title": "/bant/ - International/Random",
"directoryCode": "bant",
"features": {
@@ -325,7 +345,8 @@
}
},
{
"address": "random-nsfw.bso",
"name": "random-nsfw.bso",
"publicKey": "12D3KooWJkJYkuVbJQhapcVGpo8oNmsXsKmFRrFqc1SHezZW9fX8",
"title": "/b/ - Random",
"directoryCode": "b",
"features": {
+17 -4
View File
@@ -117,7 +117,12 @@ describe('use-directories', () => {
it('normalizes aliases and finds matching directories by exact or alias address', () => {
const communities = [
{ address: 'music-posting.bso', title: '/mu/ - Music' },
{
address: 'music-posting.bso',
name: 'music-posting.bso',
publicKey: '12D3KooWQdQ6TkVA1Xe9zzaFP6vXBgsLeMAewpLpLwbsAYKivnQy',
title: '/mu/ - Music',
},
{ address: 'business.eth', title: '/biz/ - Business & Finance' },
];
@@ -127,6 +132,7 @@ describe('use-directories', () => {
expect(findDirectoryByAddress(communities, 'music-posting.bso')?.address).toBe('music-posting.bso');
expect(findDirectoryByAddress(communities, 'music-posting.eth')?.address).toBe('music-posting.bso');
expect(findDirectoryByAddress(communities, '12D3KooWQdQ6TkVA1Xe9zzaFP6vXBgsLeMAewpLpLwbsAYKivnQy')?.address).toBe('music-posting.bso');
expect(findDirectoryByAddress(communities, undefined)).toBeUndefined();
});
@@ -149,19 +155,22 @@ describe('use-directories', () => {
updatedAt: 4,
directories: [
{
communityAddress: 'music-posting.bso',
name: 'music-posting.bso',
publicKey: 'music-public-key',
title: '/mu/ - Music',
directoryCode: 'mu',
features: { safeForWork: true, postsPerPage: 25, nested: { ignore: true } },
},
{
communityAddress: 'flash.bso',
name: 'flash.bso',
publicKey: 'flash-public-key',
title: '/f/ - Flash',
directoryCode: 'f',
features: { nsfw: true, postsPerPage: 10 },
},
{
communityAddress: 'flash.bso',
name: 'flash.bso',
publicKey: 'flash-public-key',
title: '/f/ - Duplicate Flash',
directoryCode: 'f',
},
@@ -196,6 +205,8 @@ describe('use-directories', () => {
expect(latestSnapshot?.directories).toEqual([
{
address: 'music-posting.bso',
name: 'music-posting.bso',
publicKey: 'music-public-key',
title: '/mu/ - Music',
directoryCode: 'mu',
features: { safeForWork: true, postsPerPage: 25 },
@@ -203,6 +214,8 @@ describe('use-directories', () => {
},
{
address: 'flash.bso',
name: 'flash.bso',
publicKey: 'flash-public-key',
title: '/f/ - Flash',
directoryCode: 'f',
features: { nsfw: true, postsPerPage: 10 },
+66 -22
View File
@@ -23,6 +23,8 @@ export interface DirectoryFeatures {
export interface DirectoryCommunity {
title?: string;
address: string;
name?: string;
publicKey?: string;
nsfw?: boolean;
directoryCode?: string;
features?: DirectoryFeatures;
@@ -62,6 +64,8 @@ export const __resetDirectoriesModuleStateForTests = () => {
const isRecord = (value: unknown): value is Record<string, unknown> => typeof value === 'object' && value !== null;
const getStringValue = (...values: unknown[]): string | undefined => values.find((value): value is string => typeof value === 'string' && value.length > 0);
const normalizeFeatures = (value: unknown): DirectoryFeatures | undefined => {
if (!isRecord(value)) {
return undefined;
@@ -88,8 +92,22 @@ const deriveNsfw = (value: { nsfw?: unknown; features?: { nsfw?: boolean; safeFo
return topLevelNsfw ?? featuresNsfw;
};
const toCanonicalCommunity = (value: { address: unknown; title?: unknown; nsfw?: unknown; directoryCode?: unknown; features?: unknown }): DirectoryCommunity | null => {
if (typeof value.address !== 'string') {
const getDirectoryIdentifiers = (community: DirectoryCommunity): string[] => [
...new Set([community.address, community.name, community.publicKey].filter((value): value is string => typeof value === 'string' && value.length > 0)),
];
const toCanonicalCommunity = (value: {
address?: unknown;
communityAddress?: unknown;
name?: unknown;
publicKey?: unknown;
title?: unknown;
nsfw?: unknown;
directoryCode?: unknown;
features?: unknown;
}): DirectoryCommunity | null => {
const name = getStringValue(value.name, value.address, value.communityAddress);
if (!name) {
return null;
}
@@ -97,7 +115,9 @@ const toCanonicalCommunity = (value: { address: unknown; title?: unknown; nsfw?:
const nsfw = deriveNsfw({ ...value, features });
return {
address: value.address,
address: name,
name,
...(typeof value.publicKey === 'string' ? { publicKey: value.publicKey } : {}),
...(typeof value.title === 'string' ? { title: value.title } : {}),
...(typeof value.directoryCode === 'string' ? { directoryCode: value.directoryCode } : {}),
...(features ? { features } : {}),
@@ -110,10 +130,11 @@ const dedupeCommunities = (entries: DirectoryCommunity[]): DirectoryCommunity[]
const normalizedEntries: DirectoryCommunity[] = [];
for (const entry of entries) {
if (seenAddresses.has(entry.address)) {
const dedupeKey = entry.publicKey ?? entry.address;
if (seenAddresses.has(dedupeKey)) {
continue;
}
seenAddresses.add(entry.address);
seenAddresses.add(dedupeKey);
normalizedEntries.push(entry);
}
@@ -132,7 +153,9 @@ const adaptV2Directories = (value: Record<string, unknown>): DirectoryCommunity[
}
const features = isRecord(directory.features) ? directory.features : null;
return toCanonicalCommunity({
address: directory.communityAddress,
communityAddress: directory.communityAddress,
name: directory.name,
publicKey: directory.publicKey,
title: directory.title,
directoryCode: directory.directoryCode,
features,
@@ -163,8 +186,13 @@ export const findDirectoryByAddress = (directories: DirectoryCommunity[], addres
return exactMatch;
}
const exactIdentifierMatch = directories.find((community) => getDirectoryIdentifiers(community).includes(address));
if (exactIdentifierMatch) {
return exactIdentifierMatch;
}
const normalizedAddress = normalizeBoardAddress(address);
return directories.find((community) => normalizeBoardAddress(community.address) === normalizedAddress);
return directories.find((community) => getDirectoryIdentifiers(community).some((identifier) => normalizeBoardAddress(identifier) === normalizedAddress));
};
const adaptV1Communities = (value: Record<string, unknown>): DirectoryCommunity[] => {
@@ -179,6 +207,8 @@ const adaptV1Communities = (value: Record<string, unknown>): DirectoryCommunity[
}
return toCanonicalCommunity({
address: community.address,
name: community.name,
publicKey: community.publicKey,
title: community.title,
nsfw: community.nsfw,
directoryCode: community.directoryCode,
@@ -190,6 +220,24 @@ const adaptV1Communities = (value: Record<string, unknown>): DirectoryCommunity[
return dedupeCommunities(communities);
};
const getDirectoriesMetadata = (value: unknown): DirectoriesMetadata => {
if (!isRecord(value)) {
return {
title: '',
description: '',
createdAt: 0,
updatedAt: 0,
};
}
return {
title: typeof value.title === 'string' ? value.title : '',
description: typeof value.description === 'string' ? value.description : '',
createdAt: typeof value.createdAt === 'number' ? value.createdAt : 0,
updatedAt: typeof value.updatedAt === 'number' ? value.updatedAt : 0,
};
};
const normalizeDirectoriesData = (value: unknown): DirectoriesData | null => {
if (!isRecord(value)) {
return null;
@@ -202,30 +250,26 @@ const normalizeDirectoriesData = (value: unknown): DirectoriesData | null => {
return null;
}
const raw = directoriesData as DirectoriesData;
const fallbackMetadata = getDirectoriesMetadata(directoriesData as unknown);
return {
title: typeof value.title === 'string' ? value.title : (raw.title ?? ''),
description: typeof value.description === 'string' ? value.description : (raw.description ?? ''),
createdAt: typeof value.createdAt === 'number' ? value.createdAt : (raw.createdAt ?? 0),
updatedAt: typeof value.updatedAt === 'number' ? value.updatedAt : (raw.updatedAt ?? 0),
title: typeof value.title === 'string' ? value.title : fallbackMetadata.title,
description: typeof value.description === 'string' ? value.description : fallbackMetadata.description,
createdAt: typeof value.createdAt === 'number' ? value.createdAt : fallbackMetadata.createdAt,
updatedAt: typeof value.updatedAt === 'number' ? value.updatedAt : fallbackMetadata.updatedAt,
communities,
};
};
let fallbackDirectoriesData: DirectoriesData | null = null;
const getFallbackDirectoriesData = (): DirectoriesData => {
export const getFallbackDirectoriesData = (): DirectoriesData => {
if (fallbackDirectoriesData) return fallbackDirectoriesData;
const normalized = normalizeDirectoriesData(directoriesData as unknown);
fallbackDirectoriesData =
normalized ??
({
title: (directoriesData as DirectoriesData).title ?? '',
description: (directoriesData as DirectoriesData).description ?? '',
createdAt: (directoriesData as DirectoriesData).createdAt ?? 0,
updatedAt: (directoriesData as DirectoriesData).updatedAt ?? 0,
communities: (directoriesData as DirectoriesData).communities ?? [],
} as DirectoriesData);
const metadata = getDirectoriesMetadata(directoriesData as unknown);
fallbackDirectoriesData = normalized ?? {
...metadata,
communities: [],
};
return fallbackDirectoriesData;
};
+7 -1
View File
@@ -23,7 +23,12 @@ import {
const communities = [
{ address: 'business.eth', title: '/biz/ - Business & Finance' },
{ address: 'music-posting.bso', title: '/mu/ - Music' },
{
address: 'music-posting.bso',
name: 'music-posting.bso',
publicKey: '12D3KooWQdQ6TkVA1Xe9zzaFP6vXBgsLeMAewpLpLwbsAYKivnQy',
title: '/mu/ - Music',
},
{ address: 'random.eth', directoryCode: 'b', title: 'Random' },
];
@@ -36,6 +41,7 @@ describe('directory mapping helpers', () => {
it('maps addresses to canonical board paths and back', () => {
expect(getBoardPath('business.eth', communities)).toBe('biz');
expect(getBoardPath('music-posting.eth', communities)).toBe('mu');
expect(getBoardPath('12D3KooWQdQ6TkVA1Xe9zzaFP6vXBgsLeMAewpLpLwbsAYKivnQy', communities)).toBe('mu');
expect(getBoardPath('unknown.example', communities)).toBe('unknown.example');
expect(getSubplebbitAddress('biz', communities)).toBe('business.eth');
+2 -2
View File
@@ -1,5 +1,5 @@
import { ChallengeVerification } from '@bitsocialnet/bitsocial-react-hooks';
import directoriesData from '../../data/5chan-directories.json';
import { getFallbackDirectoriesData } from '../../hooks/use-directories';
import { getBoardPath } from './route-utils';
const resolveBoardIdentifier = (communityAddress: unknown): string => {
@@ -7,7 +7,7 @@ const resolveBoardIdentifier = (communityAddress: unknown): string => {
return 'unknown board';
}
const boardPath = getBoardPath(communityAddress, directoriesData.communities);
const boardPath = getBoardPath(communityAddress, getFallbackDirectoriesData().communities);
return boardPath === communityAddress ? communityAddress : `/${boardPath}/`;
};
+7 -1
View File
@@ -18,6 +18,10 @@ let cachedAddressToDirectoryMap: Map<string, string> | null = null;
const getDirectoryCode = (community: DirectoryCommunity): string | null => community.directoryCode ?? extractDirectoryFromTitle(community.title ?? '');
const getCommunityLookupAddresses = (community: DirectoryCommunity): string[] => [
...new Set([community.address, community.name, community.publicKey].filter((value): value is string => typeof value === 'string' && value.length > 0)),
];
/**
* Create a map from directory codes to community addresses
* Uses caching to avoid recreating the map when communities array hasn't changed
@@ -58,7 +62,9 @@ const getAddressToDirectoryMap = (communities: DirectoryCommunity[]): Map<string
if (!community.address) continue;
const directory = getDirectoryCode(community);
if (directory) {
map.set(community.address, directory);
for (const address of getCommunityLookupAddresses(community)) {
map.set(address, directory);
}
}
}