Skip to content

Commit b00592c

Browse files
committed
chore: handling messages on the server
1 parent eb5cfff commit b00592c

File tree

14 files changed

+394
-34
lines changed

14 files changed

+394
-34
lines changed

apps/server/prisma/migrations/20250219233352_add_inboxes/migration.sql renamed to apps/server/prisma/migrations/20250306151228_add_inboxes/migration.sql

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ CREATE TABLE "SpaceInboxMessage" (
1919
"ciphertext" TEXT NOT NULL,
2020
"nonce" TEXT NOT NULL,
2121
"ephemeralPublicKey" TEXT NOT NULL,
22-
"authorPublicKey" TEXT NOT NULL,
22+
"signatureHex" TEXT,
23+
"signatureRecovery" INTEGER,
24+
"authorAccountId" TEXT,
2325
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
2426
CONSTRAINT "SpaceInboxMessage_spaceInboxId_fkey" FOREIGN KEY ("spaceInboxId") REFERENCES "SpaceInbox" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
2527
);
@@ -44,7 +46,9 @@ CREATE TABLE "AccountInboxMessage" (
4446
"ciphertext" TEXT NOT NULL,
4547
"nonce" TEXT NOT NULL,
4648
"ephemeralPublicKey" TEXT NOT NULL,
47-
"authorPublicKey" TEXT NOT NULL,
49+
"signatureHex" TEXT,
50+
"signatureRecovery" INTEGER,
51+
"authorAccountId" TEXT,
4852
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
4953
CONSTRAINT "AccountInboxMessage_accountInboxId_fkey" FOREIGN KEY ("accountInboxId") REFERENCES "AccountInbox" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
5054
);

apps/server/prisma/schema.prisma

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ model SpaceInboxMessage {
7676
ciphertext String
7777
nonce String
7878
ephemeralPublicKey String
79-
authorPublicKey String
79+
signatureHex String?
80+
signatureRecovery Int?
81+
authorAccountId String?
8082
createdAt DateTime @default(now())
8183
}
8284

@@ -115,7 +117,9 @@ model AccountInboxMessage {
115117
ciphertext String
116118
nonce String
117119
ephemeralPublicKey String
118-
authorPublicKey String
120+
signatureHex String?
121+
signatureRecovery Int?
122+
authorAccountId String?
119123
createdAt DateTime @default(now())
120124
}
121125

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import type { Messages } from '@graphprotocol/hypergraph';
2+
import { prisma } from '../prisma';
3+
4+
type Params = {
5+
accountId: string;
6+
inboxId: string;
7+
message: Messages.RequestCreateAccountInboxMessage;
8+
};
9+
10+
export const createAccountInboxMessage = async (params: Params) => {
11+
const { accountId, inboxId, message } = params;
12+
const accountInbox = await prisma.accountInbox.findUnique({
13+
where: {
14+
id: inboxId,
15+
accountId,
16+
},
17+
});
18+
if (!accountInbox) {
19+
throw new Error('Account inbox not found');
20+
}
21+
22+
await prisma.accountInboxMessage.create({
23+
data: {
24+
ciphertext: message.ciphertext,
25+
nonce: message.nonce,
26+
ephemeralPublicKey: message.ephemeralPublicKey,
27+
signatureHex: message.signature?.hex,
28+
signatureRecovery: message.signature?.recovery,
29+
authorAccountId: message.authorAccountId,
30+
accountInbox: {
31+
connect: {
32+
id: accountInbox.id,
33+
},
34+
},
35+
},
36+
});
37+
};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import type { Messages } from '@graphprotocol/hypergraph';
2+
import { prisma } from '../prisma';
3+
4+
type Params = {
5+
spaceId: string;
6+
inboxId: string;
7+
message: Messages.RequestCreateSpaceInboxMessage;
8+
};
9+
10+
export const createSpaceInboxMessage = async (params: Params) => {
11+
const { spaceId, inboxId, message } = params;
12+
const spaceInbox = await prisma.spaceInbox.findUnique({
13+
where: {
14+
id: inboxId,
15+
},
16+
});
17+
if (!spaceInbox) {
18+
throw new Error('Space inbox not found');
19+
}
20+
if (spaceInbox.spaceId !== spaceId) {
21+
throw new Error('Incorrect space');
22+
}
23+
await prisma.spaceInboxMessage.create({
24+
data: {
25+
spaceInbox: {
26+
connect: {
27+
id: spaceInbox.id,
28+
},
29+
},
30+
ciphertext: message.ciphertext,
31+
nonce: message.nonce,
32+
ephemeralPublicKey: message.ephemeralPublicKey,
33+
signatureHex: message.signature?.hex,
34+
signatureRecovery: message.signature?.recovery,
35+
authorAccountId: message.authorAccountId,
36+
},
37+
});
38+
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { prisma } from '../prisma';
2+
3+
export async function getAccountInbox({ accountId, inboxId }: { accountId: string; inboxId: string }) {
4+
const inbox = await prisma.accountInbox.findUnique({
5+
where: { id: inboxId, accountId },
6+
select: {
7+
id: true,
8+
account: {
9+
select: {
10+
id: true,
11+
},
12+
},
13+
isPublic: true,
14+
authPolicy: true,
15+
encryptionPublicKey: true,
16+
signatureHex: true,
17+
signatureRecovery: true,
18+
},
19+
});
20+
if (!inbox) {
21+
throw new Error('Inbox not found');
22+
}
23+
24+
return {
25+
inboxId: inbox.id,
26+
accountId: inbox.account.id,
27+
isPublic: inbox.isPublic,
28+
authPolicy: inbox.authPolicy,
29+
encryptionPublicKey: inbox.encryptionPublicKey,
30+
signature: {
31+
hex: inbox.signatureHex,
32+
recovery: inbox.signatureRecovery,
33+
},
34+
};
35+
}

apps/server/src/handlers/getSpaceInbox.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { SpaceEvents } from '@graphprotocol/hypergraph';
2+
import { prisma } from '../prisma';
13

24
export async function getSpaceInbox({ spaceId, inboxId }: { spaceId: string; inboxId: string }) {
35
const inbox = await prisma.spaceInbox.findUnique({
@@ -7,8 +9,6 @@ export async function getSpaceInbox({ spaceId, inboxId }: { spaceId: string; inb
79
isPublic: true,
810
authPolicy: true,
911
encryptionPublicKey: true,
10-
},
11-
include: {
1212
spaceEvent: {
1313
select: {
1414
event: true,
@@ -25,6 +25,6 @@ export async function getSpaceInbox({ spaceId, inboxId }: { spaceId: string; inb
2525
isPublic: inbox.isPublic,
2626
authPolicy: inbox.authPolicy,
2727
encryptionPublicKey: inbox.encryptionPublicKey,
28-
creationEvent: JSON.parse(inbox.spaceEvent),
28+
creationEvent: JSON.parse(inbox.spaceEvent.event) as SpaceEvents.CreateSpaceInboxEvent,
2929
};
3030
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { prisma } from '../prisma';
2+
3+
export async function listPublicAccountInboxes({ accountId }: { accountId: string }) {
4+
const inboxes = await prisma.accountInbox.findMany({
5+
where: { accountId, isPublic: true },
6+
select: {
7+
id: true,
8+
isPublic: true,
9+
authPolicy: true,
10+
encryptionPublicKey: true,
11+
account: {
12+
select: {
13+
id: true,
14+
},
15+
},
16+
signatureHex: true,
17+
signatureRecovery: true,
18+
},
19+
});
20+
return inboxes.map((inbox) => {
21+
return {
22+
inboxId: inbox.id,
23+
accountId: inbox.account.id,
24+
isPublic: inbox.isPublic,
25+
authPolicy: inbox.authPolicy,
26+
encryptionPublicKey: inbox.encryptionPublicKey,
27+
signature: {
28+
hex: inbox.signatureHex,
29+
recovery: inbox.signatureRecovery,
30+
},
31+
};
32+
});
33+
}

apps/server/src/handlers/listPublicSpaceInboxes.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { prisma } from "../prisma";
1+
import type { SpaceEvents } from '@graphprotocol/hypergraph';
2+
import { prisma } from '../prisma';
23

34
export async function listPublicSpaceInboxes({ spaceId }: { spaceId: string }) {
45
const inboxes = await prisma.spaceInbox.findMany({
@@ -8,8 +9,6 @@ export async function listPublicSpaceInboxes({ spaceId }: { spaceId: string }) {
89
isPublic: true,
910
authPolicy: true,
1011
encryptionPublicKey: true,
11-
},
12-
include: {
1312
spaceEvent: {
1413
select: {
1514
event: true,
@@ -23,7 +22,7 @@ export async function listPublicSpaceInboxes({ spaceId }: { spaceId: string }) {
2322
isPublic: inbox.isPublic,
2423
authPolicy: inbox.authPolicy,
2524
encryptionPublicKey: inbox.encryptionPublicKey,
26-
creationEvent: JSON.parse(inbox.spaceEvent),
25+
creationEvent: JSON.parse(inbox.spaceEvent.event) as SpaceEvents.CreateSpaceInboxEvent,
2726
};
2827
});
2928
}

0 commit comments

Comments
 (0)