Skip to content

Commit acdd82a

Browse files
committed
Clean and factor out get-private-messages
1 parent 5719ac3 commit acdd82a

File tree

5 files changed

+41
-12
lines changed

5 files changed

+41
-12
lines changed

backend/api/src/app.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import {getCurrentPrivateUser} from './get-current-private-user'
4141
import {createPrivateUserMessage} from './create-private-user-message'
4242
import {
4343
getChannelMemberships,
44-
getChannelMessages,
44+
getChannelMessagesEndpoint,
4545
getLastSeenChannelTime,
4646
setChannelLastSeenTime,
4747
} from 'api/get-private-messages'
@@ -183,7 +183,7 @@ const handlers: { [k in APIPath]: APIHandler<k> } = {
183183
'update-private-user-message-channel': updatePrivateUserMessageChannel,
184184
'leave-private-user-message-channel': leavePrivateUserMessageChannel,
185185
'get-channel-memberships': getChannelMemberships,
186-
'get-channel-messages': getChannelMessages,
186+
'get-channel-messages': getChannelMessagesEndpoint,
187187
'get-channel-seen-time': getLastSeenChannelTime,
188188
'set-channel-seen-time': setChannelLastSeenTime,
189189
'get-messages-count': getMessagesCount,

backend/api/src/get-private-messages.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import {createSupabaseDirectClient} from 'shared/supabase/init'
2-
import {APIHandler} from './helpers/endpoint'
2+
import {APIError, APIHandler} from './helpers/endpoint'
33
import {PrivateMessageChannel,} from 'common/supabase/private-messages'
44
import {groupBy, mapValues} from 'lodash'
55
import {convertPrivateChatMessage} from "shared/supabase/messages";
6+
import {tryCatch} from "common/util/try-catch";
67

78
export const getChannelMemberships: APIHandler<
89
'get-channel-memberships'
@@ -90,14 +91,24 @@ export const getChannelMemberships: APIHandler<
9091
}
9192
}
9293

93-
export const getChannelMessages: APIHandler<'get-channel-messages'> = async (
94+
export const getChannelMessagesEndpoint: APIHandler<'get-channel-messages'> = async (
9495
props,
9596
auth
9697
) => {
98+
const userId = auth.uid
99+
return await getChannelMessages({...props, userId})
100+
}
101+
102+
export async function getChannelMessages(props: {
103+
channelId: number;
104+
limit: number;
105+
id?: number | undefined;
106+
userId: string;
107+
}) {
108+
// console.log('initial message request', props)
109+
const {channelId, limit, id, userId} = props
97110
const pg = createSupabaseDirectClient()
98-
const {channelId, limit, id} = props
99-
// console.log('initial message', props)
100-
return await pg.map(
111+
const {data, error} = await tryCatch(pg.map(
101112
`select *, created_time as created_time_ts
102113
from private_user_messages
103114
where channel_id = $1
@@ -110,9 +121,12 @@ export const getChannelMessages: APIHandler<'get-channel-messages'> = async (
110121
order by created_time desc
111122
limit $3
112123
`,
113-
[channelId, auth.uid, limit, id],
124+
[channelId, userId, limit, id],
114125
convertPrivateChatMessage
115-
)
126+
))
127+
if (error) throw new APIError(401, 'Error getting messages')
128+
// console.log('final messages', data)
129+
return data
116130
}
117131

118132
export const getLastSeenChannelTime: APIHandler<

backend/api/src/helpers/endpoint.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ export const typedEndpoint = <N extends APIPath>(
272272
if (!res.headersSent) {
273273
// Convert bigint to number, b/c JSON doesn't support bigint.
274274
const convertedResult = deepConvertBigIntToNumber(result)
275-
275+
// console.debug('API result', convertedResult)
276276
res.status(200).json(convertedResult ?? {success: true})
277277
}
278278

backend/shared/src/encryption.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@ export function decryptMessage({ciphertext, iv, tag}: { ciphertext: string; iv:
5353
const decipher = crypto.createDecipheriv("aes-256-gcm", getMasterKey(), ivBuf);
5454
decipher.setAuthTag(tagBuf);
5555
const plaintext = Buffer.concat([decipher.update(ctBuf), decipher.final()]).toString("utf8");
56-
console.debug("Decrypted message:", plaintext);
56+
// console.debug("Decrypted message:", plaintext);
5757
return plaintext;
5858
}

backend/shared/src/supabase/messages.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ export const convertPrivateChatMessage = (row: Row<'private_user_messages'>) =>
1818
row,
1919
{created_time: tsToMillis as any,}
2020
);
21+
parseMessageObject(message);
22+
return message
23+
}
24+
25+
type MessageObject = Omit<ChatMessage, "id"> & { id: number; createdTimeTs: string } & {
26+
ciphertext: string;
27+
iv: string;
28+
tag: string
29+
}
30+
31+
export function parseMessageObject(message: MessageObject) {
2132
if (message.ciphertext && message.iv && message.tag) {
2233
const plaintText = decryptMessage({
2334
ciphertext: message.ciphertext,
@@ -29,5 +40,9 @@ export const convertPrivateChatMessage = (row: Row<'private_user_messages'>) =>
2940
delete (message as any).iv
3041
delete (message as any).tag
3142
}
32-
return message
43+
}
44+
45+
export function getDecryptedMessage(message: MessageObject) {
46+
parseMessageObject(message)
47+
return message.content
3348
}

0 commit comments

Comments
 (0)