feat: add localId
This commit is contained in:
parent
0b3017ef1b
commit
bed3f87cba
@ -0,0 +1,11 @@
|
|||||||
|
/*
|
||||||
|
Warnings:
|
||||||
|
|
||||||
|
- A unique constraint covering the columns `[sessionId,localId]` on the table `SessionMessage` will be added. If there are existing duplicate values, this will fail.
|
||||||
|
|
||||||
|
*/
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE "SessionMessage" ADD COLUMN "localId" TEXT;
|
||||||
|
|
||||||
|
-- CreateIndex
|
||||||
|
CREATE UNIQUE INDEX "SessionMessage_sessionId_localId_key" ON "SessionMessage"("sessionId", "localId");
|
@ -55,11 +55,13 @@ model SessionMessage {
|
|||||||
id String @id @default(cuid())
|
id String @id @default(cuid())
|
||||||
sessionId String
|
sessionId String
|
||||||
session Session @relation(fields: [sessionId], references: [id])
|
session Session @relation(fields: [sessionId], references: [id])
|
||||||
|
localId String?
|
||||||
seq Int
|
seq Int
|
||||||
/// [SessionMessageContent]
|
/// [SessionMessageContent]
|
||||||
content Json
|
content Json
|
||||||
createdAt DateTime @default(now())
|
createdAt DateTime @default(now())
|
||||||
updatedAt DateTime @updatedAt
|
updatedAt DateTime @updatedAt
|
||||||
|
@@unique([sessionId, localId])
|
||||||
}
|
}
|
||||||
|
|
||||||
model Update {
|
model Update {
|
||||||
|
@ -94,7 +94,7 @@ export async function startApi() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Send session update to all relevant connections
|
// Send session update to all relevant connections
|
||||||
let emitUpdateToInterestedClients = ({event, userId, sessionId, payload, skipSenderConnection}: {
|
let emitUpdateToInterestedClients = ({ event, userId, sessionId, payload, skipSenderConnection }: {
|
||||||
event: string,
|
event: string,
|
||||||
userId: string,
|
userId: string,
|
||||||
sessionId: string,
|
sessionId: string,
|
||||||
@ -120,7 +120,7 @@ export async function startApi() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Send to all session-scoped connections, only that match sessionId
|
// Send to all session-scoped connections, only that match sessionId
|
||||||
if (connection.connectionType === 'session-scoped'
|
if (connection.connectionType === 'session-scoped'
|
||||||
&& connection.sessionId === sessionId
|
&& connection.sessionId === sessionId
|
||||||
) {
|
) {
|
||||||
log({ module: 'websocket' }, `Sending ${event} to session-scoped connection ${connection.socket.id}`);
|
log({ module: 'websocket' }, `Sending ${event} to session-scoped connection ${connection.socket.id}`);
|
||||||
@ -595,7 +595,7 @@ export async function startApi() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
socket.on('message', async (data: any) => {
|
socket.on('message', async (data: any) => {
|
||||||
const { sid, message } = data;
|
const { sid, message, localId } = data;
|
||||||
|
|
||||||
log({ module: 'websocket' }, `Received message from socket ${socket.id}: ${sid} ${message.length} bytes`);
|
log({ module: 'websocket' }, `Received message from socket ${socket.id}: ${sid} ${message.length} bytes`);
|
||||||
|
|
||||||
@ -606,6 +606,7 @@ export async function startApi() {
|
|||||||
if (!session) {
|
if (!session) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
let useLocalId = typeof localId === 'string' ? localId : null;
|
||||||
|
|
||||||
// Create encrypted message
|
// Create encrypted message
|
||||||
const msgContent: PrismaJson.SessionMessageContent = {
|
const msgContent: PrismaJson.SessionMessageContent = {
|
||||||
@ -641,12 +642,20 @@ export async function startApi() {
|
|||||||
const msgSeq = session.seq + 1;
|
const msgSeq = session.seq + 1;
|
||||||
const updSeq = user.seq + 1;
|
const updSeq = user.seq + 1;
|
||||||
|
|
||||||
|
if (useLocalId) {
|
||||||
|
const existing = await tx.sessionMessage.findFirst({
|
||||||
|
where: { sessionId: sid, localId: useLocalId }
|
||||||
|
});
|
||||||
|
return { msg: existing, update: null };
|
||||||
|
}
|
||||||
|
|
||||||
// Create message
|
// Create message
|
||||||
const msg = await tx.sessionMessage.create({
|
const msg = await tx.sessionMessage.create({
|
||||||
data: {
|
data: {
|
||||||
sessionId: sid,
|
sessionId: sid,
|
||||||
seq: msgSeq,
|
seq: msgSeq,
|
||||||
content: msgContent
|
content: msgContent,
|
||||||
|
localId: useLocalId
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -690,21 +699,26 @@ export async function startApi() {
|
|||||||
throw error;
|
throw error;
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!result) return;
|
// If no update, we're done
|
||||||
|
if (!result) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Emit update to relevant clients
|
// Emit update to relevant clients
|
||||||
emitUpdateToInterestedClients({
|
if (result.update) {
|
||||||
event: 'update',
|
emitUpdateToInterestedClients({
|
||||||
userId,
|
event: 'update',
|
||||||
sessionId: sid,
|
userId,
|
||||||
payload: {
|
sessionId: sid,
|
||||||
id: result.update.id,
|
payload: {
|
||||||
seq: result.update.seq,
|
id: result.update.id,
|
||||||
body: result.update.content,
|
seq: result.update.seq,
|
||||||
createdAt: result.update.createdAt.getTime()
|
body: result.update.content,
|
||||||
},
|
createdAt: result.update.createdAt.getTime()
|
||||||
skipSenderConnection: connection
|
},
|
||||||
});
|
skipSenderConnection: connection
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on('update-metadata', async (data: any, callback: (response: any) => void) => {
|
socket.on('update-metadata', async (data: any, callback: (response: any) => void) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user