From 5754093507a032b1331188811a1d2f28c0c364a1 Mon Sep 17 00:00:00 2001 From: KoalaSat Date: Wed, 15 Mar 2023 16:14:44 +0100 Subject: [PATCH] reconnect contacts relays --- frontend/Components/LnPayment/index.tsx | 2 +- frontend/Contexts/RelayPoolContext.tsx | 2 +- .../RelayFunctions/Metadata/index.ts | 2 +- frontend/Locales/de.json | 1 + frontend/Locales/en.json | 1 + frontend/Locales/es.json | 1 + frontend/Locales/fr.json | 1 + frontend/Locales/ru.json | 1 + frontend/Locales/zhCn.json | 1 + .../HomePage/NotificationsFeed/index.tsx | 4 +- frontend/Pages/RelaysPage/index.tsx | 111 ++++++++++++++++-- 11 files changed, 113 insertions(+), 14 deletions(-) diff --git a/frontend/Components/LnPayment/index.tsx b/frontend/Components/LnPayment/index.tsx index 069c448..40c0a00 100644 --- a/frontend/Components/LnPayment/index.tsx +++ b/frontend/Components/LnPayment/index.tsx @@ -110,7 +110,7 @@ export const LnPayment: React.FC = ({ open, setOpen, note, user const renderZapperItem: ListRenderItem = ({ item }) => { const zapDescription = item.tags?.find((tag) => tag[0] === 'description') - const content = zapDescription ? JSON.parse(zapDescription[1])?.content : '' + const content = zapDescription ? JSON.parse(zapDescription[1])?.content : undefined return ( setDisplayUserDrawer(item.user_id)}> diff --git a/frontend/Contexts/RelayPoolContext.tsx b/frontend/Contexts/RelayPoolContext.tsx index 0fede23..920f5e2 100644 --- a/frontend/Contexts/RelayPoolContext.tsx +++ b/frontend/Contexts/RelayPoolContext.tsx @@ -96,7 +96,7 @@ export const RelayPoolContextProvider = ({ const event: Event = { content: '', created_at: getUnixTime(new Date()), - kind: 1002, + kind: 10002, pubkey: publicKey, tags: results.map((relay) => ['r', relay.url, relay.mode ?? '']), } diff --git a/frontend/Functions/RelayFunctions/Metadata/index.ts b/frontend/Functions/RelayFunctions/Metadata/index.ts index 46810e4..e1e57d7 100644 --- a/frontend/Functions/RelayFunctions/Metadata/index.ts +++ b/frontend/Functions/RelayFunctions/Metadata/index.ts @@ -20,7 +20,7 @@ export const getContactsRelays: ( relayMetadada.forEach((metadata) => { pubKeys.push(metadata.pubkey) let rTags: string[][] = getRTags(metadata) - rTags = rTags.filter(tag => tag.length < 3 || tag[2] !== 'read') + rTags = rTags.filter((tag) => tag.length < 3 || tag[2] !== 'read') const urls = rTags.map((tags) => tags[1]) urls.forEach((url) => { if (!localhostRegExp.test(url)) { diff --git a/frontend/Locales/de.json b/frontend/Locales/de.json index 14e7f63..30c135e 100644 --- a/frontend/Locales/de.json +++ b/frontend/Locales/de.json @@ -152,6 +152,7 @@ "zap": "Zap" }, "notificationsFeed": { + "connectContactRelays": "Reconnect to contacts' relays", "reposted": "Reposted", "zap": "Zapped {{amount}}", "like": "Like", diff --git a/frontend/Locales/en.json b/frontend/Locales/en.json index 25ee787..f418186 100644 --- a/frontend/Locales/en.json +++ b/frontend/Locales/en.json @@ -274,6 +274,7 @@ "relaysPage": { "pushListTitle": "Push relay list to the network", "pushListDescription": "Only relays on \"My relays\" list will be pushed.", + "connectContactRelays": "Reconnect to contacts' relays", "pushList": "Push list", "contactsList": "Contact's relays", "freeAccess": "Free access", diff --git a/frontend/Locales/es.json b/frontend/Locales/es.json index bc2a262..e3fbbbb 100644 --- a/frontend/Locales/es.json +++ b/frontend/Locales/es.json @@ -176,6 +176,7 @@ "reposted": "Reposteado", "zap": "Zap {{amount}}", "like": "Le ha gustado", + "connectContactRelays": "Reconnectar a los relays de mis contactos", "Dislike": "No le ha gustado", "replied": "Respuesta", "emptyTitle": "No tienes notificationes.", diff --git a/frontend/Locales/fr.json b/frontend/Locales/fr.json index 2f4c343..29bb4d8 100644 --- a/frontend/Locales/fr.json +++ b/frontend/Locales/fr.json @@ -312,6 +312,7 @@ } }, "relaysPage": { + "connectContactRelays": "Reconnect to contacts' relays", "pushListTitle": "Push relay list to the network", "pushListDescription": "Only relays on \"My relays\" list will be pushed.", "pushList": "Push list", diff --git a/frontend/Locales/ru.json b/frontend/Locales/ru.json index c8b0e6f..62c6ef4 100644 --- a/frontend/Locales/ru.json +++ b/frontend/Locales/ru.json @@ -307,6 +307,7 @@ } }, "relaysPage": { + "connectContactRelays": "Reconnect to contacts' relays", "pushListTitle": "Push relay list to the network", "pushListDescription": "Only relays on \"My relays\" list will be pushed.", "pushList": "Push list", diff --git a/frontend/Locales/zhCn.json b/frontend/Locales/zhCn.json index 090d262..b024f6f 100644 --- a/frontend/Locales/zhCn.json +++ b/frontend/Locales/zhCn.json @@ -283,6 +283,7 @@ "newMessages": "有{{newNotesCount}}条新的 Notes,下拉刷新" }, "relaysPage": { + "connectContactRelays": "Reconnect to contacts' relays", "pushListTitle": "Push relay list to the network", "pushListDescription": "Only relays on \"My relays\" list will be pushed.", "pushList": "Push list", diff --git a/frontend/Pages/HomePage/NotificationsFeed/index.tsx b/frontend/Pages/HomePage/NotificationsFeed/index.tsx index 0266a56..40670f7 100644 --- a/frontend/Pages/HomePage/NotificationsFeed/index.tsx +++ b/frontend/Pages/HomePage/NotificationsFeed/index.tsx @@ -77,7 +77,8 @@ export const NotificationsFeed: React.FC = () => { }, [pushedTab]) useEffect(() => { - if (pubKeys.length > 0) { + if (database && pubKeys.length > 0) { + getUsers(database, { includeIds: pubKeys }).then(setUsers) relayPool?.subscribe('notification-users', [ { kinds: [Kind.Metadata], @@ -132,7 +133,6 @@ export const NotificationsFeed: React.FC = () => { const loadNotes: () => void = async () => { if (database && publicKey) { - getUsers(database, { includeIds: pubKeys }).then(setUsers) getReactions(database, { reactedUser: publicKey, limitDate }).then((results) => { setPubKeys((prev) => [...prev, ...results.map((res) => res.pubkey)]) setReaction(results) diff --git a/frontend/Pages/RelaysPage/index.tsx b/frontend/Pages/RelaysPage/index.tsx index 086dabb..a5f8c8b 100644 --- a/frontend/Pages/RelaysPage/index.tsx +++ b/frontend/Pages/RelaysPage/index.tsx @@ -30,6 +30,10 @@ import { useFocusEffect } from '@react-navigation/native' import { UserContext } from '../../Contexts/UserContext' import { type Event } from '../../lib/nostr/Events' import { getUnixTime } from 'date-fns' +import { getAllRelayMetadata } from '../../Functions/DatabaseFunctions/RelayMetadatas' +import { getContactsRelays } from '../../Functions/RelayFunctions/Metadata' +import { AppContext } from '../../Contexts/AppContext' +import { getUsers, User } from '../../Functions/DatabaseFunctions/Users' export const RelaysPage: React.FC = () => { const defaultRelayInput = React.useMemo(() => 'wss://', []) @@ -42,31 +46,40 @@ export const RelaysPage: React.FC = () => { relays, lastEventId, loadRelays, + removeRelayItem, } = useContext(RelayPoolContext) const { publicKey } = useContext(UserContext) + const { database } = useContext(AppContext) const { t } = useTranslation('common') const theme = useTheme() const bottomSheetAddRef = React.useRef(null) const bottomSheetPushRef = React.useRef(null) + const bottomSheetContactsRef = React.useRef(null) const [addRelayInput, setAddRelayInput] = useState(defaultRelayInput) const [addRelayPaid, setAddRelayPaid] = useState(false) const [showNotification, setShowNotification] = useState() + const [asignation, setAsignation] = useState() const [showPaidRelays, setShowPaidRelays] = useState(true) const [showFreeRelays, setShowFreeRelays] = useState(true) useFocusEffect( React.useCallback(() => { relayPool?.unsubscribeAll() - if (publicKey) { - relayPool?.subscribe('relays', [ - { - kinds: [1002], - authors: [publicKey], - }, - ]) + if (publicKey && database) { + getUsers(database, {}).then((results) => { + if (results.length > 0) { + const authors = [...results.map((user: User) => user.id), publicKey] + relayPool?.subscribe('relays-contacts', [ + { + kinds: [10002], + authors, + }, + ]) + } + }) } - return () => relayPool?.unsubscribe(['relays']) + return () => relayPool?.unsubscribe(['relays-contacts']) }, []), ) @@ -172,6 +185,14 @@ export const RelaysPage: React.FC = () => { return 0 }) + const calculateContactsRelays: () => void = () => { + if (database) { + getAllRelayMetadata(database).then((relayMetadata) => { + getContactsRelays(relays, relayMetadata).then(setAsignation) + }) + } + } + const renderItem: ListRenderItem = ({ item, index }) => { return ( @@ -230,12 +251,42 @@ export const RelaysPage: React.FC = () => { ) } + const renderContactRelayItem: ListRenderItem = ({ item, index }) => { + return ( + + ( + + )} + /> + + ) + } + const pasteUrl: () => void = () => { Clipboard.getString().then((value) => { setAddRelayInput(value ?? '') }) } + const onPressAddContactRelay: () => void = () => { + relays + .filter((relay) => relay.resilient && relay.resilient > 0) + .forEach((relay) => { + removeRelayItem(relay) + }) + if (asignation) { + asignation.forEach(async (url) => await addRelayItem({ url, resilient: 1, global_feed: 0 })) + } + bottomSheetContactsRef.current?.close() + } + return ( @@ -297,6 +348,18 @@ export const RelaysPage: React.FC = () => { ItemSeparatorComponent={Divider} /> + { + calculateContactsRelays() + bottomSheetContactsRef.current?.open() + }} + animateFrom='right' + iconMode='static' + extended={false} + /> { + + + + + + + @@ -410,12 +494,16 @@ const styles = StyleSheet.create({ marginBottom: -10, }, bottomDrawerButton: { + paddingTop: 16, paddingBottom: 16, }, relayOptionButton: { margin: 0, marginRight: 10, }, + conteactRelaysList: { + maxHeight: 400, + }, container: { padding: 0, }, @@ -434,7 +522,12 @@ const styles = StyleSheet.create({ marginLeft: 32, }, relayList: { - paddingBottom: 200, + paddingBottom: 260, + }, + fabContacts: { + bottom: 212, + right: 16, + position: 'absolute', }, fabPush: { bottom: 142,