Skip to content

Commit

Permalink
自动保存最新回复改变的帖子
Browse files Browse the repository at this point in the history
  • Loading branch information
wxh06 committed Jun 28, 2023
1 parent 5d132cc commit 2f0e741
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
29 changes: 28 additions & 1 deletion packages/archive/src/lib/list.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { BaseLogger } from "pino";
import type { PrismaClient } from "@prisma/client";
import { getReponse } from "./parser";

interface LegacyDiscussList {
Expand Down Expand Up @@ -34,6 +35,7 @@ interface LegacyDiscussList {

export default async function getDiscussionList(
logger: BaseLogger,
prisma: PrismaClient,
page: number,
after: number
) {
Expand All @@ -44,7 +46,32 @@ export default async function getDiscussionList(
const {
data: { result },
} = (await response.json()) as LegacyDiscussList;
const saved = Object.fromEntries(
(
await prisma.discussion.findMany({
select: {
id: true,
replies: { select: { time: true }, orderBy: { id: "desc" }, take: 1 },
},
where: {
id: {
in: result
.filter((post) => post.SubmitTime < after)
.map((post) => post.PostID),
},
},
})
).map(({ id, replies }) => [id, replies[0]?.time])
);
return result
.filter((post) => post.SubmitTime >= after)
.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)))
)
.map((post) => post.PostID);
}
4 changes: 2 additions & 2 deletions packages/archive/src/plugins/cron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export default fastifyPlugin(
function cron(fastify, options, done) {
const save: () => Promise<unknown> = () =>
(async (after) => [
...(await getDiscussionList(fastify.log, 1, after)),
...(await getDiscussionList(fastify.log, 2, after)),
...(await getDiscussionList(fastify.log, fastify.prisma, 1, after)),
...(await getDiscussionList(fastify.log, fastify.prisma, 2, after)),
])(Math.floor(new Date().getTime() / 1000) - 86400)
.then((discussions) => {
return discussions.reduce(
Expand Down

0 comments on commit 2f0e741

Please sign in to comment.