@@ -22,6 +22,7 @@ import { t } from '../utils/Locale.js';
2222import sendMessage from '../scripts/network/sendMessage.js' ;
2323import Scheduler from '../services/SchedulerService.js' ;
2424import { captureException } from '@sentry/node' ;
25+ import Logger from '../utils/Logger.js' ;
2526
2627export interface NetworkWebhookSendResult {
2728 messageOrError : APIMessage | string ;
@@ -43,31 +44,35 @@ const MAX_STORE = 3;
4344export default class NetworkManager {
4445 private readonly scheduler : Scheduler ;
4546 private readonly antiSpamMap : Collection < string , AntiSpamUserOpts > ;
46- private connectionCache : Collection < string , Connection > ;
47+ private _connectionCache : Collection < string , Connection > ;
4748 private cachePopulated = false ;
4849
4950 constructor ( ) {
5051 this . scheduler = new Scheduler ( ) ;
5152 this . antiSpamMap = new Collection ( ) ;
52- this . connectionCache = new Collection ( ) ;
53+ this . _connectionCache = new Collection ( ) ;
5354
54- db . connectedList
55- . findMany ( { where : { connected : true } , include : { hub : true } } )
56- . then ( ( connections ) => {
57- this . connectionCache = new Collection ( connections . map ( ( c ) => [ c . channelId , c ] ) ) ;
58- this . cachePopulated = true ;
59- } )
60- . catch ( captureException ) ;
55+ this . populateConnectionCache ( ) . catch ( captureException ) ;
6156
6257 this . scheduler . addRecurringTask ( 'populateConnectionCache' , 60_000 , async ( ) => {
63- const connections = await db . connectedList . findMany ( {
64- where : { connected : true } ,
65- include : { hub : true } ,
66- } ) ;
58+ await this . populateConnectionCache ( ) . catch ( captureException ) ;
59+ } ) ;
60+ }
6761
68- // populate all at once without time delay
69- this . connectionCache = new Collection ( connections . map ( ( c ) => [ c . channelId , c ] ) ) ;
62+ protected async populateConnectionCache ( ) {
63+ Logger . debug ( '[InterChat]: Populating connection cache.' ) ;
64+ const connections = await db . connectedList . findMany ( {
65+ where : { connected : true } ,
66+ include : { hub : true } ,
7067 } ) ;
68+
69+ // populate all at once without time delay
70+ this . _connectionCache = new Collection ( connections . map ( ( c ) => [ c . channelId , c ] ) ) ;
71+ Logger . debug ( `[InterChat]: Connection cache populated with ${ this . _connectionCache . size } entries.` ) ;
72+ }
73+
74+ public get connectionCache ( ) {
75+ return this . _connectionCache ;
7176 }
7277
7378 /**
@@ -78,19 +83,18 @@ export default class NetworkManager {
7883 if ( message . author . bot || message . system || message . webhookId ) return ;
7984
8085 if ( ! this . cachePopulated ) {
86+ Logger . debug ( '[InterChat]: Cache not populated, retrying in 5 seconds...' ) ;
8187 await wait ( 5000 ) ;
8288 return this . onMessageCreate ( message ) ;
8389 }
8490
8591 const locale = await message . client . getUserLocale ( message . author . id ) ;
8692 message . author . locale = locale ;
8793
88- const connection = this . connectionCache . find (
89- ( c ) => c . channelId === message . channelId && c . connected ,
90- ) ;
94+ const connection = this . _connectionCache . get ( message . channelId ) ;
9195
9296 // check if the message was sent in a network channel
93- if ( ! connection ?. hub ) return ;
97+ if ( ! connection ?. connected || ! connection . hub ) return ;
9498
9599 const settings = new HubSettingsBitField ( connection . hub . settings ) ;
96100
@@ -104,10 +108,7 @@ export default class NetworkManager {
104108 return ;
105109 }
106110
107- const allConnections = await this . fetchHubNetworks ( {
108- hubId : connection . hubId ,
109- connected : true ,
110- } ) ;
111+ const hubConnections = this . _connectionCache . filter ( ( con ) => con . hubId === connection . hubId ) ;
111112
112113 const censoredContent = censor ( message . content ) ;
113114
@@ -161,10 +162,7 @@ export default class NetworkManager {
161162 } ) ;
162163
163164 // ---------- Broadcasting ---------
164- const sendResult = allConnections . map ( async ( otherConnection , index ) => {
165- // wait 1 second every 50 messages to avoid rate limits
166- if ( index % 50 ) await wait ( 1000 ) ;
167-
165+ const sendResult = hubConnections . map ( async ( otherConnection ) => {
168166 try {
169167 const reply = referenceInDb ?. broadcastMsgs . find (
170168 ( msg ) => msg . channelId === otherConnection . channelId ,
@@ -288,7 +286,7 @@ export default class NetworkManager {
288286 user : message . author . toString ( ) ,
289287 hub : connection . hub . name ,
290288 channel : message . channel . toString ( ) ,
291- totalServers : allConnections . length . toString ( ) ,
289+ totalServers : hubConnections . size . toString ( ) ,
292290 emoji : emojis . wave_anim ,
293291 rules_command : '</rules:924659340898619395>' ,
294292 } ,
0 commit comments