From 8699094bed2ffc3e5cbc1978c38cdf4a98707ce2 Mon Sep 17 00:00:00 2001 From: Yufan Sheng Date: Tue, 10 Dec 2024 15:15:11 +0800 Subject: [PATCH] feat: change the latest comments query for showing more users. --- package-lock.json | 64 ++++++++++++++++++++--------------------- package.json | 2 +- src/helpers/db/query.ts | 31 ++++++++++++++++++-- 3 files changed, 62 insertions(+), 35 deletions(-) diff --git a/package-lock.json b/package-lock.json index 446f1d1..92da096 100644 --- a/package-lock.json +++ b/package-lock.json @@ -29,7 +29,7 @@ "@astrojs/check": "^0.9.4", "@biomejs/biome": "^1.9.4", "@napi-rs/canvas": "^0.1.65", - "@shikijs/transformers": "^1.24.1", + "@shikijs/transformers": "^1.24.2", "@types/lodash": "^4.17.13", "@types/luxon": "^3.4.2", "@types/node": "^22.10.1", @@ -2014,54 +2014,54 @@ ] }, "node_modules/@shikijs/core": { - "version": "1.24.1", - "resolved": "https://registry.npmjs.org/@shikijs/core/-/core-1.24.1.tgz", - "integrity": "sha512-3q/9oarMVcLqJ+NQOdKL40dJVq/UKCsiWXz3QRQPBglHqa8dDJ0p6TuMuk2gHphy5FZcvFtg4UHBgpW0JtZ8+A==", + "version": "1.24.2", + "resolved": "https://registry.npmjs.org/@shikijs/core/-/core-1.24.2.tgz", + "integrity": "sha512-BpbNUSKIwbKrRRA+BQj0BEWSw+8kOPKDJevWeSE/xIqGX7K0xrCZQ9kK0nnEQyrzsUoka1l81ZtJ2mGaCA32HQ==", "license": "MIT", "dependencies": { - "@shikijs/engine-javascript": "1.24.1", - "@shikijs/engine-oniguruma": "1.24.1", - "@shikijs/types": "1.24.1", + "@shikijs/engine-javascript": "1.24.2", + "@shikijs/engine-oniguruma": "1.24.2", + "@shikijs/types": "1.24.2", "@shikijs/vscode-textmate": "^9.3.0", "@types/hast": "^3.0.4", "hast-util-to-html": "^9.0.3" } }, "node_modules/@shikijs/engine-javascript": { - "version": "1.24.1", - "resolved": "https://registry.npmjs.org/@shikijs/engine-javascript/-/engine-javascript-1.24.1.tgz", - "integrity": "sha512-lNgUSHYDYaQ6daj4lJJqcY2Ru9LgHwpFoposJkRVRPh21Yg4kaPFRhzaWoSg3PliwcDOpDuMy3xsmQaJp201Fg==", + "version": "1.24.2", + "resolved": "https://registry.npmjs.org/@shikijs/engine-javascript/-/engine-javascript-1.24.2.tgz", + "integrity": "sha512-EqsmYBJdLEwEiO4H+oExz34a5GhhnVp+jH9Q/XjPjmBPc6TE/x4/gD0X3i0EbkKKNqXYHHJTJUpOLRQNkEzS9Q==", "license": "MIT", "dependencies": { - "@shikijs/types": "1.24.1", + "@shikijs/types": "1.24.2", "@shikijs/vscode-textmate": "^9.3.0", "oniguruma-to-es": "0.7.0" } }, "node_modules/@shikijs/engine-oniguruma": { - "version": "1.24.1", - "resolved": "https://registry.npmjs.org/@shikijs/engine-oniguruma/-/engine-oniguruma-1.24.1.tgz", - "integrity": "sha512-KdrTIBIONWd+Xs61eh8HdIpfigtrseat9dpARvaOe2x0g/FNTbwbkGr3y92VSOVD1XotzEskh3v/nCzyWjkf7g==", + "version": "1.24.2", + "resolved": "https://registry.npmjs.org/@shikijs/engine-oniguruma/-/engine-oniguruma-1.24.2.tgz", + "integrity": "sha512-ZN6k//aDNWRJs1uKB12pturKHh7GejKugowOFGAuG7TxDRLod1Bd5JhpOikOiFqPmKjKEPtEA6mRCf7q3ulDyQ==", "license": "MIT", "dependencies": { - "@shikijs/types": "1.24.1", + "@shikijs/types": "1.24.2", "@shikijs/vscode-textmate": "^9.3.0" } }, "node_modules/@shikijs/transformers": { - "version": "1.24.1", - "resolved": "https://registry.npmjs.org/@shikijs/transformers/-/transformers-1.24.1.tgz", - "integrity": "sha512-IQmk8o0RNlaQ3Ig0Urx4bfen/zvS5St6as7o0q+bTDfbuFjqUMNjoMMKMN1Lu66mAh83seURwPLi2EjjLTD6+w==", + "version": "1.24.2", + "resolved": "https://registry.npmjs.org/@shikijs/transformers/-/transformers-1.24.2.tgz", + "integrity": "sha512-cIwn8YSwO3bsWKJ+pezcXY1Vq0BVwvuLes1TZSC5+Awi6Tsfqhf3vBahOIqZK1rraMKOti2VEAEF/95oXMig1w==", "dev": true, "license": "MIT", "dependencies": { - "shiki": "1.24.1" + "shiki": "1.24.2" } }, "node_modules/@shikijs/types": { - "version": "1.24.1", - "resolved": "https://registry.npmjs.org/@shikijs/types/-/types-1.24.1.tgz", - "integrity": "sha512-ZwZFbShFY/APfKNt3s9Gv8rhTm29GodSKsOW66X6N+HGsZuaHalE1VUEX4fv93UXHTZTLjb3uxn63F96RhGfXw==", + "version": "1.24.2", + "resolved": "https://registry.npmjs.org/@shikijs/types/-/types-1.24.2.tgz", + "integrity": "sha512-bdeWZiDtajGLG9BudI0AHet0b6e7FbR0EsE4jpGaI0YwHm/XJunI9+3uZnzFtX65gsyJ6ngCIWUfA4NWRPnBkQ==", "license": "MIT", "dependencies": { "@shikijs/vscode-textmate": "^9.3.0", @@ -4408,9 +4408,9 @@ } }, "node_modules/magic-string": { - "version": "0.30.14", - "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.30.14.tgz", - "integrity": "sha512-5c99P1WKTed11ZC0HMJOj6CDIue6F8ySu+bJL+85q1zBEIY8IklrJ1eiKC2NDRh3Ct3FcvmJPyQHb9erXMTJNw==", + "version": "0.30.15", + "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.30.15.tgz", + "integrity": "sha512-zXeaYRgZ6ldS1RJJUrMrYgNJ4fdwnyI6tVqoiIhyCyv5IVTK9BU8Ic2l253GGETQHxI4HNUwhJ3fjDhKqEoaAw==", "license": "MIT", "dependencies": { "@jridgewell/sourcemap-codec": "^1.5.0" @@ -6998,15 +6998,15 @@ } }, "node_modules/shiki": { - "version": "1.24.1", - "resolved": "https://registry.npmjs.org/shiki/-/shiki-1.24.1.tgz", - "integrity": "sha512-/qByWMg05+POb63c/OvnrU17FcCUa34WU4F6FCrd/mjDPEDPl8YUNRkRMbo8l3iYMLydfCgxi1r37JFoSw8A4A==", + "version": "1.24.2", + "resolved": "https://registry.npmjs.org/shiki/-/shiki-1.24.2.tgz", + "integrity": "sha512-TR1fi6mkRrzW+SKT5G6uKuc32Dj2EEa7Kj0k8kGqiBINb+C1TiflVOiT9ta6GqOJtC4fraxO5SLUaKBcSY38Fg==", "license": "MIT", "dependencies": { - "@shikijs/core": "1.24.1", - "@shikijs/engine-javascript": "1.24.1", - "@shikijs/engine-oniguruma": "1.24.1", - "@shikijs/types": "1.24.1", + "@shikijs/core": "1.24.2", + "@shikijs/engine-javascript": "1.24.2", + "@shikijs/engine-oniguruma": "1.24.2", + "@shikijs/types": "1.24.2", "@shikijs/vscode-textmate": "^9.3.0", "@types/hast": "^3.0.4" } diff --git a/package.json b/package.json index cdf1477..4f841c1 100644 --- a/package.json +++ b/package.json @@ -62,7 +62,7 @@ "@astrojs/check": "^0.9.4", "@biomejs/biome": "^1.9.4", "@napi-rs/canvas": "^0.1.65", - "@shikijs/transformers": "^1.24.1", + "@shikijs/transformers": "^1.24.2", "@types/lodash": "^4.17.13", "@types/luxon": "^3.4.2", "@types/node": "^22.10.1", diff --git a/src/helpers/db/query.ts b/src/helpers/db/query.ts index 46c515e..a3d7ea8 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, type InferSelectModel } from 'drizzle-orm'; +import { and, desc, eq, inArray, isNull, not, sql, type InferSelectModel } from 'drizzle-orm'; export interface Comment { title: string; @@ -56,6 +56,27 @@ export const queryUserId = async (email: string): Promise => { }; export const latestComments = async (): Promise => { + 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 .select({ id: atk_comments.id, @@ -67,7 +88,13 @@ export const latestComments = async (): Promise => { .from(atk_comments) .innerJoin(atk_pages, eq(atk_comments.page_key, atk_pages.key)) .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)) .limit(options.settings.sidebar.comment);