timelines: add DRY renderStatuses function
This commit is contained in:
parent
4216a7931a
commit
4d211d637e
|
@ -1,51 +1,40 @@
|
||||||
import { z } from '@/deps.ts';
|
import { z } from '@/deps.ts';
|
||||||
|
import { type DittoFilter } from '@/filter.ts';
|
||||||
import * as mixer from '@/mixer.ts';
|
import * as mixer from '@/mixer.ts';
|
||||||
import { getFeed, getPublicFeed } from '@/queries.ts';
|
import { getFeedPubkeys } from '@/queries.ts';
|
||||||
import { booleanParamSchema } from '@/schema.ts';
|
import { booleanParamSchema } from '@/schema.ts';
|
||||||
import { toStatus } from '@/transformers/nostr-to-mastoapi.ts';
|
import { toStatus } from '@/transformers/nostr-to-mastoapi.ts';
|
||||||
import { paginated, paginationSchema } from '@/utils/web.ts';
|
import { paginated, paginationSchema } from '@/utils/web.ts';
|
||||||
import { Time } from '@/utils.ts';
|
import { Time } from '@/utils.ts';
|
||||||
|
|
||||||
import type { AppController } from '@/app.ts';
|
import type { AppContext, AppController } from '@/app.ts';
|
||||||
|
|
||||||
const homeTimelineController: AppController = async (c) => {
|
const homeTimelineController: AppController = async (c) => {
|
||||||
const params = paginationSchema.parse(c.req.query());
|
const params = paginationSchema.parse(c.req.query());
|
||||||
const pubkey = c.get('pubkey')!;
|
const pubkey = c.get('pubkey')!;
|
||||||
|
const authors = await getFeedPubkeys(pubkey);
|
||||||
const events = await getFeed(pubkey, params);
|
return renderStatuses(c, [{ authors, kinds: [1], ...params }]);
|
||||||
if (!events.length) {
|
|
||||||
return c.json([]);
|
|
||||||
}
|
|
||||||
|
|
||||||
const statuses = await Promise.all(events.map(toStatus));
|
|
||||||
return paginated(c, events, statuses);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const publicQuerySchema = z.object({
|
const publicQuerySchema = z.object({
|
||||||
local: booleanParamSchema.catch(false),
|
local: booleanParamSchema.catch(false),
|
||||||
});
|
});
|
||||||
|
|
||||||
const publicTimelineController: AppController = async (c) => {
|
const publicTimelineController: AppController = (c) => {
|
||||||
const params = paginationSchema.parse(c.req.query());
|
const params = paginationSchema.parse(c.req.query());
|
||||||
const { local } = publicQuerySchema.parse(c.req.query());
|
const { local } = publicQuerySchema.parse(c.req.query());
|
||||||
|
return renderStatuses(c, [{ kinds: [1], local, ...params }]);
|
||||||
const events = await getPublicFeed(params, local);
|
|
||||||
if (!events.length) {
|
|
||||||
return c.json([]);
|
|
||||||
}
|
|
||||||
|
|
||||||
const statuses = await Promise.all(events.map(toStatus));
|
|
||||||
return paginated(c, events, statuses);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const hashtagTimelineController: AppController = async (c) => {
|
const hashtagTimelineController: AppController = (c) => {
|
||||||
const hashtag = c.req.param('hashtag')!;
|
const hashtag = c.req.param('hashtag')!;
|
||||||
const params = paginationSchema.parse(c.req.query());
|
const params = paginationSchema.parse(c.req.query());
|
||||||
|
return renderStatuses(c, [{ kinds: [1], '#t': [hashtag], ...params }]);
|
||||||
|
};
|
||||||
|
|
||||||
const events = await mixer.getFilters(
|
/** Render statuses for timelines. */
|
||||||
[{ kinds: [1], '#t': [hashtag], ...params }],
|
async function renderStatuses(c: AppContext, filters: DittoFilter<1>[]) {
|
||||||
{ timeout: Time.seconds(3) },
|
const events = await mixer.getFilters(filters, { timeout: Time.seconds(3) });
|
||||||
);
|
|
||||||
|
|
||||||
if (!events.length) {
|
if (!events.length) {
|
||||||
return c.json([]);
|
return c.json([]);
|
||||||
|
@ -53,6 +42,6 @@ const hashtagTimelineController: AppController = async (c) => {
|
||||||
|
|
||||||
const statuses = await Promise.all(events.map(toStatus));
|
const statuses = await Promise.all(events.map(toStatus));
|
||||||
return paginated(c, events, statuses);
|
return paginated(c, events, statuses);
|
||||||
};
|
}
|
||||||
|
|
||||||
export { hashtagTimelineController, homeTimelineController, publicTimelineController };
|
export { hashtagTimelineController, homeTimelineController, publicTimelineController };
|
||||||
|
|
|
@ -53,24 +53,6 @@ async function getFeedPubkeys(pubkey: string): Promise<string[]> {
|
||||||
return [...authors, pubkey];
|
return [...authors, pubkey];
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Get events from people the user follows. */
|
|
||||||
async function getFeed(pubkey: string, params: PaginationParams): Promise<Event<1>[]> {
|
|
||||||
const authors = await getFeedPubkeys(pubkey);
|
|
||||||
|
|
||||||
const filter: Filter<1> = {
|
|
||||||
authors,
|
|
||||||
kinds: [1],
|
|
||||||
...params,
|
|
||||||
};
|
|
||||||
|
|
||||||
return mixer.getFilters([filter], { timeout: 5000 });
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Get a feed of all known text notes. */
|
|
||||||
function getPublicFeed(params: PaginationParams, local: boolean): Promise<Event<1>[]> {
|
|
||||||
return mixer.getFilters([{ kinds: [1], local, ...params }], { timeout: 5000 });
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getAncestors(event: Event<1>, result = [] as Event<1>[]): Promise<Event<1>[]> {
|
async function getAncestors(event: Event<1>, result = [] as Event<1>[]): Promise<Event<1>[]> {
|
||||||
if (result.length < 100) {
|
if (result.length < 100) {
|
||||||
const replyTag = findReplyTag(event);
|
const replyTag = findReplyTag(event);
|
||||||
|
@ -106,15 +88,4 @@ async function syncUser(pubkey: string): Promise<void> {
|
||||||
], { timeout: 5000 });
|
], { timeout: 5000 });
|
||||||
}
|
}
|
||||||
|
|
||||||
export {
|
export { getAncestors, getAuthor, getDescendants, getEvent, getFeedPubkeys, getFollows, isLocallyFollowed, syncUser };
|
||||||
getAncestors,
|
|
||||||
getAuthor,
|
|
||||||
getDescendants,
|
|
||||||
getEvent,
|
|
||||||
getFeed,
|
|
||||||
getFeedPubkeys,
|
|
||||||
getFollows,
|
|
||||||
getPublicFeed,
|
|
||||||
isLocallyFollowed,
|
|
||||||
syncUser,
|
|
||||||
};
|
|
||||||
|
|
Loading…
Reference in New Issue