fix: the page view counter for new articles.

This commit is contained in:
Yufan Sheng 2024-06-30 19:02:39 +08:00
parent 6f82976175
commit d55b50ce34
Signed by: syhily
GPG Key ID: DEB186763C308C31
3 changed files with 18 additions and 14 deletions

View File

@ -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 Comment from '@/components/comment/Comment.astro';
import { urlJoin } from '@/helpers/tools'; import { urlJoin } from '@/helpers/tools';
import options from '@/options'; import options from '@/options';
@ -12,6 +12,9 @@ interface Props {
const { commentKey, title } = Astro.props; const { commentKey, title } = Astro.props;
const comments = await loadComments(commentKey, title, 0); const comments = await loadComments(commentKey, title, 0);
// Increase the PV.
await increaseViews(commentKey, title);
--- ---
<div id="comments" class="comments py-5"> <div id="comments" class="comments py-5">

View File

@ -1,5 +1,4 @@
import type { Comment, CommentItem, CommentReq, CommentResp, Comments, ErrorResp } from '@/components/comment/types'; import type { Comment, CommentItem, CommentReq, CommentResp, Comments, ErrorResp } from '@/components/comment/types';
import { increaseViews } from '@/helpers/db/query';
import { urlJoin } from '@/helpers/tools'; import { urlJoin } from '@/helpers/tools';
import options from '@/options'; import options from '@/options';
import { ARTALK_HOST } from 'astro:env/server'; import { ARTALK_HOST } from 'astro:env/server';
@ -30,12 +29,23 @@ export const loadComments = async (key: string, title: string | null, offset: nu
return null; return null;
}); });
// Increase the PV.
await increaseViews(key);
return data != null ? (data as Comments) : data; 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<ErrorResp | CommentResp> => { export const createComment = async (req: CommentReq): Promise<ErrorResp | CommentResp> => {
const response = await fetch(urlJoin(server, '/api/v2/comments'), { const response = await fetch(urlJoin(server, '/api/v2/comments'), {
method: 'POST', method: 'POST',

View File

@ -76,15 +76,6 @@ export const latestComments = async (): Promise<Comment[]> => {
}); });
}; };
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, '/'); const generatePageKey = (permalink: string): string => urlJoin(options.website, permalink, '/');
export const increaseLikes = async (permalink: string): Promise<{ likes: number; token: string }> => { export const increaseLikes = async (permalink: string): Promise<{ likes: number; token: string }> => {