refactor: move misc/count-same-renotes.ts to backend-rs

This commit is contained in:
sup39 2024-01-23 11:56:19 +09:00
parent 145cba9ae5
commit 07b5e90887
Signed by: sup39
GPG key ID: 111C00916C1641E5
7 changed files with 35 additions and 24 deletions

View file

@ -295,7 +295,7 @@ if (!nativeBinding) {
throw new Error(`Failed to load native binding`) throw new Error(`Failed to load native binding`)
} }
const { EnvConfig, readEnvironmentConfig, readServerConfig, JsDbConn, initDatabase, AntennaSrcEnum, MutedNoteReasonEnum, NoteVisibilityEnum, NotificationTypeEnum, PageVisibilityEnum, PollNotevisibilityEnum, RelayStatusEnum, UserEmojimodpermEnum, UserProfileFfvisibilityEnum, UserProfileMutingnotificationtypesEnum, stringToAcct, acctToString, getFullApAccount, isSelfHost, extractHost, toPuny, toPunyOptional, convertToHiddenPost, sqlLikeEscape, safeForSql, formatMilliseconds, nativeInitIdGenerator, nativeCreateId, nativeGetTimestamp, fetchMeta, metaToPugArgs, genString, IdConvertType, convertId } = nativeBinding const { EnvConfig, readEnvironmentConfig, readServerConfig, JsDbConn, initDatabase, AntennaSrcEnum, MutedNoteReasonEnum, NoteVisibilityEnum, NotificationTypeEnum, PageVisibilityEnum, PollNotevisibilityEnum, RelayStatusEnum, UserEmojimodpermEnum, UserProfileFfvisibilityEnum, UserProfileMutingnotificationtypesEnum, stringToAcct, acctToString, getFullApAccount, isSelfHost, extractHost, toPuny, toPunyOptional, convertToHiddenPost, countSameRenotes, sqlLikeEscape, safeForSql, formatMilliseconds, nativeInitIdGenerator, nativeCreateId, nativeGetTimestamp, fetchMeta, metaToPugArgs, genString, IdConvertType, convertId } = nativeBinding
module.exports.EnvConfig = EnvConfig module.exports.EnvConfig = EnvConfig
module.exports.readEnvironmentConfig = readEnvironmentConfig module.exports.readEnvironmentConfig = readEnvironmentConfig
@ -320,6 +320,7 @@ module.exports.extractHost = extractHost
module.exports.toPuny = toPuny module.exports.toPuny = toPuny
module.exports.toPunyOptional = toPunyOptional module.exports.toPunyOptional = toPunyOptional
module.exports.convertToHiddenPost = convertToHiddenPost module.exports.convertToHiddenPost = convertToHiddenPost
module.exports.countSameRenotes = countSameRenotes
module.exports.sqlLikeEscape = sqlLikeEscape module.exports.sqlLikeEscape = sqlLikeEscape
module.exports.safeForSql = safeForSql module.exports.safeForSql = safeForSql
module.exports.formatMilliseconds = formatMilliseconds module.exports.formatMilliseconds = formatMilliseconds

View file

@ -0,0 +1,19 @@
use crate::database::{JsDbConn, NapiDbErrExt};
use crate::model::entity::note;
use sea_orm::prelude::*;
#[napi_derive::napi]
pub async fn count_same_renotes(
conn: &JsDbConn,
user_id: String,
renote_id: String,
exclude_note_id: Option<String>,
) -> napi::Result<u64> {
let mut query = note::Entity::find()
.filter(note::Column::UserId.eq(user_id))
.filter(note::Column::RenoteId.eq(renote_id));
if let Some(exclude_note_id) = exclude_note_id {
query = query.filter(note::Column::Id.ne(exclude_note_id));
}
query.count(conn.inner()).await.map_err(NapiDbErrExt::into)
}

View file

@ -1,6 +1,7 @@
pub mod acct; pub mod acct;
pub mod convert_host; pub mod convert_host;
pub mod convert_to_hidden_post; pub mod convert_to_hidden_post;
pub mod count_same_renotes;
pub mod escape_sql; pub mod escape_sql;
pub mod format_milliseconds; pub mod format_milliseconds;
pub mod id; pub mod id;

View file

@ -2,6 +2,7 @@ import {
readServerConfig, readServerConfig,
readEnvironmentConfig, readEnvironmentConfig,
initDatabase, initDatabase,
countSameRenotes as countSameRenotesImpl,
fetchMeta as fetchMetaImpl, fetchMeta as fetchMetaImpl,
getFullApAccount as getFullApAccountImpl, getFullApAccount as getFullApAccountImpl,
isSelfHost as isSelfHostImpl, isSelfHost as isSelfHostImpl,
@ -13,6 +14,14 @@ const dbPromise = initDatabase(serverConfig.db);
type Option<T> = T | null | undefined; type Option<T> = T | null | undefined;
type WithoutState<F> = F extends (state: any, ...args: infer P) => infer R
? (...args: P) => R
: never;
export const countSameRenotes: WithoutState<typeof countSameRenotesImpl> = (
...args
) => dbPromise.then((db) => countSameRenotesImpl(db, ...args));
export const fetchMeta = (invalidateCache = false) => export const fetchMeta = (invalidateCache = false) =>
dbPromise.then((db) => fetchMetaImpl(db, invalidateCache)); dbPromise.then((db) => fetchMetaImpl(db, invalidateCache));

View file

@ -1,19 +0,0 @@
import { Notes } from "@/models/index.js";
export async function countSameRenotes(
userId: string,
renoteId: string,
excludeNoteId: string | undefined,
): Promise<number> {
// 指定したユーザーの指定したノートのリノートがいくつあるか数える
const query = Notes.createQueryBuilder("note")
.where("note.userId = :userId", { userId })
.andWhere("note.renoteId = :renoteId", { renoteId });
// 指定した投稿を除く
if (excludeNoteId) {
query.andWhere("note.id != :excludeNoteId", { excludeNoteId });
}
return await query.getCount();
}

View file

@ -47,7 +47,7 @@ import { isDuplicateKeyValueError } from "@/misc/is-duplicate-key-value-error.js
import { checkHitAntenna } from "@/misc/check-hit-antenna.js"; import { checkHitAntenna } from "@/misc/check-hit-antenna.js";
import { getWordHardMute } from "@/misc/check-word-mute.js"; import { getWordHardMute } from "@/misc/check-word-mute.js";
import { addNoteToAntenna } from "@/services/add-note-to-antenna.js"; import { addNoteToAntenna } from "@/services/add-note-to-antenna.js";
import { countSameRenotes } from "@/misc/count-same-renotes.js"; import { countSameRenotes } from "@/misc/backend-rs.js";
import { deliverToRelays, getCachedRelays } from "../relay.js"; import { deliverToRelays, getCachedRelays } from "../relay.js";
import type { Channel } from "@/models/entities/channel.js"; import type { Channel } from "@/models/entities/channel.js";
import { normalizeForSearch } from "@/misc/normalize-for-search.js"; import { normalizeForSearch } from "@/misc/normalize-for-search.js";
@ -412,7 +412,7 @@ export default async (
if ( if (
data.renote && data.renote &&
!user.isBot && !user.isBot &&
(await countSameRenotes(user.id, data.renote.id, note.id)) === 0 (await countSameRenotes(user.id, data.renote.id, note.id)) === 0n
) { ) {
incRenoteCount(data.renote); incRenoteCount(data.renote);
} }

View file

@ -13,7 +13,7 @@ import {
deliverToFollowers, deliverToFollowers,
deliverToUser, deliverToUser,
} from "@/remote/activitypub/deliver-manager.js"; } from "@/remote/activitypub/deliver-manager.js";
import { countSameRenotes } from "@/misc/count-same-renotes.js"; import { countSameRenotes } from "@/misc/backend-rs.js";
import { registerOrFetchInstanceDoc } from "@/services/register-or-fetch-instance-doc.js"; import { registerOrFetchInstanceDoc } from "@/services/register-or-fetch-instance-doc.js";
import { deliverToRelays } from "@/services/relay.js"; import { deliverToRelays } from "@/services/relay.js";
// import meilisearch from "@/db/meilisearch.js"; // import meilisearch from "@/db/meilisearch.js";
@ -34,7 +34,7 @@ export default async function (
// この投稿を除く指定したユーザーによる指定したノートのリノートが存在しないとき // この投稿を除く指定したユーザーによる指定したノートのリノートが存在しないとき
if ( if (
note.renoteId && note.renoteId &&
(await countSameRenotes(user.id, note.renoteId, note.id)) === 0 && (await countSameRenotes(user.id, note.renoteId, note.id)) === 0n &&
deleteFromDb deleteFromDb
) { ) {
Notes.decrement({ id: note.renoteId }, "renoteCount", 1); Notes.decrement({ id: note.renoteId }, "renoteCount", 1);