-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add user metadata; send zaps to correct lud16 (#67)
* 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
Showing
12 changed files
with
208 additions
and
32 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
packages/app/features/chat/hooks/useUserMetadataForChannel.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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}`) | ||
|
@@ -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} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters