cut / lib /feed.js
Surf Studio
重写:SQLite + 独立循环 + 实时 Telegram + 一码一推 + 新页面
5936c9d
Raw History Blame Contribute Delete
4.03 kB
/** 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 }