chore: minor refactor of check-word-mute
This commit is contained in:
parent
3b9f161251
commit
93769b79b1
11 changed files with 27 additions and 39 deletions
|
@ -1,6 +1,5 @@
|
|||
import RE2 from "re2";
|
||||
import type { Note } from "@/models/entities/note.js";
|
||||
import type { User } from "@/models/entities/user.js";
|
||||
|
||||
type NoteLike = {
|
||||
userId: Note["userId"];
|
||||
|
@ -9,10 +8,6 @@ type NoteLike = {
|
|||
cw?: Note["cw"];
|
||||
};
|
||||
|
||||
type UserLike = {
|
||||
id: User["id"];
|
||||
};
|
||||
|
||||
function checkWordMute(
|
||||
note: NoteLike,
|
||||
mutedWords: Array<string | string[]>,
|
||||
|
@ -61,13 +56,10 @@ function checkWordMute(
|
|||
|
||||
export async function getWordHardMute(
|
||||
note: NoteLike,
|
||||
me: UserLike | null | undefined,
|
||||
meId: string | null | undefined,
|
||||
mutedWords: Array<string | string[]>,
|
||||
): Promise<boolean> {
|
||||
// 自分自身
|
||||
if (me && note.userId === me.id) {
|
||||
return false;
|
||||
}
|
||||
if (note.userId === meId) return false;
|
||||
|
||||
if (mutedWords.length > 0) {
|
||||
return (
|
||||
|
|
|
@ -68,7 +68,7 @@ export default class extends Channel {
|
|||
// そのためレコードが存在するかのチェックでは不十分なので、改めてgetWordHardMuteを呼んでいる
|
||||
if (
|
||||
this.userProfile &&
|
||||
(await getWordHardMute(note, this.user, this.userProfile.mutedWords))
|
||||
(await getWordHardMute(note, this.user?.id, this.userProfile.mutedWords))
|
||||
)
|
||||
return;
|
||||
|
||||
|
|
|
@ -67,7 +67,7 @@ export default class extends Channel {
|
|||
// そのためレコードが存在するかのチェックでは不十分なので、改めてgetWordHardMuteを呼んでいる
|
||||
if (
|
||||
this.userProfile &&
|
||||
(await getWordHardMute(note, this.user, this.userProfile.mutedWords))
|
||||
(await getWordHardMute(note, this.user?.id, this.userProfile.mutedWords))
|
||||
)
|
||||
return;
|
||||
|
||||
|
|
|
@ -84,7 +84,7 @@ export default class extends Channel {
|
|||
// そのためレコードが存在するかのチェックでは不十分なので、改めてgetWordHardMuteを呼んでいる
|
||||
if (
|
||||
this.userProfile &&
|
||||
(await getWordHardMute(note, this.user, this.userProfile.mutedWords))
|
||||
(await getWordHardMute(note, this.user?.id, this.userProfile.mutedWords))
|
||||
)
|
||||
return;
|
||||
|
||||
|
|
|
@ -60,7 +60,7 @@ export default class extends Channel {
|
|||
// そのためレコードが存在するかのチェックでは不十分なので、改めてgetWordHardMuteを呼んでいる
|
||||
if (
|
||||
this.userProfile &&
|
||||
(await getWordHardMute(note, this.user, this.userProfile.mutedWords))
|
||||
(await getWordHardMute(note, this.user?.id, this.userProfile.mutedWords))
|
||||
)
|
||||
return;
|
||||
|
||||
|
|
|
@ -82,7 +82,7 @@ export default class extends Channel {
|
|||
// そのためレコードが存在するかのチェックでは不十分なので、改めてgetWordHardMuteを呼んでいる
|
||||
if (
|
||||
this.userProfile &&
|
||||
(await getWordHardMute(note, this.user, this.userProfile.mutedWords))
|
||||
(await getWordHardMute(note, this.user?.id, this.userProfile.mutedWords))
|
||||
)
|
||||
return;
|
||||
|
||||
|
|
|
@ -380,8 +380,7 @@ export default async (
|
|||
)
|
||||
.then((us) => {
|
||||
for (const u of us) {
|
||||
getWordHardMute(data, { id: u.userId }, u.mutedWords).then(
|
||||
(shouldMute) => {
|
||||
getWordHardMute(data, u.userId, u.mutedWords).then((shouldMute) => {
|
||||
if (shouldMute) {
|
||||
MutedNotes.insert({
|
||||
id: genId(),
|
||||
|
@ -390,8 +389,7 @@ export default async (
|
|||
reason: "word",
|
||||
});
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -273,7 +273,6 @@
|
|||
|
||||
<script lang="ts" setup>
|
||||
import { computed, inject, onMounted, ref } from "vue";
|
||||
import * as mfm from "mfm-js";
|
||||
import type { Ref } from "vue";
|
||||
import type * as firefish from "firefish-js";
|
||||
import MkSubNoteContent from "./MkSubNoteContent.vue";
|
||||
|
@ -360,7 +359,7 @@ const isDeleted = ref(false);
|
|||
const muted = ref(
|
||||
getWordSoftMute(
|
||||
note.value,
|
||||
$i,
|
||||
$i.id,
|
||||
defaultStore.state.mutedWords,
|
||||
defaultStore.state.mutedLangs,
|
||||
),
|
||||
|
|
|
@ -234,7 +234,7 @@ const isDeleted = ref(false);
|
|||
const muted = ref(
|
||||
getWordSoftMute(
|
||||
note.value,
|
||||
$i,
|
||||
$i.id,
|
||||
defaultStore.state.mutedWords,
|
||||
defaultStore.state.mutedLangs,
|
||||
),
|
||||
|
|
|
@ -268,7 +268,7 @@ const isDeleted = ref(false);
|
|||
const muted = ref(
|
||||
getWordSoftMute(
|
||||
note.value,
|
||||
$i,
|
||||
$i.id,
|
||||
defaultStore.state.mutedWords,
|
||||
defaultStore.state.mutedLangs,
|
||||
),
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
import type * as firefish from "firefish-js";
|
||||
|
||||
export interface Muted {
|
||||
muted: boolean;
|
||||
matched: string[];
|
||||
|
@ -7,7 +9,7 @@ export interface Muted {
|
|||
const NotMuted = { muted: false, matched: [] };
|
||||
|
||||
function checkLangMute(
|
||||
note: NoteLike,
|
||||
note: firefish.entities.Note,
|
||||
mutedLangs: Array<string | string[]>,
|
||||
): Muted {
|
||||
const mutedLangList = new Set(
|
||||
|
@ -20,7 +22,7 @@ function checkLangMute(
|
|||
}
|
||||
|
||||
function checkWordMute(
|
||||
note: NoteLike,
|
||||
note: firefish.entities.Note,
|
||||
mutedWords: Array<string | string[]>,
|
||||
): Muted {
|
||||
let text = `${note.cw ?? ""} ${note.text ?? ""}`;
|
||||
|
@ -72,15 +74,12 @@ function checkWordMute(
|
|||
}
|
||||
|
||||
export function getWordSoftMute(
|
||||
note: Record<string, any>,
|
||||
me: Record<string, any> | null | undefined,
|
||||
note: firefish.entities.Note,
|
||||
meId: string | null | undefined,
|
||||
mutedWords: Array<string | string[]>,
|
||||
mutedLangs: Array<string | string[]>,
|
||||
): Muted {
|
||||
// 自分自身
|
||||
if (me && note.userId === me.id) {
|
||||
return NotMuted;
|
||||
}
|
||||
if (note.userId === meId) return NotMuted;
|
||||
|
||||
if (mutedWords.length > 0) {
|
||||
const noteMuted = checkWordMute(note, mutedWords);
|
||||
|
|
Loading…
Reference in a new issue