feat: replace the comment user name with the existing comment user.

This commit is contained in:
Yufan Sheng 2024-10-15 17:32:34 +08:00
parent 68c06738c3
commit 14eac0f1d4
Signed by: syhily
GPG Key ID: 9D18A22A7DCD5A9B
3 changed files with 31 additions and 17 deletions

View File

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

View File

@ -1,4 +1,5 @@
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 { queryUser } 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';
@ -32,21 +33,14 @@ export const loadComments = async (key: string, title: string | null, offset: nu
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 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'), { const response = await fetch(urlJoin(server, '/api/v2/comments'), {
method: 'POST', method: 'POST',
headers: { headers: {

View File

@ -2,7 +2,7 @@ import { db } from '@/helpers/db/pool';
import { atk_comments, atk_likes, atk_pages, atk_users } from '@/helpers/db/schema'; import { atk_comments, atk_likes, atk_pages, atk_users } from '@/helpers/db/schema';
import { makeToken, urlJoin } from '@/helpers/tools'; import { makeToken, urlJoin } from '@/helpers/tools';
import options from '@/options'; 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 { export interface Comment {
title: string; title: string;
@ -11,6 +11,18 @@ export interface Comment {
permalink: string; permalink: string;
} }
export const queryUser = async (email: string): Promise<InferSelectModel<typeof atk_users> | 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<string | null> => { export const queryEmail = async (id: number): Promise<string | null> => {
const results = await db const results = await db
.select({ .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]; return results.length > 0 ? [results[0].like ?? 0, results[0].view ?? 0] : [0, 0];
}; };
export const increaseViews = async (key: string): Promise<void> => {
await db
.update(atk_pages)
.set({ pv: sql`${atk_pages.pv} + 1` })
.where(eq(atk_pages.key, sql`${key}`));
};