diff --git a/README.md b/README.md index 496fa91..637acb4 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,8 @@ Go 可直接提供构建后的 SPA,也可由 nginx / 静态服务器提供 `we | 研究印象 | `/impression/` 或顶栏 | 最长 4000 字,按当前账号保存 | | 账号 | `/login` | 本地登录、邀请注册、恢复、FastCAS、Research 导入及持续连接 | +arXiv 导读默认打开最近有预印本的采集日。`GET /api/news?source=arxiv` 与 `source=security` 分别返回对应范围的最近非空日期及内容;指定 `date` 时保留该日期,即使该范围当天为空。不指定来源时保留总览使用的全部资讯行为。 + 原 `/index.html`、各栏目 `/index.html`、`/auth/login`、`/auth/account` 和 `?date=YYYY-MM-DD` 继续可用。原 `top-conf/data/report/`、`secnews/data/report/` 下的 HTML/PDF 地址保持;`?download=1` 返回附件。前端已重构为 FastUI 工作台:分组导航面板、页签报告库和手机导航弹窗;旧自定义 CSS 已全部删除。视觉与控件直接使用 FastUI,宿主布局使用 UnoCSS 工具类,论文筛选条件随 URL 保存。实现约定见 [FastUI 接入说明](docs/FASTUI.md)。 研究趋势使用已有 arXiv 采集档案,不请求外部模型。`GET /api/trend?scope=ai&days=30` 按 UTC 采集日比较相邻等长窗口,以档案最新论文日为统计终点。按 arXiv ID 去重,覆盖现有 AI/NLP、安全和软件工程订阅及其中的交叉分类,不代表全量 arXiv 或引用影响力。 diff --git a/internal/catalog/catalog.go b/internal/catalog/catalog.go index d804690..2745841 100644 --- a/internal/catalog/catalog.go +++ b/internal/catalog/catalog.go @@ -221,7 +221,7 @@ func (c *Catalog) Papers() ([]Paper, error) { return nil, e } for _, r := range rows { - if !strings.Contains(strings.ToLower(Str(r, "source")), "arxiv") { + if !IsArxivSource(Str(r, "source")) { continue } id := First(Str(r, "_id"), Str(r, "link")) @@ -239,6 +239,10 @@ func (c *Catalog) Papers() ([]Paper, error) { return append([]Paper(nil), out...), nil } +func IsArxivSource(source string) bool { + return strings.Contains(strings.ToLower(source), "arxiv") +} + var nonTitle = regexp.MustCompile(`[^a-z0-9]+`) var paperNumberRE = regexp.MustCompile(`(?i)\s*\(paper\s+\d+\)\s*`) diff --git a/internal/httpapi/server.go b/internal/httpapi/server.go index 62208ab..d7ee40f 100644 --- a/internal/httpapi/server.go +++ b/internal/httpapi/server.go @@ -261,14 +261,38 @@ func (s *Server) catalogHTTP(w http.ResponseWriter, r *http.Request, p string) e } reply(w, r, 200, map[string]any{"reports": v}) case "/api/news": + source := r.URL.Query().Get("source") + if source != "" && source != "all" && source != "arxiv" && source != "security" { + return store.Fail(400, "资讯来源范围无效") + } + selectRows := func(rows []map[string]any) []map[string]any { + if source == "" || source == "all" { + return rows + } + selected := []map[string]any{} + for _, row := range rows { + if catalog.IsArxivSource(catalog.Str(row, "source")) == (source == "arxiv") { + selected = append(selected, row) + } + } + return selected + } day := r.URL.Query().Get("date") if day == "" { days, e := s.Catalog.Dates() if e != nil { return e } - if len(days) > 0 { - day = days[0] + for _, candidate := range days { + rows, err := s.Catalog.News(candidate) + if err != nil { + return err + } + rows = selectRows(rows) + if source == "" || source == "all" || len(rows) > 0 { + reply(w, r, 200, map[string]any{"date": candidate, "articles": rows}) + return nil + } } } if day == "" { @@ -282,7 +306,7 @@ func (s *Server) catalogHTTP(w http.ResponseWriter, r *http.Request, p string) e if e != nil { return e } - reply(w, r, 200, map[string]any{"date": day, "articles": v}) + reply(w, r, 200, map[string]any{"date": day, "articles": selectRows(v)}) default: papers, e := s.Catalog.Papers() if e != nil { diff --git a/internal/httpapi/server_test.go b/internal/httpapi/server_test.go index 758152f..fbe563c 100644 --- a/internal/httpapi/server_test.go +++ b/internal/httpapi/server_test.go @@ -318,6 +318,46 @@ func TestGenerationPreservesExplicitImpressionWhileDefaultingOmittedField(t *tes } } +func TestNewsScopeSelectsLatestPopulatedArchiveDate(t *testing.T) { + h := fixture(t) + directory := filepath.Join(h.app.Config.Root, "secnews/data/articles") + files := map[string]string{ + "2026-11-20": `{"_id":"news","title":"Security news","source":"bleepingcomputer"}`, + "2026-11-18": `{"_id":"paper","title":"Research paper","source":"arxiv_cs_se"}`, + } + for day, content := range files { + if err := os.WriteFile(filepath.Join(directory, day+".jsonl"), []byte(content), 0600); err != nil { + t.Fatal(err) + } + } + for _, test := range []struct { + query, date, id string + count int + }{ + {"", "2026-11-20", "news", 1}, + {"?source=arxiv", "2026-11-18", "paper", 1}, + {"?source=security", "2026-11-20", "news", 1}, + {"?source=arxiv&date=2026-11-20", "2026-11-20", "", 0}, + {"?source=security&date=2026-11-18", "2026-11-18", "", 0}, + } { + status, result, _ := h.request(t, "GET", "/api/news"+test.query, nil, "", false) + if status != 200 || result["date"] != test.date { + t.Fatal(test.query, status, result) + } + articles := result["articles"].([]any) + if len(articles) != test.count { + t.Fatal(test.query, articles) + } + if test.count > 0 && articles[0].(map[string]any)["_id"] != test.id { + t.Fatal(test.query, articles) + } + } + status, _, _ := h.request(t, "GET", "/api/news?source=unknown", nil, "", false) + if status != 400 { + t.Fatal("unknown archive scope accepted", status) + } +} + func TestReleaseSymlinkAndReportAssets(t *testing.T) { h := fixture(t) link := filepath.Join(t.TempDir(), "current") diff --git a/web/src/news.ts b/web/src/news.ts deleted file mode 100644 index dcd246a..0000000 --- a/web/src/news.ts +++ /dev/null @@ -1,5 +0,0 @@ -import type { NewsArticle } from './types' - -export function isArxiv(article: NewsArticle) { - return article.source.toLowerCase().includes('arxiv') -} diff --git a/web/src/pages/Arxiv.tsx b/web/src/pages/Arxiv.tsx index 3ce1c73..df528a6 100644 --- a/web/src/pages/Arxiv.tsx +++ b/web/src/pages/Arxiv.tsx @@ -3,14 +3,15 @@ import { useSearchParams } from 'react-router-dom' import { EmptyState, Select, Toolbar } from '@fastrd/ui-react' import { api, message } from '../api' import { useApp } from '../context' -import { isArxiv } from '../news' import type { NewsArticle } from '../types' import { Busy, ErrorMessage, PageHeading, Pagination, Panel, PaperCard } from '../components/Common' export default function Arxiv() { const { catalog } = useApp() const [params, setParams] = useSearchParams() - const date = params.get('date') || catalog?.dates[0] || '' + const requestedDate = params.get('date') || '' + const [resolvedDate, setResolvedDate] = useState('') + const date = requestedDate || resolvedDate const category = params.get('category') || '' const requestedPage = Math.max(1, Math.floor(Number(params.get('page')) || 1)) const [articles, setArticles] = useState([]) @@ -27,15 +28,20 @@ export default function Arxiv() { }) } useEffect(() => { - if (!date) return const abort = new AbortController() setBusy(true) setError('') setArticles([]) - api<{ articles: NewsArticle[] }>(`api/news?${new URLSearchParams({ date })}`, { - signal: abort.signal, - }) - .then((value) => setArticles(value.articles.filter(isArxiv))) + api<{ date: string; articles: NewsArticle[] }>( + `api/news?${new URLSearchParams({ source: 'arxiv', ...(requestedDate ? { date: requestedDate } : {}) })}`, + { + signal: abort.signal, + }, + ) + .then((value) => { + setResolvedDate(value.date) + setArticles(value.articles) + }) .catch((error) => { if (!abort.signal.aborted) setError(message(error)) }) @@ -43,7 +49,7 @@ export default function Arxiv() { if (!abort.signal.aborted) setBusy(false) }) return () => abort.abort() - }, [date]) + }, [requestedDate]) const categories = [...new Set(articles.flatMap((article) => article.categories || []))].sort() const filtered = articles.filter((article) => !category || article.categories?.includes(category)) const page = Math.min(requestedPage, Math.max(1, Math.ceil(filtered.length / 12))) @@ -62,6 +68,9 @@ export default function Arxiv() { value={date} onChange={(event) => filter('date', event.target.value)} > + {(catalog?.dates || []).map((day) => ( ))} diff --git a/web/src/pages/News.tsx b/web/src/pages/News.tsx index 79c4332..d81418a 100644 --- a/web/src/pages/News.tsx +++ b/web/src/pages/News.tsx @@ -10,7 +10,6 @@ import { } from '@fastrd/ui-react' import { ChevronLeft, ChevronRight, ExternalLink } from 'lucide-react' import { api, message, safeURL } from '../api' -import { isArxiv } from '../news' import { useApp } from '../context' import type { NewsArticle } from '../types' import { Busy, ErrorMessage, PageHeading, Panel, ReportList } from '../components/Common' @@ -35,7 +34,9 @@ function score(a: NewsArticle) { export default function News() { const { catalog } = useApp() const [params, setParams] = useSearchParams() - const date = params.get('date') || catalog?.dates[0] || '' + const requestedDate = params.get('date') || '' + const [resolvedDate, setResolvedDate] = useState('') + const date = requestedDate || resolvedDate const [month, setMonth] = useState(''), [articles, setArticles] = useState([]), [busy, setBusy] = useState(false), @@ -43,23 +44,28 @@ export default function News() { [kind, setKind] = useState('all'), [showAll, setShowAll] = useState(false) useEffect(() => { - if (date) { - setMonth(date.slice(0, 7)) - const abort = new AbortController() - setBusy(true) - setError('') - setShowAll(false) - api<{ articles: NewsArticle[] }>(`api/news?date=${date}`, { signal: abort.signal }) - .then((v) => setArticles(v.articles.filter((article) => !isArxiv(article)))) - .catch((e) => { - if (!abort.signal.aborted) setError(message(e)) - }) - .finally(() => { - if (!abort.signal.aborted) setBusy(false) - }) - return () => abort.abort() - } - }, [date]) + const abort = new AbortController() + setBusy(true) + setError('') + setArticles([]) + setShowAll(false) + api<{ date: string; articles: NewsArticle[] }>( + `api/news?${new URLSearchParams({ source: 'security', ...(requestedDate ? { date: requestedDate } : {}) })}`, + { signal: abort.signal }, + ) + .then((v) => { + setResolvedDate(v.date) + setMonth(v.date.slice(0, 7)) + setArticles(v.articles) + }) + .catch((e) => { + if (!abort.signal.aborted) setError(message(e)) + }) + .finally(() => { + if (!abort.signal.aborted) setBusy(false) + }) + return () => abort.abort() + }, [requestedDate]) const available = useMemo(() => new Set(catalog?.dates || []), [catalog]) const first = new Date( `${month || date.slice(0, 7) || new Date().toISOString().slice(0, 7)}-01T00:00:00`, @@ -122,7 +128,7 @@ export default function News() {