Update notify.ts
This commit is contained in:
1 parent
ea26eef4e4
commit
9a083b50b4
1 file changed
+9
-65
@@ -11,86 +11,30 @@ interface ArtalkComment {
|
|||||||
created_at: string;
|
created_at: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ArtalkPage {
|
|
||||||
id: number;
|
|
||||||
key: string;
|
|
||||||
title: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
let lastCheckedId = 0;
|
let lastCheckedId = 0;
|
||||||
let notifierStarted = false;
|
let notifierStarted = false;
|
||||||
|
|
||||||
// 获取所有页面
|
// 获取所有评论(使用 type=all 参数)
|
||||||
async function fetchAllPages(): Promise<ArtalkPage[]> {
|
async function fetchLatestComments(): Promise<ArtalkComment[]> {
|
||||||
try {
|
try {
|
||||||
const url = `${ARTALK_SERVER}/api/v2/pages?site_name=${encodeURIComponent(ARTALK_SITE)}&limit=100`;
|
// 尝试使用 type=all 参数获取所有评论
|
||||||
const res = await fetch(url, {
|
const url = `${ARTALK_SERVER}/api/v2/comments?site_name=${encodeURIComponent(ARTALK_SITE)}&type=all&limit=5&sort_by=created_at&sort_order=desc`;
|
||||||
headers: { "Content-Type": "application/json" },
|
console.log("[comment-notify] fetching comments with type=all...");
|
||||||
});
|
|
||||||
|
|
||||||
if (!res.ok) {
|
|
||||||
console.error("[comment-notify] fetch pages failed:", res.status);
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
const data = await res.json() as { pages?: ArtalkPage[] };
|
|
||||||
return data.pages || [];
|
|
||||||
} catch (e) {
|
|
||||||
console.error("[comment-notify] fetch pages error:", e instanceof Error ? e.message : String(e));
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取特定页面的最新评论
|
|
||||||
async function fetchCommentsForPage(pageKey: string): Promise<ArtalkComment[]> {
|
|
||||||
try {
|
|
||||||
const url = `${ARTALK_SERVER}/api/v2/comments?site_name=${encodeURIComponent(ARTALK_SITE)}&page_key=${encodeURIComponent(pageKey)}&limit=5&sort_by=created_at&sort_order=desc`;
|
|
||||||
const res = await fetch(url, {
|
const res = await fetch(url, {
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
|
const errorText = await res.text().catch(() => "Unable to read response");
|
||||||
|
console.error("[comment-notify] fetch failed:", res.status, res.statusText);
|
||||||
|
console.error("[comment-notify] response:", errorText);
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await res.json() as { comments?: ArtalkComment[] };
|
const data = await res.json() as { comments?: ArtalkComment[] };
|
||||||
|
console.log("[comment-notify] fetched", data.comments?.length || 0, "comments");
|
||||||
return data.comments || [];
|
return data.comments || [];
|
||||||
} catch (e) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取所有页面的最新评论,合并后返回最新的 5 条
|
|
||||||
async function fetchLatestComments(): Promise<ArtalkComment[]> {
|
|
||||||
try {
|
|
||||||
// 1. 获取所有页面
|
|
||||||
const pages = await fetchAllPages();
|
|
||||||
if (pages.length === 0) {
|
|
||||||
console.log("[comment-notify] no pages found");
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log("[comment-notify] found", pages.length, "pages, checking comments...");
|
|
||||||
|
|
||||||
// 2. 获取每个页面的最新评论(并行请求)
|
|
||||||
const allComments: ArtalkComment[] = [];
|
|
||||||
const promises = pages.map(async (page) => {
|
|
||||||
const comments = await fetchCommentsForPage(page.key);
|
|
||||||
return comments;
|
|
||||||
});
|
|
||||||
|
|
||||||
const results = await Promise.all(promises);
|
|
||||||
results.forEach(comments => allComments.push(...comments));
|
|
||||||
|
|
||||||
// 3. 按时间排序,取最新的 5 条
|
|
||||||
allComments.sort((a, b) =>
|
|
||||||
new Date(b.created_at).getTime() - new Date(a.created_at).getTime()
|
|
||||||
);
|
|
||||||
|
|
||||||
const latestComments = allComments.slice(0, 5);
|
|
||||||
console.log("[comment-notify] fetched", latestComments.length, "latest comments from", pages.length, "pages");
|
|
||||||
|
|
||||||
return latestComments;
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("[comment-notify] fetch error:", e instanceof Error ? e.message : String(e));
|
console.error("[comment-notify] fetch error:", e instanceof Error ? e.message : String(e));
|
||||||
return [];
|
return [];
|
||||||
|
|||||||
Reference in new issue
Block a user