Skip to content

Commit 76d7051

Browse files
committed
add methods to add a conversation and a new message
1 parent abf4ad1 commit 76d7051

File tree

8 files changed

+499
-30
lines changed

8 files changed

+499
-30
lines changed

packages/backend/src/persistance/getDatabase.ts

+37-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ import Session from './session';
1717
import Storage from './storage';
1818
import Otp from './otp';
1919
import { syncAcknowledge } from './messages/syncAcknowledge';
20+
import { PrismaClient } from '@prisma/client';
21+
import { addConversation } from './storage/postgres/addConversation';
22+
import { getConversationList } from './storage/postgres/getConversationList';
23+
import { addMessage } from './storage/postgres/addMessage';
24+
import { getMessages } from './storage/postgres/getMessages';
2025

2126
export enum RedisPrefix {
2227
Conversation = 'conversation:',
@@ -59,8 +64,16 @@ export async function getRedisClient() {
5964
return client;
6065
}
6166

62-
export async function getDatabase(_redis?: Redis): Promise<IDatabase> {
67+
export async function getPrismaClient() {
68+
return new PrismaClient();
69+
}
70+
71+
export async function getDatabase(
72+
_redis?: Redis,
73+
_prisma?: PrismaClient,
74+
): Promise<IDatabase> {
6375
const redis = _redis ?? (await getRedisClient());
76+
const prisma = _prisma ?? (await getPrismaClient());
6477

6578
return {
6679
//Messages
@@ -104,6 +117,12 @@ export async function getDatabase(_redis?: Redis): Promise<IDatabase> {
104117
setOtp: Otp.setOtp(redis),
105118
getOtp: Otp.getOtp(redis),
106119
resetOtp: Otp.resetOtp(redis),
120+
//Storage AddConversation
121+
storage_addConversation: addConversation(prisma),
122+
storage_getConversationList: getConversationList(prisma),
123+
//Storage Add Messages
124+
storage_addMessage: addMessage(prisma),
125+
storage_getMessages: getMessages(prisma),
107126
};
108127
}
109128

@@ -194,6 +213,23 @@ export interface IDatabase {
194213
ensName: string,
195214
channelType: NotificationChannelType,
196215
) => Promise<void>;
216+
217+
storage_addConversation: (
218+
ensName: string,
219+
encryptedContactName: string,
220+
) => Promise<boolean>;
221+
storage_getConversationList: (ensName: string) => Promise<string[]>;
222+
storage_addMessage: (
223+
ensName: string,
224+
encryptedContactName: string,
225+
messageId: string,
226+
encryptedEnvelopContainer: string,
227+
) => Promise<boolean>;
228+
storage_getMessages: (
229+
ensName: string,
230+
encryptedContactName: string,
231+
page: number,
232+
) => Promise<string[]>;
197233
}
198234

199235
export type Redis = Awaited<ReturnType<typeof getRedisClient>>;
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,41 @@
11
import { PrismaClient } from '@prisma/client';
22
export const addConversation =
3-
(db: PrismaClient) => async (contactName: string) => {
4-
await db.conversation.create({
5-
data: {
6-
id: contactName,
7-
},
8-
});
3+
(db: PrismaClient) => async (ensName: string, contactName: string) => {
4+
try {
5+
let account = await db.account.findFirst({
6+
where: {
7+
id: ensName,
8+
},
9+
});
10+
11+
if (!account) {
12+
//Create account
13+
account = await db.account.create({
14+
data: {
15+
id: ensName,
16+
},
17+
});
18+
}
19+
20+
const conversationAlreadyExists = await db.conversation.findFirst({
21+
where: {
22+
accountId: ensName,
23+
encryptedId: contactName,
24+
},
25+
});
26+
27+
if (!conversationAlreadyExists) {
28+
await db.conversation.create({
29+
data: {
30+
encryptedId: contactName,
31+
accountId: ensName,
32+
},
33+
});
34+
}
35+
36+
return true;
37+
} catch (e) {
38+
console.log('addConversation error ', e);
39+
return false;
40+
}
941
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { PrismaClient } from '@prisma/client';
2+
3+
export const addMessage =
4+
(db: PrismaClient) =>
5+
async (
6+
ensName: string,
7+
contactName: string,
8+
messageId: string,
9+
encryptedEnvelopContainer: string,
10+
) => {
11+
try {
12+
let account = await db.account.findFirst({
13+
where: {
14+
id: ensName,
15+
},
16+
});
17+
18+
if (!account) {
19+
//Create account
20+
account = await db.account.create({
21+
data: {
22+
id: ensName,
23+
},
24+
});
25+
}
26+
27+
let conversation = await db.conversation.findFirst({
28+
where: {
29+
accountId: ensName,
30+
encryptedId: contactName,
31+
},
32+
});
33+
34+
if (!conversation) {
35+
conversation = await db.conversation.create({
36+
data: {
37+
encryptedId: contactName,
38+
accountId: ensName,
39+
},
40+
});
41+
}
42+
console.log('conversation', conversation);
43+
44+
await db.encryptedMessage.create({
45+
data: {
46+
id: messageId,
47+
conversationId: conversation.encryptedId,
48+
encryptedEnvelopContainer,
49+
},
50+
});
51+
52+
return true;
53+
} catch (e) {
54+
console.log('addMessage error ', e);
55+
return false;
56+
}
57+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { PrismaClient } from '@prisma/client';
2+
export const getConversationList =
3+
(db: PrismaClient) => async (ensName: string) => {
4+
const account = await db.account.findFirst({
5+
where: {
6+
id: ensName,
7+
},
8+
});
9+
if (!account) {
10+
return [];
11+
}
12+
console.log('get conversations for ', ensName);
13+
const conversations = await db.conversation.findMany({
14+
where: {
15+
accountId: ensName,
16+
},
17+
});
18+
console.log('get conversations ', conversations);
19+
return conversations.map((c) => c.encryptedId);
20+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { PrismaClient } from '@prisma/client';
2+
3+
const PAGE_SIZE = 100;
4+
5+
export const getMessages =
6+
(db: PrismaClient) =>
7+
async (ensName: string, contactName: string, page: number) => {
8+
const conversation = await db.conversation.findFirst({
9+
where: {
10+
accountId: ensName,
11+
encryptedId: contactName,
12+
},
13+
});
14+
15+
if (!conversation) {
16+
return [];
17+
}
18+
19+
const messages = await db.encryptedMessage.findMany({
20+
skip: page * PAGE_SIZE,
21+
take: PAGE_SIZE,
22+
where: {
23+
conversationId: conversation.encryptedId,
24+
},
25+
});
26+
27+
return messages.map((m) => m.encryptedEnvelopContainer);
28+
};

0 commit comments

Comments
 (0)