From f6221b6802a409b8a84167633ddf11533be0879b Mon Sep 17 00:00:00 2001 From: Florian Maul Date: Fri, 28 Jul 2023 18:06:35 +0200 Subject: [PATCH] chore: refactoring --- src/components/SlideShow.tsx | 44 +++++----------------------- src/components/env.ts | 12 ++++++++ src/components/nostrImageDownload.ts | 18 ++++++++---- 3 files changed, 33 insertions(+), 41 deletions(-) diff --git a/src/components/SlideShow.tsx b/src/components/SlideShow.tsx index 26a5af0..fc12354 100644 --- a/src/components/SlideShow.tsx +++ b/src/components/SlideShow.tsx @@ -5,14 +5,13 @@ import { NostrImage, buildFilter, extractImageUrls, - hasContentWarning, - hasNsfwTag, isImage, + isNsfwRelated, isReply, isVideo, prepareContent, } from './nostrImageDownload'; -import { nfswTags, nsfwNPubs, nsfwPubKeys } from './env'; +import { defaultRelays, nfswTags, nsfwNPubs } from './env'; import Settings from './Settings'; import SlideView from './SlideView'; import GridView from './GridView'; @@ -23,6 +22,7 @@ import AdultContentInfo from './AdultContentInfo'; import IconSettings from './Icons/IconSettings'; import IconPlay from './Icons/IconPlay'; import IconGrid from './Icons/IconGrid'; +import { NDKEvent } from '@nostr-dev-kit/ndk'; /* FEATURES: @@ -45,34 +45,23 @@ FEATURES: - Prevent duplicate images (shuffle? histroy?) */ -// let oldest = Infinity; -let maxFetchCount = 1; -let eventsReceived = 0; - const SlideShow = (settings: Settings) => { const { ndk, loadNdk } = useNDK(); - const [posts, setPosts] = useState([]); + const [posts, setPosts] = useState([]); const images = useRef([]); const fetchTimeoutHandle = useRef(0); const [showGrid, setShowGrid] = useState(false); const [showSettings, setShowSettings] = useState(false); const fetch = () => { - eventsReceived = 0; - const postSubscription = ndk.subscribe(buildFilter(settings.tags, settings.npubs)); - postSubscription.on('event', event => { - eventsReceived++; - + postSubscription.on('event', (event: NDKEvent) => { setPosts(oldPosts => { if ( !isReply(event) && - oldPosts.findIndex(p => p.id === event.id) === -1 && - (settings.showNsfw || - (!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 + oldPosts.findIndex(p => p.id === event.id) === -1 && // not duplicate + (settings.showNsfw || !isNsfwRelated(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(() => { // reset all - console.log(`resetting`); setPosts([]); - maxFetchCount = 20; - eventsReceived = 0; images.current = []; clearTimeout(fetchTimeoutHandle.current); - return fetch(); }, [settings]); @@ -152,6 +123,7 @@ const SlideShow = (settings: Settings) => { }; useEffect(() => { + loadNdk(defaultRelays); window.addEventListener('keydown', onKeyDown); return () => { window.removeEventListener('keydown', onKeyDown); diff --git a/src/components/env.ts b/src/components/env.ts index 48a949e..c38375a 100644 --- a/src/components/env.ts +++ b/src/components/env.ts @@ -87,3 +87,15 @@ export const nsfwNPubs = [ export const nsfwPubKeys = nsfwNPubs.map(npub => (nip19.decode(npub).data as string).toLowerCase()); 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", +]; diff --git a/src/components/nostrImageDownload.ts b/src/components/nostrImageDownload.ts index 13fece1..80f9597 100644 --- a/src/components/nostrImageDownload.ts +++ b/src/components/nostrImageDownload.ts @@ -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 { nfswTags } from './env'; +import { nfswTags, nsfwPubKeys } from './env'; export type NostrImage = { url: string; @@ -48,21 +48,29 @@ export const extractImageUrls = (text: string): string[] => { return (text.match(urlRegex) || []).map(u => urlFix(u)); }; -export const isReply = (event: any) => { +export const isReply = (event: NDKEvent) => { // ["e", "aab5a68f29d76a04ad79fe7e489087b802ee0f946689d73b0e15931dd40a7af3", "", "reply"] 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"] 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"] 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) => { return ( url.endsWith('.jpg') ||