perf: use slacc on check-word-mute (#10721)

* perf: use slacc on check-word-mute when all of specified words are single word

* perf: use slacc as possible

* build: avoid tarball

* chore: update slacc

* build: update package name
This commit is contained in:
Acid Chicken (硫酸鶏)
2023-05-05 19:49:34 +09:00
committed by GitHub
parent 14e364a74a
commit 4a72941eda
4 changed files with 236 additions and 6 deletions

View File

@@ -1,3 +1,4 @@
import { AhoCorasick } from 'slacc';
import RE2 from 're2';
import type { Note } from '@/models/entities/Note.js';
import type { User } from '@/models/entities/User.js';
@@ -12,6 +13,8 @@ type UserLike = {
id: User['id'];
};
const acCache = new Map<string, AhoCorasick>();
export async function checkWordMute(note: NoteLike, me: UserLike | null | undefined, mutedWords: Array<string | string[]>): Promise<boolean> {
// 自分自身
if (me && (note.userId === me.id)) return false;
@@ -21,7 +24,22 @@ export async function checkWordMute(note: NoteLike, me: UserLike | null | undefi
if (text === '') return false;
const matched = mutedWords.some(filter => {
const acable = mutedWords.filter(filter => Array.isArray(filter) && filter.length === 1).map(filter => filter[0]).sort();
const unacable = mutedWords.filter(filter => !Array.isArray(filter) || filter.length !== 1);
const acCacheKey = acable.join('\n');
const ac = acCache.get(acCacheKey) ?? AhoCorasick.withPatterns(acable);
acCache.delete(acCacheKey);
for (const obsoleteKeys of acCache.keys()) {
if (acCache.size > 1000) {
acCache.delete(obsoleteKeys);
}
}
acCache.set(acCacheKey, ac);
if (ac.isMatch(text)) {
return true;
}
const matched = unacable.some(filter => {
if (Array.isArray(filter)) {
return filter.every(keyword => text.includes(keyword));
} else {