refactor note mutes and render mandatoryCW as a mute

This commit is contained in:
Hazelnoot 2025-06-27 18:52:08 -04:00
parent 09a2280513
commit 54ad6438af
13 changed files with 304 additions and 329 deletions

View file

@ -1,138 +1,184 @@
/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-FileCopyrightText: hazelnoot and other Sharkey contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
import * as Misskey from 'misskey-js';
import { computed, inject, ref } from 'vue';
import type { Ref, ComputedRef } from 'vue';
import { inject } from 'vue';
import type { Ref } from 'vue';
import { $i } from '@/i';
export function checkMutes(noteToCheck: ComputedRef<Misskey.entities.Note>, withHardMute?: ComputedRef<boolean>) {
const muteEnable = ref(true);
export interface Mute {
hardMuted?: boolean;
softMutedWords?: string[];
sensitiveMuted?: boolean;
const muted = computed<false | string[], boolean>({
get() {
if (!muteEnable.value) return false;
return checkMute(noteToCheck.value, $i?.mutedWords);
},
set(value: boolean) {
muteEnable.value = value;
},
});
isSensitive?: boolean;
const threadMuted = computed(() => {
if (!muteEnable.value) return false;
return noteToCheck.value.isMutingThread;
});
threadMuted?: boolean;
noteMuted?: boolean;
const noteMuted = computed(() => {
if (!muteEnable.value) return false;
return noteToCheck.value.isMutingNote;
});
const hardMuted = computed(() => {
if (!withHardMute?.value) return false;
return checkMute(noteToCheck.value, $i?.hardMutedWords, true);
});
return { muted, hardMuted, threadMuted, noteMuted };
// TODO show this as a single block on user timelines
userMandatoryCW?: string | null;
}
export function checkMute(note: Misskey.entities.Note, mutes: undefined | null): false;
export function checkMute(note: Misskey.entities.Note, mutes: undefined | null, checkOnly: false): false;
export function checkMute(note: Misskey.entities.Note, mutes: undefined | null, checkOnly?: boolean): false | 'sensitiveMute';
export function checkMute(note: Misskey.entities.Note, mutes: Array<string | string[]> | undefined | null): string[] | false;
export function checkMute(note: Misskey.entities.Note, mutes: Array<string | string[]> | undefined | null, checkOnly: false): string[] | false;
export function checkMute(note: Misskey.entities.Note, mutes: Array<string | string[]> | undefined | null, checkOnly?: boolean): string[] | false | 'sensitiveMute';
export function checkMute(note: Misskey.entities.Note, mutes: Array<string | string[]> | undefined | null, checkOnly = false): string[] | false | 'sensitiveMute' {
if (mutes != null) {
const result =
checkWordMute(note, $i, mutes)
|| checkWordMute(note.reply, $i, mutes)
|| checkWordMute(note.renote, $i, mutes);
export function checkMute(note: Misskey.entities.Note, withHardMute?: boolean): Mute {
const sensitiveMuted = isSensitiveMuted(note);
// Only continue to sensitiveMute if we don't match any *actual* mutes
if (result) {
return result;
}
// My own note
if ($i && $i.id === note.userId) {
return { sensitiveMuted };
}
if (checkOnly) {
const inTimeline = inject<boolean>('inTimeline', false);
const tl_withSensitive = inject<Ref<boolean> | null>('tl_withSensitive', null);
if (inTimeline && tl_withSensitive?.value === false && note.files?.some((v) => v.isSensitive)) {
return 'sensitiveMute';
}
const threadMuted = note.isMutingThread;
const noteMuted = note.isMutingNote;
const userMandatoryCW = note.user.mandatoryCW;
// Hard mute
if (withHardMute && isHardMuted(note)) {
return { hardMuted: true, sensitiveMuted, threadMuted, noteMuted, userMandatoryCW };
}
return false;
// Soft mute
const softMutedWords = isSoftMuted(note);
if (softMutedWords.length > 0) {
return { softMutedWords, sensitiveMuted, threadMuted, noteMuted, userMandatoryCW };
}
// Other / no mute
return { sensitiveMuted, threadMuted, noteMuted, userMandatoryCW };
}
export function checkWordMute(note: string | Misskey.entities.Note | undefined | null, me: Misskey.entities.UserLite | null | undefined, mutedWords: Array<string | string[]>): string[] | false {
if (note == null) return false;
function isHardMuted(note: Misskey.entities.Note): boolean {
if (!$i?.hardMutedWords.length) return false;
// 自分自身
if (me && typeof(note) === 'object' && (note.userId === me.id)) return false;
return containsMutedWord($i.hardMutedWords, note);
}
if (mutedWords.length > 0) {
const text = typeof(note) === 'object' ? getNoteText(note) : note;
function isSoftMuted(note: Misskey.entities.Note): string[] {
if (!$i?.mutedWords.length) return [];
if (text === '') return false;
return getMutedWords($i.mutedWords, note);
}
const matched = mutedWords.reduce((matchedWords, filter) => {
if (Array.isArray(filter)) {
// Clean up
const filteredFilter = filter.filter(keyword => keyword !== '');
if (filteredFilter.length > 0 && filteredFilter.every(keyword => text.includes(keyword))) {
const fullFilter = filteredFilter.join(' ');
matchedWords.add(fullFilter);
}
} else {
// represents RegExp
const regexp = filter.match(/^\/(.+)\/(.*)$/);
function isSensitiveMuted(note: Misskey.entities.Note): boolean {
// 1. At least one sensitive file
if (!note.files) return false;
if (!note.files.some((v) => v.isSensitive)) return false;
// This should never happen due to input sanitisation.
if (regexp) {
try {
const flags = regexp[2].includes('g') ? regexp[2] : (regexp[2] + 'g');
const matches = text.matchAll(new RegExp(regexp[1], flags));
for (const match of matches) {
matchedWords.add(match[0]);
}
} catch {
// This should never happen due to input sanitisation.
}
}
// 2. In a timeline
const inTimeline = inject<boolean>('inTimeline', false);
if (!inTimeline) return false;
// 3. With sensitive files hidden
const tl_withSensitive = inject<Ref<boolean> | null>('tl_withSensitive', null);
return tl_withSensitive?.value === false;
}
function getMutedWords(mutedWords: (string | string[])[], note: Misskey.entities.Note): string[] {
// Parse mutes
const { regexMutes, patternMutes } = parseMutes(mutedWords);
// Make sure we didn't filter them all out
if (regexMutes.length < 1 && patternMutes.length < 1) {
return [];
}
const matches = new Set<string>();
// Expand notes into searchable test
for (const text of expandNote(note)) {
for (const pattern of patternMutes) {
// Case-sensitive, non-boundary search for backwards compatibility
if (pattern.every(word => text.includes(word))) {
const muteLabel = pattern.join(' ');
matches.add(muteLabel);
}
}
return matchedWords;
}, new Set<string>());
for (const regex of regexMutes) {
for (const match of text.matchAll(regex)) {
matches.add(match[0]);
}
}
}
// Nested arrays are intentional, otherwise the note components will join with space (" ") and it's confusing.
if (matched.size > 0) return Array.from(matched);
return Array.from(matches);
}
function containsMutedWord(mutedWords: (string | string[])[], note: Misskey.entities.Note): boolean {
// Parse mutes
const { regexMutes, patternMutes } = parseMutes(mutedWords);
// Make sure we didn't filter them all out
if (regexMutes.length < 1 && patternMutes.length < 1) {
return false;
}
// Expand notes into searchable test
for (const text of expandNote(note)) {
for (const pattern of patternMutes) {
// Case-sensitive, non-boundary search for backwards compatibility
if (pattern.every(word => text.includes(word))) {
return true;
}
}
if (regexMutes.some(regex => text.match(regex))) {
return true;
}
}
return false;
}
function getNoteText(note: Misskey.entities.Note): string {
const textParts: string[] = [];
if (note.cw) textParts.push(note.cw);
if (note.text) textParts.push(note.text);
function *expandNote(note: Misskey.entities.Note): Generator<string> {
if (note.cw) yield note.cw;
if (note.text) yield note.text;
if (note.files) {
for (const file of note.files) {
if (file.comment) textParts.push(file.comment);
if (file.comment) yield file.comment;
}
}
if (note.poll) {
for (const choice of note.poll.choices) {
if (choice.text) textParts.push(choice.text);
if (choice.text) yield choice.text;
}
}
if (note.reply) {
yield * expandNote(note.reply);
}
if (note.renote) {
yield * expandNote(note.renote);
}
}
function parseMutes(mutedWords: (string | string[])[]) {
const regexMutes: RegExp[] = [];
const patternMutes: string[][] = [];
for (const mute of mutedWords) {
if (Array.isArray(mute)) {
if (mute.length > 0) {
const filtered = mute.filter(keyword => keyword !== '');
if (filtered.length > 0) {
patternMutes.push(filtered);
} else {
console.warn('Skipping invalid pattern mute:', mute);
}
}
} else {
const parsed = mute.match(/^\/(.+)\/(.*)$/);
if (parsed && parsed.length === 3) {
try {
const flags = parsed[2].includes('g') ? parsed[2] : `${parsed[2]}g`;
regexMutes.push(new RegExp(parsed[1], flags));
} catch {
console.warn('Skipping invalid regexp mute:', mute);
}
}
}
}
return textParts.join('\n').trim();
return { regexMutes, patternMutes };
}