From 14eac0f1d478ff92bca6e203b24171251f06c765 Mon Sep 17 00:00:00 2001 From: Yufan Sheng Date: Tue, 15 Oct 2024 17:32:34 +0800 Subject: [PATCH] feat: replace the comment user name with the existing comment user. --- src/components/comment/Comments.astro | 5 +++-- src/components/comment/artalk.ts | 22 ++++++++-------------- src/helpers/db/query.ts | 21 ++++++++++++++++++++- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/components/comment/Comments.astro b/src/components/comment/Comments.astro index bde653d..462a5f9 100644 --- a/src/components/comment/Comments.astro +++ b/src/components/comment/Comments.astro @@ -1,6 +1,7 @@ --- -import { increaseViews, loadComments } from '@/components/comment/artalk'; +import { loadComments } from '@/components/comment/artalk'; import Comment from '@/components/comment/Comment.astro'; +import { increaseViews } from '@/helpers/db/query'; import { urlJoin } from '@/helpers/tools'; import options from '@/options'; @@ -14,7 +15,7 @@ const { commentKey, title } = Astro.props; const comments = await loadComments(commentKey, title, 0); // Increase the PV. -await increaseViews(commentKey, title); +await increaseViews(commentKey); ---
diff --git a/src/components/comment/artalk.ts b/src/components/comment/artalk.ts index fe44fb6..806c237 100644 --- a/src/components/comment/artalk.ts +++ b/src/components/comment/artalk.ts @@ -1,4 +1,5 @@ import type { Comment, CommentItem, CommentReq, CommentResp, Comments, ErrorResp } from '@/components/comment/types'; +import { queryUser } from '@/helpers/db/query'; import { urlJoin } from '@/helpers/tools'; import options from '@/options'; import { ARTALK_HOST } from 'astro:env/server'; @@ -32,21 +33,14 @@ export const loadComments = async (key: string, title: string | null, offset: nu 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 user = await queryUser(req.email); + if (user !== null && user.name !== null) { + // Replace the comment user name for avoiding the duplicated users creation. + // We may add the commenter account management in the future. + req.name = user.name; + } + const response = await fetch(urlJoin(server, '/api/v2/comments'), { method: 'POST', headers: { diff --git a/src/helpers/db/query.ts b/src/helpers/db/query.ts index 2d12a82..b4f007f 100644 --- a/src/helpers/db/query.ts +++ b/src/helpers/db/query.ts @@ -2,7 +2,7 @@ import { db } from '@/helpers/db/pool'; import { atk_comments, atk_likes, atk_pages, atk_users } from '@/helpers/db/schema'; import { makeToken, urlJoin } from '@/helpers/tools'; import options from '@/options'; -import { and, desc, eq, isNull, not, sql } from 'drizzle-orm'; +import { and, desc, eq, isNull, not, sql, type InferSelectModel } from 'drizzle-orm'; export interface Comment { title: string; @@ -11,6 +11,18 @@ export interface Comment { permalink: string; } +export const queryUser = async (email: string): Promise | null> => { + const results = await db + .select() + .from(atk_users) + .where(eq(atk_users.email, sql`${email}`)); + if (results.length === 0) { + return null; + } + + return results[0]; +}; + export const queryEmail = async (id: number): Promise => { const results = await db .select({ @@ -152,3 +164,10 @@ export const queryLikesAndViews = async (permalink: string): Promise<[number, nu return results.length > 0 ? [results[0].like ?? 0, results[0].view ?? 0] : [0, 0]; }; + +export const increaseViews = async (key: string): Promise => { + await db + .update(atk_pages) + .set({ pv: sql`${atk_pages.pv} + 1` }) + .where(eq(atk_pages.key, sql`${key}`)); +};