11import { createSupabaseDirectClient } from 'shared/supabase/init'
2- import { APIHandler } from './helpers/endpoint'
2+ import { APIError , APIHandler } from './helpers/endpoint'
33import { PrivateMessageChannel , } from 'common/supabase/private-messages'
44import { groupBy , mapValues } from 'lodash'
55import { convertPrivateChatMessage } from "shared/supabase/messages" ;
6+ import { tryCatch } from "common/util/try-catch" ;
67
78export 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
118132export const getLastSeenChannelTime : APIHandler <
0 commit comments