Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix context.team_id for view interactions in a Slack Connect channel #1615

Merged
Show file tree
Hide file tree
Changes from 2 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
58 changes: 57 additions & 1 deletion src/App-basic-features.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1150,13 +1150,69 @@ describe('App basic features', () => {
];

// Act
await Promise.all(receiverEvents.map((event) => fakeReceiver.sendEvent(event)));
await Promise.all(
receiverEvents.map((event) => fakeReceiver.sendEvent(event)),
);

// Assert
assert.isTrue(fakeLogger.info.called);
assert.isTrue(ackInMiddlewareCalled);
});
});

describe('context', () => {
it('should be able to use the app_installed_team_id when provided by the payload', async () => {
// Arrange
const fakeAxiosPost = sinon.fake.resolves({});
overrides = buildOverrides([
withNoopWebClient(),
withAxiosPost(fakeAxiosPost),
]);
const MockApp = await importApp(overrides);

// Act
const app = new MockApp({
receiver: fakeReceiver,
authorize: sinon.fake.resolves(dummyAuthorizationResult),
});

app.view('view-id', async ({ ack, context, view }) => {
assert.equal('T-installed-workspace', context.teamId);
assert.notEqual('T-installed-workspace', view.team_id);
await ack();
});
app.error(fakeErrorHandler);

let ackCalled = false;

const receiverEvent = {
ack: async () => {
ackCalled = true;
},
body: {
type: 'view_submission',
team: {},
user: {},
view: {
id: 'V111',
type: 'modal',
callback_id: 'view-id',
state: {},
title: {},
close: {},
submit: {},
app_installed_team_id: 'T-installed-workspace',
},
},
};

// Act
await fakeReceiver.sendEvent(receiverEvent);

// Assert
assert.isTrue(ackCalled);
});
});
});
});

Expand Down
42 changes: 28 additions & 14 deletions src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1368,29 +1368,43 @@ function buildSource<IsEnterpriseInstall extends boolean>(
return (body as SlackCommandMiddlewareArgs['body']).team_id;
}

const parseTeamId = (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for decomposing this out into its own function! Slowly but surely, the horrible mess that is App.ts will be reformed :P

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem, we will eventually get there 🥇

bodyAs:
| SlackAction
| SlackViewAction
| SlackShortcut
| KnownOptionsPayloadFromType<OptionsSource>,
): string | undefined => {
// When the app is installed using org-wide deployment, team property will be null
if (typeof bodyAs.team !== 'undefined' && bodyAs.team !== null) {
return bodyAs.team.id;
}

// This is the only place where this function might return undefined
return bodyAs.user.team_id;
};

if (type === IncomingEventType.ViewAction) {
const bodyAsView = body as SlackViewMiddlewareArgs['body'];

if (bodyAsView.view.app_installed_team_id) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Can we also add a comment for future eyes about when this condition is met, since it's a little nuanced? Maybe mirroring what Kaz has added in his PR

return bodyAsView.view.app_installed_team_id;
}

return parseTeamId(bodyAsView);
}

if (
type === IncomingEventType.Action ||
type === IncomingEventType.Options ||
type === IncomingEventType.ViewAction ||
type === IncomingEventType.Shortcut
) {
const bodyAsActionOrOptionsOrViewActionOrShortcut = body as (
const bodyAsActionOrOptionsOrOrShortcut = body as (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const bodyAsActionOrOptionsOrOrShortcut = body as (
const bodyAsActionOrOptionsOrShortcut = body as (

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch 💯

| SlackActionMiddlewareArgs
| SlackOptionsMiddlewareArgs
| SlackViewMiddlewareArgs
| SlackShortcutMiddlewareArgs
)['body'];

// When the app is installed using org-wide deployment, team property will be null
if (
typeof bodyAsActionOrOptionsOrViewActionOrShortcut.team !== 'undefined' &&
bodyAsActionOrOptionsOrViewActionOrShortcut.team !== null
) {
return bodyAsActionOrOptionsOrViewActionOrShortcut.team.id;
}

// This is the only place where this function might return undefined
return bodyAsActionOrOptionsOrViewActionOrShortcut.user.team_id;
return parseTeamId(bodyAsActionOrOptionsOrOrShortcut);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return parseTeamId(bodyAsActionOrOptionsOrOrShortcut);
return parseTeamId(bodyAsActionOrOptionsOrShortcut);

}

return assertNever(type);
Expand Down