mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
relay: allow local users to post to the relay
This commit is contained in:
parent
a35ea6ab5d
commit
4c8a685528
1 changed files with 29 additions and 6 deletions
|
|
@ -1,6 +1,13 @@
|
||||||
import { getFilters } from '@/db/events.ts';
|
import { getFilters, insertEvent } from '@/db/events.ts';
|
||||||
|
import { findUser } from '@/db/users.ts';
|
||||||
import { jsonSchema } from '@/schema.ts';
|
import { jsonSchema } from '@/schema.ts';
|
||||||
import { type ClientMsg, clientMsgSchema, type ClientREQ } from '@/schemas/nostr.ts';
|
import {
|
||||||
|
type ClientCLOSE,
|
||||||
|
type ClientEVENT,
|
||||||
|
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';
|
||||||
|
|
@ -12,27 +19,29 @@ const FILTER_LIMIT = 100;
|
||||||
type RelayMsg =
|
type RelayMsg =
|
||||||
| ['EVENT', string, SignedEvent]
|
| ['EVENT', string, SignedEvent]
|
||||||
| ['NOTICE', string]
|
| ['NOTICE', string]
|
||||||
| ['EOSE', string];
|
| ['EOSE', string]
|
||||||
|
| ['OK', string, boolean, string];
|
||||||
|
|
||||||
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);
|
handleMsg(result.data);
|
||||||
} else {
|
} else {
|
||||||
send(['NOTICE', 'Invalid message.']);
|
send(['NOTICE', 'Invalid message.']);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
function handleClientMsg(msg: ClientMsg) {
|
function handleMsg(msg: ClientMsg) {
|
||||||
switch (msg[0]) {
|
switch (msg[0]) {
|
||||||
case 'REQ':
|
case 'REQ':
|
||||||
handleReq(msg);
|
handleReq(msg);
|
||||||
return;
|
return;
|
||||||
case 'EVENT':
|
case 'EVENT':
|
||||||
send(['NOTICE', 'EVENT not yet implemented.']);
|
handleEvent(msg);
|
||||||
return;
|
return;
|
||||||
case 'CLOSE':
|
case 'CLOSE':
|
||||||
|
handleClose(msg);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -44,6 +53,20 @@ function connectStream(socket: WebSocket) {
|
||||||
send(['EOSE', sub]);
|
send(['EOSE', sub]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function handleEvent([_, event]: ClientEVENT) {
|
||||||
|
if (await findUser({ pubkey: event.pubkey })) {
|
||||||
|
insertEvent(event);
|
||||||
|
send(['OK', event.id, true, '']);
|
||||||
|
} else {
|
||||||
|
send(['OK', event.id, false, 'blocked: only registered users can post']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleClose([_, _sub]: ClientCLOSE) {
|
||||||
|
// TODO: ???
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
function send(msg: RelayMsg) {
|
function send(msg: RelayMsg) {
|
||||||
return socket.send(JSON.stringify(msg));
|
return socket.send(JSON.stringify(msg));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue