mirror of
https://github.com/KoalaSat/nostros.git
synced 2024-09-29 14:40:43 +00:00
Refactor subscriptions (#106)
This commit is contained in:
commit
f5f4cb13f7
@ -82,7 +82,7 @@ export const NoteCard: React.FC<NoteCardProps> = ({
|
||||
kinds: [EventKind.meta],
|
||||
authors: [...rootReplies.map((reply) => reply.pubkey), note.pubkey],
|
||||
}
|
||||
relayPool?.subscribe('notecard', message)
|
||||
relayPool?.subscribe('notecard', [message])
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -54,13 +54,12 @@ export const ContactsPage: React.FC = () => {
|
||||
getUsers(database, filters).then((results) => {
|
||||
if (results && results.length > 0) {
|
||||
setUsers(results)
|
||||
const missingDataUsers = results.filter((user) => !user.picture).map((user) => user.id)
|
||||
if (missingDataUsers.length > 0) {
|
||||
relayPool?.subscribe('contacts-meta', {
|
||||
relayPool?.subscribe('contacts-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 () => {
|
||||
relayPool?.unsubscribeAll()
|
||||
if (publicKey) {
|
||||
if (selectedTab === 0) {
|
||||
relayPool?.subscribe('contacts-following', {
|
||||
relayPool?.subscribe('contacts', [
|
||||
{
|
||||
kinds: [EventKind.petNames],
|
||||
authors: [publicKey],
|
||||
})
|
||||
} else if (selectedTab === 1) {
|
||||
relayPool?.subscribe('contacts-followers', {
|
||||
},
|
||||
{
|
||||
kinds: [EventKind.petNames],
|
||||
'#p': [publicKey],
|
||||
})
|
||||
}
|
||||
},
|
||||
])
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -73,16 +73,18 @@ export const ConversationPage: React.FC = () => {
|
||||
const subscribeDirectMessages: () => void = async () => {
|
||||
relayPool?.unsubscribeAll()
|
||||
if (publicKey && otherPubKey) {
|
||||
relayPool?.subscribe('conversation-other', {
|
||||
kinds: [EventKind.directMessage],
|
||||
authors: [publicKey],
|
||||
'#p': [otherPubKey],
|
||||
})
|
||||
relayPool?.subscribe('conversation-user', {
|
||||
kinds: [EventKind.directMessage],
|
||||
authors: [otherPubKey],
|
||||
'#p': [publicKey],
|
||||
})
|
||||
relayPool?.subscribe('conversation', [
|
||||
{
|
||||
kinds: [EventKind.directMessage],
|
||||
authors: [publicKey],
|
||||
'#p': [otherPubKey],
|
||||
},
|
||||
{
|
||||
kinds: [EventKind.directMessage],
|
||||
authors: [otherPubKey],
|
||||
'#p': [publicKey],
|
||||
},
|
||||
])
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -55,10 +55,12 @@ export const DirectMessagesPage: React.FC = () => {
|
||||
if (results && results.length > 0) {
|
||||
settDirectMessages(results)
|
||||
const otherUsers = results.map((message) => getOtherPubKey(message, publicKey))
|
||||
relayPool?.subscribe('directmessages-meta', {
|
||||
kinds: [EventKind.meta],
|
||||
authors: otherUsers,
|
||||
})
|
||||
relayPool?.subscribe('directmessages-meta', [
|
||||
{
|
||||
kinds: [EventKind.meta],
|
||||
authors: otherUsers,
|
||||
},
|
||||
])
|
||||
getUsers(database, { includeIds: otherUsers }).then(settUsers)
|
||||
}
|
||||
})
|
||||
@ -69,14 +71,16 @@ export const DirectMessagesPage: React.FC = () => {
|
||||
const subscribeDirectMessages: () => void = async () => {
|
||||
relayPool?.unsubscribeAll()
|
||||
if (publicKey) {
|
||||
relayPool?.subscribe('directmessages-user', {
|
||||
kinds: [EventKind.directMessage],
|
||||
authors: [publicKey],
|
||||
})
|
||||
relayPool?.subscribe('directmessages-others', {
|
||||
kinds: [EventKind.directMessage],
|
||||
'#p': [publicKey],
|
||||
})
|
||||
relayPool?.subscribe('directmessages-user', [
|
||||
{
|
||||
kinds: [EventKind.directMessage],
|
||||
authors: [publicKey],
|
||||
},
|
||||
{
|
||||
kinds: [EventKind.directMessage],
|
||||
'#p': [publicKey],
|
||||
},
|
||||
])
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -33,10 +33,12 @@ export const HomePage: React.FC = () => {
|
||||
|
||||
const calculateInitialNotes: () => Promise<void> = async () => {
|
||||
if (database && publicKey) {
|
||||
relayPool?.subscribe('homepage-contacts', {
|
||||
kinds: [EventKind.petNames],
|
||||
authors: [publicKey],
|
||||
})
|
||||
relayPool?.subscribe('homepage-contacts', [
|
||||
{
|
||||
kinds: [EventKind.petNames],
|
||||
authors: [publicKey],
|
||||
},
|
||||
])
|
||||
const users = await getUsers(database, { contacts: true, includeIds: [publicKey] })
|
||||
subscribeNotes(users)
|
||||
setAuthors(users)
|
||||
@ -60,7 +62,7 @@ export const HomePage: React.FC = () => {
|
||||
message.limit = pageSize + initialPageSize
|
||||
}
|
||||
|
||||
relayPool?.subscribe('homepage-main', message)
|
||||
relayPool?.subscribe('homepage-main', [message])
|
||||
}
|
||||
|
||||
const loadNotes: () => void = () => {
|
||||
@ -68,14 +70,16 @@ export const HomePage: React.FC = () => {
|
||||
getMainNotes(database, publicKey, pageSize).then((notes) => {
|
||||
setNotes(notes)
|
||||
setRefreshing(false)
|
||||
relayPool?.subscribe('homepage-contacts-meta', {
|
||||
kinds: [EventKind.meta],
|
||||
authors: notes.map((note) => note.pubkey),
|
||||
})
|
||||
relayPool?.subscribe('homepage-contacts-reactions', {
|
||||
kinds: [EventKind.reaction],
|
||||
'#e': notes.map((note) => note.id ?? ''),
|
||||
})
|
||||
relayPool?.subscribe('homepage-contacts-meta', [
|
||||
{
|
||||
kinds: [EventKind.meta],
|
||||
authors: notes.map((note) => note.pubkey),
|
||||
},
|
||||
{
|
||||
kinds: [EventKind.reaction],
|
||||
'#e': notes.map((note) => note.id ?? ''),
|
||||
},
|
||||
])
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -19,10 +19,12 @@ export const Loader: React.FC = () => {
|
||||
|
||||
useEffect(() => {
|
||||
if (!loadingRelayPool && !loadingDb && publicKey) {
|
||||
relayPool?.subscribe('loading-meta', {
|
||||
kinds: [EventKind.petNames, EventKind.meta],
|
||||
authors: [publicKey],
|
||||
})
|
||||
relayPool?.subscribe('loading-meta', [
|
||||
{
|
||||
kinds: [EventKind.petNames, EventKind.meta],
|
||||
authors: [publicKey],
|
||||
},
|
||||
])
|
||||
}
|
||||
}, [loadingRelayPool, publicKey, loadingDb])
|
||||
|
||||
@ -37,11 +39,13 @@ export const Loader: React.FC = () => {
|
||||
setContactsCount(results.length)
|
||||
if (publicKey && results && results.length > 0) {
|
||||
const authors = [...results.map((user: User) => user.id), publicKey]
|
||||
relayPool?.subscribe('loading-notes', {
|
||||
kinds: [EventKind.meta, EventKind.textNote],
|
||||
authors,
|
||||
since: moment().unix() - 86400 * 2,
|
||||
})
|
||||
relayPool?.subscribe('loading-notes', [
|
||||
{
|
||||
kinds: [EventKind.meta, EventKind.textNote],
|
||||
authors,
|
||||
since: moment().unix() - 86400,
|
||||
},
|
||||
])
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -26,35 +26,35 @@ export const MentionsPage: React.FC = () => {
|
||||
const subscribeNotes: () => void = async () => {
|
||||
if (!database || !publicKey) return
|
||||
|
||||
relayPool?.subscribe('mentions-user-user', {
|
||||
kinds: [EventKind.textNote],
|
||||
'#p': [publicKey],
|
||||
limit: pageSize,
|
||||
})
|
||||
relayPool?.subscribe('mentions-user-answers', {
|
||||
kinds: [EventKind.textNote],
|
||||
'#e': [publicKey],
|
||||
limit: pageSize,
|
||||
})
|
||||
relayPool?.subscribe('mentions-user', [
|
||||
{
|
||||
kinds: [EventKind.textNote],
|
||||
'#p': [publicKey],
|
||||
limit: pageSize,
|
||||
},
|
||||
{
|
||||
kinds: [EventKind.textNote],
|
||||
'#e': [publicKey],
|
||||
limit: pageSize,
|
||||
},
|
||||
])
|
||||
}
|
||||
|
||||
const loadNotes: () => void = () => {
|
||||
if (database && publicKey) {
|
||||
getMentionNotes(database, publicKey, pageSize).then((notes) => {
|
||||
setNotes(notes)
|
||||
relayPool?.subscribe('mentions-notes-answers', {
|
||||
kinds: [EventKind.reaction],
|
||||
'#e': notes.map((note) => note.id ?? ''),
|
||||
})
|
||||
const missingDataNotes = notes
|
||||
.filter((note) => !note.picture || note.picture === '')
|
||||
.map((note) => note.pubkey)
|
||||
if (missingDataNotes.length > 0) {
|
||||
relayPool?.subscribe('mentions-meta', {
|
||||
const missingDataNotes = notes.map((note) => note.pubkey)
|
||||
relayPool?.subscribe('mentions-answers', [
|
||||
{
|
||||
kinds: [EventKind.reaction],
|
||||
'#e': notes.map((note) => note.id ?? ''),
|
||||
},
|
||||
{
|
||||
kinds: [EventKind.meta],
|
||||
authors: missingDataNotes,
|
||||
})
|
||||
}
|
||||
},
|
||||
])
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -67,7 +67,7 @@ export const NotePage: React.FC = () => {
|
||||
kinds: [EventKind.meta],
|
||||
authors: [...rootReplies.map((note) => note.pubkey), event.pubkey],
|
||||
}
|
||||
relayPool?.subscribe('meta-notepage', message)
|
||||
relayPool?.subscribe('meta-notepage', [message])
|
||||
} else {
|
||||
setReplies([])
|
||||
}
|
||||
@ -81,14 +81,16 @@ export const NotePage: React.FC = () => {
|
||||
|
||||
const subscribeNotes: (past?: boolean) => Promise<void> = async (past) => {
|
||||
if (database && eventId) {
|
||||
relayPool?.subscribe('main-notepage', {
|
||||
kinds: [EventKind.textNote],
|
||||
ids: [eventId],
|
||||
})
|
||||
relayPool?.subscribe('answers-notepage', {
|
||||
kinds: [EventKind.reaction, EventKind.textNote],
|
||||
'#e': [eventId],
|
||||
})
|
||||
relayPool?.subscribe('notepage', [
|
||||
{
|
||||
kinds: [EventKind.textNote],
|
||||
ids: [eventId],
|
||||
},
|
||||
{
|
||||
kinds: [EventKind.reaction, EventKind.textNote],
|
||||
'#e': [eventId],
|
||||
},
|
||||
])
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -85,10 +85,12 @@ export const ProfilePage: React.FC = () => {
|
||||
getNotes(database, { filters: { pubkey: userId }, limit: pageSize }).then((results) => {
|
||||
setNotes(results)
|
||||
setRefreshing(false)
|
||||
relayPool?.subscribe('answers-profile', {
|
||||
kinds: [EventKind.reaction],
|
||||
'#e': results.map((note) => note.id ?? ''),
|
||||
})
|
||||
relayPool?.subscribe('answers-profile', [
|
||||
{
|
||||
kinds: [EventKind.reaction],
|
||||
'#e': results.map((note) => note.id ?? ''),
|
||||
},
|
||||
])
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -101,14 +103,16 @@ export const ProfilePage: React.FC = () => {
|
||||
authors: [userId],
|
||||
limit: pageSize,
|
||||
}
|
||||
relayPool?.subscribe('main-profile', message)
|
||||
relayPool?.subscribe('main-profile', [message])
|
||||
}
|
||||
|
||||
const subscribeProfile: () => Promise<void> = async () => {
|
||||
relayPool?.subscribe('user-profile', {
|
||||
kinds: [EventKind.meta, EventKind.petNames],
|
||||
authors: [userId],
|
||||
})
|
||||
relayPool?.subscribe('user-profile', [
|
||||
{
|
||||
kinds: [EventKind.meta, EventKind.petNames],
|
||||
authors: [userId],
|
||||
},
|
||||
])
|
||||
}
|
||||
|
||||
const onRefresh = useCallback(() => {
|
||||
|
@ -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,
|
||||
filters,
|
||||
) => {
|
||||
@ -83,7 +83,7 @@ class RelayPool {
|
||||
if (this.subscriptions[subId]?.includes(uuid)) {
|
||||
console.log('Subscription already done!', filters)
|
||||
} else {
|
||||
this.send(['REQ', subId, filters])
|
||||
this.send([...['REQ', subId], ...(filters ?? [])])
|
||||
const newSubscriptions = [...(this.subscriptions[subId] ?? []), uuid]
|
||||
this.subscriptions[subId] = newSubscriptions
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user