Skip to content

Commit

Permalink
fmt + remove race from emojis
Browse files Browse the repository at this point in the history
  • Loading branch information
dispherical committed Jun 29, 2024
1 parent d334071 commit e03839a
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 109 deletions.
4 changes: 2 additions & 2 deletions commands/optout.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const prisma = new PrismaClient();
*/
module.exports = async function ({ app }) {
app.command("/optout-library", async ({ command, body, ack, respond }) => {
await prisma.$connect()
await prisma.$connect();
await ack();
const channelId = body.channel_id;
const channel = await app.client.conversations.info({
Expand Down Expand Up @@ -57,6 +57,6 @@ module.exports = async function ({ app }) {
channel: channel.channel.id,
});

await prisma.$disconnect()
await prisma.$disconnect();
});
};
126 changes: 64 additions & 62 deletions commands/setemoji.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,72 @@

const emojis = require("../utils/emojis")
const emojis = require("../utils/emojis");
/**
* @param {{app: import('@slack/bolt').App}} param1
*/
module.exports = async function ({ app }) {
const { PrismaClient } = require("@prisma/client");
const prisma = new PrismaClient();
app.command("/setemoji", async ({ command, body, ack, respond }) => {
await prisma.$connect()
await ack();
if (!command.text)
return await respond(
"Please provide an emoji. I.e. /setemoji 🎒 or /setemoji :hackclub:",
);
const channelId = body.channel_id;
const channel = await app.client.conversations.info({
channel: body.channel_id,
});
const user = await app.client.users.info({
user: command.user_id,
});
if (command.user_id != channel.channel.creator && !user.user.is_admin)
return await respond(
"Only channel managers and workspace admins can opt .",
);
const channelRecord = await prisma.channel.findFirst({
where: {
id: channelId,
},
});
if (!command.text && !command.text.match(/:[a-zA-Z0-9_-]+:/) && !emojis.includes(command.text))
return await respond("Please provide an emoji")


if (!channel.channel.is_member)
try {
await app.client.conversations.join({
channel: channel.channel.id,
});
} catch (e) { }
if (!channelRecord)
await prisma.channel.create({
data: {
id: channelId,
emoji: command.text
},
});
else if (channelRecord.locked && !user.user.is_admin)
return await respond(
"This channel cannot be locked as it is locked in the database. Usually, this is because it is a public, community-owned channel, i.e. #lounge, #code, etc.",
);
const { PrismaClient } = require("@prisma/client");
const prisma = new PrismaClient();
app.command("/setemoji", async ({ command, body, ack, respond }) => {
await prisma.$connect();
await ack();
if (!command.text)
return await respond(
"Please provide an emoji. I.e. /setemoji 🎒 or /setemoji :hackclub:",
);
const channelId = body.channel_id;
const channel = await app.client.conversations.info({
channel: body.channel_id,
});
const user = await app.client.users.info({
user: command.user_id,
});
if (command.user_id != channel.channel.creator && !user.user.is_admin)
return await respond(
"Only channel managers and workspace admins can opt .",
);
const channelRecord = await prisma.channel.findFirst({
where: {
id: channelId,
},
});
if (
!command.text &&
!command.text.match(/:[a-zA-Z0-9_-]+:/) &&
!emojis.includes(command.text)
)
return await respond("Please provide an emoji");

await prisma.channel.update({
where: {
id: channelId,
},
data: {
optout: false,
emoji: command.text
},
if (!channel.channel.is_member)
try {
await app.client.conversations.join({
channel: channel.channel.id,
});
await app.client.chat.postEphemeral({
channel: channel.channel.id,
user: command.user_id,
text: `Set your channel emoji to ${command.text}`,
});
await prisma.$disconnect()
} catch (e) {}
if (!channelRecord)
await prisma.channel.create({
data: {
id: channelId,
emoji: command.text,
},
});
else if (channelRecord.locked && !user.user.is_admin)
return await respond(
"This channel cannot be locked as it is locked in the database. Usually, this is because it is a public, community-owned channel, i.e. #lounge, #code, etc.",
);

await prisma.channel.update({
where: {
id: channelId,
},
data: {
optout: false,
emoji: command.text,
},
});
await app.client.chat.postEphemeral({
channel: channel.channel.id,
user: command.user_id,
text: `Set your channel emoji to ${command.text}`,
});
await prisma.$disconnect();
});
};
16 changes: 7 additions & 9 deletions commands/setlocation.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@


/**
* @param {{app: import('@slack/bolt').App}} param1
*/
module.exports = async function ({ app }) {
const { PrismaClient } = require("@prisma/client");
const prisma = new PrismaClient();
app.command("/setlocation", async ({ command, body, ack, respond }) => {
await prisma.$connect()
await prisma.$connect();
await ack();
if (!command.text)
return await respond(
Expand All @@ -32,18 +30,18 @@ module.exports = async function ({ app }) {
var locations = await (
await fetch(
`https://nominatim.openstreetmap.org/search?` +
new URLSearchParams({
q: command.text.trim(),
format: "jsonv2",
}),
new URLSearchParams({
q: command.text.trim(),
format: "jsonv2",
}),
)
).json();
if (!channel.channel.is_member)
try {
await app.client.conversations.join({
channel: channel.channel.id,
});
} catch (e) { }
} catch (e) {}
if (!locations.length)
return await respond(
"No locations found. If this keeps failing, please add it to OpenStreetMaps.",
Expand Down Expand Up @@ -77,6 +75,6 @@ module.exports = async function ({ app }) {
user: command.user_id,
text: `Set your channel location to \`${display_name}\`. Others near you will be able to discover your channel via their location.`,
});
await prisma.$disconnect()
await prisma.$disconnect();
});
};
14 changes: 6 additions & 8 deletions commands/setuserlocation.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@


/**
* @param {{app: import('@slack/bolt').App}} param1
*/
module.exports = async function ({ app }) {
const { PrismaClient } = require("@prisma/client");
const prisma = new PrismaClient();
app.command("/setuserlocation", async ({ command, body, ack, respond }) => {
await prisma.$connect()
await prisma.$connect();
await ack();
if (!command.text)
return await respond(
Expand All @@ -22,10 +20,10 @@ module.exports = async function ({ app }) {
var locations = await (
await fetch(
`https://nominatim.openstreetmap.org/search?` +
new URLSearchParams({
q: command.text.trim(),
format: "jsonv2",
}),
new URLSearchParams({
q: command.text.trim(),
format: "jsonv2",
}),
)
).json();
if (!locations.length)
Expand Down Expand Up @@ -56,6 +54,6 @@ module.exports = async function ({ app }) {
user: command.user_id,
text: `Set your user location to \`${display_name}\`.`,
});
await prisma.$disconnect()
await prisma.$disconnect();
});
};
10 changes: 4 additions & 6 deletions sections/30-recent.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = {
render: async function ({ app, client }) {
const { PrismaClient } = require("@prisma/client");
const prisma = new PrismaClient();
await prisma.$connect()
await prisma.$connect();
var messages = await app.client.search.messages({
query: utils.queries.topChannels,
sort: "timestamp",
Expand Down Expand Up @@ -42,14 +42,13 @@ module.exports = {
sortedChannels.map(async (channel) => {
const channelRecord = await prisma.channel.findFirst({
where: {
id: channel
}
})
id: channel,
},
});
if (!channelRecord || !channelRecord.emoji) {
return `- <#${channel}>\n`;
} else {
return `- ${channelRecord.emoji} <#${channel}>\n`;

}
// (${await timeline({ app, channel })})
}),
Expand All @@ -63,6 +62,5 @@ ${await generateFullTimeline(channels)}
` + text
);

},
};
18 changes: 9 additions & 9 deletions utils/allTimeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ module.exports = async function generateFullTimeline(messages) {
const startTime = Math.min(...Object.keys(intervalMessages).map(Number));
const endTime = Math.max(...Object.keys(intervalMessages).map(Number));
let output = "";
await prisma.$connect()
await prisma.$connect();

for (let time = startTime; time <= endTime; time += 10) {
if (intervalMessages[time]) {
for (const channelId of intervalMessages[time]) {
var emoji = "💥,"
var emoji = "💥,";
const channelRecord = await prisma.channel.findFirst({
where: {
id: channelId
}
})
id: channelId,
},
});
if (!channelRecord || !channelRecord.emoji) {
output += emoji
continue
output += emoji;
continue;
}
output += channelRecord.emoji + ","
output += channelRecord.emoji + ",";
}
} else {
output += "-,";
Expand All @@ -43,6 +43,6 @@ module.exports = async function generateFullTimeline(messages) {
chars = chars
.map((char) => (emojis.includes(char) || char == "-" ? char : ""))
.slice(chars.length - 40, chars.length);
await prisma.$disconnect()
await prisma.$disconnect();
return chars.join(",").replaceAll(",", "");
};
16 changes: 7 additions & 9 deletions utils/joinall.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/**
* @param {{app: import('@slack/bolt').App}} param1
*/
Expand All @@ -10,7 +9,7 @@ Array.prototype.random = function () {
module.exports = async function ({ app, client }) {
const { PrismaClient } = require("@prisma/client");
const prisma = new PrismaClient();
await prisma.$connect()
await prisma.$connect();

async function rake(cursor) {
const convos = await app.client.conversations.list({
Expand All @@ -36,13 +35,13 @@ module.exports = async function ({ app, client }) {
await app.client.conversations.join({
channel: channel.id,
});
if (!channelRecord){
if (!channelRecord) {
await prisma.channel.create({
data:{
data: {
id: channel.id,
emoji: emojis.random()
}
})
emoji: emojis.random(),
},
});
}
console.log(`Joined ${channel.name_normalized} (${channel.id})`);
setTimeout(resolve, 1200);
Expand All @@ -60,9 +59,8 @@ module.exports = async function ({ app, client }) {
}, 3000);
else {
console.log("Finished joining all channels");
await prisma.$disconnect()
await prisma.$disconnect();
}

}
rake();
};
5 changes: 1 addition & 4 deletions utils/updateMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ module.exports = async function ({ app, text, blocks, priority, client }) {
await app.client.chat.update({
channel: process.env.SLACK_CHANNEL,
ts: messageId,
text: text.replaceAll(
"@",
"​@",
),
text: text.replaceAll("@", "​@").replaceAll(/[\u{1F3FB}-\u{1F3FF}]/gmu, ""),
blocks,
});
await client.set(
Expand Down

0 comments on commit e03839a

Please sign in to comment.