Refactor subscriptions (#106)

This commit is contained in:
KoalaSat 2023-01-09 14:57:52 +00:00 committed by GitHub
commit f5f4cb13f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 117 additions and 99 deletions

View File

@ -82,7 +82,7 @@ export const NoteCard: React.FC<NoteCardProps> = ({
kinds: [EventKind.meta], kinds: [EventKind.meta],
authors: [...rootReplies.map((reply) => reply.pubkey), note.pubkey], authors: [...rootReplies.map((reply) => reply.pubkey), note.pubkey],
} }
relayPool?.subscribe('notecard', message) relayPool?.subscribe('notecard', [message])
} }
}) })
} }

View File

@ -54,13 +54,12 @@ export const ContactsPage: React.FC = () => {
getUsers(database, filters).then((results) => { getUsers(database, filters).then((results) => {
if (results && results.length > 0) { if (results && results.length > 0) {
setUsers(results) setUsers(results)
const missingDataUsers = results.filter((user) => !user.picture).map((user) => user.id) relayPool?.subscribe('contacts-meta', [
if (missingDataUsers.length > 0) { {
relayPool?.subscribe('contacts-meta', {
kinds: [EventKind.meta], kinds: [EventKind.meta],
authors: missingDataUsers, authors: results.map((user) => user.id),
}) },
} ])
} }
}) })
} }
@ -69,17 +68,16 @@ export const ContactsPage: React.FC = () => {
const subscribeContacts: () => void = async () => { const subscribeContacts: () => void = async () => {
relayPool?.unsubscribeAll() relayPool?.unsubscribeAll()
if (publicKey) { if (publicKey) {
if (selectedTab === 0) { relayPool?.subscribe('contacts', [
relayPool?.subscribe('contacts-following', { {
kinds: [EventKind.petNames], kinds: [EventKind.petNames],
authors: [publicKey], authors: [publicKey],
}) },
} else if (selectedTab === 1) { {
relayPool?.subscribe('contacts-followers', {
kinds: [EventKind.petNames], kinds: [EventKind.petNames],
'#p': [publicKey], '#p': [publicKey],
}) },
} ])
} }
} }

View File

@ -73,16 +73,18 @@ export const ConversationPage: React.FC = () => {
const subscribeDirectMessages: () => void = async () => { const subscribeDirectMessages: () => void = async () => {
relayPool?.unsubscribeAll() relayPool?.unsubscribeAll()
if (publicKey && otherPubKey) { if (publicKey && otherPubKey) {
relayPool?.subscribe('conversation-other', { relayPool?.subscribe('conversation', [
kinds: [EventKind.directMessage], {
authors: [publicKey], kinds: [EventKind.directMessage],
'#p': [otherPubKey], authors: [publicKey],
}) '#p': [otherPubKey],
relayPool?.subscribe('conversation-user', { },
kinds: [EventKind.directMessage], {
authors: [otherPubKey], kinds: [EventKind.directMessage],
'#p': [publicKey], authors: [otherPubKey],
}) '#p': [publicKey],
},
])
} }
} }

View File

@ -55,10 +55,12 @@ export const DirectMessagesPage: React.FC = () => {
if (results && results.length > 0) { if (results && results.length > 0) {
settDirectMessages(results) settDirectMessages(results)
const otherUsers = results.map((message) => getOtherPubKey(message, publicKey)) const otherUsers = results.map((message) => getOtherPubKey(message, publicKey))
relayPool?.subscribe('directmessages-meta', { relayPool?.subscribe('directmessages-meta', [
kinds: [EventKind.meta], {
authors: otherUsers, kinds: [EventKind.meta],
}) authors: otherUsers,
},
])
getUsers(database, { includeIds: otherUsers }).then(settUsers) getUsers(database, { includeIds: otherUsers }).then(settUsers)
} }
}) })
@ -69,14 +71,16 @@ export const DirectMessagesPage: React.FC = () => {
const subscribeDirectMessages: () => void = async () => { const subscribeDirectMessages: () => void = async () => {
relayPool?.unsubscribeAll() relayPool?.unsubscribeAll()
if (publicKey) { if (publicKey) {
relayPool?.subscribe('directmessages-user', { relayPool?.subscribe('directmessages-user', [
kinds: [EventKind.directMessage], {
authors: [publicKey], kinds: [EventKind.directMessage],
}) authors: [publicKey],
relayPool?.subscribe('directmessages-others', { },
kinds: [EventKind.directMessage], {
'#p': [publicKey], kinds: [EventKind.directMessage],
}) '#p': [publicKey],
},
])
} }
} }

View File

@ -33,10 +33,12 @@ export const HomePage: React.FC = () => {
const calculateInitialNotes: () => Promise<void> = async () => { const calculateInitialNotes: () => Promise<void> = async () => {
if (database && publicKey) { if (database && publicKey) {
relayPool?.subscribe('homepage-contacts', { relayPool?.subscribe('homepage-contacts', [
kinds: [EventKind.petNames], {
authors: [publicKey], kinds: [EventKind.petNames],
}) authors: [publicKey],
},
])
const users = await getUsers(database, { contacts: true, includeIds: [publicKey] }) const users = await getUsers(database, { contacts: true, includeIds: [publicKey] })
subscribeNotes(users) subscribeNotes(users)
setAuthors(users) setAuthors(users)
@ -60,7 +62,7 @@ export const HomePage: React.FC = () => {
message.limit = pageSize + initialPageSize message.limit = pageSize + initialPageSize
} }
relayPool?.subscribe('homepage-main', message) relayPool?.subscribe('homepage-main', [message])
} }
const loadNotes: () => void = () => { const loadNotes: () => void = () => {
@ -68,14 +70,16 @@ export const HomePage: React.FC = () => {
getMainNotes(database, publicKey, pageSize).then((notes) => { getMainNotes(database, publicKey, pageSize).then((notes) => {
setNotes(notes) setNotes(notes)
setRefreshing(false) setRefreshing(false)
relayPool?.subscribe('homepage-contacts-meta', { relayPool?.subscribe('homepage-contacts-meta', [
kinds: [EventKind.meta], {
authors: notes.map((note) => note.pubkey), kinds: [EventKind.meta],
}) authors: notes.map((note) => note.pubkey),
relayPool?.subscribe('homepage-contacts-reactions', { },
kinds: [EventKind.reaction], {
'#e': notes.map((note) => note.id ?? ''), kinds: [EventKind.reaction],
}) '#e': notes.map((note) => note.id ?? ''),
},
])
}) })
} }
} }

View File

@ -19,10 +19,12 @@ export const Loader: React.FC = () => {
useEffect(() => { useEffect(() => {
if (!loadingRelayPool && !loadingDb && publicKey) { if (!loadingRelayPool && !loadingDb && publicKey) {
relayPool?.subscribe('loading-meta', { relayPool?.subscribe('loading-meta', [
kinds: [EventKind.petNames, EventKind.meta], {
authors: [publicKey], kinds: [EventKind.petNames, EventKind.meta],
}) authors: [publicKey],
},
])
} }
}, [loadingRelayPool, publicKey, loadingDb]) }, [loadingRelayPool, publicKey, loadingDb])
@ -37,11 +39,13 @@ export const Loader: React.FC = () => {
setContactsCount(results.length) setContactsCount(results.length)
if (publicKey && results && results.length > 0) { if (publicKey && results && results.length > 0) {
const authors = [...results.map((user: User) => user.id), publicKey] const authors = [...results.map((user: User) => user.id), publicKey]
relayPool?.subscribe('loading-notes', { relayPool?.subscribe('loading-notes', [
kinds: [EventKind.meta, EventKind.textNote], {
authors, kinds: [EventKind.meta, EventKind.textNote],
since: moment().unix() - 86400 * 2, authors,
}) since: moment().unix() - 86400,
},
])
} }
}) })
} }

View File

@ -26,35 +26,35 @@ export const MentionsPage: React.FC = () => {
const subscribeNotes: () => void = async () => { const subscribeNotes: () => void = async () => {
if (!database || !publicKey) return if (!database || !publicKey) return
relayPool?.subscribe('mentions-user-user', { relayPool?.subscribe('mentions-user', [
kinds: [EventKind.textNote], {
'#p': [publicKey], kinds: [EventKind.textNote],
limit: pageSize, '#p': [publicKey],
}) limit: pageSize,
relayPool?.subscribe('mentions-user-answers', { },
kinds: [EventKind.textNote], {
'#e': [publicKey], kinds: [EventKind.textNote],
limit: pageSize, '#e': [publicKey],
}) limit: pageSize,
},
])
} }
const loadNotes: () => void = () => { const loadNotes: () => void = () => {
if (database && publicKey) { if (database && publicKey) {
getMentionNotes(database, publicKey, pageSize).then((notes) => { getMentionNotes(database, publicKey, pageSize).then((notes) => {
setNotes(notes) setNotes(notes)
relayPool?.subscribe('mentions-notes-answers', { const missingDataNotes = notes.map((note) => note.pubkey)
kinds: [EventKind.reaction], relayPool?.subscribe('mentions-answers', [
'#e': notes.map((note) => note.id ?? ''), {
}) kinds: [EventKind.reaction],
const missingDataNotes = notes '#e': notes.map((note) => note.id ?? ''),
.filter((note) => !note.picture || note.picture === '') },
.map((note) => note.pubkey) {
if (missingDataNotes.length > 0) {
relayPool?.subscribe('mentions-meta', {
kinds: [EventKind.meta], kinds: [EventKind.meta],
authors: missingDataNotes, authors: missingDataNotes,
}) },
} ])
}) })
} }
} }

View File

@ -67,7 +67,7 @@ export const NotePage: React.FC = () => {
kinds: [EventKind.meta], kinds: [EventKind.meta],
authors: [...rootReplies.map((note) => note.pubkey), event.pubkey], authors: [...rootReplies.map((note) => note.pubkey), event.pubkey],
} }
relayPool?.subscribe('meta-notepage', message) relayPool?.subscribe('meta-notepage', [message])
} else { } else {
setReplies([]) setReplies([])
} }
@ -81,14 +81,16 @@ export const NotePage: React.FC = () => {
const subscribeNotes: (past?: boolean) => Promise<void> = async (past) => { const subscribeNotes: (past?: boolean) => Promise<void> = async (past) => {
if (database && eventId) { if (database && eventId) {
relayPool?.subscribe('main-notepage', { relayPool?.subscribe('notepage', [
kinds: [EventKind.textNote], {
ids: [eventId], kinds: [EventKind.textNote],
}) ids: [eventId],
relayPool?.subscribe('answers-notepage', { },
kinds: [EventKind.reaction, EventKind.textNote], {
'#e': [eventId], kinds: [EventKind.reaction, EventKind.textNote],
}) '#e': [eventId],
},
])
} }
} }

View File

@ -85,10 +85,12 @@ export const ProfilePage: React.FC = () => {
getNotes(database, { filters: { pubkey: userId }, limit: pageSize }).then((results) => { getNotes(database, { filters: { pubkey: userId }, limit: pageSize }).then((results) => {
setNotes(results) setNotes(results)
setRefreshing(false) setRefreshing(false)
relayPool?.subscribe('answers-profile', { relayPool?.subscribe('answers-profile', [
kinds: [EventKind.reaction], {
'#e': results.map((note) => note.id ?? ''), kinds: [EventKind.reaction],
}) '#e': results.map((note) => note.id ?? ''),
},
])
}) })
} }
} }
@ -101,14 +103,16 @@ export const ProfilePage: React.FC = () => {
authors: [userId], authors: [userId],
limit: pageSize, limit: pageSize,
} }
relayPool?.subscribe('main-profile', message) relayPool?.subscribe('main-profile', [message])
} }
const subscribeProfile: () => Promise<void> = async () => { const subscribeProfile: () => Promise<void> = async () => {
relayPool?.subscribe('user-profile', { relayPool?.subscribe('user-profile', [
kinds: [EventKind.meta, EventKind.petNames], {
authors: [userId], kinds: [EventKind.meta, EventKind.petNames],
}) authors: [userId],
},
])
} }
const onRefresh = useCallback(() => { const onRefresh = useCallback(() => {

View File

@ -72,7 +72,7 @@ class RelayPool {
} }
} }
public readonly subscribe: (subId: string, filters?: RelayFilters) => void = async ( public readonly subscribe: (subId: string, filters?: RelayFilters[]) => void = async (
subId, subId,
filters, filters,
) => { ) => {
@ -83,7 +83,7 @@ class RelayPool {
if (this.subscriptions[subId]?.includes(uuid)) { if (this.subscriptions[subId]?.includes(uuid)) {
console.log('Subscription already done!', filters) console.log('Subscription already done!', filters)
} else { } else {
this.send(['REQ', subId, filters]) this.send([...['REQ', subId], ...(filters ?? [])])
const newSubscriptions = [...(this.subscriptions[subId] ?? []), uuid] const newSubscriptions = [...(this.subscriptions[subId] ?? []), uuid]
this.subscriptions[subId] = newSubscriptions this.subscriptions[subId] = newSubscriptions
} }