mirror of
https://github.com/aljazceru/rabbit.git
synced 2025-12-17 05:54:19 +01:00
feat: improve performance for mute
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { createRoot, type Accessor, type Setter } from 'solid-js';
|
import { createRoot, type Accessor, type Setter, createMemo } from 'solid-js';
|
||||||
|
|
||||||
import sortBy from 'lodash/sortBy';
|
import sortBy from 'lodash/sortBy';
|
||||||
import uniq from 'lodash/uniq';
|
import uniq from 'lodash/uniq';
|
||||||
@@ -21,6 +21,7 @@ import {
|
|||||||
} from '@/hooks/createSignalWithStorage';
|
} from '@/hooks/createSignalWithStorage';
|
||||||
import { useTranslation } from '@/i18n/useTranslation';
|
import { useTranslation } from '@/i18n/useTranslation';
|
||||||
import { genericEvent } from '@/nostr/event';
|
import { genericEvent } from '@/nostr/event';
|
||||||
|
import { asCaseInsensitive, wordsRegex } from '@/utils/regex';
|
||||||
|
|
||||||
export type CustomEmojiConfig = {
|
export type CustomEmojiConfig = {
|
||||||
shortcode: string;
|
shortcode: string;
|
||||||
@@ -218,11 +219,13 @@ const useConfig = (): UseConfig => {
|
|||||||
[(e) => e.shortcode.length],
|
[(e) => e.shortcode.length],
|
||||||
);
|
);
|
||||||
|
|
||||||
const isPubkeyMuted = (pubkey: string) => config.mutedPubkeys.includes(pubkey);
|
const mutedPubkeySet = createMemo(() => new Set(config.mutedPubkeys));
|
||||||
|
const isPubkeyMuted = (pubkey: string) => mutedPubkeySet().has(pubkey);
|
||||||
|
|
||||||
|
const mutedKeywordsRegex = createMemo(() => asCaseInsensitive(wordsRegex(config.mutedKeywords)));
|
||||||
const hasMutedKeyword = (event: NostrEvent) => {
|
const hasMutedKeyword = (event: NostrEvent) => {
|
||||||
if (event.kind === Kind.ShortTextNote) {
|
if (event.kind === Kind.ShortTextNote) {
|
||||||
return config.mutedKeywords.some((keyword) => event.content.includes(keyword));
|
return mutedKeywordsRegex().test(event.content);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ const useEvent = (propsProvider: () => UseEventProps | null): UseEvent => {
|
|||||||
const promise = task.firstEventPromise().catch(() => {
|
const promise = task.firstEventPromise().catch(() => {
|
||||||
throw new Error(`event not found: ${eventId}`);
|
throw new Error(`event not found: ${eventId}`);
|
||||||
});
|
});
|
||||||
|
console.log('useEvent', props());
|
||||||
registerTask({ task, signal });
|
registerTask({ task, signal });
|
||||||
return timeout(15000, `useEvent: ${eventId}`)(promise);
|
return timeout(15000, `useEvent: ${eventId}`)(promise);
|
||||||
},
|
},
|
||||||
|
|||||||
15
src/utils/regex.test.ts
Normal file
15
src/utils/regex.test.ts
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
import assert from 'assert';
|
||||||
|
|
||||||
|
import { describe, it } from 'vitest';
|
||||||
|
|
||||||
|
import { escapeRegExpSymbols } from '@/utils/regex';
|
||||||
|
|
||||||
|
describe('escapeRegExpString', () => {
|
||||||
|
it.each<{ given: string; expected: string }>([
|
||||||
|
{ given: '^hello$', expected: '\\^hello\\$' },
|
||||||
|
{ given: '.*+?^${}()|[]\\', expected: '\\.\\*\\+\\?\\^\\$\\{\\}\\(\\)\\|\\[\\]\\\\' },
|
||||||
|
])('should return $expected for $given', ({ given, expected }) => {
|
||||||
|
const actual = escapeRegExpSymbols(given);
|
||||||
|
assert.deepStrictEqual(actual, expected);
|
||||||
|
});
|
||||||
|
});
|
||||||
8
src/utils/regex.ts
Normal file
8
src/utils/regex.ts
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
export const escapeRegExpSymbols = (s: string): string => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||||
|
|
||||||
|
export const wordsRegex = (words: string[]) => new RegExp(words.map(escapeRegExpSymbols).join('|'));
|
||||||
|
|
||||||
|
export const asCaseInsensitive = (regex: RegExp) => {
|
||||||
|
if (regex.flags.indexOf('i') >= 0) return regex;
|
||||||
|
return new RegExp(regex.source, `${regex.flags}i`);
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user