55 DiscordMembers ,
66 DiscordMention ,
77 DiscordStreamProcessResult ,
8+ ProcessedChannel ,
9+ ProcessedChannels ,
810} from '../../types/discordTypes'
911import { DISCORD_CONFIG } from '../../../../config'
1012import { DiscordMemberAttributes } from '../../../../database/attributes/member/discord'
@@ -20,7 +22,6 @@ import { IntegrationType, PlatformType } from '../../../../types/integrationEnum
2022import { timeout } from '../../../../utils/timing'
2123import Operations from '../../../dbOperations/operations'
2224import { DiscordGrid } from '../../grid/discordGrid'
23- import { Channels } from '../../types/regularTypes'
2425import getChannels from '../../usecases/discord/getChannels'
2526import getMembers from '../../usecases/discord/getMembers'
2627import getMessages from '../../usecases/discord/getMessages'
@@ -29,6 +30,7 @@ import { sendNodeWorkerMessage } from '../../../utils/nodeWorkerSQS'
2930import { NodeWorkerIntegrationProcessMessage } from '../../../../types/mq/nodeWorkerIntegrationProcessMessage'
3031import { AddActivitiesSingle } from '../../types/messageTypes'
3132import { singleOrDefault } from '../../../../utils/arrays'
33+ import getThreads from '../../usecases/discord/getThreads'
3234
3335/* eslint class-methods-use-this: 0 */
3436
@@ -83,18 +85,24 @@ export class DiscordIntegrationService extends IntegrationServiceBase {
8385 async preprocess ( context : IStepContext ) : Promise < void > {
8486 const guildId = context . integration . integrationIdentifier
8587
86- let channelsFromDiscordAPI : Channels = await getChannels (
88+ const fromDiscordApi : ProcessedChannels = await getChannels (
8789 {
8890 guildId,
8991 token : this . getToken ( context ) ,
9092 } ,
9193 this . logger ( context ) ,
9294 )
9395
96+ let channelsFromDiscordAPI : ProcessedChannel [ ] = fromDiscordApi . channels
97+
9498 const channels = context . integration . settings . channels
9599 ? context . integration . settings . channels
96100 : [ ]
97101
102+ const forumChannels = context . integration . settings . forumChannels
103+ ? context . integration . settings . forumChannels
104+ : [ ]
105+
98106 // Add bool new property to new channels
99107 channelsFromDiscordAPI = channelsFromDiscordAPI . map ( ( c ) => {
100108 if ( channels . filter ( ( a ) => a . id === c . id ) . length <= 0 ) {
@@ -103,16 +111,46 @@ export class DiscordIntegrationService extends IntegrationServiceBase {
103111 return c
104112 } )
105113
114+ const threads = await getThreads (
115+ {
116+ guildId,
117+ token : this . getToken ( context ) ,
118+ } ,
119+ this . logger ( context ) ,
120+ )
121+
122+ const forumChannelsFromDiscordAPi = [ ]
123+
124+ for ( const thread of threads ) {
125+ const forumChannel : any = lodash . find ( fromDiscordApi . forumChannels , { id : thread . parentId } )
126+ if ( forumChannel ) {
127+ forumChannelsFromDiscordAPi . push ( {
128+ ...forumChannel ,
129+ threadId : thread . id ,
130+ new : forumChannels . filter ( ( c ) => c . id === forumChannel . id ) . length <= 0 ,
131+ threadName : thread . name ,
132+ } )
133+ }
134+ }
135+
106136 context . pipelineData = {
107137 settingsChannels : channels ,
108138 channels : channelsFromDiscordAPI ,
139+ forumChannels : forumChannelsFromDiscordAPi ,
109140 channelsInfo : channelsFromDiscordAPI . reduce ( ( acc , channel ) => {
110141 acc [ channel . id ] = {
111142 name : channel . name ,
112143 new : ! ! channel . new ,
113144 }
114145 return acc
115146 } , { } ) ,
147+ forumChannelsInfo : forumChannelsFromDiscordAPi . reduce ( ( acc , forumChannel ) => {
148+ acc [ forumChannel . id ] = {
149+ name : forumChannel . name ,
150+ new : ! ! forumChannel . new ,
151+ }
152+ return acc
153+ } , { } ) ,
116154 guildId : context . integration . integrationIdentifier ,
117155 }
118156 }
@@ -132,15 +170,27 @@ export class DiscordIntegrationService extends IntegrationServiceBase {
132170 } ,
133171 ]
134172
135- return predefined . concat (
136- context . pipelineData . channels . map ( ( c ) => ( {
137- value : 'channel' ,
138- metadata : {
139- id : c . id ,
140- page : '' ,
141- } ,
142- } ) ) ,
143- )
173+ return predefined
174+ . concat (
175+ context . pipelineData . channels . map ( ( c ) => ( {
176+ value : 'channel' ,
177+ metadata : {
178+ id : c . id ,
179+ page : '' ,
180+ } ,
181+ } ) ) ,
182+ )
183+ . concat (
184+ context . pipelineData . forumChannels . map ( ( c ) => ( {
185+ value : 'forumChannel' ,
186+ metadata : {
187+ id : c . threadId ,
188+ page : '' ,
189+ forumChannelId : c . id ,
190+ threadName : c . threadName ,
191+ } ,
192+ } ) ) ,
193+ )
144194 }
145195
146196 async processStream (
@@ -260,6 +310,15 @@ export class DiscordIntegrationService extends IntegrationServiceBase {
260310 const { new : _ , ...raw } = ch
261311 return raw
262312 } )
313+
314+ context . integration . settings . forumChannels = lodash . uniqBy (
315+ context . pipelineData . forumChannels . map ( ( ch ) => {
316+ const { new : _ , ...raw } = ch
317+ delete raw . threadId
318+ return raw
319+ } ) ,
320+ ( ch : any ) => ch . id ,
321+ )
263322 }
264323
265324 parseActivities (
@@ -330,7 +389,12 @@ export class DiscordIntegrationService extends IntegrationServiceBase {
330389 const activities : AddActivitiesSingle [ ] = records . reduce ( ( acc , record ) => {
331390 let parent = ''
332391
333- const channelInfo = context . pipelineData . channelsInfo [ stream . metadata . id ]
392+ const isForum = stream . metadata . forumChannelId !== undefined
393+
394+ let channelInfo = context . pipelineData . channelsInfo [ stream . metadata . id ]
395+ if ( isForum ) {
396+ channelInfo = context . pipelineData . forumChannelsInfo [ stream . metadata . forumChannelId ]
397+ }
334398
335399 if ( ! channelInfo ) {
336400 const log = this . logger ( context )
@@ -352,6 +416,7 @@ export class DiscordIntegrationService extends IntegrationServiceBase {
352416 value : 'thread' ,
353417 metadata : {
354418 id : record . thread . id ,
419+ forumChannelId : stream . metadata . forumChannelId ,
355420 } ,
356421 } )
357422
@@ -371,6 +436,8 @@ export class DiscordIntegrationService extends IntegrationServiceBase {
371436 // record.parentId means that it's a reply
372437 else if ( record . message_reference && record . message_reference . message_id ) {
373438 parent = record . message_reference . message_id
439+ } else if ( stream . value === 'forumChannel' ) {
440+ parent = stream . metadata . id
374441 }
375442
376443 let avatarUrl : string | boolean = false
@@ -382,19 +449,25 @@ export class DiscordIntegrationService extends IntegrationServiceBase {
382449 const activityObject = {
383450 tenant : context . integration . tenantId ,
384451 platform : PlatformType . DISCORD ,
385- type : 'message' ,
452+ type : isForum && record . id === parent ? 'thread_started' : 'message' ,
386453 sourceId : record . id ,
387454 sourceParentId : parent ,
388455 timestamp : moment ( record . timestamp ) . utc ( ) . toDate ( ) ,
456+ ...( stream . value === 'forumChannel' &&
457+ record . id === parent && { title : stream . metadata . threadName } ) ,
389458 body : record . content
390459 ? DiscordIntegrationService . replaceMentions ( record . content , record . mentions )
391460 : '' ,
392461 url : `https://discordapp.com/channels/${ context . pipelineData . guildId } /${ stream . metadata . id } /${ record . id } ` ,
393462 channel : channelInfo . name ,
394463 attributes : {
395- thread : record . thread !== undefined || stream . value === 'thread' ,
464+ thread :
465+ record . thread !== undefined ||
466+ stream . value === 'thread' ||
467+ stream . value === 'forumChannel' ,
396468 reactions : record . reactions ? record . reactions : [ ] ,
397469 attachments : record . attachments ? record . attachments : [ ] ,
470+ forum : isForum ,
398471 } ,
399472 member : {
400473 username : record . author . username ,
@@ -465,6 +538,7 @@ export class DiscordIntegrationService extends IntegrationServiceBase {
465538 return { fn : getMembers , arg : { guildId } }
466539 case 'channel' :
467540 case 'thread' :
541+ case 'forumChannel' :
468542 return { fn : getMessages , arg : { channelId : stream . metadata . id } }
469543 default :
470544 throw new Error ( `Unknown stream ${ stream . value } !` )
0 commit comments