refactor: move misc/count-same-renotes.ts to backend-rs
This commit is contained in:
parent
b63c5952d7
commit
bf75da4f95
7 changed files with 34 additions and 24 deletions
|
@ -295,7 +295,7 @@ if (!nativeBinding) {
|
|||
throw new Error(`Failed to load native binding`)
|
||||
}
|
||||
|
||||
const { EnvConfig, readEnvironmentConfig, readServerConfig, JsDbConn, connectToDatabase, 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, connectToDatabase, 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, hasOtherRenoteOfThisNote, genString, IdConvertType, convertId } = nativeBinding
|
||||
|
||||
module.exports.EnvConfig = EnvConfig
|
||||
module.exports.readEnvironmentConfig = readEnvironmentConfig
|
||||
|
@ -328,6 +328,7 @@ module.exports.nativeCreateId = nativeCreateId
|
|||
module.exports.nativeGetTimestamp = nativeGetTimestamp
|
||||
module.exports.fetchMeta = fetchMeta
|
||||
module.exports.metaToPugArgs = metaToPugArgs
|
||||
module.exports.hasOtherRenoteOfThisNote = hasOtherRenoteOfThisNote
|
||||
module.exports.genString = genString
|
||||
module.exports.IdConvertType = IdConvertType
|
||||
module.exports.convertId = convertId
|
||||
|
|
|
@ -5,4 +5,5 @@ pub mod escape_sql;
|
|||
pub mod format_milliseconds;
|
||||
pub mod id;
|
||||
pub mod meta;
|
||||
pub mod note;
|
||||
pub mod random;
|
||||
|
|
25
packages/backend-rs/src/util/note.rs
Normal file
25
packages/backend-rs/src/util/note.rs
Normal file
|
@ -0,0 +1,25 @@
|
|||
use crate::database::{JsDbConn, NapiDbErrExt};
|
||||
use crate::model::entity::note;
|
||||
use sea_orm::prelude::*;
|
||||
|
||||
/**
|
||||
* Returns true if user {user_id}
|
||||
* has any renote of note {renote_id}
|
||||
* except the specified renote {excluded_note_id}
|
||||
*/
|
||||
#[napi_derive::napi]
|
||||
pub async fn has_other_renote_of_this_note(
|
||||
conn: &JsDbConn,
|
||||
user_id: String,
|
||||
renote_id: String,
|
||||
excluded_note_id: String,
|
||||
) -> napi::Result<bool> {
|
||||
note::Entity::find()
|
||||
.filter(note::Column::UserId.eq(user_id))
|
||||
.filter(note::Column::RenoteId.eq(renote_id))
|
||||
.filter(note::Column::Id.ne(excluded_note_id))
|
||||
.one(conn.inner())
|
||||
.await
|
||||
.map(|row| row.is_some())
|
||||
.map_err(NapiDbErrExt::into)
|
||||
}
|
|
@ -4,6 +4,7 @@ import {
|
|||
connectToDatabase,
|
||||
fetchMeta as fetchMetaImpl,
|
||||
getFullApAccount as getFullApAccountImpl,
|
||||
hasOtherRenoteOfThisNote as hasOtherRenoteOfThisNoteImpl,
|
||||
isSelfHost as isSelfHostImpl,
|
||||
JsDbConn,
|
||||
} from "backend-rs";
|
||||
|
@ -20,6 +21,7 @@ const curryDb =
|
|||
dbPromise.then((db) => f(db, ...args));
|
||||
|
||||
export const fetchMeta = curryDb(fetchMetaImpl);
|
||||
export const hasOtherRenoteOfThisNote = curryDb(hasOtherRenoteOfThisNoteImpl);
|
||||
|
||||
export function getFullApAccount(
|
||||
username: string,
|
||||
|
|
|
@ -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();
|
||||
}
|
|
@ -47,7 +47,7 @@ import { isDuplicateKeyValueError } from "@/misc/is-duplicate-key-value-error.js
|
|||
import { checkHitAntenna } from "@/misc/check-hit-antenna.js";
|
||||
import { getWordHardMute } from "@/misc/check-word-mute.js";
|
||||
import { addNoteToAntenna } from "@/services/add-note-to-antenna.js";
|
||||
import { countSameRenotes } from "@/misc/count-same-renotes.js";
|
||||
import { hasOtherRenoteOfThisNote } from "@/misc/backend-rs.js";
|
||||
import { deliverToRelays, getCachedRelays } from "../relay.js";
|
||||
import type { Channel } from "@/models/entities/channel.js";
|
||||
import { normalizeForSearch } from "@/misc/normalize-for-search.js";
|
||||
|
@ -412,7 +412,7 @@ export default async (
|
|||
if (
|
||||
data.renote &&
|
||||
!user.isBot &&
|
||||
(await countSameRenotes(user.id, data.renote.id, note.id)) === 0
|
||||
!(await hasOtherRenoteOfThisNote(user.id, data.renote.id, note.id))
|
||||
) {
|
||||
incRenoteCount(data.renote);
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ import {
|
|||
deliverToFollowers,
|
||||
deliverToUser,
|
||||
} from "@/remote/activitypub/deliver-manager.js";
|
||||
import { countSameRenotes } from "@/misc/count-same-renotes.js";
|
||||
import { hasOtherRenoteOfThisNote } from "@/misc/backend-rs.js";
|
||||
import { registerOrFetchInstanceDoc } from "@/services/register-or-fetch-instance-doc.js";
|
||||
import { deliverToRelays } from "@/services/relay.js";
|
||||
// import meilisearch from "@/db/meilisearch.js";
|
||||
|
@ -34,7 +34,7 @@ export default async function (
|
|||
// この投稿を除く指定したユーザーによる指定したノートのリノートが存在しないとき
|
||||
if (
|
||||
note.renoteId &&
|
||||
(await countSameRenotes(user.id, note.renoteId, note.id)) === 0 &&
|
||||
!(await hasOtherRenoteOfThisNote(user.id, note.renoteId, note.id)) &&
|
||||
deleteFromDb
|
||||
) {
|
||||
Notes.decrement({ id: note.renoteId }, "renoteCount", 1);
|
||||
|
|
Loading…
Reference in a new issue