use standard mute logic
This commit is contained in:
parent
7ad36d33e9
commit
9b572be0df
2 changed files with 14 additions and 62 deletions
|
@ -70,7 +70,7 @@ import * as os from '@/os.js';
|
|||
import MkPageHeader from '@/components/global/MkPageHeader.vue';
|
||||
import { $i } from '@/account.js';
|
||||
import MkLoading from '@/components/global/MkLoading.vue';
|
||||
import { getNoteText } from '@/scripts/check-word-mute.js';
|
||||
import { checkWordMute } from '@/scripts/check-word-mute.js';
|
||||
|
||||
const props = withDefaults(defineProps<{
|
||||
initialTab?: FollowingFeedTab,
|
||||
|
@ -160,83 +160,35 @@ async function onChangeTab(): Promise<void> {
|
|||
await showUserNotes('');
|
||||
}
|
||||
|
||||
const softMutePatterns = ref(buildMutePatterns($i?.mutedWords));
|
||||
const hardMutePatterns = ref(buildMutePatterns($i?.hardMutedWords));
|
||||
|
||||
function buildMutePatterns(mutedWords: (string | string[])[] | undefined): RegExp[] {
|
||||
if (!mutedWords || mutedWords.length < 1) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// flags -> pattern[]
|
||||
const patternMap = new Map<string, Set<string>>();
|
||||
for (const mute of mutedWords) {
|
||||
let flags: string;
|
||||
let patterns: string[];
|
||||
|
||||
// Translate the pattern, which can be a raw string or a regular expression.
|
||||
// For unknown reasons, raw strings are expressed as an array and expressions are plain strings.
|
||||
if (!mute) {
|
||||
continue;
|
||||
} else if (Array.isArray(mute)) {
|
||||
patterns = mute;
|
||||
flags = 'i';
|
||||
} else {
|
||||
const match = mute.match(/^\/(.+)\/(.*)$/);
|
||||
if (!match) {
|
||||
continue;
|
||||
} else {
|
||||
patterns = [match[1]];
|
||||
flags = match[2];
|
||||
}
|
||||
}
|
||||
|
||||
// Group the patterns based on shared flags
|
||||
let flagPatterns = patternMap.get(flags);
|
||||
if (!flagPatterns) {
|
||||
flagPatterns = new Set<string>();
|
||||
patternMap.set(flags, flagPatterns);
|
||||
}
|
||||
|
||||
for (const pattern of patterns) {
|
||||
flagPatterns.add(pattern);
|
||||
}
|
||||
}
|
||||
|
||||
// Parse all the patterns into regular expressions
|
||||
return Array
|
||||
.from(patternMap)
|
||||
.map(([flag, patterns]) => {
|
||||
const pattern = Array.from(patterns).map(p => `(${p})`).join('|');
|
||||
return new RegExp(pattern, flag);
|
||||
});
|
||||
}
|
||||
|
||||
// Adapted from MkNote.ts
|
||||
function isSoftMuted(note: Misskey.entities.Note): boolean {
|
||||
return isMuted(note, softMutePatterns.value);
|
||||
return isMuted(note, $i?.mutedWords);
|
||||
}
|
||||
|
||||
function isHardMuted(note: Misskey.entities.Note): boolean {
|
||||
return isMuted(note, hardMutePatterns.value);
|
||||
return isMuted(note, $i?.hardMutedWords);
|
||||
}
|
||||
|
||||
function isMuted(note: Misskey.entities.Note, mutes: RegExp[]): boolean {
|
||||
if (mutes.length < 1) return false;
|
||||
// Match the typing used by Misskey
|
||||
type Mutes = (string | string[])[] | null | undefined;
|
||||
|
||||
// Adapted from MkNote.ts
|
||||
function isMuted(note: Misskey.entities.Note, mutes: Mutes): boolean {
|
||||
return checkMute(note, mutes)
|
||||
|| checkMute(note.reply, mutes)
|
||||
|| checkMute(note.renote, mutes);
|
||||
}
|
||||
|
||||
// Adapted from check-word-mute.ts
|
||||
function checkMute(note: Misskey.entities.Note | undefined | null, mutes: RegExp[]): boolean {
|
||||
function checkMute(note: Misskey.entities.Note | undefined | null, mutes: Mutes): boolean {
|
||||
if (!note) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const noteText = getNoteText(note);
|
||||
return mutes.some(p => p.test(noteText));
|
||||
if (!mutes || mutes.length < 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return checkWordMute(note, $i, mutes);
|
||||
}
|
||||
|
||||
const latestNotesPaging = shallowRef<InstanceType<typeof MkPagination>>();
|
||||
|
|
|
@ -43,7 +43,7 @@ export function checkWordMute(note: Note, me: MeDetailed | null | undefined, mut
|
|||
return false;
|
||||
}
|
||||
|
||||
export function getNoteText(note: Note): string {
|
||||
function getNoteText(note: Note): string {
|
||||
const textParts: string[] = [];
|
||||
|
||||
if (note.cw) textParts.push(note.cw);
|
||||
|
|
Loading…
Reference in a new issue