Files
upstream-mirror 30810eecd7
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 2s
snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:21:02 +05:00

200 lines
6.1 KiB
TypeScript

// This is a JS File that was renamed to TS so it won't lose its git history when converted to TS
// TODO: Remove the following lint/ts instructions when the file gets properly converted
/* eslint-disable-next-line @typescript-eslint/ban-ts-comment */
// @ts-nocheck
import { debounce } from 'lodash';
import RocketAdapter from './RocketAdapter';
import SlackAdapter from './SlackAdapter';
import { classLogger, connLogger } from './logger';
import { settings } from '../../settings';
/**
* SlackBridge interfaces between this Rocket installation and a remote Slack installation.
*/
class SlackBridgeClass {
constructor() {
this.isEnabled = false;
this.isLegacyRTM = true;
this.slackAdapters = [];
this.rocket = new RocketAdapter(this);
this.reactionsMap = new Map(); // Sync object between rocket and slack
this.connected = false;
this.rocket.clearSlackAdapters();
// Settings that we cache versus looking up at runtime
this.apiTokens = false;
this.botTokens = false;
this.appTokens = false;
this.signingSecrets = false;
this.aliasFormat = '';
this.excludeBotnames = '';
this.isReactionsEnabled = true;
this.processSettings();
}
connect() {
if (this.connected === false) {
this.slackAdapters = [];
this.rocket.clearSlackAdapters();
if (this.isLegacyRTM) {
const tokenList = this.apiTokens.split('\n');
tokenList.forEach((apiToken) => {
const slack = new SlackAdapter(this);
slack.setRocket(this.rocket);
this.rocket.addSlack(slack);
this.slackAdapters.push(slack);
slack.connect({ apiToken }).catch((err) => connLogger.error({ msg: 'error connecting to slack', err }));
});
} else {
const botTokenList = this.botTokens.split('\n'); // Bot token list
const appTokenList = this.appTokens.split('\n'); // App token list
const signingSecretList = this.signingSecrets.split('\n'); // Signing secret list
// Check if the number of tokens are the same
if (botTokenList.length !== appTokenList.length || botTokenList.length !== signingSecretList.length) {
connLogger.error('error connecting to slack: number of tokens are not the same');
return;
}
const appCredentials = botTokenList.map((botToken, i) => ({
botToken,
appToken: appTokenList[i],
signingSecret: signingSecretList[i],
}));
appCredentials.forEach((appCredential) => {
const slack = new SlackAdapter(this);
slack.setRocket(this.rocket);
this.rocket.addSlack(slack);
this.slackAdapters.push(slack);
slack.connect({ appCredential }).catch((err) => connLogger.error({ msg: 'error connecting to slack', err }));
});
}
if (settings.get('SlackBridge_Out_Enabled')) {
this.rocket.connect();
}
this.connected = true;
connLogger.info('Enabled');
}
}
async reconnect() {
await this.disconnect();
// connect if either apiTokens or appCredentials are set
if (this.isLegacyRTM && this.apiTokens) {
this.connect();
} else if (!this.isLegacyRTM && this.botTokens && this.appTokens && this.signingSecrets) {
this.connect();
}
}
debouncedReconnectIfEnabled = debounce(() => {
if (this.isEnabled) {
this.reconnect();
}
}, 500);
async disconnect() {
try {
if (this.connected === true) {
await this.rocket.disconnect();
await Promise.all(this.slackAdapters.map((slack) => slack.disconnect()));
this.slackAdapters = [];
this.connected = false;
connLogger.info('Slack Bridge Disconnected');
}
} catch (error) {
connLogger.error({ msg: 'An error occurred during disconnection', err: error });
}
}
processSettings() {
// Check if legacy realtime api is enabled
settings.watch('SlackBridge_UseLegacy', (value) => {
if (value !== this.isLegacyRTM) {
this.isLegacyRTM = value;
this.debouncedReconnectIfEnabled();
}
classLogger.debug({ msg: 'Setting: SlackBridge_UseLegacy', value });
});
// Slack installtion Bot token
settings.watch('SlackBridge_BotToken', (value) => {
if (value !== this.botTokens) {
this.botTokens = value;
this.debouncedReconnectIfEnabled();
}
classLogger.debug({ msg: 'Setting: SlackBridge_BotToken', value });
});
// Slack installtion App token
settings.watch('SlackBridge_AppToken', (value) => {
if (value !== this.appTokens) {
this.appTokens = value;
this.debouncedReconnectIfEnabled();
}
classLogger.debug({ msg: 'Setting: SlackBridge_AppToken', value });
});
// Slack installtion Signing token
settings.watch('SlackBridge_SigningSecret', (value) => {
if (value !== this.signingSecrets) {
this.signingSecrets = value;
this.debouncedReconnectIfEnabled();
}
classLogger.debug({ msg: 'Setting: SlackBridge_SigningSecret', value });
});
// Slack installation API token
settings.watch('SlackBridge_APIToken', (value) => {
if (value !== this.apiTokens) {
this.apiTokens = value;
this.debouncedReconnectIfEnabled();
}
classLogger.debug({ msg: 'Setting: SlackBridge_APIToken', value });
});
// Import messages from Slack with an alias; %s is replaced by the username of the user. If empty, no alias will be used.
settings.watch('SlackBridge_AliasFormat', (value) => {
this.aliasFormat = value;
classLogger.debug({ msg: 'Setting: SlackBridge_AliasFormat', value });
});
// Do not propagate messages from bots whose name matches the regular expression above. If left empty, all messages from bots will be propagated.
settings.watch('SlackBridge_ExcludeBotnames', (value) => {
this.excludeBotnames = value;
classLogger.debug({ msg: 'Setting: SlackBridge_ExcludeBotnames', value });
});
// Reactions
settings.watch('SlackBridge_Reactions_Enabled', (value) => {
this.isReactionsEnabled = value;
classLogger.debug({ msg: 'Setting: SlackBridge_Reactions_Enabled', value });
});
// Is this entire SlackBridge enabled
settings.watch('SlackBridge_Enabled', (value) => {
if (this.isEnabled !== value) {
this.isEnabled = value;
if (this.isEnabled) {
this.debouncedReconnectIfEnabled();
} else {
this.disconnect();
}
}
classLogger.debug({ msg: 'Setting: SlackBridge_Enabled', value });
});
}
}
export const SlackBridge = new SlackBridgeClass();