@@ -4,28 +4,22 @@ import {
44 type Message ,
55 type MessageInThread ,
66 type ServerContext ,
7+ type User ,
78 zIncludeFilters ,
9+ zMessageFilter ,
810 zMessageInThread ,
911 zUser ,
1012} from '../types.js' ;
11- import { convertTsToTimestamp } from '../util/formatTs.js' ;
1213import { getUsersMap } from '../util/getUsersMap.js' ;
1314import { getMessageFields } from '../util/messageFields.js' ;
1415import { messagesToTree } from '../util/messagesToTree.js' ;
16+ import { normalizeMessageFilterQueryParameters } from '../util/normalizeMessageFilterQueryParameters.js' ;
1517
1618const inputSchema = {
1719 ...zIncludeFilters . shape ,
18- channel : z
19- . string ( )
20- . min ( 1 )
21- . describe ( 'The ID of the channel to fetch messages from.' ) ,
22-
23- ts : z
24- . string ( )
25- . min ( 1 )
26- . describe (
27- 'The thread timestamp to fetch messages for. This is the ts of the parent message. Use the `thread_ts` field from a known message in the thread.' ,
28- ) ,
20+ messageFilters : z
21+ . array ( zMessageFilter )
22+ . describe ( 'The messages to fetch the threads for.' ) ,
2923} as const ;
3024
3125const outputSchema = {
@@ -52,28 +46,45 @@ export const getThreadMessagesFactory: ApiFactory<
5246 outputSchema,
5347 } ,
5448 fn : async ( {
55- channel,
5649 includeFiles,
5750 includePermalinks,
58- ts ,
51+ messageFilters : passedMessageFilters ,
5952 } ) : Promise < {
6053 messages : MessageInThread [ ] ;
61- users : Record < string , z . infer < typeof zUser > > ;
54+ users : Record < string , User > ;
6255 } > => {
56+ const messageFilters = normalizeMessageFilterQueryParameters (
57+ pgPool ,
58+ passedMessageFilters ,
59+ ) ;
60+
6361 const result = await pgPool . query < Message > (
6462 /* sql */ `
65- SELECT ${ getMessageFields ( { includeFiles } ) } FROM slack.message
66- WHERE channel_id = $1 AND (thread_ts = $2 OR ts = $2)
67- ORDER BY ts DESC` , // messagesToTree expects messages in descending order
68- [ channel , convertTsToTimestamp ( ts ) ] ,
63+ WITH filters AS (
64+ SELECT
65+ f->>'channel' AS channel_id,
66+ (f->>'ts')::timestamptz AS ts
67+ FROM jsonb_array_elements($1::jsonb) AS f
68+ )
69+ SELECT ${ getMessageFields ( { includeFiles } ) }
70+ FROM slack.message m
71+ WHERE EXISTS (
72+ SELECT 1 FROM filters f
73+ WHERE m.channel_id = f.channel_id
74+ AND (m.thread_ts = f.ts OR m.ts = f.ts)
75+ )
76+ ORDER BY ts DESC` , // messagesToTree expects messages in descending order
77+ [ JSON . stringify ( messageFilters ) ] ,
6978 ) ;
7079
7180 const { involvedUsers, channels } = messagesToTree (
7281 result . rows ,
7382 includePermalinks || false ,
7483 ) ;
7584 const users = await getUsersMap ( pgPool , involvedUsers ) ;
76- const messages = channels [ channel ] ?. messages || [ ] ;
85+
86+ // Flatten messages from all channels
87+ const messages = Object . values ( channels ) . flatMap ( ( c ) => c . messages ) ;
7788
7889 return {
7990 messages,
0 commit comments