chore: minor refactor of check-word-mute

This commit is contained in:
naskya 2023-10-17 07:28:58 +09:00
parent 3b9f161251
commit 93769b79b1
No known key found for this signature in database
GPG Key ID: 164DFF24E2D40139
11 changed files with 27 additions and 39 deletions

View File

@ -1,6 +1,5 @@
import RE2 from "re2"; import RE2 from "re2";
import type { Note } from "@/models/entities/note.js"; import type { Note } from "@/models/entities/note.js";
import type { User } from "@/models/entities/user.js";
type NoteLike = { type NoteLike = {
userId: Note["userId"]; userId: Note["userId"];
@ -9,10 +8,6 @@ type NoteLike = {
cw?: Note["cw"]; cw?: Note["cw"];
}; };
type UserLike = {
id: User["id"];
};
function checkWordMute( function checkWordMute(
note: NoteLike, note: NoteLike,
mutedWords: Array<string | string[]>, mutedWords: Array<string | string[]>,
@ -61,13 +56,10 @@ function checkWordMute(
export async function getWordHardMute( export async function getWordHardMute(
note: NoteLike, note: NoteLike,
me: UserLike | null | undefined, meId: string | null | undefined,
mutedWords: Array<string | string[]>, mutedWords: Array<string | string[]>,
): Promise<boolean> { ): Promise<boolean> {
// 自分自身 if (note.userId === meId) return false;
if (me && note.userId === me.id) {
return false;
}
if (mutedWords.length > 0) { if (mutedWords.length > 0) {
return ( return (

View File

@ -68,7 +68,7 @@ export default class extends Channel {
// そのためレコードが存在するかのチェックでは不十分なので、改めてgetWordHardMuteを呼んでいる // そのためレコードが存在するかのチェックでは不十分なので、改めてgetWordHardMuteを呼んでいる
if ( if (
this.userProfile && this.userProfile &&
(await getWordHardMute(note, this.user, this.userProfile.mutedWords)) (await getWordHardMute(note, this.user?.id, this.userProfile.mutedWords))
) )
return; return;

View File

@ -67,7 +67,7 @@ export default class extends Channel {
// そのためレコードが存在するかのチェックでは不十分なので、改めてgetWordHardMuteを呼んでいる // そのためレコードが存在するかのチェックでは不十分なので、改めてgetWordHardMuteを呼んでいる
if ( if (
this.userProfile && this.userProfile &&
(await getWordHardMute(note, this.user, this.userProfile.mutedWords)) (await getWordHardMute(note, this.user?.id, this.userProfile.mutedWords))
) )
return; return;

View File

@ -84,7 +84,7 @@ export default class extends Channel {
// そのためレコードが存在するかのチェックでは不十分なので、改めてgetWordHardMuteを呼んでいる // そのためレコードが存在するかのチェックでは不十分なので、改めてgetWordHardMuteを呼んでいる
if ( if (
this.userProfile && this.userProfile &&
(await getWordHardMute(note, this.user, this.userProfile.mutedWords)) (await getWordHardMute(note, this.user?.id, this.userProfile.mutedWords))
) )
return; return;

View File

@ -60,7 +60,7 @@ export default class extends Channel {
// そのためレコードが存在するかのチェックでは不十分なので、改めてgetWordHardMuteを呼んでいる // そのためレコードが存在するかのチェックでは不十分なので、改めてgetWordHardMuteを呼んでいる
if ( if (
this.userProfile && this.userProfile &&
(await getWordHardMute(note, this.user, this.userProfile.mutedWords)) (await getWordHardMute(note, this.user?.id, this.userProfile.mutedWords))
) )
return; return;

View File

@ -82,7 +82,7 @@ export default class extends Channel {
// そのためレコードが存在するかのチェックでは不十分なので、改めてgetWordHardMuteを呼んでいる // そのためレコードが存在するかのチェックでは不十分なので、改めてgetWordHardMuteを呼んでいる
if ( if (
this.userProfile && this.userProfile &&
(await getWordHardMute(note, this.user, this.userProfile.mutedWords)) (await getWordHardMute(note, this.user?.id, this.userProfile.mutedWords))
) )
return; return;

View File

@ -380,18 +380,16 @@ export default async (
) )
.then((us) => { .then((us) => {
for (const u of us) { for (const u of us) {
getWordHardMute(data, { id: u.userId }, u.mutedWords).then( getWordHardMute(data, u.userId, u.mutedWords).then((shouldMute) => {
(shouldMute) => { if (shouldMute) {
if (shouldMute) { MutedNotes.insert({
MutedNotes.insert({ id: genId(),
id: genId(), userId: u.userId,
userId: u.userId, noteId: note.id,
noteId: note.id, reason: "word",
reason: "word", });
}); }
} });
},
);
} }
}); });

View File

@ -273,7 +273,6 @@
<script lang="ts" setup> <script lang="ts" setup>
import { computed, inject, onMounted, ref } from "vue"; import { computed, inject, onMounted, ref } from "vue";
import * as mfm from "mfm-js";
import type { Ref } from "vue"; import type { Ref } from "vue";
import type * as firefish from "firefish-js"; import type * as firefish from "firefish-js";
import MkSubNoteContent from "./MkSubNoteContent.vue"; import MkSubNoteContent from "./MkSubNoteContent.vue";
@ -360,7 +359,7 @@ const isDeleted = ref(false);
const muted = ref( const muted = ref(
getWordSoftMute( getWordSoftMute(
note.value, note.value,
$i, $i.id,
defaultStore.state.mutedWords, defaultStore.state.mutedWords,
defaultStore.state.mutedLangs, defaultStore.state.mutedLangs,
), ),

View File

@ -234,7 +234,7 @@ const isDeleted = ref(false);
const muted = ref( const muted = ref(
getWordSoftMute( getWordSoftMute(
note.value, note.value,
$i, $i.id,
defaultStore.state.mutedWords, defaultStore.state.mutedWords,
defaultStore.state.mutedLangs, defaultStore.state.mutedLangs,
), ),

View File

@ -268,7 +268,7 @@ const isDeleted = ref(false);
const muted = ref( const muted = ref(
getWordSoftMute( getWordSoftMute(
note.value, note.value,
$i, $i.id,
defaultStore.state.mutedWords, defaultStore.state.mutedWords,
defaultStore.state.mutedLangs, defaultStore.state.mutedLangs,
), ),

View File

@ -1,3 +1,5 @@
import type * as firefish from "firefish-js";
export interface Muted { export interface Muted {
muted: boolean; muted: boolean;
matched: string[]; matched: string[];
@ -7,7 +9,7 @@ export interface Muted {
const NotMuted = { muted: false, matched: [] }; const NotMuted = { muted: false, matched: [] };
function checkLangMute( function checkLangMute(
note: NoteLike, note: firefish.entities.Note,
mutedLangs: Array<string | string[]>, mutedLangs: Array<string | string[]>,
): Muted { ): Muted {
const mutedLangList = new Set( const mutedLangList = new Set(
@ -20,7 +22,7 @@ function checkLangMute(
} }
function checkWordMute( function checkWordMute(
note: NoteLike, note: firefish.entities.Note,
mutedWords: Array<string | string[]>, mutedWords: Array<string | string[]>,
): Muted { ): Muted {
let text = `${note.cw ?? ""} ${note.text ?? ""}`; let text = `${note.cw ?? ""} ${note.text ?? ""}`;
@ -72,15 +74,12 @@ function checkWordMute(
} }
export function getWordSoftMute( export function getWordSoftMute(
note: Record<string, any>, note: firefish.entities.Note,
me: Record<string, any> | null | undefined, meId: string | null | undefined,
mutedWords: Array<string | string[]>, mutedWords: Array<string | string[]>,
mutedLangs: Array<string | string[]>, mutedLangs: Array<string | string[]>,
): Muted { ): Muted {
// 自分自身 if (note.userId === meId) return NotMuted;
if (me && note.userId === me.id) {
return NotMuted;
}
if (mutedWords.length > 0) { if (mutedWords.length > 0) {
const noteMuted = checkWordMute(note, mutedWords); const noteMuted = checkWordMute(note, mutedWords);