From d55b50ce34b22baae8d69f8ce1058e58bcffba73 Mon Sep 17 00:00:00 2001 From: Yufan Sheng Date: Sun, 30 Jun 2024 19:02:39 +0800 Subject: [PATCH] fix: the page view counter for new articles. --- src/components/comment/Comments.astro | 5 ++++- src/components/comment/artalk.ts | 18 ++++++++++++++---- src/helpers/db/query.ts | 9 --------- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/components/comment/Comments.astro b/src/components/comment/Comments.astro index a2d1b6c..bde653d 100644 --- a/src/components/comment/Comments.astro +++ b/src/components/comment/Comments.astro @@ -1,5 +1,5 @@ --- -import { loadComments } from '@/components/comment/artalk'; +import { increaseViews, loadComments } from '@/components/comment/artalk'; import Comment from '@/components/comment/Comment.astro'; import { urlJoin } from '@/helpers/tools'; import options from '@/options'; @@ -12,6 +12,9 @@ interface Props { const { commentKey, title } = Astro.props; const comments = await loadComments(commentKey, title, 0); + +// Increase the PV. +await increaseViews(commentKey, title); ---
diff --git a/src/components/comment/artalk.ts b/src/components/comment/artalk.ts index a5e3445..8fe709f 100644 --- a/src/components/comment/artalk.ts +++ b/src/components/comment/artalk.ts @@ -1,5 +1,4 @@ import type { Comment, CommentItem, CommentReq, CommentResp, Comments, ErrorResp } from '@/components/comment/types'; -import { increaseViews } from '@/helpers/db/query'; import { urlJoin } from '@/helpers/tools'; import options from '@/options'; import { ARTALK_HOST } from 'astro:env/server'; @@ -30,12 +29,23 @@ export const loadComments = async (key: string, title: string | null, offset: nu return null; }); - // Increase the PV. - await increaseViews(key); - return data != null ? (data as Comments) : data; }; +export const increaseViews = async (key: string, title: string) => { + await fetch(urlJoin(server, '/api/v2/pages/pv'), { + method: 'POST', + headers: { + 'Content-type': 'application/json; charset=UTF-8', + }, + body: JSON.stringify({ + page_key: key, + page_title: title, + site_name: options.title, + }), + }); +}; + export const createComment = async (req: CommentReq): Promise => { const response = await fetch(urlJoin(server, '/api/v2/comments'), { method: 'POST', diff --git a/src/helpers/db/query.ts b/src/helpers/db/query.ts index 2590a07..de526b9 100644 --- a/src/helpers/db/query.ts +++ b/src/helpers/db/query.ts @@ -76,15 +76,6 @@ export const latestComments = async (): Promise => { }); }; -export const increaseViews = async (pageKey: string) => { - await db - .update(atk_pages) - .set({ - pv: sql`${atk_pages.pv} + 1`, - }) - .where(eq(atk_pages.key, sql`${pageKey}`)); -}; - const generatePageKey = (permalink: string): string => urlJoin(options.website, permalink, '/'); export const increaseLikes = async (permalink: string): Promise<{ likes: number; token: string }> => {