Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Find the Slack profile

Kiran K04cc89695360
diff --git a/apps/web/lib/integrations/slack/slash-commands.ts b/apps/web/lib/integrations/slack/slash-commands.ts
index cbd1a76f44..823aa0b585 100644
--- a/apps/web/lib/integrations/slack/slash-commands.ts
+++ b/apps/web/lib/integrations/slack/slash-commands.ts
@@ -4,12 +4,13 @@ import { WorkspaceProps } from "@/lib/types";
import z from "@/lib/zod";
import { createLinkBodySchema } from "@/lib/zod/schemas/links";
import { SLACK_INTEGRATION_ID } from "@dub/utils";
-import { InstalledIntegration } from "@prisma/client";
+import { SlackCredential } from "./type";
import { verifySlackSignature } from "./verify-request";
const schema = z.object({
api_app_id: z.string(),
team_id: z.string(),
+ user_id: z.string(),
text: z.string().transform((text) => text.trim().split(" ")),
command: z.enum(["/shorten"]),
});
@@ -56,6 +57,36 @@ export const handleSlashCommand = async (
};
}
+ // Find Dub user matching the Slack user profile
+ const credentials = installation.credentials as SlackCredential;
+ let slackUser: undefined | { email: string };
+
+ try {
+ slackUser = await findSlackUser({
+ userId: data.user_id,
+ credentials,
+ });
+ } catch (error: any) {
+ return {
+ text: `Error fetching user profile: ${error.message}`,
+ };
+ }
+
+ const dubUser = await prisma.user.findUnique({
+ where: {
+ email: slackUser?.email,
+ },
+ select: {
+ id: true,
+ },
+ });
+
+ if (!dubUser) {
+ return {
+ text: "Unable to find Dub account matching your Slack account. Only Dub users can use this command.",
+ };
+ }
+
const workspace = (await prisma.project.findUniqueOrThrow({
where: {
id: installation.projectId,
@@ -67,7 +98,7 @@ export const handleSlashCommand = async (
})) as WorkspaceProps;
if (data.command === "/shorten") {
- return createShortLink({ data, workspace, installation });
+ return createShortLink({ data, workspace, userId: dubUser.id });
}
return {
@@ -75,15 +106,44 @@ export const handleSlashCommand = async (
};
};
+// Find Dub user for the given Slack user
+// TODO: Cache the profile for better performance
+const findSlackUser = async ({
+ userId,
+ credentials,
+}: {
+ userId: string;
+ credentials: SlackCredential;
+}) => {
+ const response = await fetch(
+ `https://slack.com/api/users.profile.get?user=${userId}`,
+ {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/x-www-form-urlencoded",
+ Authorization: `Bearer ${credentials.accessToken}`,
+ },
+ },
+ );
+
+ const slackUser = await response.json();
+
+ if (!slackUser.ok) {
+ throw new Error(slackUser.error);
+ }
+
+ return slackUser.profile as { email: string };
+};
+
// Handle `/shorten` command from Slack
const createShortLink = async ({
data,
workspace,
- installation,
+ userId,
}: {
data: z.infer<typeof schema>;
workspace: Pick<WorkspaceProps, "id" | "plan">;
- installation: InstalledIntegration;
+ userId: string;
}) => {
const [url, key, domain] = data.text;
const body = createLinkBodySchema.parse({ url, key, domain });
@@ -91,7 +151,7 @@ const createShortLink = async ({
const { link, error } = await processLink({
payload: body,
workspace: workspace as WorkspaceProps,
- userId: installation.userId,
+ userId,
});
if (error != null) {