chore: refactoring
This commit is contained in:
parent
fa096e5635
commit
f6221b6802
@ -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<any[]>([]);
|
||||
const [posts, setPosts] = useState<NDKEvent[]>([]);
|
||||
const images = useRef<NostrImage[]>([]);
|
||||
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);
|
||||
|
@ -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",
|
||||
];
|
||||
|
@ -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') ||
|
||||
|
Loading…
Reference in New Issue
Block a user