mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
relay: refactor into smaller functions
This commit is contained in:
parent
b2f538ed94
commit
8e47c9dda2
2 changed files with 13 additions and 10 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
import { getFilters } from '@/db/events.ts';
|
import { getFilters } from '@/db/events.ts';
|
||||||
import { jsonSchema } from '@/schema.ts';
|
import { jsonSchema } from '@/schema.ts';
|
||||||
import { clientMsgSchema, type ClientREQ } from '@/schemas/nostr.ts';
|
import { type ClientMsg, clientMsgSchema, type ClientREQ } from '@/schemas/nostr.ts';
|
||||||
|
|
||||||
import type { AppController } from '@/app.ts';
|
import type { AppController } from '@/app.ts';
|
||||||
import type { Filter } from '@/deps.ts';
|
import type { Filter } from '@/deps.ts';
|
||||||
|
|
@ -17,17 +17,17 @@ type RelayMsg =
|
||||||
function connectStream(socket: WebSocket) {
|
function connectStream(socket: WebSocket) {
|
||||||
socket.onmessage = (e) => {
|
socket.onmessage = (e) => {
|
||||||
const result = jsonSchema.pipe(clientMsgSchema).safeParse(e.data);
|
const result = jsonSchema.pipe(clientMsgSchema).safeParse(e.data);
|
||||||
|
if (result.success) {
|
||||||
if (!result.success) {
|
handleClientMsg(result.data);
|
||||||
|
} else {
|
||||||
send(['NOTICE', 'Invalid message.']);
|
send(['NOTICE', 'Invalid message.']);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const clientMsg = result.data;
|
function handleClientMsg(msg: ClientMsg) {
|
||||||
|
switch (msg[0]) {
|
||||||
switch (clientMsg[0]) {
|
|
||||||
case 'REQ':
|
case 'REQ':
|
||||||
handleReq(clientMsg);
|
handleReq(msg);
|
||||||
return;
|
return;
|
||||||
case 'EVENT':
|
case 'EVENT':
|
||||||
send(['NOTICE', 'EVENT not yet implemented.']);
|
send(['NOTICE', 'EVENT not yet implemented.']);
|
||||||
|
|
@ -35,7 +35,7 @@ function connectStream(socket: WebSocket) {
|
||||||
case 'CLOSE':
|
case 'CLOSE':
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
async function handleReq([_, sub, ...filters]: ClientREQ) {
|
async function handleReq([_, sub, ...filters]: ClientREQ) {
|
||||||
for (const event of await getFilters(prepareFilters(filters))) {
|
for (const event of await getFilters(prepareFilters(filters))) {
|
||||||
|
|
@ -45,7 +45,7 @@ function connectStream(socket: WebSocket) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function send(msg: RelayMsg) {
|
function send(msg: RelayMsg) {
|
||||||
socket.send(JSON.stringify(msg));
|
return socket.send(JSON.stringify(msg));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -51,6 +51,8 @@ type ClientREQ = z.infer<typeof clientReqSchema>;
|
||||||
type ClientEVENT = z.infer<typeof clientEventSchema>;
|
type ClientEVENT = z.infer<typeof clientEventSchema>;
|
||||||
/** CLOSE message from client to relay. */
|
/** CLOSE message from client to relay. */
|
||||||
type ClientCLOSE = z.infer<typeof clientCloseSchema>;
|
type ClientCLOSE = z.infer<typeof clientCloseSchema>;
|
||||||
|
/** Client message to a Nostr relay. */
|
||||||
|
type ClientMsg = z.infer<typeof clientMsgSchema>;
|
||||||
|
|
||||||
/** Kind 0 content schema. */
|
/** Kind 0 content schema. */
|
||||||
const metaContentSchema = z.object({
|
const metaContentSchema = z.object({
|
||||||
|
|
@ -68,6 +70,7 @@ const jsonMetaContentSchema = jsonSchema.pipe(metaContentSchema).catch({});
|
||||||
export {
|
export {
|
||||||
type ClientCLOSE,
|
type ClientCLOSE,
|
||||||
type ClientEVENT,
|
type ClientEVENT,
|
||||||
|
type ClientMsg,
|
||||||
clientMsgSchema,
|
clientMsgSchema,
|
||||||
type ClientREQ,
|
type ClientREQ,
|
||||||
filterSchema,
|
filterSchema,
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue