/** * What counts as a gift, and what counts as relevant. * * gift — carries one or more asksurf.ai/mystery-box/g/ links (every code is kept) * box — links to the mystery-box page but without a code * talk — talks about the Surf mystery box / Surf Pro gifts without a link */ const CODE_RE = /asksurf\.ai\/(?:[a-z]{2}(?:-[a-z]{2,4})?\/)?mystery-box\/g\/([A-Za-z0-9]{3,40})/gi const BOX_RE = /asksurf\.ai\/(?:[a-z]{2}(?:-[a-z]{2,4})?\/)?mystery-box/i const BOX_TALK_RE = /mystery[\s-]*box|盲盒|神秘盒/i const SURF_RE = /surf/i const GIFT_TALK_RE = /surf[^。!!\n]{0,24}(会员|盲盒|mystery\s*box|pro\s*会员|邀请码|赠链)|(送|赠|免费领|白嫖|自取|抽)[^。!!\n]{0,20}(surf|盲盒)/i const URL_RE = /https?:\/\/[^\s<>()"',。]+/g function normalizeCode(code) { return String(code || '').trim().toUpperCase() } /** Every distinct code in the given strings (text, expanded links, buttons …). */ function extractCodes(...parts) { const found = new Set() for (const part of parts.flat()) { if (!part) continue for (const match of String(part).matchAll(CODE_RE)) found.add(normalizeCode(match[1])) } return [...found] } /** Accept a bare code, a gift link, or any text containing gift links. */ function codesFromInput(input) { const raw = String(input || '').trim() const codes = extractCodes(raw) if (codes.length) return codes // One or more bare codes separated by spaces / commas / new lines. const tokens = raw.split(/[\s,,、;;]+/).filter(Boolean) if (tokens.length && tokens.every((t) => /^[A-Za-z0-9]{4,40}$/.test(t))) return [...new Set(tokens.map(normalizeCode))] return [] } /** * @param {{text?: string, links?: string[]}} post * @param {{full?: boolean}} options full = the chat is read fully (any Surf mention counts) */ function classify(post, { full = false } = {}) { const text = String(post.text || '') const links = (post.links || []).filter(Boolean) const codes = extractCodes(text, links) if (codes.length) return { kind: 'gift', codes } if (BOX_RE.test(text) || links.some((url) => BOX_RE.test(url))) return { kind: 'box', codes } if (BOX_TALK_RE.test(text) && SURF_RE.test(text)) return { kind: 'talk', codes } if (GIFT_TALK_RE.test(text)) return { kind: 'talk', codes } if (full && SURF_RE.test(text)) return { kind: 'talk', codes } return { kind: null, codes } } /** Cheap pre-filter before doing any work on a message. */ function maybeRelevant(text, full = false) { const s = String(text || '') return /asksurf|mystery|盲盒|神秘盒/i.test(s) || GIFT_TALK_RE.test(s) || (full && SURF_RE.test(s)) } function urlsIn(text) { return String(text || '').match(URL_RE) || [] } module.exports = { extractCodes, codesFromInput, classify, maybeRelevant, normalizeCode, urlsIn, BOX_RE }