File size: 2,825 Bytes
5936c9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
 * What counts as a gift, and what counts as relevant.
 *
 *   gift β€” carries one or more asksurf.ai/mystery-box/g/<CODE> 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 }