/** * Single entry point for every source: X search, X profiles, Telegram realtime, * Telegram catch-up, manual adds. A post goes in, codes come out, new codes are * checked and pushed immediately — no waiting for the next timer tick. */ const db = require('./db') const log = require('./log') const detect = require('./detect') const invites = require('./invites') const alerts = require('./alerts') const stats = { ingested: 0, newPosts: 0, newCodes: 0, lastNewCodeAt: null } /** * @param {object} post { id, source, via, chat_id, author, author_name, text, url, created_at, * views, likes, reposts, replies, media[], links[] } * @param {object} opts { full } — chat is read fully (any Surf mention is kept) * @returns {{kept: boolean, isNew?: boolean, codes?: string[], newCodes?: string[]}} */ function store(post, { full = false } = {}) { const { kind, codes } = detect.classify(post, { full }) if (!kind) return { kept: false } const now = log.now() const createdAt = Number(post.created_at) || now const existing = db.get('SELECT kind FROM posts WHERE id = $id', { id: post.id }) const newCodes = [] db.tx(() => { db.run( `INSERT INTO posts (id, source, via, chat_id, author, author_name, text, url, kind, created_at, seen_at, updated_at, views, likes, reposts, replies, media, links) VALUES ($id, $source, $via, $chat_id, $author, $author_name, $text, $url, $kind, $created_at, $now, $now, $views, $likes, $reposts, $replies, $media, $links) ON CONFLICT (id) DO UPDATE SET text = excluded.text, kind = CASE WHEN posts.kind = 'gift' THEN 'gift' ELSE excluded.kind END, views = max(COALESCE(posts.views, 0), COALESCE(excluded.views, 0)), likes = max(COALESCE(posts.likes, 0), COALESCE(excluded.likes, 0)), reposts = max(COALESCE(posts.reposts, 0), COALESCE(excluded.reposts, 0)), replies = max(COALESCE(posts.replies, 0), COALESCE(excluded.replies, 0)), links = excluded.links, updated_at = excluded.updated_at`, { id: post.id, source: post.source, via: post.via || null, chat_id: post.chat_id || null, author: post.author || null, author_name: post.author_name || null, text: String(post.text || '').slice(0, 4000), url: post.url || null, kind, created_at: createdAt, now, views: Number(post.views || 0), likes: Number(post.likes || 0), reposts: Number(post.reposts || 0), replies: Number(post.replies || 0), media: JSON.stringify(post.media || []), links: JSON.stringify([...new Set(post.links || [])].slice(0, 20)), } ) for (const code of codes) { const added = db.run( `INSERT INTO codes (code, first_seen, first_post, next_check) VALUES ($code, $now, $post, 0) ON CONFLICT (code) DO NOTHING`, { code, now, post: post.id } ) if (added.changes === 1) newCodes.push(code) db.run('INSERT OR IGNORE INTO post_codes (post_id, code) VALUES ($post, $code)', { post: post.id, code }) } }) stats.ingested += 1 if (!existing) stats.newPosts += 1 if (newCodes.length) { stats.newCodes += newCodes.length stats.lastNewCodeAt = now log.info('codes', `新赠链 ${newCodes.join(', ')}(${post.source} · ${post.via || post.author || ''})`) } if (!existing || newCodes.length) db.changed() return { kept: true, isNew: !existing, codes, newCodes, kind } } /** Store + immediately check seats and push for codes never seen before. */ async function ingest(post, opts) { const result = store(post, opts) if (result.newCodes?.length) { await invites.checkNow(result.newCodes).catch(() => {}) await alerts.pushPending().catch(() => {}) } return result } module.exports = { ingest, store, stats }