mirror of
https://github.com/aljazceru/ditto.git
synced 2026-01-06 07:04:24 +01:00
Make Home feed kind of work
This commit is contained in:
@@ -2,30 +2,30 @@ import { Author, RelayPool } from '@/deps.ts';
|
||||
|
||||
import { poolRelays } from './config.ts';
|
||||
|
||||
import type { Event } from './event.ts';
|
||||
import type { Event, SignedEvent } from './event.ts';
|
||||
|
||||
const pool = new RelayPool(poolRelays);
|
||||
|
||||
/** Fetch a Nostr event by its ID. */
|
||||
const fetchEvent = async (id: string): Promise<Event | null> => {
|
||||
const event = await (pool.getEventById(id, poolRelays, 0) as Promise<Event>);
|
||||
const fetchEvent = async (id: string): Promise<SignedEvent | null> => {
|
||||
const event = await (pool.getEventById(id, poolRelays, 0) as Promise<SignedEvent>);
|
||||
return event?.id === id ? event : null;
|
||||
};
|
||||
|
||||
/** Fetch a Nostr `set_medatadata` event for a user's pubkey. */
|
||||
const fetchUser = async (pubkey: string): Promise<Event<0> | null> => {
|
||||
const fetchUser = async (pubkey: string): Promise<SignedEvent<0> | null> => {
|
||||
const author = new Author(pool, poolRelays, pubkey);
|
||||
const event: Event<0> | null = await new Promise((resolve) => author.metaData(resolve, 0));
|
||||
const event: SignedEvent<0> | null = await new Promise((resolve) => author.metaData(resolve, 0));
|
||||
return event?.pubkey === pubkey ? event : null;
|
||||
};
|
||||
|
||||
/** Fetch users the given pubkey follows. */
|
||||
const fetchFollows = (pubkey: string): Promise<Event<3> | null> => {
|
||||
const fetchFollows = (pubkey: string): Promise<SignedEvent<3> | null> => {
|
||||
return new Promise((resolve) => {
|
||||
pool.subscribe(
|
||||
[{ authors: [pubkey], kinds: [3] }],
|
||||
poolRelays,
|
||||
(event: Event<3> | null) => {
|
||||
(event: SignedEvent<3> | null) => {
|
||||
resolve(event?.pubkey === pubkey ? event : null);
|
||||
},
|
||||
undefined,
|
||||
@@ -34,4 +34,25 @@ const fetchFollows = (pubkey: string): Promise<Event<3> | null> => {
|
||||
});
|
||||
};
|
||||
|
||||
export { fetchEvent, fetchFollows, fetchUser, pool };
|
||||
/** Fetch 20 events from people the user follows. */
|
||||
function fetchFeed(event3: Event<3>): Promise<SignedEvent<1>[]> {
|
||||
const authors = event3.tags.filter((tag) => tag[0] === 'p').map((tag) => tag[1]);
|
||||
const results: SignedEvent<1>[] = [];
|
||||
|
||||
return new Promise((resolve) => {
|
||||
pool.subscribe(
|
||||
[{ authors, kinds: [1], limit: 20 }],
|
||||
poolRelays,
|
||||
(event: SignedEvent<1> | null) => {
|
||||
if (event) {
|
||||
results.push(event);
|
||||
}
|
||||
},
|
||||
void 0,
|
||||
() => resolve(results),
|
||||
{ unsubscribeOnEose: true },
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
export { fetchEvent, fetchFeed, fetchFollows, fetchUser, pool };
|
||||
|
||||
Reference in New Issue
Block a user