feat: change the latest comments query for showing more users.

This commit is contained in:
Yufan Sheng 2024-12-10 15:15:11 +08:00
parent 92aca0943a
commit 8699094bed
Signed by: syhily
GPG Key ID: 9D18A22A7DCD5A9B
3 changed files with 62 additions and 35 deletions

64
package-lock.json generated
View File

@ -29,7 +29,7 @@
"@astrojs/check": "^0.9.4", "@astrojs/check": "^0.9.4",
"@biomejs/biome": "^1.9.4", "@biomejs/biome": "^1.9.4",
"@napi-rs/canvas": "^0.1.65", "@napi-rs/canvas": "^0.1.65",
"@shikijs/transformers": "^1.24.1", "@shikijs/transformers": "^1.24.2",
"@types/lodash": "^4.17.13", "@types/lodash": "^4.17.13",
"@types/luxon": "^3.4.2", "@types/luxon": "^3.4.2",
"@types/node": "^22.10.1", "@types/node": "^22.10.1",
@ -2014,54 +2014,54 @@
] ]
}, },
"node_modules/@shikijs/core": { "node_modules/@shikijs/core": {
"version": "1.24.1", "version": "1.24.2",
"resolved": "https://registry.npmjs.org/@shikijs/core/-/core-1.24.1.tgz", "resolved": "https://registry.npmjs.org/@shikijs/core/-/core-1.24.2.tgz",
"integrity": "sha512-3q/9oarMVcLqJ+NQOdKL40dJVq/UKCsiWXz3QRQPBglHqa8dDJ0p6TuMuk2gHphy5FZcvFtg4UHBgpW0JtZ8+A==", "integrity": "sha512-BpbNUSKIwbKrRRA+BQj0BEWSw+8kOPKDJevWeSE/xIqGX7K0xrCZQ9kK0nnEQyrzsUoka1l81ZtJ2mGaCA32HQ==",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@shikijs/engine-javascript": "1.24.1", "@shikijs/engine-javascript": "1.24.2",
"@shikijs/engine-oniguruma": "1.24.1", "@shikijs/engine-oniguruma": "1.24.2",
"@shikijs/types": "1.24.1", "@shikijs/types": "1.24.2",
"@shikijs/vscode-textmate": "^9.3.0", "@shikijs/vscode-textmate": "^9.3.0",
"@types/hast": "^3.0.4", "@types/hast": "^3.0.4",
"hast-util-to-html": "^9.0.3" "hast-util-to-html": "^9.0.3"
} }
}, },
"node_modules/@shikijs/engine-javascript": { "node_modules/@shikijs/engine-javascript": {
"version": "1.24.1", "version": "1.24.2",
"resolved": "https://registry.npmjs.org/@shikijs/engine-javascript/-/engine-javascript-1.24.1.tgz", "resolved": "https://registry.npmjs.org/@shikijs/engine-javascript/-/engine-javascript-1.24.2.tgz",
"integrity": "sha512-lNgUSHYDYaQ6daj4lJJqcY2Ru9LgHwpFoposJkRVRPh21Yg4kaPFRhzaWoSg3PliwcDOpDuMy3xsmQaJp201Fg==", "integrity": "sha512-EqsmYBJdLEwEiO4H+oExz34a5GhhnVp+jH9Q/XjPjmBPc6TE/x4/gD0X3i0EbkKKNqXYHHJTJUpOLRQNkEzS9Q==",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@shikijs/types": "1.24.1", "@shikijs/types": "1.24.2",
"@shikijs/vscode-textmate": "^9.3.0", "@shikijs/vscode-textmate": "^9.3.0",
"oniguruma-to-es": "0.7.0" "oniguruma-to-es": "0.7.0"
} }
}, },
"node_modules/@shikijs/engine-oniguruma": { "node_modules/@shikijs/engine-oniguruma": {
"version": "1.24.1", "version": "1.24.2",
"resolved": "https://registry.npmjs.org/@shikijs/engine-oniguruma/-/engine-oniguruma-1.24.1.tgz", "resolved": "https://registry.npmjs.org/@shikijs/engine-oniguruma/-/engine-oniguruma-1.24.2.tgz",
"integrity": "sha512-KdrTIBIONWd+Xs61eh8HdIpfigtrseat9dpARvaOe2x0g/FNTbwbkGr3y92VSOVD1XotzEskh3v/nCzyWjkf7g==", "integrity": "sha512-ZN6k//aDNWRJs1uKB12pturKHh7GejKugowOFGAuG7TxDRLod1Bd5JhpOikOiFqPmKjKEPtEA6mRCf7q3ulDyQ==",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@shikijs/types": "1.24.1", "@shikijs/types": "1.24.2",
"@shikijs/vscode-textmate": "^9.3.0" "@shikijs/vscode-textmate": "^9.3.0"
} }
}, },
"node_modules/@shikijs/transformers": { "node_modules/@shikijs/transformers": {
"version": "1.24.1", "version": "1.24.2",
"resolved": "https://registry.npmjs.org/@shikijs/transformers/-/transformers-1.24.1.tgz", "resolved": "https://registry.npmjs.org/@shikijs/transformers/-/transformers-1.24.2.tgz",
"integrity": "sha512-IQmk8o0RNlaQ3Ig0Urx4bfen/zvS5St6as7o0q+bTDfbuFjqUMNjoMMKMN1Lu66mAh83seURwPLi2EjjLTD6+w==", "integrity": "sha512-cIwn8YSwO3bsWKJ+pezcXY1Vq0BVwvuLes1TZSC5+Awi6Tsfqhf3vBahOIqZK1rraMKOti2VEAEF/95oXMig1w==",
"dev": true, "dev": true,
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"shiki": "1.24.1" "shiki": "1.24.2"
} }
}, },
"node_modules/@shikijs/types": { "node_modules/@shikijs/types": {
"version": "1.24.1", "version": "1.24.2",
"resolved": "https://registry.npmjs.org/@shikijs/types/-/types-1.24.1.tgz", "resolved": "https://registry.npmjs.org/@shikijs/types/-/types-1.24.2.tgz",
"integrity": "sha512-ZwZFbShFY/APfKNt3s9Gv8rhTm29GodSKsOW66X6N+HGsZuaHalE1VUEX4fv93UXHTZTLjb3uxn63F96RhGfXw==", "integrity": "sha512-bdeWZiDtajGLG9BudI0AHet0b6e7FbR0EsE4jpGaI0YwHm/XJunI9+3uZnzFtX65gsyJ6ngCIWUfA4NWRPnBkQ==",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@shikijs/vscode-textmate": "^9.3.0", "@shikijs/vscode-textmate": "^9.3.0",
@ -4408,9 +4408,9 @@
} }
}, },
"node_modules/magic-string": { "node_modules/magic-string": {
"version": "0.30.14", "version": "0.30.15",
"resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.30.14.tgz", "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.30.15.tgz",
"integrity": "sha512-5c99P1WKTed11ZC0HMJOj6CDIue6F8ySu+bJL+85q1zBEIY8IklrJ1eiKC2NDRh3Ct3FcvmJPyQHb9erXMTJNw==", "integrity": "sha512-zXeaYRgZ6ldS1RJJUrMrYgNJ4fdwnyI6tVqoiIhyCyv5IVTK9BU8Ic2l253GGETQHxI4HNUwhJ3fjDhKqEoaAw==",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@jridgewell/sourcemap-codec": "^1.5.0" "@jridgewell/sourcemap-codec": "^1.5.0"
@ -6998,15 +6998,15 @@
} }
}, },
"node_modules/shiki": { "node_modules/shiki": {
"version": "1.24.1", "version": "1.24.2",
"resolved": "https://registry.npmjs.org/shiki/-/shiki-1.24.1.tgz", "resolved": "https://registry.npmjs.org/shiki/-/shiki-1.24.2.tgz",
"integrity": "sha512-/qByWMg05+POb63c/OvnrU17FcCUa34WU4F6FCrd/mjDPEDPl8YUNRkRMbo8l3iYMLydfCgxi1r37JFoSw8A4A==", "integrity": "sha512-TR1fi6mkRrzW+SKT5G6uKuc32Dj2EEa7Kj0k8kGqiBINb+C1TiflVOiT9ta6GqOJtC4fraxO5SLUaKBcSY38Fg==",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@shikijs/core": "1.24.1", "@shikijs/core": "1.24.2",
"@shikijs/engine-javascript": "1.24.1", "@shikijs/engine-javascript": "1.24.2",
"@shikijs/engine-oniguruma": "1.24.1", "@shikijs/engine-oniguruma": "1.24.2",
"@shikijs/types": "1.24.1", "@shikijs/types": "1.24.2",
"@shikijs/vscode-textmate": "^9.3.0", "@shikijs/vscode-textmate": "^9.3.0",
"@types/hast": "^3.0.4" "@types/hast": "^3.0.4"
} }

View File

@ -62,7 +62,7 @@
"@astrojs/check": "^0.9.4", "@astrojs/check": "^0.9.4",
"@biomejs/biome": "^1.9.4", "@biomejs/biome": "^1.9.4",
"@napi-rs/canvas": "^0.1.65", "@napi-rs/canvas": "^0.1.65",
"@shikijs/transformers": "^1.24.1", "@shikijs/transformers": "^1.24.2",
"@types/lodash": "^4.17.13", "@types/lodash": "^4.17.13",
"@types/luxon": "^3.4.2", "@types/luxon": "^3.4.2",
"@types/node": "^22.10.1", "@types/node": "^22.10.1",

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, type InferSelectModel } from 'drizzle-orm'; import { and, desc, eq, inArray, isNull, not, sql, type InferSelectModel } from 'drizzle-orm';
export interface Comment { export interface Comment {
title: string; title: string;
@ -56,6 +56,27 @@ export const queryUserId = async (email: string): Promise<bigint | null> => {
}; };
export const latestComments = async (): Promise<Comment[]> => { export const latestComments = async (): Promise<Comment[]> => {
const latestDistinctCommentsQuery = sql`SELECT id
FROM (
SELECT id,
user_id,
created_at,
ROW_NUMBER() OVER (
PARTITION BY user_id
ORDER BY created_at DESC
) rn
FROM atk_comments
WHERE user_id != 3
AND is_pending = FALSE
) AS most_recent
WHERE rn = 1
ORDER BY created_at DESC
LIMIT ${options.settings.sidebar.comment}`;
const latestDistinctComments = (await db.execute(latestDistinctCommentsQuery)).rows
.map((row) => row.id)
.map((id) => BigInt(`${id}`));
const results = await db const results = await db
.select({ .select({
id: atk_comments.id, id: atk_comments.id,
@ -67,7 +88,13 @@ export const latestComments = async (): Promise<Comment[]> => {
.from(atk_comments) .from(atk_comments)
.innerJoin(atk_pages, eq(atk_comments.page_key, atk_pages.key)) .innerJoin(atk_pages, eq(atk_comments.page_key, atk_pages.key))
.innerJoin(atk_users, eq(atk_comments.user_id, atk_users.id)) .innerJoin(atk_users, eq(atk_comments.user_id, atk_users.id))
.where(and(not(eq(atk_users.email, options.author.email)), eq(atk_comments.is_pending, false))) .where(
and(
not(eq(atk_users.email, options.author.email)),
eq(atk_comments.is_pending, false),
inArray(atk_comments.id, latestDistinctComments),
),
)
.orderBy(desc(atk_comments.created_at)) .orderBy(desc(atk_comments.created_at))
.limit(options.settings.sidebar.comment); .limit(options.settings.sidebar.comment);