Spaces:
Sleeping
Sleeping
Surf Studio commited on
Commit ·
7e409fc
1
Parent(s): a885574
群组可切换「仅赠链 / 全部消息」;读取方启动即接管单读锁,重启后不再空等
Browse files- frontend/src/components/SourcePanel.tsx +30 -1
- frontend/tsconfig.tsbuildinfo +0 -0
- lib/telegram.js +1 -0
- lib/telegramUser.js +40 -3
- routes/telegram.js +10 -0
frontend/src/components/SourcePanel.tsx
CHANGED
|
@@ -25,7 +25,14 @@ type XStatus = {
|
|
| 25 |
searchCount?: number
|
| 26 |
}
|
| 27 |
|
| 28 |
-
type TelegramChannel = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
type TelegramDialog = {
|
| 30 |
id: string
|
| 31 |
ref: string
|
|
@@ -228,6 +235,16 @@ export default function SourcePanel({ onChanged }: { onChanged: () => void }) {
|
|
| 228 |
}
|
| 229 |
}
|
| 230 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
async function removeChannel(username: string) {
|
| 232 |
const body = await call(
|
| 233 |
`tg-del-${username}`,
|
|
@@ -623,6 +640,18 @@ export default function SourcePanel({ onChanged }: { onChanged: () => void }) {
|
|
| 623 |
{item.title || item.username}
|
| 624 |
{item.username ? ` · @${item.username}` : ' · 私有'}
|
| 625 |
</span>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 626 |
<button
|
| 627 |
type="button"
|
| 628 |
onClick={() => removeChannel(item.id)}
|
|
|
|
| 25 |
searchCount?: number
|
| 26 |
}
|
| 27 |
|
| 28 |
+
type TelegramChannel = {
|
| 29 |
+
id: string
|
| 30 |
+
ref?: string
|
| 31 |
+
username?: string | null
|
| 32 |
+
title?: string
|
| 33 |
+
auto?: boolean
|
| 34 |
+
full?: boolean
|
| 35 |
+
}
|
| 36 |
type TelegramDialog = {
|
| 37 |
id: string
|
| 38 |
ref: string
|
|
|
|
| 235 |
}
|
| 236 |
}
|
| 237 |
|
| 238 |
+
async function toggleFull(item: TelegramChannel) {
|
| 239 |
+
const body = await call(
|
| 240 |
+
`tg-full-${item.id}`,
|
| 241 |
+
`telegram/channels/${encodeURIComponent(item.ref || item.id)}/full`,
|
| 242 |
+
{ method: 'POST', body: JSON.stringify({ full: !item.full }) },
|
| 243 |
+
() => (item.full ? '已恢复只收录赠链相关消息' : '已开启:该群所有 Surf 相关消息都会收录')
|
| 244 |
+
)
|
| 245 |
+
if (body) setTelegram((prev) => (prev ? { ...prev, channels: body.channels } : prev))
|
| 246 |
+
}
|
| 247 |
+
|
| 248 |
async function removeChannel(username: string) {
|
| 249 |
const body = await call(
|
| 250 |
`tg-del-${username}`,
|
|
|
|
| 640 |
{item.title || item.username}
|
| 641 |
{item.username ? ` · @${item.username}` : ' · 私有'}
|
| 642 |
</span>
|
| 643 |
+
<button
|
| 644 |
+
type="button"
|
| 645 |
+
onClick={() => toggleFull(item)}
|
| 646 |
+
className={`shrink-0 rounded-[6px] px-1.5 py-0.5 text-[10px] transition-colors ${
|
| 647 |
+
item.full
|
| 648 |
+
? 'bg-[var(--accent)]/15 text-[var(--accent)]'
|
| 649 |
+
: 'text-fg-muted hover:text-fg-base'
|
| 650 |
+
}`}
|
| 651 |
+
title="开启后这个群里所有 Surf 相关消息(包括聊天)都会收录"
|
| 652 |
+
>
|
| 653 |
+
{item.full ? '全部消息' : '仅赠链'}
|
| 654 |
+
</button>
|
| 655 |
<button
|
| 656 |
type="button"
|
| 657 |
onClick={() => removeChannel(item.id)}
|
frontend/tsconfig.tsbuildinfo
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|
lib/telegram.js
CHANGED
|
@@ -314,6 +314,7 @@ async function sync({ upsert, upsertParams, takeover = false }) {
|
|
| 314 |
module.exports = {
|
| 315 |
status,
|
| 316 |
reconnect: (...args) => telegramUser.reconnect(...args),
|
|
|
|
| 317 |
readerEnabled: () => telegramUser.readerEnabled(),
|
| 318 |
remoteHint: () => telegramUser.remoteHint,
|
| 319 |
inspect: (...args) => telegramUser.inspect(...args),
|
|
|
|
| 314 |
module.exports = {
|
| 315 |
status,
|
| 316 |
reconnect: (...args) => telegramUser.reconnect(...args),
|
| 317 |
+
setChannelFull: (...args) => telegramUser.setChannelFull(...args),
|
| 318 |
readerEnabled: () => telegramUser.readerEnabled(),
|
| 319 |
remoteHint: () => telegramUser.remoteHint,
|
| 320 |
inspect: (...args) => telegramUser.inspect(...args),
|
lib/telegramUser.js
CHANGED
|
@@ -691,11 +691,39 @@ function messageToRow(message, channel) {
|
|
| 691 |
}
|
| 692 |
}
|
| 693 |
|
| 694 |
-
|
|
|
|
|
|
|
|
|
|
| 695 |
if (row.has_link) return true
|
| 696 |
if (SURF_BOX_RE.test(row.text_content)) return true
|
| 697 |
if (BOX_TALK_RE.test(row.text_content) && row.links.length > 0) return true
|
| 698 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 699 |
}
|
| 700 |
|
| 701 |
/** Look at the newest messages of one chat, with the reason each one is kept or dropped. */
|
|
@@ -812,7 +840,7 @@ async function sync({ upsert, upsertParams, takeover = false }) {
|
|
| 812 |
if (!message || message.className !== 'Message') continue
|
| 813 |
const row = messageToRow(message, channel)
|
| 814 |
if (row.created_at < cutoff) continue
|
| 815 |
-
if (!isRelevant(row)) continue
|
| 816 |
if (row.has_link) withLink += 1
|
| 817 |
stored += 1
|
| 818 |
channel.hits = Number(channel.hits || 0) + 1
|
|
@@ -850,8 +878,17 @@ async function sync({ upsert, upsertParams, takeover = false }) {
|
|
| 850 |
}
|
| 851 |
}
|
| 852 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 853 |
module.exports = {
|
| 854 |
status,
|
|
|
|
| 855 |
readerEnabled,
|
| 856 |
remoteHint: REMOTE_HINT,
|
| 857 |
reconnect,
|
|
|
|
| 691 |
}
|
| 692 |
}
|
| 693 |
|
| 694 |
+
/** Mentions of Surf in any form — used for chats the user asked to read fully. */
|
| 695 |
+
const SURF_MENTION_RE = /surf/i
|
| 696 |
+
|
| 697 |
+
function isRelevant(row, channel = {}) {
|
| 698 |
if (row.has_link) return true
|
| 699 |
if (SURF_BOX_RE.test(row.text_content)) return true
|
| 700 |
if (BOX_TALK_RE.test(row.text_content) && row.links.length > 0) return true
|
| 701 |
+
if (GIFT_TALK_RE.test(row.text_content)) return true
|
| 702 |
+
// "读取全部" chats: keep every Surf-related message, gift link or not.
|
| 703 |
+
if (channel.full && SURF_MENTION_RE.test(`${row.text_content} ${row.handle} ${row.author_name}`)) {
|
| 704 |
+
return true
|
| 705 |
+
}
|
| 706 |
+
return false
|
| 707 |
+
}
|
| 708 |
+
|
| 709 |
+
/** Turn "read everything Surf-related" on/off for one chat. */
|
| 710 |
+
async function setChannelFull(ref, full) {
|
| 711 |
+
const list = await getJson('telegram_channels', [])
|
| 712 |
+
const key = String(ref || '')
|
| 713 |
+
let touched = 0
|
| 714 |
+
for (const item of list) {
|
| 715 |
+
if (String(item.ref || '') === key || String(item.id || '') === key) {
|
| 716 |
+
item.full = Boolean(full)
|
| 717 |
+
touched += 1
|
| 718 |
+
}
|
| 719 |
+
}
|
| 720 |
+
if (touched === 0) {
|
| 721 |
+
const err = new Error('没有找到这个群组')
|
| 722 |
+
err.status = 404
|
| 723 |
+
throw err
|
| 724 |
+
}
|
| 725 |
+
await setSetting('telegram_channels', JSON.stringify(list))
|
| 726 |
+
return { ok: true, channels: list }
|
| 727 |
}
|
| 728 |
|
| 729 |
/** Look at the newest messages of one chat, with the reason each one is kept or dropped. */
|
|
|
|
| 840 |
if (!message || message.className !== 'Message') continue
|
| 841 |
const row = messageToRow(message, channel)
|
| 842 |
if (row.created_at < cutoff) continue
|
| 843 |
+
if (!isRelevant(row, channel)) continue
|
| 844 |
if (row.has_link) withLink += 1
|
| 845 |
stored += 1
|
| 846 |
channel.hits = Number(channel.hits || 0) + 1
|
|
|
|
| 878 |
}
|
| 879 |
}
|
| 880 |
|
| 881 |
+
// A freshly booted reader owns the lease immediately (a dead process cannot
|
| 882 |
+
// hold it). Non-readers never claim, so the deployed app always wins.
|
| 883 |
+
if (readerEnabled()) {
|
| 884 |
+
setTimeout(() => {
|
| 885 |
+
acquireLease({ force: true }).catch(() => {})
|
| 886 |
+
}, 3000)
|
| 887 |
+
}
|
| 888 |
+
|
| 889 |
module.exports = {
|
| 890 |
status,
|
| 891 |
+
setChannelFull,
|
| 892 |
readerEnabled,
|
| 893 |
remoteHint: REMOTE_HINT,
|
| 894 |
reconnect,
|
routes/telegram.js
CHANGED
|
@@ -87,6 +87,16 @@ router.delete('/channels/:channel', requireKey, async (req, res) => {
|
|
| 87 |
}
|
| 88 |
})
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
/** Force a fresh Telegram connection (used when the session gets stuck). */
|
| 91 |
router.post('/reconnect', requireKey, async (_req, res) => {
|
| 92 |
try {
|
|
|
|
| 87 |
}
|
| 88 |
})
|
| 89 |
|
| 90 |
+
/** Toggle "read all Surf-related messages" for one chat. */
|
| 91 |
+
router.post('/channels/:ref/full', requireKey, async (req, res) => {
|
| 92 |
+
try {
|
| 93 |
+
const result = await telegram.setChannelFull(req.params.ref, Boolean(req.body?.full))
|
| 94 |
+
res.json({ ok: true, ...result })
|
| 95 |
+
} catch (err) {
|
| 96 |
+
res.status(err.status || 500).json({ error: err.message })
|
| 97 |
+
}
|
| 98 |
+
})
|
| 99 |
+
|
| 100 |
/** Force a fresh Telegram connection (used when the session gets stuck). */
|
| 101 |
router.post('/reconnect', requireKey, async (_req, res) => {
|
| 102 |
try {
|