Clean and factor out get-private-messages

This commit is contained in:
MartinBraquet
2025-10-24 15:18:04 +02:00
parent 5719ac3209
commit acdd82a680
5 changed files with 41 additions and 12 deletions

View File

@@ -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<k> } = {
'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,

View File

@@ -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<

View File

@@ -272,7 +272,7 @@ export const typedEndpoint = <N extends APIPath>(
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})
}

View File

@@ -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;
}

View File

@@ -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<ChatMessage, "id"> & { 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
}