Use createEvent in more places

This commit is contained in:
Alex Gleason 2023-08-19 22:25:45 -05:00
parent a82ae40c43
commit bcfc3e2414
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
3 changed files with 9 additions and 38 deletions

View File

@ -1,13 +1,11 @@
import { type AppController } from '@/app.ts';
import { type Filter, findReplyTag, z } from '@/deps.ts';
import * as mixer from '@/mixer.ts';
import * as pipeline from '@/pipeline.ts';
import { getAuthor, getFollows, syncUser } from '@/queries.ts';
import { booleanParamSchema } from '@/schema.ts';
import { jsonMetaContentSchema } from '@/schemas/nostr.ts';
import { signEvent } from '@/sign.ts';
import { toAccount, toRelationship, toStatus } from '@/transformers/nostr-to-mastoapi.ts';
import { eventDateComparator, isFollowing, lookupAccount, nostrNow } from '@/utils.ts';
import { eventDateComparator, isFollowing, lookupAccount } from '@/utils.ts';
import { buildLinkHeader, paginationSchema, parseBody } from '@/utils/web.ts';
import { createEvent } from '@/utils/web.ts';
@ -147,21 +145,12 @@ const updateCredentialsController: AppController = async (c) => {
meta.name = result.data.display_name ?? meta.name;
meta.about = result.data.note ?? meta.about;
const event = await signEvent({
const event = await createEvent({
kind: 0,
content: JSON.stringify(meta),
tags: [],
created_at: nostrNow(),
}, c);
try {
await pipeline.handleEvent(event);
} catch (e) {
if (e instanceof pipeline.RelayError) {
return c.json({ error: e.message }, 422);
}
}
const account = await toAccount(event);
return c.json(account);
};

View File

@ -1,11 +1,8 @@
import { type AppController } from '@/app.ts';
import { ISO6391, Kind, z } from '@/deps.ts';
import * as pipeline from '@/pipeline.ts';
import { getAncestors, getDescendants, getEvent } from '@/queries.ts';
import { signEvent } from '@/sign.ts';
import { toStatus } from '@/transformers/nostr-to-mastoapi.ts';
import { nostrNow } from '@/utils.ts';
import { parseBody } from '@/utils/web.ts';
import { createEvent, parseBody } from '@/utils/web.ts';
const createStatusSchema = z.object({
in_reply_to_id: z.string().regex(/[0-9a-f]{64}/).nullish(),
@ -71,21 +68,12 @@ const createStatusController: AppController = async (c) => {
tags.push(['subject', data.spoiler_text]);
}
const event = await signEvent({
const event = await createEvent({
kind: Kind.Text,
content: data.status ?? '',
tags,
created_at: nostrNow(),
}, c);
try {
await pipeline.handleEvent(event);
} catch (e) {
if (e instanceof pipeline.RelayError) {
return c.json({ error: e.message }, 422);
}
}
return c.json(await toStatus(event));
} else {
return c.json({ error: 'Bad request', schema: result.error }, 400);
@ -115,24 +103,15 @@ const favouriteController: AppController = async (c) => {
const target = await getEvent(id, { kind: 1 });
if (target) {
const event = await signEvent({
await createEvent({
kind: Kind.Reaction,
content: '+',
tags: [
['e', target.id],
['p', target.pubkey],
],
created_at: nostrNow(),
}, c);
try {
await pipeline.handleEvent(event);
} catch (e) {
if (e instanceof pipeline.RelayError) {
return c.json({ error: e.message }, 422);
}
}
const status = await toStatus(target);
if (status) {

View File

@ -7,7 +7,10 @@ import { nostrNow } from '@/utils.ts';
import type { AppContext } from '@/app.ts';
/** Publish an event through the API, throwing a Hono exception on failure. */
async function createEvent<K extends number>(t: Omit<EventTemplate<K>, 'created_at'>, c: AppContext) {
async function createEvent<K extends number>(
t: Omit<EventTemplate<K>, 'created_at'>,
c: AppContext,
): Promise<Event<K>> {
const pubkey = c.get('pubkey');
if (!pubkey) {