Rename some client functions

This commit is contained in:
Alex Gleason 2023-04-29 15:54:21 -05:00
parent 6341987088
commit 5118aaf46c
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
4 changed files with 17 additions and 17 deletions

View File

@ -1,12 +1,12 @@
import { type AppController } from '@/app.ts';
import { fetchUser } from '../client.ts';
import { getAuthor } from '../client.ts';
import { toAccount } from '../transmute.ts';
const credentialsController: AppController = async (c) => {
const pubkey = c.get('pubkey')!;
const event = await fetchUser(pubkey);
const event = await getAuthor(pubkey);
if (event) {
return c.json(toAccount(event));
}

View File

@ -1,7 +1,7 @@
import { type AppController } from '@/app.ts';
import { z } from '@/deps.ts';
import { fetchFeed, fetchFollows } from '../client.ts';
import { getFeed, getFollows } from '../client.ts';
import { toStatus } from '../transmute.ts';
import { LOCAL_DOMAIN } from '../config.ts';
@ -12,12 +12,12 @@ const homeController: AppController = async (c) => {
const pubkey = c.get('pubkey')!;
const follows = await fetchFollows(pubkey);
const follows = await getFollows(pubkey);
if (!follows) {
return c.json([]);
}
const events = await fetchFeed(follows, { since, until });
const events = await getFeed(follows, { since, until });
const statuses = (await Promise.all(events.map(toStatus))).filter(Boolean);
const next = `${LOCAL_DOMAIN}/api/v1/timelines/home?until=${events[events.length - 1].created_at}`;

View File

@ -7,21 +7,21 @@ import { eventDateComparator, nostrNow } from './utils.ts';
const pool = new RelayPool(poolRelays);
/** Fetch a Nostr event by its ID. */
const fetchEvent = async (id: string): Promise<SignedEvent | null> => {
/** Get a Nostr event by its ID. */
const getEvent = 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<SignedEvent<0> | null> => {
/** Get a Nostr `set_medatadata` event for a user's pubkey. */
const getAuthor = async (pubkey: string): Promise<SignedEvent<0> | null> => {
const author = new Author(pool, poolRelays, pubkey);
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<SignedEvent<3> | null> => {
/** Get users the given pubkey follows. */
const getFollows = (pubkey: string): Promise<SignedEvent<3> | null> => {
return new Promise((resolve) => {
pool.subscribe(
[{ authors: [pubkey], kinds: [3] }],
@ -41,8 +41,8 @@ interface PaginationParams {
limit?: number;
}
/** Fetch events from people the user follows. */
function fetchFeed(event3: Event<3>, params: PaginationParams = {}): Promise<SignedEvent<1>[]> {
/** Get events from people the user follows. */
function getFeed(event3: Event<3>, params: PaginationParams = {}): Promise<SignedEvent<1>[]> {
const limit = Math.max(params.limit ?? 20, 40);
const authors = event3.tags.filter((tag) => tag[0] === 'p').map((tag) => tag[1]);
const results: SignedEvent<1>[] = [];
@ -74,4 +74,4 @@ function fetchFeed(event3: Event<3>, params: PaginationParams = {}): Promise<Sig
});
}
export { fetchEvent, fetchFeed, fetchFollows, fetchUser, pool };
export { getAuthor, getEvent, getFeed, getFollows, pool };

View File

@ -3,7 +3,7 @@ import { type Event } from '@/event.ts';
import { type MetaContent, parseContent } from '@/schema.ts';
import { LOCAL_DOMAIN } from './config.ts';
import { fetchUser } from './client.ts';
import { getAuthor } from './client.ts';
const DEFAULT_AVATAR = 'https://gleasonator.com/images/avi.png';
@ -38,7 +38,7 @@ function toAccount(event: Event<0>) {
}
async function toMention(tag: string[]) {
const profile = await fetchUser(tag[1]);
const profile = await getAuthor(tag[1]);
const account = profile ? toAccount(profile) : undefined;
return {
@ -50,7 +50,7 @@ async function toMention(tag: string[]) {
}
async function toStatus(event: Event<1>) {
const profile = await fetchUser(event.pubkey);
const profile = await getAuthor(event.pubkey);
const account = profile ? toAccount(profile) : undefined;
if (!account) return;