Skip to content

Commit

Permalink
add message stream ws
Browse files Browse the repository at this point in the history
  • Loading branch information
dispherical committed Oct 20, 2024
1 parent d3a2dd7 commit 1532bc8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
21 changes: 14 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ const cron = require("node-cron");
const { PrismaClient } = require("@prisma/client");
const prisma = new PrismaClient();
const getCloseChannels = require("./utils/getCloseChannels")
const { WebSocketServer } = require("ws")
const { createServer } = require('node:http');
const receiver = new ExpressReceiver({
signingSecret: process.env.SLACK_SIGNING_SECRET,
});

});
const server = createServer(receiver.app);
const wss = new WebSocketServer({ server: server })
const app = new App({
token: process.env.SLACK_BOT_TOKEN,
signingSecret: process.env.SLACK_SIGNING_SECRET,
Expand All @@ -28,9 +32,9 @@ Array.prototype.random = function () {
})
.on("error", (err) => console.error("Redis Client Error", err))
.connect();
receiver.router.get("/", async (req, res) => {
res.redirect(302, "https://github.com/hackclub/channel-directory");
});
/* receiver.router.get("/", async (req, res) => {
res.redirect(302, "https://github.com/hackclub/channel-directory");
});*/
receiver.router.get("/sls/:id", async (req, res) => {
const { id } = req.params
try {
Expand Down Expand Up @@ -81,7 +85,7 @@ Array.prototype.random = function () {
// This runs the same thing on startup
await require("./utils/redo")({ app, client, prisma });
// app.message functions go here
await require("./interactions/message")({ app, client, prisma });
await require("./interactions/message")({ app, client, prisma, wss });


setInterval(async function () {
Expand All @@ -93,8 +97,11 @@ Array.prototype.random = function () {
await require("./utils/joinall")({ app, client, prisma });
});

console.log("Librarian has started.");
await app.start(process.env.PORT || 3000);

server.listen(process.env.PORT || 3000, () => {
console.log("Librarian has started.");
});
await app.start();
require("./interactions/channel_created")({ app, client });
if (process.env.INSTANCE_ID == "production") await require("./utils/joinall")({ app, client, prisma });

Expand Down
6 changes: 4 additions & 2 deletions interactions/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const utils = require("../utils");
/**
* @param {{app: import('@slack/bolt').App, client: import('redis').RedisClientType}} param1
*/
module.exports = ({ app, client }) => {
module.exports = ({ app, client, wss }) => {
app.message(/.*/gim, async ({ message, say, body }) => {
if (message.channel == process.env.SLACK_CHANNEL)
await app.client.chat.delete({
Expand All @@ -12,7 +12,9 @@ module.exports = ({ app, client }) => {
token: process.env.SLACK_BOT_TOKEN,
});
if (utils.blockedChannels.includes(message.channel)) return;

wss.on('connection', function connection(ws) {
ws.send(JSON.stringify(message));
});
message.sort_ts = +new Date() / 1000.0;
client.lPush(
`${process.env.INSTANCE_ID || "production"}.messageCache`,
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"node-cron": "^3.0.3",
"pretty-ms": "7.0.1",
"prisma": "^5.21.1",
"redis": "4.7.0"
"redis": "4.7.0",
"ws": "^8.18.0"
}
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1946,6 +1946,11 @@ ws@^7.5.3:
resolved "https://registry.yarnpkg.com/ws/-/ws-7.5.10.tgz#58b5c20dc281633f6c19113f39b349bd8bd558d9"
integrity sha512-+dbF1tHwZpXcbOJdVOkzLDxZP1ailvSxM6ZweXTegylPny803bFhA+vqBYw4s31NSAk4S2Qz+AKXK9a4wkdjcQ==

ws@^8.18.0:
version "8.18.0"
resolved "https://registry.yarnpkg.com/ws/-/ws-8.18.0.tgz#0d7505a6eafe2b0e712d232b42279f53bc289bbc"
integrity sha512-8VbfWfHLbbwu3+N6OKsOMpBdT4kXPDDB9cJk2bJ6mh9ucxdlnNvH1e+roYkKmN9Nxw2yjz7VzeO9oOz2zJ04Pw==

[email protected]:
version "4.0.0"
resolved "https://registry.yarnpkg.com/yallist/-/yallist-4.0.0.tgz#9bb92790d9c0effec63be73519e11a35019a3a72"
Expand Down

0 comments on commit 1532bc8

Please sign in to comment.