Skip to content

Commit

Permalink
Add user metadata; send zaps to correct lud16 (#67)
Browse files Browse the repository at this point in the history
* create button text brighter

* before ai brain

* before use list

* horrible hook but works

* use correct lud16
  • Loading branch information
Christopher David authored Mar 30, 2023
1 parent a37fc46 commit c2d8e47
Show file tree
Hide file tree
Showing 12 changed files with 208 additions and 32 deletions.
Binary file modified .yarn/install-state.gz
Binary file not shown.
1 change: 1 addition & 0 deletions apps/expo/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// registerRootComponent happens in "expo-router/entry"
import 'react-native-url-polyfill/auto'
import 'text-encoding-polyfill'
// import { LogBox } from 'react-native'r
// LogBox.ignoreLogs(['Require cycle', 'No native'])
Expand Down
7 changes: 4 additions & 3 deletions packages/app/features/chat/components/MessageList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { FlatList, View } from 'react-native'
import { Channel } from 'stores/types'
import { Stack, Text } from 'tamagui'
import { Message } from '@my/ui/src/components/Message'

// import { Message } from './Message'
import { useUserMetadataForMessages } from 'app/lib/hooks/useUserMetadataForMessages'

type Props = {
channel: Channel
Expand All @@ -15,10 +14,12 @@ export const MessageList: React.FC<Props> = ({ channel }) => {
const messages = useChannelMessages(channel).sort((a, b) => {
return a.created_at - b.created_at
})
const messagesWithMetadata = useUserMetadataForMessages(messages)
// console.log('messagesWithMetadata', messagesWithMetadata)

return (
<FlatList
data={messages}
data={messagesWithMetadata}
renderItem={({ item }) => <Message message={item} />}
keyExtractor={(item) => item.id}
style={{ paddingHorizontal: 10 }}
Expand Down
3 changes: 0 additions & 3 deletions packages/app/features/chat/hooks/useMessages.ts

This file was deleted.

10 changes: 10 additions & 0 deletions packages/app/features/chat/hooks/useUserMetadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { useStore } from 'stores/index'

export const useUserMetadata = (pubkey: string) => {
const userMetadata = useStore((s) => s.userMetadata)
let metadata: any = null
if (userMetadata.has(pubkey)) {
metadata = userMetadata.get(pubkey)
}
return metadata
}
24 changes: 24 additions & 0 deletions packages/app/features/chat/hooks/useUserMetadataForChannel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { useStore } from 'stores/index'
import { useEffect } from 'react'
import { useChannelMessages } from 'app/lib/hooks'
import { Channel, ChannelMessage } from 'app/stores/types'

export const useUserMetadataForChannel = (channel: Channel) => {
const messages: ChannelMessage[] = useChannelMessages(channel)
useEffect(() => {
console.log('messages', messages.length)
}, [messages])
// useEffect(() => {
// // Extract the list of unique public keys of the senders of the messages
// const uniquePubkeys = [
// ...new Set(messages.map((message) => message.pubkey)),
// ]
// console.log('uniquePubkeys', uniquePubkeys.length)
// // Now fetch metadata for each pubkey
// // uniquePubkeys.forEach((pubkey) => {
// // if (!userMetadata.has(pubkey)) {
// // fetchUser(pubkey)
// // }
// // })
// }, [messages])
}
3 changes: 3 additions & 0 deletions packages/app/features/chat/screens/ChannelScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useEffect } from 'react'
import { ActivityIndicator } from 'react-native'
import { Screen } from '@my/ui/src'
import { MessageInput, MessageList } from '../components'
import { useUserMetadataForChannel } from '../hooks/useUserMetadataForChannel'

const { useParam } = createParam<{ channel: string }>()

Expand All @@ -15,6 +16,8 @@ export const ChannelScreen = ({ navigation, route }) => {
? JSON.parse(channelString)
: channelString

// useUserMetadataForChannel(channel)

useEffect(() => {
setOptions({ title: channel?.title ?? 'Unnamed Channel' })
}, [channel])
Expand Down
1 change: 1 addition & 0 deletions packages/app/features/user/CreateAccountScreen.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export const CreateAccountScreen = () => {
w="100%"
iconAfter={ChevronsRight}
onPress={handleSubmit}
color="$color11"
>
Create
</Button>
Expand Down
101 changes: 101 additions & 0 deletions packages/app/lib/hooks/useUserMetadataForMessages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { ChannelMessage } from 'app/stores/types'
import { SimplePool } from 'nostr-tools'
import { useState, useEffect, useMemo } from 'react'
import { generateRandomPlacekitten } from '../utils'

const relays = [
'wss://relay.damus.io',
'wss://arc1.arcadelabs.co',
'wss://relay.snort.social',
]

const pool = new SimplePool()

// const relays = [
// 'wss://relay.damus.io',
// 'wss://arc1.arcadelabs.co',
// 'wss://relay.snort.social',
// 'wss://eden.nostr.land',
// 'wss://nos.lol',
// 'wss://brb.io',
// 'wss://relay.current.fyi',
// 'wss://nostr.orangepill.dev',
// 'wss://nostr-pub.wellorder.net',
// 'wss://nostr.bitcoiner.social',
// 'wss://nostr.oxtr.dev',
// ]

export function useUserMetadataForMessages(messages: ChannelMessage[]) {
const [messagesWithMetadata, setMessagesWithMetadata] = useState<
ChannelMessage[]
>([])

useEffect(() => {
const pubkeys = getUniquePubkeys(messages)
console.log('Unique pubkeys:', pubkeys.length)

async function fetchData() {
const userMetadata = await fetchUserMetadata(pubkeys)
console.log('User metadata:', Object.keys(userMetadata).length, 'users')

const updatedMessages = messages.map((message) => {
const metadata = userMetadata[message.pubkey] || {
name: message.pubkey.slice(0, 10),
avatar: generateRandomPlacekitten(),
}

return {
...message,
user: {
pubkey: message.pubkey,
...metadata,
},
}
})

setMessagesWithMetadata(updatedMessages)
}

fetchData()
}, [messages])

return messagesWithMetadata
}

async function fetchUserMetadata(pubkeys: string[]) {
const userMetadata: {
[pubkey: string]: { name: string; avatar: string; lud16: string | null }
} = {}

try {
await pool.ensureRelay(relays[0] as string)
await pool.ensureRelay(relays[1] as string)
await pool.ensureRelay(relays[2] as string)

const events = await pool.list(relays, [{ authors: pubkeys, kinds: [0] }])

events.forEach((event) => {
const metadata = JSON.parse(event.content)
userMetadata[event.pubkey] = {
name: metadata.name || 'Unknown',
avatar: metadata.picture || generateRandomPlacekitten(),
lud16: metadata.lud16 || null,
}
})

return userMetadata
} catch (error) {
console.error('Error fetching user metadata:', error)
throw error
}
}

function getUniquePubkeys(messages: ChannelMessage[]): string[] {
const pubkeySet = new Set<string>()

messages.forEach((message) => {
pubkeySet.add(message.pubkey)
})

return Array.from(pubkeySet)
}
3 changes: 2 additions & 1 deletion packages/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"expo-clipboard": "~4.1.2",
"expo-linking": "~4.0.1",
"js-lnurl": "^0.5.1",
"nostr-tools": "^1.1.1",
"nostr-tools": "^1.8.1",
"react-native-url-polyfill": "^1.3.0",
"solito": "2.1.3",
"text-encoding-polyfill": "0.6.7",
"zustand": "4.3.6"
Expand Down
43 changes: 25 additions & 18 deletions packages/ui/src/components/Message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ import * as Linking from 'expo-linking'

type Props = {
currentUser: string // pubkey
message: ChannelMessage
message: any
}

export const Message: React.FC<Props> = ({ currentUser, message }) => {
// console.log(message)
const userMetadata = {
name: message.pubkey.slice(0, 10),
picture: 'https://placekitten.com/200/200',
name: message.user?.name ?? message.pubkey.slice(0, 10),
picture: message.user?.avatar ?? 'https://placekitten.com/200/200',
lud16: message.user?.lud16 ?? null,
}
console.log(userMetadata.lud16)
const align = message.pubkey === currentUser ? 'flex-end' : 'flex-start'
const isCurrentUser = message.pubkey === currentUser
const pic = isCurrentUser
Expand All @@ -24,7 +27,7 @@ export const Message: React.FC<Props> = ({ currentUser, message }) => {
const onLongPress = async () => {
const invoice = await saveZap({
eventId: message.id,
lud16: '[email protected]',
lud16: message.user.lud16,
})
try {
Linking.openURL(`lightning:${invoice}`)
Expand Down Expand Up @@ -77,20 +80,24 @@ export const Message: React.FC<Props> = ({ currentUser, message }) => {
>
{userMetadata?.name ?? truncateString(message.pubkey, 10)}
</Paragraph>
<XStack ai="center" jc="center">
<Zap color="$orange11" size={14} />
<Paragraph
color="$orange11"
lineHeight={14}
// fontWeight="700"
fontSize="$2"
fontFamily="$body"
mt={1}
ml={1}
>
{satsZapped}
</Paragraph>
</XStack>
{userMetadata?.lud16 ? (
<XStack ai="center" jc="center">
<Zap color="$orange11" size={14} />
<Paragraph
color="$orange11"
lineHeight={14}
// fontWeight="700"
fontSize="$2"
fontFamily="$body"
mt={1}
ml={1}
>
{satsZapped}
</Paragraph>
</XStack>
) : (
<></>
)}
</XStack>
<Paragraph
mt={2}
Expand Down
44 changes: 37 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5417,7 +5417,8 @@ __metadata:
expo-clipboard: ~4.1.2
expo-linking: ~4.0.1
js-lnurl: ^0.5.1
nostr-tools: ^1.1.1
nostr-tools: ^1.8.1
react-native-url-polyfill: ^1.3.0
solito: 2.1.3
text-encoding-polyfill: 0.6.7
zustand: 4.3.6
Expand Down Expand Up @@ -6120,7 +6121,7 @@ __metadata:
languageName: node
linkType: hard

"buffer@npm:^5.5.0, buffer@npm:^5.6.0":
"buffer@npm:^5.4.3, buffer@npm:^5.5.0, buffer@npm:^5.6.0":
version: 5.7.1
resolution: "buffer@npm:5.7.1"
dependencies:
Expand Down Expand Up @@ -12509,17 +12510,17 @@ __metadata:
languageName: node
linkType: hard

"nostr-tools@npm:^1.1.1":
version: 1.7.5
resolution: "nostr-tools@npm:1.7.5"
"nostr-tools@npm:^1.8.1":
version: 1.8.1
resolution: "nostr-tools@npm:1.8.1"
dependencies:
"@noble/hashes": 1.0.0
"@noble/secp256k1": ^1.7.1
"@scure/base": ^1.1.1
"@scure/bip32": ^1.1.5
"@scure/bip39": ^1.1.1
prettier: ^2.8.4
checksum: a46de3778a1462d7bf07720ed1e50d960d8499044d2cd6c679c13dbb176b5301724cf71896a2c445494595245b6f703fed827a91281b5c8c12084a9492f214e6
checksum: 6012159ade2fe4711d837a5472064aea31710a1d72753a9daafb476e158464d5eb244d357ae89ab8d1aa27e4e19dd7da28d4d8bbc10d94214e4fc0f3d2a805d1
languageName: node
linkType: hard

Expand Down Expand Up @@ -13469,7 +13470,7 @@ prisma@latest:
languageName: node
linkType: hard

"punycode@npm:^2.1.0":
"punycode@npm:^2.1.0, punycode@npm:^2.1.1":
version: 2.3.0
resolution: "punycode@npm:2.3.0"
checksum: 39f760e09a2a3bbfe8f5287cf733ecdad69d6af2fe6f97ca95f24b8921858b91e9ea3c9eeec6e08cede96181b3bb33f95c6ffd8c77e63986508aa2e8159fa200
Expand Down Expand Up @@ -13777,6 +13778,17 @@ prisma@latest:
languageName: node
linkType: hard

"react-native-url-polyfill@npm:^1.3.0":
version: 1.3.0
resolution: "react-native-url-polyfill@npm:1.3.0"
dependencies:
whatwg-url-without-unicode: 8.0.0-3
peerDependencies:
react-native: "*"
checksum: 24ef81493a642b9359c1e8048de4da84fb554ffcf35677f9d8b11956d700faa4b9ac9d74f56aee67dd8d8b4ef6d9879e7431848785880ada657f8bec0211b00a
languageName: node
linkType: hard

"react-native-web-internals@npm:1.9.16":
version: 1.9.16
resolution: "react-native-web-internals@npm:1.9.16"
Expand Down Expand Up @@ -16625,6 +16637,13 @@ prisma@latest:
languageName: node
linkType: hard

"webidl-conversions@npm:^5.0.0":
version: 5.0.0
resolution: "webidl-conversions@npm:5.0.0"
checksum: ccf1ec2ca7c0b5671e5440ace4a66806ae09c49016ab821481bec0c05b1b82695082dc0a27d1fe9d804d475a408ba0c691e6803fd21be608e710955d4589cd69
languageName: node
linkType: hard

"webpack-sources@npm:^1.4.3":
version: 1.4.3
resolution: "webpack-sources@npm:1.4.3"
Expand All @@ -16642,6 +16661,17 @@ prisma@latest:
languageName: node
linkType: hard

"whatwg-url-without-unicode@npm:8.0.0-3":
version: 8.0.0-3
resolution: "whatwg-url-without-unicode@npm:8.0.0-3"
dependencies:
buffer: ^5.4.3
punycode: ^2.1.1
webidl-conversions: ^5.0.0
checksum: 1fe266f7161e0bd961087c1254a5a59d1138c3d402064495eed65e7590d9caed5a1d9acfd6e7a1b0bf0431253b0e637ee3e4ffc08387cd60e0b2ddb9d4687a4b
languageName: node
linkType: hard

"whatwg-url@npm:^5.0.0":
version: 5.0.0
resolution: "whatwg-url@npm:5.0.0"
Expand Down

0 comments on commit c2d8e47

Please sign in to comment.