Skip to content

Commit

Permalink
改用新的讨论列表接口
Browse files Browse the repository at this point in the history
  • Loading branch information
wxh06 authored and immccn123 committed Aug 4, 2024
1 parent f8c5e74 commit 2ccc2ee
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 42 deletions.
81 changes: 41 additions & 40 deletions packages/archive/src/lib/list.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
import type { BaseLogger } from "pino";
import type { PrismaClient } from "@prisma/client";
import type { UserSummary } from "./user";
import type { ForumData, ReplyContent } from "./post";
import { getResponse } from "./parser";

interface LegacyDiscussList {
status: 200;
data: {
count: number;
result: {
PostID: number;
Title: string;
Author: { _instance: "Luogu\\Model\\User\\User" };
Forum: {
Forum: {
ForumID: number;
Name: string;
InternalName: string;
_instance: "Luogu\\Model\\Discuss\\Forum";
};
};
Top: number;
SubmitTime: number;
isValid: boolean;
LatestReply: {
Author: { _instance: "Luogu\\Model\\User\\User" };
ReplyTime: number;
Content: string;
_instance: "Luogu\\Model\\Discuss\\PostReply";
} | null;
RepliesCount: number;
_instance: "Luogu\\Model\\Discuss\\Post";
}[];
interface PostData {
id: number;
title: string;
author: UserSummary;
time: number;
forum: ForumData;
topped: boolean;
valid: boolean;
locked: false;
replyCount: number;
recentReply: ReplyContent | false;
}

interface PostListResponse {
code: 200;
currentTemplate: "DiscussList";
currentData: {
forum: ForumData | null;
publicForums: ForumData[];
posts: {
perPage: number;
count: number;
result: PostData[];
};
canPost: boolean;
};
}

Expand All @@ -41,12 +40,14 @@ export default async function getPostList(
) {
const response = await getResponse(
logger,
`https://www.luogu.com/api/discuss?page=${page}`,
`https://www.luogu.com/discuss?_contentOnly&page=${page}`,
false,
);
const {
data: { result },
} = (await response.json()) as LegacyDiscussList;
currentData: {
posts: { result },
},
} = (await response.json()) as PostListResponse;
const saved = Object.fromEntries(
(
await prisma.post.findMany({
Expand All @@ -57,8 +58,8 @@ export default async function getPostList(
where: {
id: {
in: result
.filter((post) => post.SubmitTime < after)
.map((post) => post.PostID),
.filter((post) => post.time < after)
.map((post) => post.id),
},
},
})
Expand All @@ -67,12 +68,12 @@ export default async function getPostList(
return result
.filter(
(post) =>
post.SubmitTime >= after ||
!(post.PostID in saved) ||
(post.LatestReply &&
(!saved[post.PostID] ||
Math.floor(saved[post.PostID].getTime() / 60000) !==
Math.floor(post.LatestReply.ReplyTime / 60))),
post.time >= after ||
!(post.id in saved) ||
(post.recentReply &&
(!saved[post.id] ||
Math.floor(saved[post.id].getTime() / 60000) !==
Math.floor(post.recentReply.time / 60))),
)
.map((post) => post.PostID);
.map((post) => post.id);
}
4 changes: 2 additions & 2 deletions packages/archive/src/lib/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { UserSummary, upsertUserSnapshotHook } from "./user";
const PAGES_PER_SAVE = parseInt(process.env.PAGES_PER_SAVE ?? "64", 10);
export const emitters: Record<number, EventEmitter> = {};

interface ForumData {
export interface ForumData {
name: string;
type: number;
slug: string;
Expand All @@ -24,7 +24,7 @@ interface PostData {
content: string;
}

interface ReplyContent {
export interface ReplyContent {
id: number;
author: UserSummary;
time: number;
Expand Down

0 comments on commit 2ccc2ee

Please sign in to comment.