cleanup, simplify, and merge duplicate word mute implementations

This commit is contained in:
Hazelnoot 2025-05-13 22:58:24 -04:00
parent b52db71e18
commit 8348a36f24
6 changed files with 44 additions and 72 deletions

View file

@ -13,34 +13,39 @@ export function checkMutes(noteToCheck: Misskey.entities.Note, withHardMute = fa
return { muted, hardMuted };
}
/* Overload FunctionLint
function checkMute(noteToCheck: Misskey.entities.Note, mutedWords: Array<string | string[]> | undefined | null, checkOnly: true): boolean;
function checkMute(noteToCheck: Misskey.entities.Note, mutedWords: Array<string | string[]> | undefined | null, checkOnly: false): Array<string | string[]> | false | 'sensitiveMute';
*/
export function checkMute(noteToCheck: Misskey.entities.Note, mutedWords: Array<string | string[]> | undefined | null, checkOnly = false): Array<string | string[]> | false | 'sensitiveMute' {
if (mutedWords != null) {
const result = checkWordMute(noteToCheck, $i, mutedWords);
if (Array.isArray(result)) return result;
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);
const replyResult = noteToCheck.reply && checkWordMute(noteToCheck.reply, $i, mutedWords);
if (Array.isArray(replyResult)) return replyResult;
const renoteResult = noteToCheck.renote && checkWordMute(noteToCheck.renote, $i, mutedWords);
if (Array.isArray(renoteResult)) return renoteResult;
// Only continue to sensitiveMute if we don't match any *actual* mutes
if (result) {
return result;
}
}
if (checkOnly) return false;
const inTimeline = inject<boolean>('inTimeline', false);
const tl_withSensitive = inject<Ref<boolean> | null>('tl_withSensitive', null);
if (inTimeline && tl_withSensitive?.value === false && noteToCheck.files?.some((v) => v.isSensitive)) {
return 'sensitiveMute';
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';
}
}
return false;
}
export function checkWordMute(note: string | Misskey.entities.Note, me: Misskey.entities.UserLite | null | undefined, mutedWords: Array<string | string[]>): Array<string | string[]> | false {
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;
// 自分自身
if (me && typeof(note) === 'object' && (note.userId === me.id)) return false;
@ -79,7 +84,7 @@ export function checkWordMute(note: string | Misskey.entities.Note, me: Misskey.
}, new Set<string>());
// Nested arrays are intentional, otherwise the note components will join with space (" ") and it's confusing.
if (matched.size > 0) return [[Array.from(matched).join(', ')]];
if (matched.size > 0) return Array.from(matched);
}
return false;

View file

@ -4,7 +4,6 @@
*/
import { computed } from 'vue';
import * as Misskey from 'misskey-js';
import type { Ref, WritableComputedRef } from 'vue';
import type { PageHeaderItem } from '@/types/page-header.js';
import type { MenuItem } from '@/types/menu.js';
@ -14,8 +13,6 @@ import { i18n } from '@/i18n.js';
import { popupMenu } from '@/os.js';
import { prefer } from '@/preferences.js';
import { followingTab, followersTab, mutualsTab, defaultFollowingFeedState } from '@/types/following-feed.js';
import { $i } from '@/i';
import { checkWordMute } from '@/utility/check-word-mute';
export function followingTabName(tab: FollowingFeedTab): string;
export function followingTabName(tab: FollowingFeedTab | null | undefined): null;
@ -153,34 +150,3 @@ function createDefaultStorage(): Ref<StorageInterface> {
}));
}
export function getSoftMutedWords(note: Misskey.entities.Note): string | null {
return getMutedWords(note, $i?.mutedWords);
}
export function getHardMutedWords(note: Misskey.entities.Note): string | null {
return getMutedWords(note, $i?.hardMutedWords);
}
// Match the typing used by Misskey
type Mutes = (string | string[])[] | null | undefined;
// Adapted from MkNote.ts
function getMutedWords(note: Misskey.entities.Note, mutes: Mutes): string | null {
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: Mutes): string | null {
if (!note) {
return null;
}
if (!mutes || mutes.length < 1) {
return null;
}
const mutedWords = checkWordMute(note, $i, mutes);
return mutedWords ? mutedWords.flat(2).join(', ') : null;
}