Remove admin.ts, move to sign.ts, add createAdminEvent function
This commit is contained in:
parent
3c279175bc
commit
60cecafdb5
15
src/admin.ts
15
src/admin.ts
|
@ -1,15 +0,0 @@
|
|||
import { Conf } from '@/config.ts';
|
||||
import { type Event, type EventTemplate, finishEvent, nip19 } from '@/deps.ts';
|
||||
|
||||
// deno-lint-ignore require-await
|
||||
async function signAdminEvent<K extends number = number>(event: EventTemplate<K>): Promise<Event<K>> {
|
||||
if (!Conf.nsec) throw new Error('No secret key. Set one with DITTO_NSEC.');
|
||||
|
||||
const result = nip19.decode(Conf.nsec);
|
||||
|
||||
if (result.type !== 'nsec') throw new Error('Invalid DITTO_NSEC. It should start with "nsec1..."');
|
||||
|
||||
return finishEvent(event, result.data);
|
||||
}
|
||||
|
||||
export { signAdminEvent };
|
17
src/sign.ts
17
src/sign.ts
|
@ -1,5 +1,6 @@
|
|||
import { type AppContext } from '@/app.ts';
|
||||
import { type Event, type EventTemplate, getEventHash, getPublicKey, getSignature, HTTPException, z } from '@/deps.ts';
|
||||
import { Conf } from '@/config.ts';
|
||||
import { type Event, type EventTemplate, finishEvent, HTTPException, z } from '@/deps.ts';
|
||||
import { signedEventSchema } from '@/schemas/nostr.ts';
|
||||
import { ws } from '@/stream.ts';
|
||||
|
||||
|
@ -61,11 +62,13 @@ async function signEvent<K extends number = number>(event: EventTemplate<K>, c:
|
|||
});
|
||||
}
|
||||
|
||||
(event as Event<K>).pubkey = getPublicKey(seckey);
|
||||
(event as Event<K>).id = getEventHash(event as Event<K>);
|
||||
(event as Event<K>).sig = getSignature(event as Event<K>, seckey);
|
||||
|
||||
return event as Event<K>;
|
||||
return finishEvent(event, seckey);
|
||||
}
|
||||
|
||||
export { signEvent };
|
||||
/** Sign event as the Ditto server. */
|
||||
// deno-lint-ignore require-await
|
||||
async function signAdminEvent<K extends number = number>(event: EventTemplate<K>): Promise<Event<K>> {
|
||||
return finishEvent(event, Conf.seckey);
|
||||
}
|
||||
|
||||
export { signAdminEvent, signEvent };
|
||||
|
|
|
@ -1,16 +1,18 @@
|
|||
import { Conf } from '@/config.ts';
|
||||
import { type Context, type Event, EventTemplate, HTTPException, parseFormData, z } from '@/deps.ts';
|
||||
import * as pipeline from '@/pipeline.ts';
|
||||
import { signEvent } from '@/sign.ts';
|
||||
import { signAdminEvent, signEvent } from '@/sign.ts';
|
||||
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,
|
||||
): Promise<Event<K>> {
|
||||
/** EventTemplate with or without a timestamp. If no timestamp is given, it will be generated. */
|
||||
interface PendingEvent<K extends number = number> extends Omit<EventTemplate<K>, 'created_at'> {
|
||||
created_at?: number;
|
||||
}
|
||||
|
||||
/** Publish an event through the pipeline. */
|
||||
async function createEvent<K extends number>(t: PendingEvent<K>, c: AppContext): Promise<Event<K>> {
|
||||
const pubkey = c.get('pubkey');
|
||||
|
||||
if (!pubkey) {
|
||||
|
@ -22,6 +24,21 @@ async function createEvent<K extends number>(
|
|||
...t,
|
||||
}, c);
|
||||
|
||||
return publishEvent(event, c);
|
||||
}
|
||||
|
||||
/** Publish an admin event through the pipeline. */
|
||||
async function createAdminEvent<K extends number>(t: PendingEvent<K>, c: AppContext): Promise<Event<K>> {
|
||||
const event = await signAdminEvent({
|
||||
created_at: nostrNow(),
|
||||
...t,
|
||||
});
|
||||
|
||||
return publishEvent(event, c);
|
||||
}
|
||||
|
||||
/** Push the event through the pipeline, rethrowing any RelayError. */
|
||||
async function publishEvent<K extends number>(event: Event<K>, c: AppContext): Promise<Event<K>> {
|
||||
try {
|
||||
await pipeline.handleEvent(event);
|
||||
} catch (e) {
|
||||
|
@ -90,4 +107,12 @@ function activityJson<T, P extends string>(c: Context<any, P>, object: T) {
|
|||
return response;
|
||||
}
|
||||
|
||||
export { activityJson, buildLinkHeader, createEvent, type PaginationParams, paginationSchema, parseBody };
|
||||
export {
|
||||
activityJson,
|
||||
buildLinkHeader,
|
||||
createAdminEvent,
|
||||
createEvent,
|
||||
type PaginationParams,
|
||||
paginationSchema,
|
||||
parseBody,
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue