-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
184 lines (165 loc) · 5.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
require("dotenv").config();
const { App, ExpressReceiver } = require("@slack/bolt");
const { createClient } = require("redis");
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,
socketMode: !Boolean(process.env.PORT),
appToken: process.env.SLACK_APP_TOKEN,
port: process.env.PORT,
receiver: process.env.PORT ? receiver : undefined,
});
Array.prototype.random = function () {
return this[Math.floor(Math.random() * this.length)];
};
var activeConnections = [];
(async () => {
const client = await createClient({
url: process.env.REDIS_DATABASE,
})
.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("/sls/:id", async (req, res) => {
const { id } = req.params
try {
const text = await getCloseChannels(id)
res.set("Content-Type", "text/plain").send(text)
} catch (e) {
res.set("Content-Type", "text/plain").send("User not found").status(404)
}
});
receiver.router.get("/personal", async (req, res) => {
const channels = await prisma.channel.findMany({
where: {
personal: true
}
})
res.json(channels)
});
receiver.router.get("/affinity", async (req, res) => {
const channels = await prisma.channel.findMany({
where: {
affinity: true
}
})
res.json(channels)
});
receiver.router.get("/featured", async (req, res) => {
const channels = await prisma.channel.findMany({
where: {
featured: true
}
})
res.json(channels)
});
receiver.router.get("/name/:id", async (req, res) => {
const { id } = req.params
const channel = await prisma.channel.findFirst({
where: {
id
}
})
if (!channel) return res.json({ name: null })
else return res.json({ name: channel.name })
});
receiver.router.get("/channels", async (req, res) => {
const channels = await prisma.channel.findMany({
where: {
id: {
not: "123"
}
}
})
return res.json(channels.map(channel => {
channel.lat = null;
channel.lon = null;
return channel
}))
});
receiver.router.get("/id/:name", async (req, res) => {
const { name } = req.params
const channel = await prisma.channel.findFirst({
where: {
name
}
})
if (!channel) return res.json({ id: null })
else return res.json({ id: channel.id })
});
await require("./commands/optout")({ app, client, prisma });
await require("./commands/setlocation")({ app, client, prisma });
await require("./commands/setuserlocation")({ app, client, prisma });
await require("./commands/setemoji")({ app, client, prisma });
await require("./commands/setfeatured")({ app, client, prisma });
await require("./commands/setpersonal")({ app, client, prisma });
await require("./commands/setaffinity")({ app, client, prisma });
await require("./commands/useroptout")({ app, client, prisma });
wss.on('connection', function connection(ws) {
activeConnections.push(ws);
ws.on('message', (message) => {
try {
const data = JSON.parse(message);
if (data.apiKey && data.apiKey === process.env.WS_API_KEY) {
ws.loggedIn = true
const index = activeConnections.indexOf(ws);
if (index > -1) {
activeConnections.splice(index, 1);
activeConnections.push(ws)
}
}
} catch (e) {
console.error('Failed to parse message', e);
}
});
ws.on('close', () => {
const index = activeConnections.indexOf(ws);
if (index > -1) {
activeConnections.splice(index, 1);
}
});
});
function broadcastMessage(message) {
activeConnections.forEach((ws) => {
if (ws.loggedIn) {
try {
ws.send(JSON.stringify(message));
} catch (e) {
console.error('Failed to send message', e);
}
}
});
}
// This deletes and sends a new message to bypass the 10 day editing limit and to show up on the user's unread channel list
// 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, broadcastMessage });
setInterval(async function () {
await require("./utils/pull")({ app, client, prisma });
}, 1000 * 10)
cron.schedule("0 0,12 * * *", async () => {
await client.del(`${process.env.INSTANCE_ID || "production"}.messageCache`);
await require("./utils/redo")({ app, client, prisma });
await require("./utils/joinall")({ app, client, prisma });
});
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 });
})();