mirror of
https://github.com/makeplane/plane
synced 2024-06-14 14:31:34 +00:00
feat: added notification handlers
This commit is contained in:
parent
cf9a6f1e87
commit
8abd174314
@ -4,11 +4,20 @@ import { getSlackMessageTemplate } from "../utils/slack/message-templates";
|
||||
import { SlackService } from "../services/slack.service";
|
||||
import { issueActivitySummary } from "../utils/slack/generateActivityMessage";
|
||||
import { logger } from "../utils/logger";
|
||||
import { CreateIssueModalViewProjects } from "../utils/slack/create-issue-modal";
|
||||
import {
|
||||
CreateIssueModalViewProjects,
|
||||
notificationModal,
|
||||
} from "../utils/slack/create-issue-modal";
|
||||
import { ProjectService } from "../services/project.service";
|
||||
import { convertToSlackOptions } from "../utils/slack/convert-to-slack-options";
|
||||
import { processSlackPayload } from "../handlers/slack/core";
|
||||
import { TSlackPayload } from "types/slack";
|
||||
import { TSlackPayload } from "../types/slack";
|
||||
import { MQSingleton } from "../mq/singleton";
|
||||
import { DatabaseSingleton } from "../db/singleton";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { users } from "../db/schema";
|
||||
import { notifications } from "../db/schema/notifications.schema";
|
||||
import { generateNotificationMessage } from "../utils/generateNotificationMessage";
|
||||
|
||||
@Controller("api/slack")
|
||||
export class SlackController {
|
||||
@ -36,41 +45,155 @@ export class SlackController {
|
||||
}
|
||||
}
|
||||
|
||||
@Post("trigger/ui/notifications/")
|
||||
async triggerNotificationsModal(req: Request, res: Response) {
|
||||
const slackService = new SlackService();
|
||||
const db = DatabaseSingleton.getInstance().db;
|
||||
|
||||
const slackUserInfo = await slackService.getUserInfo(req.body.user_id);
|
||||
const displayedSlackUser = await slackUserInfo?.json();
|
||||
const planeUser = await db?.query.users.findFirst({
|
||||
where: eq(users.email, displayedSlackUser?.user?.profile?.email),
|
||||
});
|
||||
|
||||
if (planeUser === undefined || planeUser === null) {
|
||||
console.log("no plane user");
|
||||
res.status(200).send("");
|
||||
}
|
||||
|
||||
const fetchedNotifications = await db?.query.notifications.findMany({
|
||||
where: eq(notifications.receiverId, planeUser.id),
|
||||
with: { triggeredBy: true, project: true, createdBy: true },
|
||||
});
|
||||
|
||||
const notificationMessages: string[] = fetchedNotifications.map(
|
||||
(notification) => generateNotificationMessage(notification),
|
||||
);
|
||||
|
||||
const response = await slackService.openModal(
|
||||
req.body.trigger_id,
|
||||
notificationModal(notificationMessages),
|
||||
);
|
||||
|
||||
const json = await response.json();
|
||||
|
||||
res.status(200);
|
||||
}
|
||||
|
||||
@Post("trigger/ui/create-issue/")
|
||||
async triggerCreateIssueModal(req: Request, res: Response) {
|
||||
const slackService = new SlackService();
|
||||
const projectService = new ProjectService();
|
||||
const mq = MQSingleton.getInstance();
|
||||
|
||||
const text = req.body.text;
|
||||
|
||||
const teamId = req.body.team_id;
|
||||
|
||||
const workspaceId = await slackService.getWorkspaceId(teamId);
|
||||
|
||||
if (!workspaceId) {
|
||||
res.sendStatus(500);
|
||||
return;
|
||||
return res.json({
|
||||
response_type: "ephemeral",
|
||||
text: "Workspace not found, Are you sure you have installed the slack in your plane workspace?",
|
||||
});
|
||||
}
|
||||
|
||||
const projectList =
|
||||
await projectService.getProjectsForWorkspace(workspaceId);
|
||||
const projectPlainTextOption = convertToSlackOptions(projectList);
|
||||
if (text === "") {
|
||||
const projectList =
|
||||
await projectService.getProjectsForWorkspace(workspaceId);
|
||||
const projectPlainTextOption = convertToSlackOptions(projectList);
|
||||
|
||||
await slackService.openModal(
|
||||
req.body.trigger_id,
|
||||
CreateIssueModalViewProjects(projectPlainTextOption),
|
||||
);
|
||||
res.sendStatus(200);
|
||||
await slackService.openModal(
|
||||
req.body.trigger_id,
|
||||
CreateIssueModalViewProjects(projectPlainTextOption),
|
||||
);
|
||||
|
||||
res.status(200).send("");
|
||||
} else {
|
||||
const issue_parts = text.split(" ");
|
||||
if (issue_parts.length < 3) {
|
||||
return res.json({
|
||||
response_type: "ephemeral",
|
||||
text: "Incorrect format, please use the following format: /takeoff <project-identifier> <issue-title> <issue-description>",
|
||||
});
|
||||
}
|
||||
|
||||
const projectIdentifier = issue_parts[0].toUpperCase();
|
||||
const issueTitle = issue_parts[1];
|
||||
const issueDescription = issue_parts[2];
|
||||
|
||||
const project =
|
||||
await projectService.getProjectByIdentifier(projectIdentifier);
|
||||
|
||||
if (!project) {
|
||||
return res.json({
|
||||
response_type: "ephemeral",
|
||||
text: "Project not found",
|
||||
});
|
||||
}
|
||||
|
||||
const userId = req.body.user_id;
|
||||
const userInfoResponse = await slackService.getUserInfo(userId);
|
||||
if (!userInfoResponse) {
|
||||
return res.json({
|
||||
response_type: "ephemeral",
|
||||
text: "Unable to get user info at this moment.",
|
||||
});
|
||||
}
|
||||
|
||||
const displayedUser = await userInfoResponse.json();
|
||||
|
||||
const issueSync = {
|
||||
args: [],
|
||||
kwargs: {
|
||||
data: {
|
||||
type: "slack.create_issue",
|
||||
title: issueTitle,
|
||||
description: issueDescription,
|
||||
created_by: {
|
||||
email: displayedUser?.user?.profile?.email,
|
||||
name: displayedUser?.user?.name,
|
||||
},
|
||||
priority: "none",
|
||||
workspace_id: workspaceId,
|
||||
project_id: project.id,
|
||||
assignees: [],
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
mq.publish(issueSync, "plane.bgtasks.importer_task.import_task");
|
||||
|
||||
res.json({
|
||||
response_type: "ephemeral",
|
||||
text: "Successfully created issue",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Post("events")
|
||||
async handleSlackEvents(req: Request, res: Response) {
|
||||
const payload = JSON.parse(req.body.payload) as TSlackPayload;
|
||||
console.log(payload);
|
||||
|
||||
const success = await processSlackPayload(payload);
|
||||
|
||||
if (!success) {
|
||||
console.log("error");
|
||||
res.sendStatus(500);
|
||||
return res.json({
|
||||
response_type: "ephemeral",
|
||||
text: "Unable to process payload, please try again later.",
|
||||
});
|
||||
}
|
||||
res.sendStatus(200);
|
||||
|
||||
if (success && payload.type === "view_submission") {
|
||||
return res.send({
|
||||
response_action: "clear",
|
||||
});
|
||||
}
|
||||
|
||||
return res.json({
|
||||
response_type: "ephemeral",
|
||||
text: "Event Processed Successfully",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user