Skip to content

Commit

Permalink
Merge pull request #678 from tsg-ut/rm-rtm-api
Browse files Browse the repository at this point in the history
rm rtm-api
  • Loading branch information
cookie-s committed Jun 3, 2022
2 parents d0d2f76 + 2200b6a commit b0a492b
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 303 deletions.
12 changes: 4 additions & 8 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ process.on('unhandledRejection', (error: Error) => {
});

import os from 'os';
import {rtmClient, webClient, messageClient, eventClient, tsgEventClient} from './lib/slack';
import {webClient, messageClient, eventClient, tsgEventClient} from './lib/slack';
import Fastify from 'fastify';

import logger from './lib/logger';
Expand Down Expand Up @@ -158,13 +158,13 @@ eventClient.on('error', (error) => {
await Promise.all(plugins.map(async (name) => {
const plugin = await import(`./${name}`);
if (typeof plugin === 'function') {
await plugin({rtmClient, webClient, eventClient: tsgEventClient, messageClient});
await plugin({webClient, eventClient: tsgEventClient, messageClient});
}
if (typeof plugin.default === 'function') {
await plugin.default({rtmClient, webClient, eventClient: tsgEventClient, messageClient});
await plugin.default({webClient, eventClient: tsgEventClient, messageClient});
}
if (typeof plugin.server === 'function') {
await fastify.register(plugin.server({rtmClient, webClient, eventClient: tsgEventClient, messageClient}));
await fastify.register(plugin.server({webClient, eventClient: tsgEventClient, messageClient}));
}
loadedPlugins.add(name);
logger.info(`plugin "${name}" successfully loaded`);
Expand All @@ -186,8 +186,4 @@ eventClient.on('error', (error) => {
channel: process.env.CHANNEL_SANDBOX,
text: argv.startup,
});

rtmClient.on('authenticated', (data) => {
logger.info(`Logged in as ${data.self.name} of team ${data.team.name}`);
});
})();
18 changes: 0 additions & 18 deletions lib/slack.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {WebClient} from '@slack/web-api';
import {RTMClient} from '@slack/rtm-api';
import {createMessageAdapter} from '@slack/interactive-messages';
import {createEventAdapter} from '@slack/events-api';
import sql from 'sql-template-strings';
Expand All @@ -11,7 +10,6 @@ import {Deferred} from './utils';
import {Token} from '../oauth/tokens';

export interface SlackInterface {
rtmClient: RTMClient;
webClient: WebClient;
eventClient: TeamEventClient;
messageClient: ReturnType<typeof createMessageAdapter>;
Expand Down Expand Up @@ -42,7 +40,6 @@ export interface SlackOauthEndpoint {
};
}

export const rtmClient = new RTMClient(process.env.SLACK_TOKEN);
export const webClient = new WebClient(process.env.SLACK_TOKEN);
export const eventClient = createEventAdapter(process.env.SIGNING_SECRET, {includeBody: true});
export const messageClient = createMessageAdapter(process.env.SIGNING_SECRET);
Expand All @@ -52,10 +49,6 @@ export const tsgEventClient = new TeamEventClient(
);


rtmClient.start();
const rtmClients = new Map<string, RTMClient>();
rtmClients.set(process.env.TEAM_ID, rtmClient);

const loadTokensDeferred = new Deferred<Token[]>();
const loadTokens = async () => {
const db = await sqlite.open({
Expand All @@ -65,12 +58,6 @@ const loadTokens = async () => {
const tokens = await db.all(sql`SELECT * FROM tokens WHERE bot_access_token <> ''`).catch(() => []);
await db.close();

for (const token of tokens) {
const rtmClient = new RTMClient(token.bot_access_token);
rtmClient.start();
rtmClients.set(token.team_id, rtmClient);
}

loadTokensDeferred.resolve(tokens.concat([{
team_id: process.env.TEAM_ID,
team_name: process.env.TEAMNAME,
Expand All @@ -82,8 +69,3 @@ const loadTokens = async () => {
loadTokens();

export const getTokens = (): Promise<Token[]> => loadTokensDeferred.promise;

export const getRtmClient = async (teamId: string): Promise<RTMClient> => {
await loadTokensDeferred.promise;
return rtmClients.get(teamId);
};
2 changes: 0 additions & 2 deletions lib/slackMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ module.exports = class SlackMock extends EventEmitter {
this.fakeChannel = 'C00000000';
this.fakeUser = 'U00000000';
this.fakeTimestamp = '1234567890.123456';
this.rtmClient = new EventEmitter();
this.eventClient = new EventEmitter();
this.webClient = createWebClient((...args) => this.handleWebcall(...args));
this.messageClient = {
Expand Down Expand Up @@ -69,7 +68,6 @@ module.exports = class SlackMock extends EventEmitter {
ts: this.fakeTimestamp,
type: "message",
};
this.rtmClient.emit('message', data);
this.eventClient.emit('message', data);
});
}
Expand Down

0 comments on commit b0a492b

Please sign in to comment.