Refactor hard word mutes
This commit is contained in:
parent
bf262b972f
commit
075e5a1c7a
10 changed files with 72 additions and 55 deletions
|
@ -12,33 +12,15 @@ type UserLike = {
|
||||||
id: User["id"];
|
id: User["id"];
|
||||||
};
|
};
|
||||||
|
|
||||||
export type Muted = {
|
function escapeRegExp(x: string): string {
|
||||||
muted: boolean;
|
|
||||||
matched: string[];
|
|
||||||
};
|
|
||||||
|
|
||||||
const NotMuted = { muted: false, matched: [] };
|
|
||||||
|
|
||||||
function escapeRegExp(x: string) {
|
|
||||||
return x.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
|
return x.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getWordMute(
|
function checkWordMute(note: NoteLike): boolean {
|
||||||
note: NoteLike,
|
if (note == null) return false;
|
||||||
me: UserLike | null | undefined,
|
|
||||||
mutedWords: Array<string | string[]>,
|
|
||||||
): Promise<Muted> {
|
|
||||||
// 自分自身
|
|
||||||
if (me && note.userId === me.id) {
|
|
||||||
return NotMuted;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mutedWords.length > 0) {
|
const text = ((note.cw ?? "") + " " + (note.text ?? "")).trim();
|
||||||
const text = ((note.cw ?? "") + "\n" + (note.text ?? "")).trim();
|
if (text === "") return false;
|
||||||
|
|
||||||
if (text === "") {
|
|
||||||
return NotMuted;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const mutePattern of mutedWords) {
|
for (const mutePattern of mutedWords) {
|
||||||
let mute: RE2;
|
let mute: RE2;
|
||||||
|
@ -65,14 +47,28 @@ export async function getWordMute(
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (mute.test(text)) {
|
if (mute.test(text)) return true;
|
||||||
return { muted: true, matched };
|
|
||||||
}
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// This should never happen due to input sanitisation.
|
// This should never happen due to input sanitisation.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return notMuted;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getWordMute(
|
||||||
|
note: NoteLike,
|
||||||
|
me: UserLike | null | undefined,
|
||||||
|
mutedWords: Array<string | string[]>,
|
||||||
|
): Promise<boolean> {
|
||||||
|
// 自分自身
|
||||||
|
if (me && note.userId === me.id) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return NotMuted;
|
if (mutedWords.length > 0) {
|
||||||
|
return checkWordMute(note) || checkWordMute(reply) || checkWordMute(renote)
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import Channel from "../channel.js";
|
import Channel from "../channel.js";
|
||||||
import { Notes } from "@/models/index.js";
|
import { Notes } from "@/models/index.js";
|
||||||
import { isUserRelated } from "@/misc/is-user-related.js";
|
import { isUserRelated } from "@/misc/is-user-related.js";
|
||||||
|
import { getWordMute } from "@/misc/check-word-mute.js";
|
||||||
import type { StreamMessages } from "../types.js";
|
import type { StreamMessages } from "../types.js";
|
||||||
import { IdentifiableError } from "@/misc/identifiable-error.js";
|
import { IdentifiableError } from "@/misc/identifiable-error.js";
|
||||||
|
|
||||||
|
@ -37,6 +38,12 @@ export default class extends Channel {
|
||||||
if (note.renote && !note.text && isUserRelated(note, this.renoteMuting))
|
if (note.renote && !note.text && isUserRelated(note, this.renoteMuting))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
if (
|
||||||
|
this.userProfile &&
|
||||||
|
(await getWordMute(note, this.user, this.userProfile.mutedWords))
|
||||||
|
)
|
||||||
|
return;
|
||||||
|
|
||||||
this.connection.cacheNote(note);
|
this.connection.cacheNote(note);
|
||||||
|
|
||||||
this.send("note", note);
|
this.send("note", note);
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import Channel from "../channel.js";
|
import Channel from "../channel.js";
|
||||||
import { Users } from "@/models/index.js";
|
import { Users } from "@/models/index.js";
|
||||||
import { isUserRelated } from "@/misc/is-user-related.js";
|
import { isUserRelated } from "@/misc/is-user-related.js";
|
||||||
|
import { getWordMute } from "@/misc/check-word-mute.js";
|
||||||
import type { User } from "@/models/entities/user.js";
|
import type { User } from "@/models/entities/user.js";
|
||||||
import type { StreamMessages } from "../types.js";
|
import type { StreamMessages } from "../types.js";
|
||||||
import type { Packed } from "@/misc/schema.js";
|
import type { Packed } from "@/misc/schema.js";
|
||||||
|
@ -39,6 +40,12 @@ export default class extends Channel {
|
||||||
if (note.renote && !note.text && isUserRelated(note, this.renoteMuting))
|
if (note.renote && !note.text && isUserRelated(note, this.renoteMuting))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
if (
|
||||||
|
this.userProfile &&
|
||||||
|
(await getWordMute(note, this.user, this.userProfile.mutedWords))
|
||||||
|
)
|
||||||
|
return;
|
||||||
|
|
||||||
this.connection.cacheNote(note);
|
this.connection.cacheNote(note);
|
||||||
|
|
||||||
this.send("note", note);
|
this.send("note", note);
|
||||||
|
|
|
@ -66,7 +66,7 @@ export default class extends Channel {
|
||||||
// そのためレコードが存在するかのチェックでは不十分なので、改めてgetWordMuteを呼んでいる
|
// そのためレコードが存在するかのチェックでは不十分なので、改めてgetWordMuteを呼んでいる
|
||||||
if (
|
if (
|
||||||
this.userProfile &&
|
this.userProfile &&
|
||||||
(await getWordMute(note, this.user, this.userProfile.mutedWords)).muted
|
(await getWordMute(note, this.user, this.userProfile.mutedWords))
|
||||||
)
|
)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
|
|
@ -64,7 +64,7 @@ export default class extends Channel {
|
||||||
// そのためレコードが存在するかのチェックでは不十分なので、改めてgetWordMuteを呼んでいる
|
// そのためレコードが存在するかのチェックでは不十分なので、改めてgetWordMuteを呼んでいる
|
||||||
if (
|
if (
|
||||||
this.userProfile &&
|
this.userProfile &&
|
||||||
(await getWordMute(note, this.user, this.userProfile.mutedWords)).muted
|
(await getWordMute(note, this.user, this.userProfile.mutedWords))
|
||||||
)
|
)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
|
|
@ -81,7 +81,7 @@ export default class extends Channel {
|
||||||
// そのためレコードが存在するかのチェックでは不十分なので、改めてgetWordMuteを呼んでいる
|
// そのためレコードが存在するかのチェックでは不十分なので、改めてgetWordMuteを呼んでいる
|
||||||
if (
|
if (
|
||||||
this.userProfile &&
|
this.userProfile &&
|
||||||
(await getWordMute(note, this.user, this.userProfile.mutedWords)).muted
|
(await getWordMute(note, this.user, this.userProfile.mutedWords))
|
||||||
)
|
)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
|
|
@ -58,7 +58,7 @@ export default class extends Channel {
|
||||||
// そのためレコードが存在するかのチェックでは不十分なので、改めてgetWordMuteを呼んでいる
|
// そのためレコードが存在するかのチェックでは不十分なので、改めてgetWordMuteを呼んでいる
|
||||||
if (
|
if (
|
||||||
this.userProfile &&
|
this.userProfile &&
|
||||||
(await getWordMute(note, this.user, this.userProfile.mutedWords)).muted
|
(await getWordMute(note, this.user, this.userProfile.mutedWords))
|
||||||
)
|
)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
|
|
@ -79,7 +79,7 @@ export default class extends Channel {
|
||||||
// そのためレコードが存在するかのチェックでは不十分なので、改めてgetWordMuteを呼んでいる
|
// そのためレコードが存在するかのチェックでは不十分なので、改めてgetWordMuteを呼んでいる
|
||||||
if (
|
if (
|
||||||
this.userProfile &&
|
this.userProfile &&
|
||||||
(await getWordMute(note, this.user, this.userProfile.mutedWords)).muted
|
(await getWordMute(note, this.user, this.userProfile.mutedWords))
|
||||||
)
|
)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ import Channel from "../channel.js";
|
||||||
import { UserListJoinings, UserLists } from "@/models/index.js";
|
import { UserListJoinings, UserLists } from "@/models/index.js";
|
||||||
import type { User } from "@/models/entities/user.js";
|
import type { User } from "@/models/entities/user.js";
|
||||||
import { isUserRelated } from "@/misc/is-user-related.js";
|
import { isUserRelated } from "@/misc/is-user-related.js";
|
||||||
|
import { getWordMute } from "@/misc/check-word-mute.js";
|
||||||
import type { Packed } from "@/misc/schema.js";
|
import type { Packed } from "@/misc/schema.js";
|
||||||
|
|
||||||
export default class extends Channel {
|
export default class extends Channel {
|
||||||
|
@ -59,6 +60,12 @@ export default class extends Channel {
|
||||||
if (note.renote && !note.text && isUserRelated(note, this.renoteMuting))
|
if (note.renote && !note.text && isUserRelated(note, this.renoteMuting))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
if (
|
||||||
|
this.userProfile &&
|
||||||
|
(await getWordMute(note, this.user, this.userProfile.mutedWords))
|
||||||
|
)
|
||||||
|
return;
|
||||||
|
|
||||||
this.send("note", note);
|
this.send("note", note);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -356,7 +356,7 @@ export default async (
|
||||||
for (const u of us) {
|
for (const u of us) {
|
||||||
getWordMute(note, { id: u.userId }, u.mutedWords).then(
|
getWordMute(note, { id: u.userId }, u.mutedWords).then(
|
||||||
(shouldMute) => {
|
(shouldMute) => {
|
||||||
if (shouldMute.muted) {
|
if (shouldMute) {
|
||||||
MutedNotes.insert({
|
MutedNotes.insert({
|
||||||
id: genId(),
|
id: genId(),
|
||||||
userId: u.userId,
|
userId: u.userId,
|
||||||
|
|
Loading…
Reference in a new issue