Skip to content

Commit

Permalink
Fix context.teamId for view interactions in a Slack Connect channel (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
seratch authored Oct 5, 2022
1 parent 5f62ed9 commit a87ae51
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ public ViewClosedRequest(
} else if (payload.getTeam() != null) {
getContext().setEnterpriseId(payload.getTeam().getEnterpriseId());
}
if (payload.getTeam() != null && payload.getTeam().getId() != null) {
if (payload.getView() != null && payload.getView().getAppInstalledTeamId() != null) {
// view_submission payloads can have `view.app_installed_team_id` when a modal view that was opened
// in a different workspace via some operations inside a Slack Connect channel.
// Note that the same for enterprise_id does not exist. When you need to know the enterprise_id as well,
// you have to run some query toward your InstallationStore to know the org where the team_id belongs to.
getContext().setTeamId(payload.getView().getAppInstalledTeamId());
} else if (payload.getTeam() != null && payload.getTeam().getId() != null) {
getContext().setTeamId(payload.getTeam().getId());
} else if (payload.getView() != null && payload.getView().getTeamId() != null) {
getContext().setTeamId(payload.getView().getTeamId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ public ViewSubmissionRequest(
} else if (payload.getTeam() != null) {
getContext().setEnterpriseId(payload.getTeam().getEnterpriseId());
}
if (payload.getTeam() != null && payload.getTeam().getId() != null) {
if (payload.getView() != null && payload.getView().getAppInstalledTeamId() != null) {
// view_submission payloads can have `view.app_installed_team_id` when a modal view that was opened
// in a different workspace via some operations inside a Slack Connect channel.
// Note that the same for enterprise_id does not exist. When you need to know the enterprise_id as well,
// you have to run some query toward your InstallationStore to know the org where the team_id belongs to.
getContext().setTeamId(payload.getView().getAppInstalledTeamId());
} else if (payload.getTeam() != null && payload.getTeam().getId() != null) {
getContext().setTeamId(payload.getTeam().getId());
} else if (payload.getView() != null && payload.getView().getTeamId() != null) {
getContext().setTeamId(payload.getView().getTeamId());
Expand Down
90 changes: 87 additions & 3 deletions bolt/src/test/java/test_locally/request/ViewClosedRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ public class ViewClosedRequestTest {
" \"id\": \"W111\",\n" +
" \"username\": \"primary-owner\",\n" +
" \"name\": \"primary-owner\",\n" +
" \"team_id\": \"T_expected\"\n" +
" \"team_id\": \"T_unexpected\"\n" +
" },\n" +
" \"api_app_id\": \"A111\",\n" +
" \"token\": \"fixed-value\",\n" +
" \"view\": {\n" +
" \"id\": \"V111\",\n" +
" \"team_id\": \"T_expected\",\n" +
" \"team_id\": \"T_unexpected\",\n" +
" \"type\": \"modal\",\n" +
" \"blocks\": [\n" +
" {\n" +
Expand Down Expand Up @@ -72,7 +72,7 @@ public class ViewClosedRequestTest {
" \"root_view_id\": \"V111\",\n" +
" \"app_id\": \"A111\",\n" +
" \"external_id\": \"\",\n" +
" \"app_installed_team_id\": \"E111\",\n" +
" \"app_installed_team_id\": \"T_expected\",\n" +
" \"bot_id\": \"B0302M47727\"\n" +
" },\n" +
" \"is_cleared\": false,\n" +
Expand All @@ -89,4 +89,88 @@ public void orgWideInstallation() throws UnsupportedEncodingException {
ViewClosedRequest req = new ViewClosedRequest("payload=" + URLEncoder.encode(orgWideInstallationPayload, "UTF-8"), orgWideInstallationPayload, headers);
assertEquals("T_expected", req.getContext().getTeamId());
}

String sharedChannelPayload = "{\n" +
" \"type\": \"view_closed\",\n" +
" \"team\": {\n" +
" \"id\": \"T-other-side\",\n" +
" \"domain\": \"other-side\",\n" +
" \"enterprise_id\": \"E-other-side\",\n" +
" \"enterprise_name\": \"Kaz Sandbox Org\"\n" +
" },\n" +
" \"user\": {\n" +
" \"id\": \"W111\",\n" +
" \"username\": \"kaz\",\n" +
" \"name\": \"kaz\",\n" +
" \"team_id\": \"T-other-side\"\n" +
" },\n" +
" \"api_app_id\": \"A1111\",\n" +
" \"token\": \"legacy-fixed-token\",\n" +
" \"trigger_id\": \"111.222.xxx\",\n" +
" \"view\": {\n" +
" \"id\": \"V11111\",\n" +
" \"team_id\": \"T-other-side\",\n" +
" \"type\": \"modal\",\n" +
" \"blocks\": [\n" +
" {\n" +
" \"type\": \"input\",\n" +
" \"block_id\": \"zniAM\",\n" +
" \"label\": {\n" +
" \"type\": \"plain_text\",\n" +
" \"text\": \"Label\"\n" +
" },\n" +
" \"element\": {\n" +
" \"type\": \"plain_text_input\",\n" +
" \"dispatch_action_config\": {\n" +
" \"trigger_actions_on\": [\n" +
" \"on_enter_pressed\"\n" +
" ]\n" +
" },\n" +
" \"action_id\": \"qEJr\"\n" +
" }\n" +
" }\n" +
" ],\n" +
" \"private_metadata\": \"\",\n" +
" \"callback_id\": \"view-id\",\n" +
" \"state\": {\n" +
" \"values\": {\n" +
" \"zniAM\": {\n" +
" \"qEJr\": {\n" +
" \"type\": \"plain_text_input\",\n" +
" \"value\": \"Hi there!\"\n" +
" }\n" +
" }\n" +
" }\n" +
" },\n" +
" \"hash\": \"1664950703.CmTS8F7U\",\n" +
" \"title\": {\n" +
" \"type\": \"plain_text\",\n" +
" \"text\": \"My App\"\n" +
" },\n" +
" \"close\": {\n" +
" \"type\": \"plain_text\",\n" +
" \"text\": \"Cancel\"\n" +
" },\n" +
" \"submit\": {\n" +
" \"type\": \"plain_text\",\n" +
" \"text\": \"Submit\"\n" +
" },\n" +
" \"root_view_id\": \"V00000\",\n" +
" \"app_id\": \"A1111\",\n" +
" \"external_id\": \"\",\n" +
" \"app_installed_team_id\": \"T-installed-workspace\",\n" +
" \"bot_id\": \"B1111\"\n" +
" },\n" +
" \"enterprise\": {\n" +
" \"id\": \"E-other-side\",\n" +
" \"name\": \"Kaz Sandbox Org\"\n" +
" }\n" +
"}\n";

@Test
public void sharedChannels() throws UnsupportedEncodingException {
RequestHeaders headers = new RequestHeaders(Collections.emptyMap());
ViewClosedRequest req = new ViewClosedRequest("payload=" + URLEncoder.encode(sharedChannelPayload, "UTF-8"), sharedChannelPayload, headers);
assertEquals("T-installed-workspace", req.getContext().getTeamId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,14 @@ public void responseUrl_single() throws UnsupportedEncodingException {
" \"id\": \"W111\",\n" +
" \"username\": \"primary-owner\",\n" +
" \"name\": \"primary-owner\",\n" +
" \"team_id\": \"T_expected\"\n" +
" \"team_id\": \"T_unexpected\"\n" +
" },\n" +
" \"api_app_id\": \"A111\",\n" +
" \"token\": \"fixed-value\",\n" +
" \"trigger_id\": \"1111.222.xxx\",\n" +
" \"view\": {\n" +
" \"id\": \"V111\",\n" +
" \"team_id\": \"T_expected\",\n" +
" \"team_id\": \"T_unexpected\",\n" +
" \"type\": \"modal\",\n" +
" \"blocks\": [\n" +
" {\n" +
Expand Down Expand Up @@ -238,7 +238,7 @@ public void responseUrl_single() throws UnsupportedEncodingException {
" \"root_view_id\": \"V111\",\n" +
" \"app_id\": \"A111\",\n" +
" \"external_id\": \"\",\n" +
" \"app_installed_team_id\": \"E111\",\n" +
" \"app_installed_team_id\": \"T_expected\",\n" +
" \"bot_id\": \"B111\"\n" +
" },\n" +
" \"response_urls\": [],\n" +
Expand All @@ -255,4 +255,88 @@ public void orgWideInstallation() throws UnsupportedEncodingException {
ViewSubmissionRequest req = new ViewSubmissionRequest("payload=" + URLEncoder.encode(orgWideInstallationPayload, "UTF-8"), orgWideInstallationPayload, headers);
assertEquals("T_expected", req.getContext().getTeamId());
}

String sharedChannelPayload = "{\n" +
" \"type\": \"view_submission\",\n" +
" \"team\": {\n" +
" \"id\": \"T-other-side\",\n" +
" \"domain\": \"other-side\",\n" +
" \"enterprise_id\": \"E-other-side\",\n" +
" \"enterprise_name\": \"Kaz Sandbox Org\"\n" +
" },\n" +
" \"user\": {\n" +
" \"id\": \"W111\",\n" +
" \"username\": \"kaz\",\n" +
" \"name\": \"kaz\",\n" +
" \"team_id\": \"T-other-side\"\n" +
" },\n" +
" \"api_app_id\": \"A1111\",\n" +
" \"token\": \"legacy-fixed-token\",\n" +
" \"trigger_id\": \"111.222.xxx\",\n" +
" \"view\": {\n" +
" \"id\": \"V11111\",\n" +
" \"team_id\": \"T-other-side\",\n" +
" \"type\": \"modal\",\n" +
" \"blocks\": [\n" +
" {\n" +
" \"type\": \"input\",\n" +
" \"block_id\": \"zniAM\",\n" +
" \"label\": {\n" +
" \"type\": \"plain_text\",\n" +
" \"text\": \"Label\"\n" +
" },\n" +
" \"element\": {\n" +
" \"type\": \"plain_text_input\",\n" +
" \"dispatch_action_config\": {\n" +
" \"trigger_actions_on\": [\n" +
" \"on_enter_pressed\"\n" +
" ]\n" +
" },\n" +
" \"action_id\": \"qEJr\"\n" +
" }\n" +
" }\n" +
" ],\n" +
" \"private_metadata\": \"\",\n" +
" \"callback_id\": \"view-id\",\n" +
" \"state\": {\n" +
" \"values\": {\n" +
" \"zniAM\": {\n" +
" \"qEJr\": {\n" +
" \"type\": \"plain_text_input\",\n" +
" \"value\": \"Hi there!\"\n" +
" }\n" +
" }\n" +
" }\n" +
" },\n" +
" \"hash\": \"1664950703.CmTS8F7U\",\n" +
" \"title\": {\n" +
" \"type\": \"plain_text\",\n" +
" \"text\": \"My App\"\n" +
" },\n" +
" \"close\": {\n" +
" \"type\": \"plain_text\",\n" +
" \"text\": \"Cancel\"\n" +
" },\n" +
" \"submit\": {\n" +
" \"type\": \"plain_text\",\n" +
" \"text\": \"Submit\"\n" +
" },\n" +
" \"root_view_id\": \"V00000\",\n" +
" \"app_id\": \"A1111\",\n" +
" \"external_id\": \"\",\n" +
" \"app_installed_team_id\": \"T-installed-workspace\",\n" +
" \"bot_id\": \"B1111\"\n" +
" },\n" +
" \"enterprise\": {\n" +
" \"id\": \"E-other-side\",\n" +
" \"name\": \"Kaz Sandbox Org\"\n" +
" }\n" +
"}\n";

@Test
public void sharedChannels() throws UnsupportedEncodingException {
RequestHeaders headers = new RequestHeaders(Collections.emptyMap());
ViewSubmissionRequest req = new ViewSubmissionRequest("payload=" + URLEncoder.encode(sharedChannelPayload, "UTF-8"), sharedChannelPayload, headers);
assertEquals("T-installed-workspace", req.getContext().getTeamId());
}
}

0 comments on commit a87ae51

Please sign in to comment.