Update code to use new DittoEvent and DittoFilter. Event -> NostrEvent

This commit is contained in:
Alex Gleason
2024-01-23 12:07:22 -06:00
parent f58c2098f0
commit aaf01462c1
41 changed files with 256 additions and 466 deletions

View File

@@ -1,13 +1,12 @@
import { type AppController } from '@/app.ts';
import { Conf } from '@/config.ts';
import { insertUser } from '@/db/users.ts';
import { findReplyTag, nip19, z } from '@/deps.ts';
import { type DittoFilter } from '@/filter.ts';
import { nip19, z } from '@/deps.ts';
import { getAuthor, getFollowedPubkeys } from '@/queries.ts';
import { booleanParamSchema, fileSchema } from '@/schema.ts';
import { jsonMetaContentSchema } from '@/schemas/nostr.ts';
import { eventsDB } from '@/storages.ts';
import { addTag, deleteTag, getTagSet } from '@/tags.ts';
import { addTag, deleteTag, findReplyTag, getTagSet } from '@/tags.ts';
import { uploadFile } from '@/upload.ts';
import { lookupAccount, nostrNow } from '@/utils.ts';
import { createEvent, paginated, paginationSchema, parseBody, updateListEvent } from '@/utils/api.ts';
@@ -15,6 +14,7 @@ import { renderAccounts, renderEventAccounts, renderStatuses } from '@/views.ts'
import { accountFromPubkey, renderAccount } from '@/views/mastodon/accounts.ts';
import { renderRelationship } from '@/views/mastodon/relationships.ts';
import { renderStatus } from '@/views/mastodon/statuses.ts';
import { DittoFilter } from '@/interfaces/DittoFilter.ts';
const usernameSchema = z
.string().min(1).max(30)
@@ -143,7 +143,7 @@ const accountStatusesController: AppController = async (c) => {
}
}
const filter: DittoFilter<1> = {
const filter: DittoFilter = {
authors: [pubkey],
kinds: [1],
relations: ['author', 'event_stats', 'author_stats'],
@@ -159,7 +159,7 @@ const accountStatusesController: AppController = async (c) => {
let events = await eventsDB.filter([filter]);
if (exclude_replies) {
events = events.filter((event) => !findReplyTag(event));
events = events.filter((event) => !findReplyTag(event.tags));
}
const statuses = await Promise.all(events.map((event) => renderStatus(event, c.get('pubkey'))));

View File

@@ -1,6 +1,6 @@
import { AppController } from '@/app.ts';
import { type Event, nip19, z } from '@/deps.ts';
import { type DittoFilter } from '@/filter.ts';
import { nip19, type NostrEvent, z } from '@/deps.ts';
import { type DittoFilter } from '@/interfaces/DittoFilter.ts';
import { booleanParamSchema } from '@/schema.ts';
import { nostrIdSchema } from '@/schemas/nostr.ts';
import { searchStore } from '@/storages.ts';
@@ -46,12 +46,12 @@ const searchController: AppController = async (c) => {
const [accounts, statuses] = await Promise.all([
Promise.all(
results
.filter((event): event is Event<0> => event.kind === 0)
.filter((event): event is NostrEvent => event.kind === 0)
.map((event) => renderAccount(event)),
),
Promise.all(
results
.filter((event): event is Event<1> => event.kind === 1)
.filter((event): event is NostrEvent => event.kind === 1)
.map((event) => renderStatus(event, c.get('pubkey'))),
),
]);
@@ -64,7 +64,7 @@ const searchController: AppController = async (c) => {
};
/** Get events for the search params. */
function searchEvents({ q, type, limit, account_id }: SearchQuery, signal: AbortSignal): Promise<Event[]> {
function searchEvents({ q, type, limit, account_id }: SearchQuery, signal: AbortSignal): Promise<NostrEvent[]> {
if (type === 'hashtags') return Promise.resolve([]);
const filter: DittoFilter = {
@@ -94,7 +94,7 @@ function typeToKinds(type: SearchQuery['type']): number[] {
}
/** Resolve a searched value into an event, if applicable. */
async function lookupEvent(query: SearchQuery, signal: AbortSignal): Promise<Event | undefined> {
async function lookupEvent(query: SearchQuery, signal: AbortSignal): Promise<NostrEvent | undefined> {
const filters = await getLookupFilters(query, signal);
const [event] = await searchStore.filter(filters, { limit: 1, signal });
return event;

View File

@@ -1,7 +1,7 @@
import { type AppController } from '@/app.ts';
import { Conf } from '@/config.ts';
import { getUnattachedMediaByIds } from '@/db/unattached-media.ts';
import { type Event, ISO6391, z } from '@/deps.ts';
import { ISO6391, type NostrEvent, z } from '@/deps.ts';
import { getAncestors, getAuthor, getDescendants, getEvent } from '@/queries.ts';
import { jsonMetaContentSchema } from '@/schemas/nostr.ts';
import { addTag, deleteTag } from '@/tags.ts';
@@ -100,7 +100,7 @@ const contextController: AppController = async (c) => {
const id = c.req.param('id');
const event = await getEvent(id, { kind: 1, relations: ['author', 'event_stats', 'author_stats'] });
async function renderStatuses(events: Event<1>[]) {
async function renderStatuses(events: NostrEvent[]) {
const statuses = await Promise.all(events.map((event) => renderStatus(event, c.get('pubkey'))));
return statuses.filter(Boolean);
}

View File

@@ -1,6 +1,6 @@
import { type AppController } from '@/app.ts';
import { Debug, z } from '@/deps.ts';
import { type DittoFilter } from '@/filter.ts';
import { DittoFilter } from '@/interfaces/DittoFilter.ts';
import { getAuthor, getFeedPubkeys } from '@/queries.ts';
import { Sub } from '@/subs.ts';
import { bech32ToPubkey } from '@/utils.ts';
@@ -86,7 +86,7 @@ async function topicToFilter(
topic: Stream,
pubkey: string,
query: Record<string, string>,
): Promise<DittoFilter<1> | undefined> {
): Promise<DittoFilter | undefined> {
switch (topic) {
case 'public':
return { kinds: [1] };

View File

@@ -1,13 +1,12 @@
import { eventsDB } from '@/storages.ts';
import { type AppContext, type AppController } from '@/app.ts';
import { z } from '@/deps.ts';
import { type DittoFilter } from '@/filter.ts';
import { type DittoFilter } from '@/interfaces/DittoFilter.ts';
import { getFeedPubkeys } from '@/queries.ts';
import { booleanParamSchema } from '@/schema.ts';
import { eventsDB } from '@/storages.ts';
import { paginated, paginationSchema } from '@/utils/api.ts';
import { renderStatus } from '@/views/mastodon/statuses.ts';
import type { AppContext, AppController } from '@/app.ts';
const homeTimelineController: AppController = async (c) => {
const params = paginationSchema.parse(c.req.query());
const pubkey = c.get('pubkey')!;
@@ -32,7 +31,7 @@ const hashtagTimelineController: AppController = (c) => {
};
/** Render statuses for timelines. */
async function renderStatuses(c: AppContext, filters: DittoFilter<1>[], signal = AbortSignal.timeout(1000)) {
async function renderStatuses(c: AppContext, filters: DittoFilter[], signal = AbortSignal.timeout(1000)) {
const events = await eventsDB.filter(
filters.map((filter) => ({ ...filter, relations: ['author', 'event_stats', 'author_stats'] })),
{ signal },