diff --git a/setup/migrate-v2/db.ts b/setup/migrate-v2/db.ts index fb15ab0..da2bab7 100644 --- a/setup/migrate-v2/db.ts +++ b/setup/migrate-v2/db.ts @@ -29,6 +29,7 @@ import { readEnvFile } from '../../src/env.js'; import { buildDiscordResolver, type DiscordResolver } from './discord-resolver.js'; import { generateId, + inferIsGroup, parseJid, triggerToEngage, v2PlatformId, @@ -148,7 +149,7 @@ async function main(): Promise { channel_type: channelType, platform_id: platformId, name: g.name || null, - is_group: 1, + is_group: inferIsGroup(channelType, platformId), unknown_sender_policy: 'public', created_at: createdAt, }); diff --git a/setup/migrate-v2/shared.ts b/setup/migrate-v2/shared.ts index ff219df..58c0b16 100644 --- a/setup/migrate-v2/shared.ts +++ b/setup/migrate-v2/shared.ts @@ -74,6 +74,27 @@ export function v2PlatformId(channelType: string, jid: string): string { return id.startsWith(`${channelType}:`) ? id : `${channelType}:${id}`; } +/** + * Infer messaging_groups.is_group from a v2 platform_id, given a channel type. + * + * v1 didn't track is_group, but most channels encode it in the JID/id format: + * - whatsapp: `@g.us` is a group, `@s.whatsapp.net` / `@lid` is a DM + * - telegram: negative chat IDs are groups, positive are DMs + * - everything else: default to 1 (group/channel) — least-surprising guess + * for chats v1 chose to register, where DM auto-create paths weren't used + */ +export function inferIsGroup(channelType: string, platformId: string): number { + if (channelType === 'whatsapp') { + return platformId.endsWith('@g.us') ? 1 : 0; + } + if (channelType === 'telegram') { + // platform_id is `telegram:` — negative chatId means group/channel. + const chatId = platformId.replace(/^telegram:/, ''); + return chatId.startsWith('-') ? 1 : 0; + } + return 1; +} + // ── Trigger mapping ───────────────────────────────────────────────────── /**