Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/scripts/inclusion-bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const fs = require("fs");
const path = require("path");
const yaml = require("js-yaml");
const {
optOut,
slack: { addEmojiReaction, postEphemeralResponse },
stats: { incrementStats },
helpMessage,
Expand Down Expand Up @@ -42,6 +43,12 @@ module.exports = async (app) => {
"Charlie passively listens for language with racist, ableist, sexist, or other exclusionary histories. When it hears such words or phrases, it quietly lets the speaker know and offers some suggestions. What a great bot, helping nudge us all to thoughtful, inclusive language!",
);

const optout = optOut(
"inclusion_bot_extended",
"Inclusion Bot | Extended mode",
"Opt out of extended inclusive language notifications. These notifications are meant to help educate and remind us to be mindful of our colleagues.",
);

// Use the module exported version here, so that it can be stubbed for testing
const { link, message, triggers } = module.exports.getTriggers();

Expand All @@ -55,10 +62,12 @@ module.exports = async (app) => {
const combinedRegex = new RegExp(`\\b${combinedString}\\b`, "i");

app.message(combinedRegex, (msg) => {
const extended = !optout.isOptedOut(msg.event.user);

// Find the specific match that triggered this bot. At this point, go ahead
// and remove things that should be ignored.
const specificMatch = triggers
.map(({ alternatives, ignore, matches }) => {
.map(({ alternatives, ignore, optional, matches }) => {
const { text } = msg.message;

// Wrap the match in word boundaries, so we don't match something that's
Expand All @@ -72,10 +81,12 @@ module.exports = async (app) => {
// makes the regexes a lot simpler.
return {
alternatives,
optional,
text: text.replace(ignoreRegex, "").match(matchRegex),
};
})
.filter(({ text }) => !!text)
.filter(({ optional }) => extended || !optional)
.map(({ text: [match], ...rest }) => ({ text: match, ...rest }));

// If there aren't any specific matches, it means the bot was triggered only
Expand All @@ -100,7 +111,11 @@ module.exports = async (app) => {
color: "#2eb886",
fallback: message,
blocks: [
{ type: "section", text: { type: "mrkdwn", text: message } },
{
type: "section",
text: { type: "mrkdwn", text: message },
...(extended ? optout.button : {}),
},
{
type: "section",
text: { type: "mrkdwn", text: pretexts.join("\n") },
Expand Down
82 changes: 81 additions & 1 deletion src/scripts/inclusion-bot.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const fs = require("fs");
const {
getApp,
utils: {
optOut,
slack: { addEmojiReaction, postEphemeralResponse },
},
} = require("../utils/test");
Expand Down Expand Up @@ -56,6 +57,9 @@ describe("Inclusion bot", () => {
const app = getApp();

const msg = {
event: {
user: "abc123",
},
message: {
id: "message id",
room: "channel id",
Expand All @@ -64,9 +68,18 @@ describe("Inclusion bot", () => {
},
};

const isOptedOut = jest.fn();

beforeEach(() => {
jest.resetAllMocks();

isOptedOut.mockReturnValue(false);

optOut.mockReturnValue({
button: { button: "goes here" },
isOptedOut,
});

fs.readFileSync.mockReturnValue(`
link: https://link.url
message: "This is the message"
Expand All @@ -85,14 +98,19 @@ triggers:
- match 2b
alternatives:
- b1
- matches:
- match 3
alternatives:
- b3
optional: true
`);
});

it("subscribes to case-insensitive utterances of uninclusive language", () => {
bot(app);

expect(app.message).toHaveBeenCalledWith(
/\b(match 1)(?=[^"“”']*(["“”'][^"“”']*["“”'][^"“”']*)*$)|(match 2a|match 2b)(?=[^"“”']*(["“”'][^"“”']*["“”'][^"“”']*)*$)\b/i,
/\b(match 1)(?=[^"“”']*(["“”'][^"“”']*["“”'][^"“”']*)*$)|(match 2a|match 2b)(?=[^"“”']*(["“”'][^"“”']*["“”'][^"“”']*)*$)|(match 3)(?=[^"“”']*(["“”'][^"“”']*["“”'][^"“”']*)*$)\b/i,
expect.any(Function),
);
});
Expand All @@ -110,6 +128,7 @@ triggers:
type: "mrkdwn",
text: "This is the message",
},
button: "goes here",
},
{
accessory: {
Expand Down Expand Up @@ -150,6 +169,8 @@ triggers:
beforeEach(() => {
bot(app);
handler = app.getHandler();

expectedMessage.attachments[0].blocks[1].accessory.value = "match 1";
});

it("handles a single triggering phrase", () => {
Expand Down Expand Up @@ -236,5 +257,64 @@ triggers:
// Reset the parts of the expected message that we changed above.
expectedMessage.attachments[0].blocks[1].accessory.value = "match 1";
});

it("does not add the opt-out button if the user is already opted-out", () => {
isOptedOut.mockReturnValue(true);

const noButton = JSON.parse(JSON.stringify(expectedMessage));
delete noButton.attachments[0].blocks[0].button;

msg.message.text = "hello this is the match 1 trigger";
handler(msg);

expect(addEmojiReaction).toHaveBeenCalledWith(msg, "wave");

noButton.attachments[0].blocks[1].text.text = expect.stringMatching(
/• Instead of saying "match 1," how about \*(a1|a2|a3)\*\?/,
);
expect(postEphemeralResponse).toHaveBeenCalledWith(msg, noButton);
});

it("does trigger on optional words if the user is not opted out", () => {
msg.message.text = "hello this is the match 3 trigger";
handler(msg);

expect(addEmojiReaction).toHaveBeenCalledWith(msg, "wave");

expectedMessage.attachments[0].blocks[1].accessory.value = "match 3";
expectedMessage.attachments[0].blocks[1].text.text =
expect.stringMatching(
/• Instead of saying "match 3," how about \*b3\*\?/,
);
expect(postEphemeralResponse).toHaveBeenCalledWith(msg, expectedMessage);
});

it("does not trigger if the user is opted-out and the trigger phrase is optional", () => {
isOptedOut.mockReturnValue(true);

msg.message.text = "hello this is the match 3 trigger";
handler(msg);

expect(addEmojiReaction).not.toHaveBeenCalled();

expect(postEphemeralResponse).not.toHaveBeenCalled();
});

it("does trigger on non-optional words but leaves out optional words if the user is opted-out", () => {
isOptedOut.mockReturnValue(true);

msg.message.text = "hello this is the match 1 and the match 3 trigger";
handler(msg);

expect(addEmojiReaction).toHaveBeenCalledWith(msg, "wave");

const noButton = { ...expectedMessage };
delete noButton.attachments[0].blocks[0].button;

noButton.attachments[0].blocks[1].text.text = expect.stringMatching(
/• Instead of saying "match 1," how about \*(a1|a2|a3)\*\?/,
);
expect(postEphemeralResponse).toHaveBeenCalledWith(msg, noButton);
});
});
});
37 changes: 23 additions & 14 deletions sync-inclusion-bot-words.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,32 +44,41 @@ const markdownRowToTrigger = ([, matches, alternatives, why]) => ({
.replace(/this term/i, '":TERM:"'),
});

const getTriggerIgnoreMap = (currentConfig) => {
const triggerWithIgnore = (newTrigger) => {
const getTriggerExtraMetadataMap = (currentConfig) => {
const triggerWithExtraMetadata = (newTrigger) => {
// Find a trigger in the existing config that has at least one of the same
// matches as the new trigger. There may not be one, but if...
const existing = currentConfig.triggers.find(
({ matches: currentMatches }) =>
currentMatches.some((v) => newTrigger.matches.includes(v)),
);

// Create a new mapped trigger. We don't use an object spread notation here
// so we control the order the properties get rendered into YAML, to keep
// things consistent and nice.
const mapped = {
matches: newTrigger.matches,
alternatives: newTrigger.alternatives,
ignore: existing.ignore,
};

// If there is an existing config that matches AND it has an ignore property
// return a new trigger that includes the ignore property. (We do it this
// way instead of using an object spread so we get the properties in the
// order we want for readability when it gets written to yaml.)
// add the existing ignore property on to the new trigger.
if (existing?.ignore) {
return {
matches: newTrigger.matches,
alternatives: newTrigger.alternatives,
ignore: existing.ignore,
why: newTrigger.why,
};
mapped.ignore = existing.ignore;
}
// If there is an existing config that matches AND it has an ignore property
// add the existing ignore property on to the new trigger.
if (existing?.optional) {
mapped.optional = existing.optional;
}

mapped.why = newTrigger.why;

// Otherwise just return what we got.
return newTrigger;
return mapped;
};
return triggerWithIgnore;
return triggerWithExtraMetadata;
};

const main = async () => {
Expand All @@ -82,7 +91,7 @@ const main = async () => {
// Also find the frontmatter comments so we can preserve it.
const frontmatter = getYamlFrontmatter(currentYamlStr);

const triggerWithIgnore = getTriggerIgnoreMap(currentConfig);
const triggerWithIgnore = getTriggerExtraMetadataMap(currentConfig);

// Read and parse the markdown.
const mdf = await fs.readFile("InclusionBot.md", { encoding: "utf-8" });
Expand Down