Skip to content

Commit

Permalink
introduce user-level optout
Browse files Browse the repository at this point in the history
  • Loading branch information
dispherical committed Nov 9, 2024
1 parent 5d54a7d commit 9a94568
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
52 changes: 52 additions & 0 deletions commands/useroptout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @param {{app: import('@slack/bolt').App, prisma: import('@prisma/client').PrismaClient}} param1
*/
module.exports = async function ({ app, prisma }) {
app.command("/useroptout-library", async ({ command, body, ack, respond }) => {
await ack();
const userId = body.user_id;
const userRecord = await prisma.user.findFirst({
where: {
id: userId,
},
});
if (!userRecord) {
await prisma.user.create({
data: {
id: userId,
optout: true,
},
});
return await respond(
"You've been opted out. Run the command again to opt in.",
);
}
else if (userRecord.optout) {
await prisma.user.update({
where: {
id: userId,
},
data: {
optout: false,
},
});
return await respond(
"You've been opted in. Run the command again to opt out.",
);
}

else {
await prisma.user.update({
where: {
id: userId,
},
data: {
optout: true,
},
});
return await respond(
"You've been opted out. Run the command again to opt in.",
);
}
});
};
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ var activeConnections = [];
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) {
Expand Down
10 changes: 8 additions & 2 deletions interactions/message.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const utils = require("../utils");

/**
* @param {{app: import('@slack/bolt').App, client: import('redis').RedisClientType}} param1
* @param {{app: import('@slack/bolt').App, client: import('redis').RedisClientType, prisma: import('@prisma/client').PrismaClient}} param1
*/
module.exports = ({ app, client, broadcastMessage }) => {
module.exports = ({ app, client, broadcastMessage, prisma }) => {
app.message(/.*/gim, async ({ message, say, body }) => {
if (message.channel == process.env.SLACK_CHANNEL)
await app.client.chat.delete({
Expand All @@ -12,6 +12,12 @@ module.exports = ({ app, client, broadcastMessage }) => {
token: process.env.SLACK_BOT_TOKEN,
});
if (utils.blockedChannels.includes(message.channel)) return;
const userRecord = await prisma.user.findFirst({
where: {
id: message.user
}
})
if (userRecord && userRecord.optout) return
broadcastMessage(message)
message.sort_ts = +new Date() / 1000.0;
client.lPush(
Expand Down
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ model User {
createdAt DateTime @default(now())
lat String?
lon String?
optout Boolean @default(false)
}

0 comments on commit 9a94568

Please sign in to comment.