Revert "fix (backend): no need to check word mutes twice"

It turns out that this is very slow :(

This reverts commit 284a41ae82.
This commit is contained in:
naskya 2024-05-04 12:55:01 +09:00
parent 7a49e42bb6
commit 2ddb2d7ae2
No known key found for this signature in database
GPG key ID: 712D413B3A9FED5C
3 changed files with 45 additions and 19 deletions

View file

@ -1,8 +1,11 @@
use crate::config::CONFIG;
use crate::database::{cache, db_conn};
use crate::misc::acct::{string_to_acct, Acct};
use crate::misc::check_word_mute::check_word_mute_bare;
use crate::misc::get_note_all_texts::{all_texts, NoteLike};
use crate::model::entity::{antenna, blocking, following, note, sea_orm_active_enums::*};
use crate::model::entity::{
antenna, blocking, following, note, sea_orm_active_enums::*, user_profile,
};
use sea_orm::{ColumnTrait, DbErr, EntityTrait, QueryFilter, QuerySelect};
#[derive(thiserror::Error, Debug)]
@ -149,6 +152,35 @@ pub async fn check_hit_antenna(
}
}
type WordMute = (
Vec<String>, // muted words
Vec<String>, // muted patterns
);
let word_mute: WordMute = cache::get_one(cache::Category::WordMute, &antenna.user_id)?
.unwrap_or({
// cache miss
let mute = user_profile::Entity::find()
.select_only()
.columns([
user_profile::Column::MutedWords,
user_profile::Column::MutedPatterns,
])
.into_tuple::<WordMute>()
.one(db)
.await?
.ok_or({
tracing::warn!("there is no user_profile for user {}", &antenna.user_id);
AntennaCheckError::UserProfileNotFoundErr(antenna.user_id.clone())
})?;
cache::set_one(cache::Category::WordMute, &antenna.user_id, &mute, 10 * 60)?;
mute
});
if check_word_mute_bare(&note_texts, &word_mute.0, &word_mute.1) {
return Ok(false);
}
Ok(true)
}

View file

@ -8,7 +8,7 @@ fn convert_regex(js_regex: &str) -> String {
RE.replace(js_regex, "(?$2)$1").to_string()
}
fn check_word_mute_impl(
pub fn check_word_mute_bare(
texts: &[String],
muted_words: &[String],
muted_patterns: &[String],
@ -38,7 +38,7 @@ pub async fn check_word_mute(
if muted_words.is_empty() && muted_patterns.is_empty() {
Ok(false)
} else {
Ok(check_word_mute_impl(
Ok(check_word_mute_bare(
&all_texts(note).await?,
muted_words,
muted_patterns,

View file

@ -370,10 +370,8 @@ export default async (
// Increment notes count (user)
incNotesCountOfUser(user);
// Word mute & antenna
const thisNoteIsMutedBy = [] as string[];
await hardMutesCache
// Word mute
hardMutesCache
.fetch(null, () =>
UserProfiles.find({
where: {
@ -382,13 +380,12 @@ export default async (
select: ["userId", "mutedWords", "mutedPatterns"],
}),
)
.then(async (us) => {
.then((us) => {
for (const u of us) {
if (u.userId === user.id) return;
await checkWordMute(note, u.mutedWords, u.mutedPatterns).then(
checkWordMute(note, u.mutedWords, u.mutedPatterns).then(
(shouldMute: boolean) => {
if (shouldMute) {
thisNoteIsMutedBy.push(u.userId);
MutedNotes.insert({
id: genId(),
userId: u.userId,
@ -401,17 +398,14 @@ export default async (
}
});
// Antenna
const antennas = await getAntennas();
for await (const antenna of antennas) {
// skip if the note is muted
if (!thisNoteIsMutedBy.includes(antenna.userId)) {
await updateAntennaOnCreateNote(
asRustObject(antenna),
asRustObject(note),
user,
);
}
await updateAntennaOnCreateNote(
asRustObject(antenna),
asRustObject(note),
user,
);
}
// Channel