From acdd82a6808cb501979cb1f0ed1e95e6cd75a5c6 Mon Sep 17 00:00:00 2001 From: MartinBraquet Date: Fri, 24 Oct 2025 15:18:04 +0200 Subject: [PATCH] Clean and factor out get-private-messages --- backend/api/src/app.ts | 4 ++-- backend/api/src/get-private-messages.ts | 28 ++++++++++++++++++------- backend/api/src/helpers/endpoint.ts | 2 +- backend/shared/src/encryption.ts | 2 +- backend/shared/src/supabase/messages.ts | 17 ++++++++++++++- 5 files changed, 41 insertions(+), 12 deletions(-) diff --git a/backend/api/src/app.ts b/backend/api/src/app.ts index 2f5f3730..d78a3705 100644 --- a/backend/api/src/app.ts +++ b/backend/api/src/app.ts @@ -41,7 +41,7 @@ import {getCurrentPrivateUser} from './get-current-private-user' import {createPrivateUserMessage} from './create-private-user-message' import { getChannelMemberships, - getChannelMessages, + getChannelMessagesEndpoint, getLastSeenChannelTime, setChannelLastSeenTime, } from 'api/get-private-messages' @@ -183,7 +183,7 @@ const handlers: { [k in APIPath]: APIHandler } = { 'update-private-user-message-channel': updatePrivateUserMessageChannel, 'leave-private-user-message-channel': leavePrivateUserMessageChannel, 'get-channel-memberships': getChannelMemberships, - 'get-channel-messages': getChannelMessages, + 'get-channel-messages': getChannelMessagesEndpoint, 'get-channel-seen-time': getLastSeenChannelTime, 'set-channel-seen-time': setChannelLastSeenTime, 'get-messages-count': getMessagesCount, diff --git a/backend/api/src/get-private-messages.ts b/backend/api/src/get-private-messages.ts index 9dc9be9a..b44e8b27 100644 --- a/backend/api/src/get-private-messages.ts +++ b/backend/api/src/get-private-messages.ts @@ -1,8 +1,9 @@ import {createSupabaseDirectClient} from 'shared/supabase/init' -import {APIHandler} from './helpers/endpoint' +import {APIError, APIHandler} from './helpers/endpoint' import {PrivateMessageChannel,} from 'common/supabase/private-messages' import {groupBy, mapValues} from 'lodash' import {convertPrivateChatMessage} from "shared/supabase/messages"; +import {tryCatch} from "common/util/try-catch"; export const getChannelMemberships: APIHandler< 'get-channel-memberships' @@ -90,14 +91,24 @@ export const getChannelMemberships: APIHandler< } } -export const getChannelMessages: APIHandler<'get-channel-messages'> = async ( +export const getChannelMessagesEndpoint: APIHandler<'get-channel-messages'> = async ( props, auth ) => { + const userId = auth.uid + return await getChannelMessages({...props, userId}) +} + +export async function getChannelMessages(props: { + channelId: number; + limit: number; + id?: number | undefined; + userId: string; +}) { + // console.log('initial message request', props) + const {channelId, limit, id, userId} = props const pg = createSupabaseDirectClient() - const {channelId, limit, id} = props - // console.log('initial message', props) - return await pg.map( + const {data, error} = await tryCatch(pg.map( `select *, created_time as created_time_ts from private_user_messages where channel_id = $1 @@ -110,9 +121,12 @@ export const getChannelMessages: APIHandler<'get-channel-messages'> = async ( order by created_time desc limit $3 `, - [channelId, auth.uid, limit, id], + [channelId, userId, limit, id], convertPrivateChatMessage - ) + )) + if (error) throw new APIError(401, 'Error getting messages') + // console.log('final messages', data) + return data } export const getLastSeenChannelTime: APIHandler< diff --git a/backend/api/src/helpers/endpoint.ts b/backend/api/src/helpers/endpoint.ts index a67c5728..737bb580 100644 --- a/backend/api/src/helpers/endpoint.ts +++ b/backend/api/src/helpers/endpoint.ts @@ -272,7 +272,7 @@ export const typedEndpoint = ( if (!res.headersSent) { // Convert bigint to number, b/c JSON doesn't support bigint. const convertedResult = deepConvertBigIntToNumber(result) - + // console.debug('API result', convertedResult) res.status(200).json(convertedResult ?? {success: true}) } diff --git a/backend/shared/src/encryption.ts b/backend/shared/src/encryption.ts index a5b742ba..aeb6b03c 100644 --- a/backend/shared/src/encryption.ts +++ b/backend/shared/src/encryption.ts @@ -53,6 +53,6 @@ export function decryptMessage({ciphertext, iv, tag}: { ciphertext: string; iv: const decipher = crypto.createDecipheriv("aes-256-gcm", getMasterKey(), ivBuf); decipher.setAuthTag(tagBuf); const plaintext = Buffer.concat([decipher.update(ctBuf), decipher.final()]).toString("utf8"); - console.debug("Decrypted message:", plaintext); + // console.debug("Decrypted message:", plaintext); return plaintext; } \ No newline at end of file diff --git a/backend/shared/src/supabase/messages.ts b/backend/shared/src/supabase/messages.ts index 74660d6f..04448a4e 100644 --- a/backend/shared/src/supabase/messages.ts +++ b/backend/shared/src/supabase/messages.ts @@ -18,6 +18,17 @@ export const convertPrivateChatMessage = (row: Row<'private_user_messages'>) => row, {created_time: tsToMillis as any,} ); + parseMessageObject(message); + return message +} + +type MessageObject = Omit & { id: number; createdTimeTs: string } & { + ciphertext: string; + iv: string; + tag: string +} + +export function parseMessageObject(message: MessageObject) { if (message.ciphertext && message.iv && message.tag) { const plaintText = decryptMessage({ ciphertext: message.ciphertext, @@ -29,5 +40,9 @@ export const convertPrivateChatMessage = (row: Row<'private_user_messages'>) => delete (message as any).iv delete (message as any).tag } - return message +} + +export function getDecryptedMessage(message: MessageObject) { + parseMessageObject(message) + return message.content } \ No newline at end of file