chore: refactoring

This commit is contained in:
florian 2023-07-28 18:06:35 +02:00
parent fa096e5635
commit f6221b6802
3 changed files with 33 additions and 41 deletions

View File

@ -5,14 +5,13 @@ import {
NostrImage, NostrImage,
buildFilter, buildFilter,
extractImageUrls, extractImageUrls,
hasContentWarning,
hasNsfwTag,
isImage, isImage,
isNsfwRelated,
isReply, isReply,
isVideo, isVideo,
prepareContent, prepareContent,
} from './nostrImageDownload'; } from './nostrImageDownload';
import { nfswTags, nsfwNPubs, nsfwPubKeys } from './env'; import { defaultRelays, nfswTags, nsfwNPubs } from './env';
import Settings from './Settings'; import Settings from './Settings';
import SlideView from './SlideView'; import SlideView from './SlideView';
import GridView from './GridView'; import GridView from './GridView';
@ -23,6 +22,7 @@ import AdultContentInfo from './AdultContentInfo';
import IconSettings from './Icons/IconSettings'; import IconSettings from './Icons/IconSettings';
import IconPlay from './Icons/IconPlay'; import IconPlay from './Icons/IconPlay';
import IconGrid from './Icons/IconGrid'; import IconGrid from './Icons/IconGrid';
import { NDKEvent } from '@nostr-dev-kit/ndk';
/* /*
FEATURES: FEATURES:
@ -45,34 +45,23 @@ FEATURES:
- Prevent duplicate images (shuffle? histroy?) - Prevent duplicate images (shuffle? histroy?)
*/ */
// let oldest = Infinity;
let maxFetchCount = 1;
let eventsReceived = 0;
const SlideShow = (settings: Settings) => { const SlideShow = (settings: Settings) => {
const { ndk, loadNdk } = useNDK(); const { ndk, loadNdk } = useNDK();
const [posts, setPosts] = useState<any[]>([]); const [posts, setPosts] = useState<NDKEvent[]>([]);
const images = useRef<NostrImage[]>([]); const images = useRef<NostrImage[]>([]);
const fetchTimeoutHandle = useRef(0); const fetchTimeoutHandle = useRef(0);
const [showGrid, setShowGrid] = useState(false); const [showGrid, setShowGrid] = useState(false);
const [showSettings, setShowSettings] = useState(false); const [showSettings, setShowSettings] = useState(false);
const fetch = () => { const fetch = () => {
eventsReceived = 0;
const postSubscription = ndk.subscribe(buildFilter(settings.tags, settings.npubs)); const postSubscription = ndk.subscribe(buildFilter(settings.tags, settings.npubs));
postSubscription.on('event', event => { postSubscription.on('event', (event: NDKEvent) => {
eventsReceived++;
setPosts(oldPosts => { setPosts(oldPosts => {
if ( if (
!isReply(event) && !isReply(event) &&
oldPosts.findIndex(p => p.id === event.id) === -1 && oldPosts.findIndex(p => p.id === event.id) === -1 && // not duplicate
(settings.showNsfw || (settings.showNsfw || !isNsfwRelated(event))
(!hasContentWarning(event) && // only allow content warnings on profile content
!hasNsfwTag(event) && // only allow nsfw on profile content
!nsfwPubKeys.includes(event.pubkey.toLowerCase()))) // block nsfw authors
) { ) {
return [...oldPosts, event]; return [...oldPosts, event];
} }
@ -89,29 +78,11 @@ const SlideShow = (settings: Settings) => {
}; };
}; };
useEffect(() => {
loadNdk([
'wss://relay.damus.io',
'wss://relay.nostr.band',
'wss://nos.lol',
'wss://relay.mostr.pub',
'wss://relay.shitforce.one/',
"wss://nostr.wine",
// "wss://nostr1.current.fyi/",
'wss://purplepag.es/', // needed for user profiles
//"wss://feeds.nostr.band/pics",
]);
}, []);
useEffect(() => { useEffect(() => {
// reset all // reset all
console.log(`resetting`);
setPosts([]); setPosts([]);
maxFetchCount = 20;
eventsReceived = 0;
images.current = []; images.current = [];
clearTimeout(fetchTimeoutHandle.current); clearTimeout(fetchTimeoutHandle.current);
return fetch(); return fetch();
}, [settings]); }, [settings]);
@ -152,6 +123,7 @@ const SlideShow = (settings: Settings) => {
}; };
useEffect(() => { useEffect(() => {
loadNdk(defaultRelays);
window.addEventListener('keydown', onKeyDown); window.addEventListener('keydown', onKeyDown);
return () => { return () => {
window.removeEventListener('keydown', onKeyDown); window.removeEventListener('keydown', onKeyDown);

View File

@ -87,3 +87,15 @@ export const nsfwNPubs = [
export const nsfwPubKeys = nsfwNPubs.map(npub => (nip19.decode(npub).data as string).toLowerCase()); export const nsfwPubKeys = nsfwNPubs.map(npub => (nip19.decode(npub).data as string).toLowerCase());
export const spamAccounts = []; export const spamAccounts = [];
export const defaultRelays = [
'wss://relay.damus.io',
'wss://relay.nostr.band',
'wss://nos.lol',
'wss://relay.mostr.pub',
'wss://relay.shitforce.one/',
'wss://nostr.wine',
// "wss://nostr1.current.fyi/",
'wss://purplepag.es/', // needed for user profiles
//"wss://feeds.nostr.band/pics",
];

View File

@ -1,6 +1,6 @@
import { NDKFilter } from '@nostr-dev-kit/ndk'; import { NDKEvent, NDKFilter } from '@nostr-dev-kit/ndk';
import { nip19 } from 'nostr-tools'; import { nip19 } from 'nostr-tools';
import { nfswTags } from './env'; import { nfswTags, nsfwPubKeys } from './env';
export type NostrImage = { export type NostrImage = {
url: string; url: string;
@ -48,21 +48,29 @@ export const extractImageUrls = (text: string): string[] => {
return (text.match(urlRegex) || []).map(u => urlFix(u)); return (text.match(urlRegex) || []).map(u => urlFix(u));
}; };
export const isReply = (event: any) => { export const isReply = (event: NDKEvent) => {
// ["e", "aab5a68f29d76a04ad79fe7e489087b802ee0f946689d73b0e15931dd40a7af3", "", "reply"] // ["e", "aab5a68f29d76a04ad79fe7e489087b802ee0f946689d73b0e15931dd40a7af3", "", "reply"]
return event.tags.filter((t: string[]) => t[0] === 'e' && t[3] === 'reply').length > 0; return event.tags.filter((t: string[]) => t[0] === 'e' && t[3] === 'reply').length > 0;
}; };
export const hasContentWarning = (event: any) => { export const hasContentWarning = (event: NDKEvent) => {
// ["content-warning", "NSFW: implied nudity"] // ["content-warning", "NSFW: implied nudity"]
return event.tags.filter((t: string[]) => t[0] === 'content-warning').length > 0; return event.tags.filter((t: string[]) => t[0] === 'content-warning').length > 0;
}; };
export const hasNsfwTag = (event: any) => { export const hasNsfwTag = (event: NDKEvent) => {
// ["e", "aab5a68f29d76a04ad79fe7e489087b802ee0f946689d73b0e15931dd40a7af3", "", "reply"] // ["e", "aab5a68f29d76a04ad79fe7e489087b802ee0f946689d73b0e15931dd40a7af3", "", "reply"]
return event.tags.filter((t: string[]) => t[0] === 't' && nfswTags.includes(t[1])).length > 0; return event.tags.filter((t: string[]) => t[0] === 't' && nfswTags.includes(t[1])).length > 0;
}; };
export const isNsfwRelated = (event: NDKEvent) => {
return (
hasContentWarning(event) || // block content warning
hasNsfwTag(event) || // block nsfw tags
nsfwPubKeys.includes(event.pubkey.toLowerCase()) // block nsfw authors
);
};
export const isImage = (url: string) => { export const isImage = (url: string) => {
return ( return (
url.endsWith('.jpg') || url.endsWith('.jpg') ||