feat(chat): Improve recipient handle parsing for swarm messaging

- Strip leading @ symbol from recipient handle before normalization
- Replace simple split() with substring-based parsing for more robust handle@domain extraction
- Add clarifying comments explaining handle format expectations (handle@domain)
- Prevent potential issues with malformed recipient handles containing multiple @ symbols
- Ensure consistent handle normalization across local and remote recipients
This commit is contained in:
Christomatt
2026-01-27 01:54:59 +01:00
parent 8e7c6353f3
commit 998a1bcf4a
+6 -2
View File
@@ -60,7 +60,8 @@ export async function POST(request: NextRequest) {
console.log('[Chat Send] Sender retrieved:', sender.handle); console.log('[Chat Send] Sender retrieved:', sender.handle);
// Parse recipient handle (could be local or remote) // Parse recipient handle (could be local or remote)
const recipientHandle = data.recipientHandle.toLowerCase(); // Strip leading @ if present, then normalize
const recipientHandle = data.recipientHandle.toLowerCase().replace(/^@/, '');
const isRemote = recipientHandle.includes('@'); const isRemote = recipientHandle.includes('@');
let recipientUser: typeof users.$inferSelect | undefined; let recipientUser: typeof users.$inferSelect | undefined;
@@ -69,7 +70,10 @@ export async function POST(request: NextRequest) {
if (isRemote) { if (isRemote) {
// Remote user - need to fetch their public key // Remote user - need to fetch their public key
const [handle, domain] = recipientHandle.split('@'); // Format: handle@domain (e.g., matterbator@batorbros.bond)
const atIndex = recipientHandle.indexOf('@');
const handle = recipientHandle.substring(0, atIndex);
const domain = recipientHandle.substring(atIndex + 1);
recipientNodeDomain = domain; recipientNodeDomain = domain;
console.log('[Chat Send] Processing remote recipient:', handle, '@', domain); console.log('[Chat Send] Processing remote recipient:', handle, '@', domain);