mirror of
https://github.com/CompassConnections/Compass.git
synced 2026-04-04 06:51:45 -04:00
Clean and factor out get-private-messages
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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<
|
||||
|
||||
@@ -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})
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user