slack-integration.ts 937 B

1234567891011121314151617181920212223242526272829
  1. import type { IChannelOptionalId } from '@growi/slack';
  2. import { getSupportedGrowiActionsRegExp } from '@growi/slack/dist/utils/get-supported-growi-actions-regexps';
  3. import { permissionParser } from '@growi/slack/dist/utils/permission-parser';
  4. type CommandPermission = { [key: string]: string[] | boolean };
  5. export const checkPermission = (
  6. commandPermission: CommandPermission,
  7. commandOrActionIdOrCallbackId: string,
  8. fromChannel: IChannelOptionalId,
  9. ): boolean => {
  10. let isPermitted = false;
  11. // help
  12. if (commandOrActionIdOrCallbackId === 'help') {
  13. return true;
  14. }
  15. Object.entries(commandPermission).forEach((entry) => {
  16. const [command, value] = entry;
  17. const permission = value;
  18. const commandRegExp = getSupportedGrowiActionsRegExp(command);
  19. if (!commandRegExp.test(commandOrActionIdOrCallbackId)) return;
  20. isPermitted = permissionParser(permission, fromChannel);
  21. });
  22. return isPermitted;
  23. };