fix: robust handle extraction — reject non-platform URLs, fix type safety

discover-channels: new extractHandle() validates each handle belongs to
its platform (rejects hospital-internal URLs like /idtube/view being
treated as YouTube). Extracts handles from full URLs correctly.

collect-channel-data: explicit Record<string,unknown> typing for DB JSON
fields — fixes TypeScript property access on VerifiedChannels from DB.

verifyHandles: fix TikTok double-URL concatenation.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Haewon KamandClaude Opus 4.6 committed 2026-04-04 00:03:26 +09:00
1 parent 0d72750982
commit f65f0e85b3
3 files changed
+68 -16

No files matched your search

+53 -6
View File
@@ -202,13 +202,60 @@ Deno.serve(async (req) => {
const merged = mergeSocialLinks(linkHandles, firecrawlHandles, perplexityHandles);
// Clean up handles (remove @ prefix, URL parts)
// Robust handle extraction — handles may be full URLs, @handles, or bare usernames
function extractHandle(raw: string, platform: string): string | null {
if (!raw || raw.length < 2) return null;
let h = raw.trim();
// Platform-specific URL extraction
if (platform === 'instagram') {
const m = h.match(/instagram\.com\/([a-zA-Z0-9._]+)/);
if (m) return m[1];
h = h.replace(/^@/, '').replace(/\/$/, '');
if (/^[a-zA-Z0-9._]+$/.test(h) && h.length >= 2) return h;
return null;
}
if (platform === 'youtube') {
const m = h.match(/youtube\.com\/(?:@([a-zA-Z0-9._-]+)|channel\/(UC[a-zA-Z0-9_-]+)|c\/([a-zA-Z0-9._-]+))/);
if (m) return m[1] ? `@${m[1]}` : m[2] || m[3] || null;
h = h.replace(/^@/, '');
// Reject if it looks like a non-YouTube URL
if (h.includes('http') || h.includes('/') || h.includes('.com')) return null;
if (/^[a-zA-Z0-9._-]+$/.test(h) && h.length >= 2) return `@${h}`;
return null;
}
if (platform === 'facebook') {
const m = h.match(/facebook\.com\/([a-zA-Z0-9._-]+)/);
if (m) return m[1];
h = h.replace(/^@/, '').replace(/\/$/, '');
if (h.includes('http') || h.includes('/')) return null;
if (/^[a-zA-Z0-9._-]+$/.test(h) && h.length >= 2) return h;
return null;
}
if (platform === 'naverBlog') {
const m = h.match(/blog\.naver\.com\/([a-zA-Z0-9_-]+)/);
if (m) return m[1];
if (h.includes('http') || h.includes('/')) return null;
if (/^[a-zA-Z0-9_-]+$/.test(h) && h.length >= 2) return h;
return null;
}
if (platform === 'tiktok') {
const m = h.match(/tiktok\.com\/@([a-zA-Z0-9._-]+)/);
if (m) return m[1];
h = h.replace(/^@/, '');
if (h.includes('http') || h.includes('/')) return null;
if (/^[a-zA-Z0-9._-]+$/.test(h) && h.length >= 2) return h;
return null;
}
return h;
}
const cleanHandles = {
instagram: merged.instagram.map(h => h.replace(/^@/, '').replace(/\/$/, '')).filter(h => h.length > 1),
youtube: merged.youtube.map(h => h.replace(/^https?:\/\/(www\.)?youtube\.com\//, '')).filter(h => h.length > 1),
facebook: merged.facebook.map(h => h.replace(/^@/, '').replace(/\/$/, '')).filter(h => h.length > 1),
naverBlog: merged.naverBlog.filter(h => h.length > 1),
tiktok: merged.tiktok.map(h => h.replace(/^@/, '')).filter(h => h.length > 1),
instagram: merged.instagram.map(h => extractHandle(h, 'instagram')).filter((h): h is string => h !== null),
youtube: merged.youtube.map(h => extractHandle(h, 'youtube')).filter((h): h is string => h !== null),
facebook: merged.facebook.map(h => extractHandle(h, 'facebook')).filter((h): h is string => h !== null),
naverBlog: merged.naverBlog.map(h => extractHandle(h, 'naverBlog')).filter((h): h is string => h !== null),
tiktok: merged.tiktok.map(h => extractHandle(h, 'tiktok')).filter((h): h is string => h !== null),
};
const verified: VerifiedChannels = await verifyAllHandles(