File size: 4,032 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/** Read models for the page. */
const db = require('./db')
const log = require('./log')
const invites = require('./invites')

const parse = (value, fallback) => {
  try {
    return value ? JSON.parse(value) : fallback
  } catch {
    return fallback
  }
}

function codeView(row) {
  return {
    code: row.code,
    status: row.status,
    seats: row.seats,
    expires_at: row.expires_at,
    inviter: row.inviter,
    tier: row.tier,
    checked_at: row.checked_at,
    first_seen: row.first_seen,
    pushed_at: row.push_note === 'baseline' ? null : row.pushed_at,
    usable: invites.isUsable(row),
  }
}

function postView(row, codeMap) {
  const codes = (row.codes ? String(row.codes).split(',') : []).map((code) => codeMap.get(code)).filter(Boolean)
  return {
    id: row.id,
    source: row.source,
    via: row.via,
    author: row.author,
    author_name: row.author_name,
    text: row.text,
    url: row.url,
    kind: row.kind,
    created_at: row.created_at,
    seen_at: row.seen_at,
    views: row.views,
    likes: row.likes,
    reposts: row.reposts,
    replies: row.replies,
    media: parse(row.media, []),
    links: parse(row.links, []),
    codes,
  }
}

const VIEWS = {
  gifts: `p.kind = 'gift'`,
  links: `p.kind IN ('gift', 'box')`,
  talk: `p.kind = 'talk'`,
  all: '1 = 1',
}

function feed({ view = 'gifts', source = '', q = '', limit = 150 } = {}) {
  const where = [VIEWS[view] || VIEWS.gifts]
  const params = { limit: Math.min(Math.max(Number(limit) || 150, 1), 500) }
  if (source === 'x' || source === 'telegram' || source === 'manual') {
    where.push('p.source = $source')
    params.source = source
  }
  if (q) {
    where.push(`(p.text LIKE $q OR p.author LIKE $q OR p.author_name LIKE $q OR p.id IN (SELECT post_id FROM post_codes WHERE code LIKE $q))`)
    params.q = `%${String(q).slice(0, 60)}%`
  }
  const rows = db.all(
    `SELECT p.*, (SELECT group_concat(code) FROM post_codes WHERE post_id = p.id) AS codes
       FROM posts p WHERE ${where.join(' AND ')}
      ORDER BY p.created_at DESC LIMIT $limit`,
    params
  )
  const wanted = new Set(rows.flatMap((row) => (row.codes ? String(row.codes).split(',') : [])))
  const codeMap = new Map()
  if (wanted.size) {
    const list = [...wanted]
    const marks = list.map((_, i) => `$c${i}`).join(',')
    const bind = Object.fromEntries(list.map((code, i) => [`c${i}`, code]))
    for (const row of db.all(`SELECT * FROM codes WHERE code IN (${marks})`, bind)) codeMap.set(row.code, codeView(row))
  }
  return rows.map((row) => postView(row, codeMap))
}

/** Codes that can be claimed right now, newest first, with where they came from. */
function usable() {
  const now = log.now()
  return db
    .all(
      `SELECT c.*, p.source AS p_source, p.author AS p_author, p.author_name AS p_author_name, p.url AS p_url,
              p.via AS p_via, p.created_at AS p_created
         FROM codes c LEFT JOIN posts p ON p.id = c.first_post
        WHERE c.seats > 0 AND c.checked_at IS NOT NULL AND COALESCE(c.status, '') NOT IN ('EXPIRED', 'INVALID')
          AND (COALESCE(c.expires_at, 0) = 0 OR c.expires_at > $now)
        ORDER BY c.first_seen DESC`,
      { now }
    )
    .map((row) => ({
      ...codeView(row),
      source: row.p_source,
      author: row.p_author,
      author_name: row.p_author_name,
      via: row.p_via,
      url: row.p_url,
      posted_at: row.p_created,
    }))
}

function stats() {
  const now = log.now()
  return db.get(
    `SELECT
       (SELECT count(*) FROM posts) AS posts,
       (SELECT count(*) FROM posts WHERE kind = 'gift') AS gift_posts,
       (SELECT count(*) FROM codes) AS codes,
       (SELECT count(*) FROM codes WHERE first_seen > $day) AS codes_24h,
       (SELECT count(*) FROM codes WHERE pushed_at IS NOT NULL AND COALESCE(push_note, '') NOT IN ('baseline')) AS pushed,
       (SELECT max(first_seen) FROM codes) AS last_code_at,
       (SELECT max(seen_at) FROM posts) AS last_post_at`,
    { day: now - 86400 }
  )
}

module.exports = { feed, usable, stats }